客户标签维护.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. if (CodeSoft.Checked)
  112. {
  113. string LabelName = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString();
  114. doc = lbl.Documents.Open(BaseUtil.GetLabelUrl(LabelPath, LabelName));
  115. if (doc == null)
  116. {
  117. Console.WriteLine("doc is null");
  118. }
  119. ParamNum = doc.Variables.FreeVariables.Count;
  120. //去除之前添加的控件
  121. for (int i = 0; i < (ParamName == null ? 0 : ParamName.Length); i++)
  122. {
  123. if (this.Controls["LabelParam"].Controls[ParamName[i] + "_label"] != null)
  124. {
  125. this.Controls["LabelParam"].Controls.Remove(this.Controls["LabelParam"].Controls[ParamName[i] + "_label"]);
  126. this.Controls["LabelParam"].Controls.Remove(this.Controls["LabelParam"].Controls[ParamName[i]]);
  127. }
  128. }
  129. //如果文件已不存在则进行提示
  130. if (doc == null)
  131. {
  132. //LabelParamPreview.SelectionColor = Color.Red;
  133. //LabelParamPreview.AppendText("指定路径已找不到该文件\n");
  134. return;
  135. }
  136. ParamName = new string[ParamNum];
  137. for (int i = 0; i < ParamNum; i++)
  138. {
  139. ParamName[i] = doc.Variables.FreeVariables.Item(i + 1).Name;
  140. }
  141. int y = 20;
  142. for (int i = 0; i < ParamName.Length; i++)
  143. {
  144. //变量的名称Label
  145. Label Param = new Label();
  146. Param.Text = ParamName[i];
  147. Param.Name = ParamName[i] + "_label";
  148. Param.AutoSize = true;
  149. Param.Anchor = AnchorStyles.Left;
  150. Param.Anchor = AnchorStyles.Top;
  151. Param.Location = new Point(20, y + 5);
  152. this.Controls["LabelParam"].Controls.Add(Param);
  153. //维护变量的打印测试参数
  154. EnterTextBox TempParam = new EnterTextBox();
  155. TempParam.Anchor = AnchorStyles.Left;
  156. TempParam.Anchor = AnchorStyles.Top;
  157. TempParam.Name = ParamName[i];
  158. TempParam.Location = new Point(100, y);
  159. y = y + 30;
  160. TempParam.Size = new Size(170, 22);
  161. this.Controls["LabelParam"].Controls.Add(TempParam);
  162. y = y + 30;
  163. }
  164. }
  165. }
  166. if (LabelDataGridView.Columns[e.ColumnIndex].HeaderText == "编辑")
  167. {
  168. if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
  169. {
  170. CL_ID2 = LabelDataGridView.Rows[e.RowIndex].Cells["cl_id"].Value.ToString();
  171. LabelPath = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelurl"].Value.ToString();
  172. string LabelName = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString();
  173. BaseUtil.GetLabelUrl(LabelPath, LabelName);
  174. System.Diagnostics.Process.Start(ftpOperater.DownLoadTo + LabelName);
  175. 客户编号.Text = LabelDataGridView.Rows[e.RowIndex].Cells["CL_CUSTCODE2"].Value.ToString();
  176. la_type1.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labeltype"].Value.ToString();
  177. la_code.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelcode"].Value.ToString();
  178. la_name.Text = LabelDataGridView.Rows[e.RowIndex].Cells["cl_labelname"].Value.ToString();
  179. FolderPath.Text = ftpOperater.DownLoadTo;
  180. }
  181. }
  182. //asc.controllInitializeSize(this);
  183. }
  184. private void Screen_Click(object sender, EventArgs e)
  185. {
  186. sql.Clear();
  187. condition.Clear();
  188. //如果条件全部为空的时候
  189. if (cu_code.Text == "" && cu_name.Text == "" && la_type.Text == "")
  190. {
  191. LoadData();
  192. }
  193. else
  194. {
  195. sql.Append("select 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 ");
  196. //设置筛选的条件
  197. condition.Append(BaseUtil.GetScreenSqlCondition(cu_code, la_type));
  198. //最终刷选条件拼接成的sql
  199. LoadData();
  200. }
  201. }
  202. private void Update_Click(object sender, EventArgs e)
  203. {
  204. string LabelSoft = "";
  205. //需要处理新增的行和原有数据然后更新的行
  206. if (CodeSoft.Checked)
  207. {
  208. LabelSoft = "CodeSoft";
  209. }
  210. DataTable dt = (DataTable)LabelDataGridView.DataSource;
  211. //如果有改变的行才传递到后台
  212. if (dt.GetChanges() != null)
  213. {
  214. //如果是新增的默认加上Radio当前选中的打印软件
  215. for (int i = 0; i < LabelDataGridView.Rows.Count; i++)
  216. {
  217. if (LabelDataGridView.Rows[i].Cells["cl_labelsoft"].Value.ToString() == "")
  218. {
  219. LabelDataGridView.Rows[i].Cells["cl_labelsoft"].Value = LabelSoft;
  220. }
  221. }
  222. string sql = "Insert into customerlabel (cl_ID,cl_custCODE,cl_LABELTYPE,cl_LABELCODE,cl_LABELURL,cl_DETNO,cl_LABELSOFT,cl_ISDEFAULT) values( customerlabel_seq.nextval,'" + cu_code.Text + "',:cl_LABELTYPE,:cl_LABELCODE,:cl_LABELURL,:cl_DETNO,:cl_LABELSOFT,:cl_ISDEFAULT )";
  223. dh.UpDateTableByCondition(dt.GetChanges(), "customerlabel", "cl_id", sql.ToLower());
  224. LoadData();
  225. }
  226. }
  227. private void DeleteDetail_Click(object sender, EventArgs e)
  228. {
  229. string[] deleteid = new string[LabelDataGridView.RowCount];
  230. for (int i = 0; i < LabelDataGridView.RowCount; i++)
  231. {
  232. deleteid[i] = LabelDataGridView.Rows[i].Cells["cl_id"].Value.ToString();
  233. }
  234. dh.DeleteDataByID("customerlabel", "cl_id", deleteid);
  235. LoadData();
  236. }
  237. private void OpenFolder_Click(object sender, EventArgs e)
  238. {
  239. //根据勾选的不同设置不同的文件后缀过滤
  240. if (CodeSoft.Checked)
  241. {
  242. openFileDialog1.Filter = "(*.lab)|*.lab";
  243. }
  244. DialogResult result = openFileDialog1.ShowDialog();
  245. if (result == DialogResult.OK)
  246. {
  247. FolderPath.Text = openFileDialog1.FileName;
  248. la_name.Text = FolderPath.Text.Substring(FolderPath.Text.LastIndexOf("\\") + 1);
  249. for (int i = 0; i < LabelDataGridView.Rows.Count; i++)
  250. {
  251. if (LabelDataGridView.Rows[i].Cells["Choose"].Value != null)
  252. {
  253. if ((bool)LabelDataGridView.Rows[i].Cells["Choose"].Value == true)
  254. {
  255. LabelDataGridView.Rows[i].Cells["cl_labelurl"].Value = FolderPath.Text;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. private void LabelDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  262. {
  263. if (LabelDataGridView.Columns[e.ColumnIndex].Name == "cl_isdefault")
  264. {
  265. if (e.Value != null)
  266. {
  267. switch (e.Value.ToString())
  268. {
  269. case "0":
  270. e.Value = "否";
  271. break;
  272. case "1":
  273. e.Value = "是";
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. }
  280. }
  281. private void LabelDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
  282. {
  283. //覆盖参数错误的事件,使得不会报错
  284. }
  285. private void LoadData()
  286. {
  287. //如果条件和上次一样则不进行数据读取
  288. //if (LastCondition != condition.ToString())
  289. {
  290. //如果没有任何筛选条件直接选取customerlabel的数据
  291. if (sql.Length == 0)
  292. {
  293. dt = (DataTable)dh.ExecuteSql("select 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");
  294. }
  295. else
  296. {
  297. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  298. }
  299. BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt);
  300. string Field = BaseUtil.GetGridViewSelectContent(LabelDataGridView);
  301. 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);
  302. LastCondition = condition.ToString();
  303. }
  304. }
  305. private void PrintTest_Click(object sender, EventArgs e)
  306. {
  307. if (LabelCode != "")
  308. {
  309. for (int j = 0; j < doc.Variables.FreeVariables.Count; j++)
  310. {
  311. //将维护的模板参数和模板本身的参数名称进行比对
  312. for (int k = 0; k < ParamName.Length; k++)
  313. {
  314. //名称相等的时候,取SQL进行值的查询
  315. if (doc.Variables.FreeVariables.Item(j + 1).Name == ParamName[k])
  316. {
  317. doc.Variables.FreeVariables.Item(j + 1).Value = Controls["LabelParam"].Controls[ParamName[k]].Text;
  318. }
  319. }
  320. }
  321. //保存参数打印
  322. doc.Save();
  323. doc.Printer.SwitchTo(PrinterList.Text);
  324. doc.PrintDocument();
  325. doc.Close();
  326. }
  327. }
  328. private void 产品标签维护_FormClosing(object sender, FormClosingEventArgs e)
  329. {
  330. try
  331. {
  332. lbl.Documents.CloseAll();
  333. lbl.Application.Quit();
  334. }
  335. catch (Exception)
  336. {
  337. }
  338. }
  339. private void Save_Click(object sender, EventArgs e)
  340. {
  341. if (la_type1.Text != "" && FolderPath.Text != "" && la_code.Text != "")
  342. {
  343. //文件名
  344. string fileName;
  345. //源路径
  346. string sourcePath;
  347. //未选择文件的之后默认是这个,从Text输入框中取
  348. if (openFileDialog1.FileName == "openFileDialog1")
  349. {
  350. fileName = la_name.Text;
  351. sourcePath = FolderPath.Text;
  352. }
  353. else
  354. {
  355. sourcePath = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.LastIndexOf(@"\"));
  356. fileName = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf(@"\") + 1);
  357. }
  358. if (FTPShare.Checked)
  359. {
  360. ftpOperater ftp = new ftpOperater();
  361. ftp.UpLoadFile(sourcePath, fileName);
  362. string SoftWare = "CodeSoft";
  363. if (CL_ID2 != null)
  364. {
  365. sql.Clear();
  366. sql.Append("update customerlabel set cl_labelcode='" + la_code.Text + "',cl_custcode='" + 客户编号.Text + "',");
  367. sql.Append("cl_labeltype='" + la_type1.Text + "',cl_labelname='" + la_name.Text + "' where cl_id=" + CL_ID2);
  368. }
  369. else
  370. {
  371. sql.Clear();
  372. sql.Append("insert into customerlabel (cl_id,cl_labelcode,cl_custcode,cl_labeltype,cl_labelurl,cl_labelname,cl_labelsoft) values( customerlabel_seq.nextval, ");
  373. sql.Append("'" + la_code.Text + "','" + (客户编号.Text == "" ? "" : 客户编号.Text) + "','" + la_type1.Text + "','" + ftpOperater.FTPAddress + "','" + la_name.Text + "','" + SoftWare + "')");
  374. }
  375. dh.ExecuteSql(sql.ToString(), "insert");
  376. sql.Clear();
  377. }
  378. else
  379. {
  380. //目标路径
  381. string targetPath = NetURL.Text;
  382. //var file = Directory.GetFiles(targetPath);
  383. string sourceFile = Path.Combine(sourcePath, fileName);
  384. string destFile = Path.Combine(targetPath, fileName);
  385. //获取指定路径下的全部文件名
  386. var file = Directory.GetFiles(targetPath);
  387. string overwrite = "";
  388. for (int i = 0; i < file.Length; i++)
  389. {
  390. if (file[i].Substring(file[i].LastIndexOf(@"\") + 1) == fileName)
  391. {
  392. overwrite = MessageBox.Show(this.ParentForm, "已存在名为" + fileName + "的文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  393. break;
  394. }
  395. }
  396. if (overwrite == "Yes" || overwrite == "")
  397. {
  398. //不存在文件家的话进行创建
  399. if (!Directory.Exists(targetPath))
  400. Directory.CreateDirectory(targetPath);
  401. //将文件复制到指定位置
  402. File.Copy(sourceFile, destFile, true);
  403. string SoftWare = "CodeSoft";
  404. sql.Clear();
  405. sql.Append("insert into customerlabel (cl_id,cl_labelcode,cl_custcode,cl_labeltype,cl_labelurl,cl_labelname,cl_labelsoft) values( customerlabel_seq.nextval, ");
  406. sql.Append("'" + la_code.Text + "','" + (客户编号.Text == "" ? "共用" : 客户编号.Text) + "','" + la_type1.Text + "','" + destFile + "','" + la_name.Text + "','" + SoftWare + "')");
  407. dh.ExecuteSql(sql.ToString(), "insert");
  408. sql.Clear();
  409. }
  410. }
  411. MessageBox.Show("保存成功!");
  412. LoadData();
  413. }
  414. else
  415. {
  416. MessageBox.Show("请填写完整参数!");
  417. }
  418. }
  419. private void 产品标签维护_SizeChanged(object sender, EventArgs e)
  420. {
  421. asc.controlAutoSize(this);
  422. }
  423. private void NetURL_TextChanged(object sender, EventArgs e)
  424. {
  425. Settings.Default.ShareUrl = NetURL.Text;
  426. Settings.Default.Save();
  427. }
  428. private void NewLabel_Click(object sender, EventArgs e)
  429. {
  430. 客户编号.Text = "";
  431. la_type1.Text = "";
  432. la_code.Text = "";
  433. la_name.Text = "";
  434. FolderPath.Text = "";
  435. CL_ID2 = null;
  436. }
  437. }
  438. }