客户标签维护.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using LabelManager2;
  2. using System;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using UAS_LabelMachine.CustomControl;
  9. using UAS_LabelMachine.Properties;
  10. using UAS_LabelMachine.PublicMethod;
  11. namespace UAS_LabelMachine
  12. {
  13. public partial class 客户标签维护 : Form
  14. {
  15. DataHelper dh;
  16. DataTable dt;
  17. //设置Grid中Combox的静态数据,这个格式是必须的,#前面是展示值,#后面是实际值
  18. string[] labeltype = { "单盘#单盘", "中盒#中盒", "外箱#外箱", "全部#" };
  19. string[] defaultype = { "是#-1", "否#0" };
  20. //记录上次筛选时的条件,如果本次和上次相同则不进行查询
  21. string LastCondition = "";
  22. //用于拼接条件查询
  23. StringBuilder condition = new StringBuilder();
  24. StringBuilder sql = new StringBuilder();
  25. /*CodeSoft新建打印机引擎*/
  26. ApplicationClass lbl;
  27. Document doc;
  28. /*BarTender新建打印机引擎*/
  29. //private Engine engine = null;
  30. //private LabelFormatDocument format ;
  31. //点击行的LabelCode
  32. string LabelCode = "";
  33. //模板的路径
  34. string LabelPath = "";
  35. //模板编号的ID
  36. string CL_ID1;
  37. string CL_ID2;
  38. //Label表的主键,用于弹窗的传参
  39. string La_id1;
  40. //参数个数
  41. int ParamNum;
  42. //模板的参数名称
  43. string[] ParamName;
  44. //添加在grid末列的操作列
  45. DataGridViewImageColumn PreviewColumn = new DataGridViewImageColumn
  46. {
  47. Image = Resources.preview_16px_25980_easyicon_net,
  48. Width = 40,
  49. Name = "预览",
  50. ToolTipText = "预览模板参数",
  51. Tag = "Read",
  52. AutoSizeMode = DataGridViewAutoSizeColumnMode.None
  53. };
  54. DataGridViewImageColumn OperateColumn = new DataGridViewImageColumn
  55. {
  56. Image = Resources.write_15_893687707641px_1181413_easyicon_net,
  57. Width = 40,
  58. Name = "编辑",
  59. ToolTipText = "编辑模板",
  60. Tag = "Read",
  61. AutoSizeMode = DataGridViewAutoSizeColumnMode.None
  62. };
  63. AutoSizeFormClass asc = new AutoSizeFormClass();
  64. public 客户标签维护()
  65. {
  66. InitializeComponent();
  67. cu_code.FormName = Name;
  68. cu_code.SetValueField = new string[] { "cu_code", "cu_name" };
  69. cu_code.TableName = "customer left join customerlabel on cu_code=cl_custcode";
  70. cu_code.SelectField = "cu_code # 客户编号,cu_name # 客户名称";
  71. 客户编号.FormName = Name;
  72. 客户编号.SetValueField = new string[] { "客户编号" };
  73. 客户编号.TableName = "customer left join customerlabel on cu_code=cl_custcode";
  74. 客户编号.SelectField = "cu_code # 客户编号,cu_name # 客户名称";
  75. la_code.FormName = Name;
  76. la_code.SetValueField = new string[] { "la_code" };
  77. la_code.TableName = "label";
  78. la_code.SelectField = "la_code # 模板编号,la_name # 模板名称";
  79. NetURL.Text = Settings.Default.ShareUrl;
  80. }
  81. private void 条码维护_Load(object sender, EventArgs e)
  82. {
  83. if (Settings.Default.FTPAddress == "")
  84. {
  85. FTPShare.Visible = false;
  86. }
  87. dh = new DataHelper();
  88. condition.Append("");
  89. ChooseAll.ChooseAll(LabelDataGridView);
  90. BaseUtil.SetComboxData(la_type, "display", "value", labeltype);
  91. //加载下拉框的静态值
  92. LoadData();
  93. Width = Width + 1;
  94. asc.controllInitializeSize(this);
  95. lbl = new ApplicationClass();
  96. Width = Width + 1;
  97. }
  98. private void LabelDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
  99. {
  100. if (LabelDataGridView.Columns[e.ColumnIndex].HeaderText == "预览")
  101. {
  102. if (lbl.Documents.Count > 0)
  103. {
  104. lbl.ActiveDocument.Close();
  105. }
  106. //获取路径和标签名称
  107. LabelPath = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelurl"].Value.ToString();
  108. LabelCode = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelcode"].Value.ToString();
  109. CL_ID1 = LabelDataGridView.Rows[e.RowIndex].Cells["cl_id"].Value.ToString();
  110. La_id1 = LabelDataGridView.Rows[e.RowIndex].Cells["la_id"].Value.ToString();
  111. System.DateTime time = Convert.ToDateTime(LabelDataGridView.Rows[e.RowIndex].Cells["cl_date"].Value.ToString());
  112. if (CodeSoft.Checked)
  113. {
  114. string LabelName = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString();
  115. doc = lbl.Documents.Open(BaseUtil.GetLabelUrl(LabelPath, LabelName,time));
  116. ParamNum = doc.Variables.FreeVariables.Count;
  117. //去除之前添加的控件
  118. for (int i = 0; i < (ParamName == null ? 0 : ParamName.Length); i++)
  119. {
  120. if (this.Controls["LabelParam"].Controls[ParamName[i] + "_label"] != null)
  121. {
  122. this.Controls["LabelParam"].Controls.Remove(this.Controls["LabelParam"].Controls[ParamName[i] + "_label"]);
  123. this.Controls["LabelParam"].Controls.Remove(this.Controls["LabelParam"].Controls[ParamName[i]]);
  124. }
  125. }
  126. //如果文件已不存在则进行提示
  127. if (doc == null)
  128. {
  129. //LabelParamPreview.SelectionColor = Color.Red;
  130. //LabelParamPreview.AppendText("指定路径已找不到该文件\n");
  131. return;
  132. }
  133. ParamName = new string[ParamNum];
  134. for (int i = 0; i < ParamNum; i++)
  135. {
  136. ParamName[i] = doc.Variables.FreeVariables.Item(i + 1).Name;
  137. }
  138. int y = 20;
  139. for (int i = 0; i < ParamName.Length; i++)
  140. {
  141. //变量的名称Label
  142. Label Param = new Label();
  143. Param.Text = ParamName[i];
  144. Param.Name = ParamName[i] + "_label";
  145. Param.AutoSize = true;
  146. Param.Anchor = AnchorStyles.Left;
  147. Param.Anchor = AnchorStyles.Top;
  148. Param.Location = new Point(20, y + 5);
  149. this.Controls["LabelParam"].Controls.Add(Param);
  150. //维护变量的打印测试参数
  151. EnterTextBox TempParam = new EnterTextBox();
  152. TempParam.Anchor = AnchorStyles.Left;
  153. TempParam.Anchor = AnchorStyles.Top;
  154. TempParam.Name = ParamName[i];
  155. TempParam.Location = new Point(100, y);
  156. y = y + 30;
  157. TempParam.Size = new Size(170, 22);
  158. this.Controls["LabelParam"].Controls.Add(TempParam);
  159. y = y + 30;
  160. }
  161. }
  162. }
  163. if (LabelDataGridView.Columns[e.ColumnIndex].HeaderText == "编辑")
  164. {
  165. if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
  166. {
  167. CL_ID2 = LabelDataGridView.Rows[e.RowIndex].Cells["cl_id"].Value.ToString();
  168. LabelPath = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelurl"].Value.ToString();
  169. string LabelName = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString();
  170. System.DateTime time = Convert.ToDateTime(LabelDataGridView.Rows[e.RowIndex].Cells["cl_date"].Value.ToString());
  171. BaseUtil.GetLabelUrl(LabelPath, LabelName, time);
  172. System.Diagnostics.Process.Start(ftpOperater.DownLoadTo + LabelName);
  173. 客户编号.Text = LabelDataGridView.Rows[e.RowIndex].Cells["CL_CUSTCODE2"].Value.ToString();
  174. la_type1.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labeltype"].Value.ToString();
  175. la_code.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelcode"].Value.ToString();
  176. la_name.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString();
  177. FolderPath.Text = ftpOperater.DownLoadTo;
  178. }
  179. }
  180. //asc.controllInitializeSize(this);
  181. }
  182. private void Screen_Click(object sender, EventArgs e)
  183. {
  184. sql.Clear();
  185. condition.Clear();
  186. //如果条件全部为空的时候
  187. if (cu_code.Text == "" && cu_name.Text == "" && la_type.Text == "")
  188. {
  189. LoadData();
  190. }
  191. else
  192. {
  193. sql.Append("select to_char(nvl(cl_date,sysdate),'YYYY-MM-DD HH24:Mi:SS') cl_date,cu_name,cl_labelurl,cl_labeltype,cl_isdefault,cl_custcode,cl_labelname,cl_id,cl_labelcode,cl_detno,cl_labelsoft,la_id from customerlabel left join customer on cl_custcode=cu_code left join label on la_code=cl_custcode order by cl_custcode ");
  194. //设置筛选的条件
  195. condition.Append(BaseUtil.GetScreenSqlCondition(cu_code, la_type));
  196. //最终刷选条件拼接成的sql
  197. LoadData();
  198. }
  199. }
  200. private void Update_Click(object sender, EventArgs e)
  201. {
  202. string LabelSoft = "";
  203. //需要处理新增的行和原有数据然后更新的行
  204. if (CodeSoft.Checked)
  205. {
  206. LabelSoft = "CodeSoft";
  207. }
  208. DataTable dt = (DataTable)LabelDataGridView.DataSource;
  209. //如果有改变的行才传递到后台
  210. if (dt.GetChanges() != null)
  211. {
  212. //如果是新增的默认加上Radio当前选中的打印软件
  213. for (int i = 0; i < LabelDataGridView.Rows.Count; i++)
  214. {
  215. if (LabelDataGridView.Rows[i].Cells["cl_labelsoft"].Value.ToString() == "")
  216. {
  217. LabelDataGridView.Rows[i].Cells["cl_labelsoft"].Value = LabelSoft;
  218. }
  219. }
  220. string sql = "Insert into customerlabel (cl_ID,cl_custCODE,cl_LABELTYPE,cl_LABELCODE,cl_LABELURL,cl_DETNO,cl_LABELSOFT,cl_ISDEFAULT, cl_date) values(customerlabel_seq.nextval,'" + cu_code.Text + "',:cl_LABELTYPE,:cl_LABELCODE,:cl_LABELURL,:cl_DETNO,:cl_LABELSOFT,:cl_ISDEFAULT,sysdate)";
  221. dh.UpDateTableByCondition(dt.GetChanges(), "customerlabel", "cl_id", sql.ToLower());
  222. LoadData();
  223. }
  224. }
  225. private void DeleteDetail_Click(object sender, EventArgs e)
  226. {
  227. string[] deleteid = new string[LabelDataGridView.RowCount];
  228. for (int i = 0; i < LabelDataGridView.RowCount; i++)
  229. {
  230. deleteid[i] = LabelDataGridView.Rows[i].Cells["cl_id"].Value.ToString();
  231. }
  232. dh.DeleteDataByID("customerlabel", "cl_id", deleteid);
  233. LoadData();
  234. }
  235. private void OpenFolder_Click(object sender, EventArgs e)
  236. {
  237. //根据勾选的不同设置不同的文件后缀过滤
  238. if (CodeSoft.Checked)
  239. {
  240. openFileDialog1.Filter = "(*.lab)|*.lab";
  241. }
  242. DialogResult result = openFileDialog1.ShowDialog();
  243. if (result == DialogResult.OK)
  244. {
  245. FolderPath.Text = openFileDialog1.FileName;
  246. la_name.Text = FolderPath.Text.Substring(FolderPath.Text.LastIndexOf("\\") + 1);
  247. for (int i = 0; i < LabelDataGridView.Rows.Count; i++)
  248. {
  249. if (LabelDataGridView.Rows[i].Cells["Choose"].Value != null)
  250. {
  251. if ((bool)LabelDataGridView.Rows[i].Cells["Choose"].Value == true)
  252. {
  253. LabelDataGridView.Rows[i].Cells["cl_labelurl"].Value = FolderPath.Text;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. private void LabelDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  260. {
  261. if (LabelDataGridView.Columns[e.ColumnIndex].Name == "cl_isdefault")
  262. {
  263. if (e.Value != null)
  264. {
  265. switch (e.Value.ToString())
  266. {
  267. case "0":
  268. e.Value = "否";
  269. break;
  270. case "1":
  271. e.Value = "是";
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. }
  278. }
  279. private void LabelDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
  280. {
  281. //覆盖参数错误的事件,使得不会报错
  282. }
  283. private void LoadData()
  284. {
  285. //如果条件和上次一样则不进行数据读取
  286. //if (LastCondition != condition.ToString())
  287. {
  288. //如果没有任何筛选条件直接选取customerlabel的数据
  289. if (sql.Length == 0)
  290. {
  291. dt = (DataTable)dh.ExecuteSql("select to_char(nvl(cl_date,sysdate),'YYYY-MM-DD HH24:Mi:SS') cl_date,cu_name,cl_labelurl,cl_labeltype,cl_isdefault,cl_labelname,cl_custcode,cl_id,cl_labelcode,cl_detno,cl_labelsoft,la_id from customerlabel left join customer on cl_custcode=cu_code left join label on la_code=CL_LABELCODE order by cl_custcode", "select");
  292. }
  293. else
  294. {
  295. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  296. }
  297. BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt);
  298. string Field = BaseUtil.GetGridViewSelectContent(LabelDataGridView);
  299. pagination1.BindDataToNavigator(LabelDataGridView, "CustomerLabel left join customer on cl_custcode=cu_code left join label on la_code=CL_LABELCODE", Field, "cl_id", "", condition.Replace("where", "").ToString(), PreviewColumn, OperateColumn);
  300. LastCondition = condition.ToString();
  301. }
  302. }
  303. private void PrintTest_Click(object sender, EventArgs e)
  304. {
  305. if (LabelCode != "")
  306. {
  307. for (int j = 0; j < doc.Variables.FreeVariables.Count; j++)
  308. {
  309. //将维护的模板参数和模板本身的参数名称进行比对
  310. for (int k = 0; k < ParamName.Length; k++)
  311. {
  312. //名称相等的时候,取SQL进行值的查询
  313. if (doc.Variables.FreeVariables.Item(j + 1).Name == ParamName[k])
  314. {
  315. doc.Variables.FreeVariables.Item(j + 1).Value = Controls["LabelParam"].Controls[ParamName[k]].Text;
  316. }
  317. }
  318. }
  319. //保存参数打印
  320. doc.Save();
  321. doc.Printer.SwitchTo(PrinterList.Text);
  322. doc.PrintDocument();
  323. doc.Close();
  324. }
  325. }
  326. private void 产品标签维护_FormClosing(object sender, FormClosingEventArgs e)
  327. {
  328. try
  329. {
  330. lbl.Documents.CloseAll();
  331. lbl.Application.Quit();
  332. }
  333. catch (Exception)
  334. {
  335. }
  336. }
  337. private void Save_Click(object sender, EventArgs e)
  338. {
  339. if (la_type1.Text != "" && FolderPath.Text != "" && la_code.Text != "")
  340. {
  341. //文件名
  342. string fileName;
  343. //源路径
  344. string sourcePath;
  345. //未选择文件的之后默认是这个,从Text输入框中取
  346. if (openFileDialog1.FileName == "openFileDialog1")
  347. {
  348. fileName = la_name.Text;
  349. sourcePath = FolderPath.Text;
  350. }
  351. else
  352. {
  353. sourcePath = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.LastIndexOf(@"\"));
  354. fileName = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf(@"\") + 1);
  355. }
  356. if (FTPShare.Checked)
  357. {
  358. ftpOperater ftp = new ftpOperater();
  359. ftp.UpLoadFile(sourcePath, fileName);
  360. string SoftWare = "CodeSoft";
  361. if (CL_ID2 != null)
  362. {
  363. sql.Clear();
  364. sql.Append("update customerlabel set cl_labelcode='" + la_code.Text + "',cl_custcode='" + 客户编号.Text + "',");
  365. sql.Append("cl_labeltype='" + la_type1.Text + "',cl_labelname='" + la_name.Text + "',cl_date=sysdate where cl_id=" + CL_ID2);
  366. }
  367. else
  368. {
  369. sql.Clear();
  370. sql.Append("insert into customerlabel (cl_id,cl_labelcode,cl_custcode,cl_labeltype,cl_labelurl,cl_labelname,cl_labelsoft,cl_date) values( customerlabel_seq.nextval, ");
  371. sql.Append("'" + la_code.Text + "','" + (客户编号.Text == "" ? "" : 客户编号.Text) + "','" + la_type1.Text + "','" + ftpOperater.FTPAddress + "','" + la_name.Text + "','" + SoftWare + "',sysdate)");
  372. }
  373. dh.ExecuteSql(sql.ToString(), "insert");
  374. sql.Clear();
  375. }
  376. else
  377. {
  378. //目标路径
  379. string targetPath = NetURL.Text;
  380. //var file = Directory.GetFiles(targetPath);
  381. string sourceFile = Path.Combine(sourcePath, fileName);
  382. string destFile = Path.Combine(targetPath, fileName);
  383. //获取指定路径下的全部文件名
  384. var file = Directory.GetFiles(targetPath);
  385. string overwrite = "";
  386. for (int i = 0; i < file.Length; i++)
  387. {
  388. if (file[i].Substring(file[i].LastIndexOf(@"\") + 1) == fileName)
  389. {
  390. overwrite = MessageBox.Show(this.ParentForm, "已存在名为" + fileName + "的文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  391. break;
  392. }
  393. }
  394. if (overwrite == "Yes" || overwrite == "")
  395. {
  396. //不存在文件家的话进行创建
  397. if (!Directory.Exists(targetPath))
  398. Directory.CreateDirectory(targetPath);
  399. //将文件复制到指定位置
  400. File.Copy(sourceFile, destFile, true);
  401. string SoftWare = "CodeSoft";
  402. sql.Clear();
  403. sql.Append("insert into customerlabel (cl_id,cl_labelcode,cl_custcode,cl_labeltype,cl_labelurl,cl_labelname,cl_labelsoft,cl_date) values( customerlabel_seq.nextval, ");
  404. sql.Append("'" + la_code.Text + "','" + (客户编号.Text == "" ? "共用" : 客户编号.Text) + "','" + la_type1.Text + "','" + destFile + "','" + la_name.Text + "','" + SoftWare + "',sysdate)");
  405. dh.ExecuteSql(sql.ToString(), "insert");
  406. sql.Clear();
  407. }
  408. }
  409. MessageBox.Show("保存成功!");
  410. LoadData();
  411. }
  412. else
  413. {
  414. MessageBox.Show("请填写完整参数!");
  415. }
  416. }
  417. private void 产品标签维护_SizeChanged(object sender, EventArgs e)
  418. {
  419. asc.controlAutoSize(this);
  420. }
  421. private void NetURL_TextChanged(object sender, EventArgs e)
  422. {
  423. Settings.Default.ShareUrl = NetURL.Text;
  424. Settings.Default.Save();
  425. }
  426. private void NewLabel_Click(object sender, EventArgs e)
  427. {
  428. 客户编号.Text = "";
  429. la_type1.Text = "";
  430. la_code.Text = "";
  431. la_name.Text = "";
  432. FolderPath.Text = "";
  433. CL_ID2 = null;
  434. }
  435. }
  436. }