frmCloudProductInfo.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using LYFZ.Software.MainBusiness.Dresses;
  2. using LYFZ.WinAPI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using ThoughtWorks.QRCode.Codec;
  11. namespace LYFZ.Software.MainBusiness.OAShopManagement
  12. {
  13. public class frmCloudProductInfo : Software.UI.OAShopManagement.frmCloudProductInfo
  14. {
  15. public string servicePath = "http://webapi.lyfz.net/";
  16. string EnterpriseID = "";
  17. Newtonsoft.Json.Linq.JArray jarray;
  18. PrintDocument printDocument1 = new PrintDocument();
  19. int cPageCount = 0;//当前打印页
  20. int PageSumCount = 1;//总页数
  21. List<QRCodeData> qrPathList = new List<QRCodeData>();
  22. Timer timer;
  23. public frmCloudProductInfo()
  24. {
  25. this.btn_Print.Click += btn_Print_Click;
  26. this.btn_Delete.Click += btn_Delete_Click;
  27. this.btn_Dress.Click += btn_Dress_Click;
  28. this.btn_Product.Click += btn_Product_Click;
  29. this.btn_Search.Click += btn_Search_Click;
  30. this.Load += frmCloudProductInfo_Load;
  31. GetEnterpriseID();
  32. this.dataGridView_Data.CellDoubleClick += dataGridView_Data_CellDoubleClick;
  33. this.dataGridView_Data.CellClick += dataGridView_Data_CellClick;
  34. this.printDocument1.BeginPrint += printDocument1_BeginPrint;
  35. this.printDocument1.PrintPage += printDocument1_PrintPage;
  36. }
  37. void dataGridView_Data_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
  38. {
  39. if (e.ColumnIndex == 0 && e.RowIndex>-1)
  40. {
  41. System.Windows.Forms.DataGridViewRow dr = this.dataGridView_Data.Rows[e.RowIndex];
  42. dr.Cells[0].Value = !Convert.ToBoolean(dr.Cells[0].Value);
  43. }
  44. }
  45. void dataGridView_Data_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
  46. {
  47. if (e.ColumnIndex > 0 && e.RowIndex>-1)
  48. {
  49. System.Windows.Forms.DataGridViewRow dr = this.dataGridView_Data.Rows[e.RowIndex];
  50. frmSubmitDataToService frmDataToService = new frmSubmitDataToService();
  51. frmDataToService.InitDataInfo(dr.Cells[4].Value.ToString(), dr.Cells[2].Tag.ToString());
  52. frmDataToService.ShowDialog();
  53. }
  54. }
  55. void frmCloudProductInfo_Load(object sender, EventArgs e)
  56. {
  57. timer = new Timer();
  58. timer.Enabled = true;
  59. timer.Interval = 200;
  60. timer.Tick += timer_Tick;
  61. }
  62. void timer_Tick(object sender, EventArgs e)
  63. {
  64. timer.Enabled = false;
  65. GetServiceDataList();
  66. }
  67. void GetServiceDataList()
  68. {
  69. if (!string.IsNullOrEmpty(EnterpriseID))
  70. {
  71. dynamic dyObj = new System.Dynamic.ExpandoObject();
  72. if (dyObj != null)
  73. {
  74. dyObj.type = "GetScanProductInfoList";
  75. dyObj.EnterpriseID = EnterpriseID;
  76. try
  77. {
  78. string jsonData = HttpClientHelper.PostResponse(servicePath + "admin/ajax/AjaxScanProductInfoHandler.ashx", Newtonsoft.Json.JsonConvert.SerializeObject(dyObj), "application/x-www-form-urlencoded");
  79. Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonData);
  80. if (Convert.ToBoolean(jobject.GetValue("status")))
  81. {
  82. jarray = (Newtonsoft.Json.Linq.JArray)jobject.GetValue("Data");
  83. BindServiceDataList(jarray);
  84. }
  85. }
  86. catch(Exception e)
  87. {
  88. MessageBoxCustom.Show(e.Message);
  89. }
  90. }
  91. }
  92. }
  93. void BindServiceDataList(Newtonsoft.Json.Linq.JArray _jarray)
  94. {
  95. this.dataGridView_Data.Rows.Clear();
  96. foreach (Newtonsoft.Json.Linq.JObject jObj in _jarray)
  97. {
  98. int indx = this.dataGridView_Data.Rows.Add();
  99. this.dataGridView_Data.Rows[indx].Cells[0].Value = false;
  100. this.dataGridView_Data.Rows[indx].Cells[1].Value = jObj.GetValue("EnterpriseID").ToString();
  101. this.dataGridView_Data.Rows[indx].Cells[2].Value = jObj.GetValue("ProType").ToString() == "Product"?"商品":"礼服";
  102. this.dataGridView_Data.Rows[indx].Cells[2].Tag = jObj.GetValue("ProType").ToString();
  103. this.dataGridView_Data.Rows[indx].Cells[3].Value = jObj.GetValue("ProTypeList").ToString();
  104. this.dataGridView_Data.Rows[indx].Cells[4].Value = jObj.GetValue("ProNumber").ToString();
  105. this.dataGridView_Data.Rows[indx].Cells[5].Value = jObj.GetValue("ProName").ToString();
  106. this.dataGridView_Data.Rows[indx].Tag = jObj.GetValue("ID").ToString();
  107. }
  108. }
  109. void GetEnterpriseID()
  110. {
  111. if (string.IsNullOrEmpty(EnterpriseID))
  112. {
  113. LYFZ.BLL.BLL_ErpSystemConfigure config = new BLL.BLL_ErpSystemConfigure();
  114. System.Data.DataSet ds = config.GetSystemConfigure("*", "AppInterface");
  115. if (ds.Tables[0].Rows.Count > 0)
  116. {
  117. LYFZ.Model.Model_ErpSystemConfigure model = config.GetModel(Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]));
  118. try
  119. {
  120. Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(model.Sconfig_Value);
  121. EnterpriseID = jobject.GetValue("AppEnId").ToString();
  122. }
  123. catch
  124. {
  125. }
  126. }
  127. }
  128. }
  129. void btn_Search_Click(object sender, EventArgs e)
  130. {
  131. string searchText = tb_SearchText.Text;
  132. if (!string.IsNullOrEmpty(searchText))
  133. {
  134. Newtonsoft.Json.Linq.JArray tempJarray = new Newtonsoft.Json.Linq.JArray();
  135. foreach (Newtonsoft.Json.Linq.JObject jobj in jarray)
  136. {
  137. string ProNumber = jobj.GetValue("ProNumber").ToString();
  138. string ProName = jobj.GetValue("ProName").ToString();
  139. if (ProNumber.Contains(searchText) || ProName.Contains(searchText))
  140. {
  141. tempJarray.Add(jobj);
  142. }
  143. }
  144. BindServiceDataList(tempJarray);
  145. }
  146. else
  147. {
  148. BindServiceDataList(jarray);
  149. }
  150. }
  151. void btn_Product_Click(object sender, EventArgs e)
  152. {
  153. LYFZ.Software.MainBusiness.InitialSet.FrmProductSet productSet = new InitialSet.FrmProductSet();
  154. productSet.ShowDialog();
  155. timer.Enabled = true;
  156. }
  157. void btn_Dress_Click(object sender, EventArgs e)
  158. {
  159. LYFZ.Software.MainBusiness.Dresses.DressEnteredManagedFormMain manageFromMain = new Dresses.DressEnteredManagedFormMain();
  160. manageFromMain.ShowDialog();
  161. timer.Enabled = true;
  162. }
  163. void btn_Delete_Click(object sender, EventArgs e)
  164. {
  165. if (this.dataGridView_Data.SelectedRows.Count > 0)
  166. {
  167. if (MessageBoxCustom.Show("是否确定删除?", "提示", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
  168. {
  169. System.Windows.Forms.DataGridViewRow dr = this.dataGridView_Data.SelectedRows[0];
  170. dynamic dyObj = new System.Dynamic.ExpandoObject();
  171. if (dyObj != null)
  172. {
  173. dyObj.type = "DeleteScanProductInfoById";
  174. dyObj.ID = dr.Tag.ToString();
  175. try
  176. {
  177. string jsonData = HttpClientHelper.PostResponse(servicePath + "admin/ajax/AjaxScanProductInfoHandler.ashx", Newtonsoft.Json.JsonConvert.SerializeObject(dyObj), "application/x-www-form-urlencoded");
  178. Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonData);
  179. if (Convert.ToBoolean(jobject.GetValue("status")))
  180. {
  181. GetServiceDataList();
  182. }
  183. }catch(Exception ex)
  184. {
  185. MessageBoxCustom.Show(ex.Message);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. void btn_Print_Click(object sender, EventArgs e)
  192. {
  193. var dataGridView = dataGridView_Data.Rows;
  194. qrPathList.Clear();
  195. List<System.Windows.Forms.DataGridViewRow> rowList = new List<System.Windows.Forms.DataGridViewRow>();
  196. foreach(System.Windows.Forms.DataGridViewRow row in dataGridView)
  197. {
  198. if(Convert.ToBoolean(row.Cells[0].Value))
  199. {
  200. rowList.Add(row);
  201. qrPathList.Add(new QRCodeData() {
  202. Name = row.Cells["商品名称"].Value.ToString(),
  203. UrlPath = servicePath + "WebApp/Product/ProductInfo.aspx?enterpriseID=" + row.Cells["企业编号"].Value.ToString() + "&proNumber=" + row.Cells["商品编号"].Value.ToString() + "&proType=" + row.Cells["商品类型"].Tag.ToString() + ""
  204. });
  205. }
  206. }
  207. foreach(QRCodeData mod in qrPathList)
  208. {
  209. mod.DrawImage = CreateQRCode(mod.UrlPath);
  210. }
  211. if (qrPathList.Count==0)
  212. {
  213. MessageBoxCustom.Show("无打印的数据");
  214. return;
  215. }
  216. cPageCount = 0;
  217. PageSumCount = qrPathList.Count;
  218. this.printDocument1.Print();
  219. }
  220. Image CreateQRImage(string path,string Name)
  221. {
  222. string LoginQrCodeImageValue = path;//LYFZ.WinAPI.SDKSecurity.EncodeBase64(Encoding.UTF8, path);
  223. System.Drawing.Image qrBmp = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.CreateCode_Choose(LoginQrCodeImageValue, "Byte", "M", 8, 4);
  224. Bitmap LoginQrCodeImage = new System.Drawing.Bitmap(150, 150);
  225. using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(LoginQrCodeImage))
  226. {
  227. g.Clear(System.Drawing.Color.White);
  228. g.DrawImage(qrBmp, new System.Drawing.Rectangle(50, 60, 280, 280), new System.Drawing.Rectangle(0, 0, qrBmp.Size.Width, qrBmp.Size.Height), System.Drawing.GraphicsUnit.Pixel);
  229. g.DrawString(Name, new Font("黑体",9f), System.Drawing.Brushes.Black, new System.Drawing.PointF(80, 20));
  230. }
  231. qrBmp.Dispose();
  232. Image image = qrBmp;
  233. return image;
  234. }
  235. /// <summary>
  236. /// 生成二维码,如果有Logo,则在二维码中添加Logo
  237. /// </summary>
  238. /// <param name="content"></param>
  239. /// <returns></returns>
  240. public Bitmap CreateQRCode(string content)
  241. {
  242. QRCodeEncoder qrEncoder = new QRCodeEncoder();
  243. qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //编码方式:BYTE 能支持中文,ALPHA_NUMERIC 扫描出来的都是数字
  244. qrEncoder.QRCodeScale = Convert.ToInt32(4); //大小:值越大生成的二维码图片像素越高
  245. qrEncoder.QRCodeVersion = Convert.ToInt32(8);//版本
  246. qrEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //错误校验、更正等级
  247. try
  248. {
  249. Bitmap qrcode = qrEncoder.Encode(content, Encoding.UTF8);
  250. /*if (!logoImagepath.Equals(string.Empty))
  251. {
  252. Graphics g = Graphics.FromImage(qrcode);
  253. Bitmap bitmapLogo = new Bitmap(logoImagepath);
  254. int logoSize = Convert.ToInt32(txtLogoSize.Text);
  255. bitmapLogo = new Bitmap(bitmapLogo, new System.Drawing.Size(logoSize, logoSize));
  256. PointF point = new PointF(qrcode.Width / 2 - logoSize / 2, qrcode.Height / 2 - logoSize / 2);
  257. g.DrawImage(bitmapLogo, point);
  258. }*/
  259. return qrcode;
  260. }
  261. catch (IndexOutOfRangeException ex)
  262. {
  263. MessageBox.Show("超出当前二维码版本的容量上限,请选择更高的二维码版本!", "系统提示");
  264. return new Bitmap(100, 100);
  265. }
  266. catch (Exception ex)
  267. {
  268. MessageBox.Show("生成二维码出错!", "系统提示");
  269. return new Bitmap(100, 100);
  270. }
  271. }
  272. /// <summary>
  273. /// 打印开始重置为初始
  274. /// </summary>
  275. /// <param name="sender"></param>
  276. /// <param name="e"></param>
  277. void printDocument1_BeginPrint(object sender, PrintEventArgs e)
  278. {
  279. cPageCount = 0;
  280. }
  281. /// <summary>
  282. /// 打印2358865
  283. /// </summary>
  284. /// <param name="sender"></param>
  285. /// <param name="e"></param>
  286. void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  287. {
  288. float fscaling = 1f;
  289. try
  290. {
  291. fscaling = float.Parse("0.58");
  292. }
  293. catch (Exception ex)
  294. {
  295. e.Cancel = true;
  296. MessageBox.Show(ex.Message);
  297. return;
  298. }
  299. //e.PageSettings.Margins.Left = absCode128.MMToHundredthsInch(1);
  300. //e.PageSettings.Margins.Right = absCode128.MMToHundredthsInch(1);
  301. e.Graphics.Clear(Color.White);
  302. if (this.qrPathList.Count == 0)
  303. {
  304. e.Cancel = true;
  305. MessageBox.Show("请先生成条二维码");
  306. return;
  307. }
  308. QRCodeData myBarcode = null;
  309. //是按图片打印还是按模板
  310. bool IsPrintphotomode = false;
  311. int PaperWidth = e.PageSettings.PaperSize.Width;
  312. bool IsLandscape = e.PageSettings.Landscape;
  313. int Location_X = 1;
  314. int Location_Y = 2;
  315. if (IsLandscape)
  316. {
  317. Location_X = 13;
  318. Location_Y = 2;
  319. }
  320. if (PageSumCount <= 0)
  321. {
  322. if (IsPrintphotomode)
  323. {
  324. Image img = qrPathList[cPageCount].DrawImage;
  325. e.Graphics.DrawImage(img, new Rectangle(Location_X, Location_Y, 100, 100));
  326. }
  327. else
  328. {
  329. Image img = qrPathList[cPageCount].DrawImage;
  330. e.Graphics.DrawImage(img, new Rectangle(Location_X, Location_Y,100,100));
  331. //myBarcode.DrawBarCode(e.Graphics, Location_X, Location_Y, fscaling, PaperWidth: PaperWidth);
  332. }
  333. e.Graphics.DrawString(qrPathList[cPageCount].Name, new Font("黑体", 9f), System.Drawing.Brushes.Black, new System.Drawing.PointF(5, 103));
  334. }
  335. else
  336. {
  337. if (IsPrintphotomode)
  338. {
  339. Image img = qrPathList[cPageCount].DrawImage;
  340. e.Graphics.DrawImage(img, new Rectangle(Location_X, Location_Y, 100, 100));
  341. //Image img = qrPathList[cPageCount].GetBarCodeImage(e.Graphics.DpiX, e.Graphics.DpiY);
  342. //e.Graphics.DrawImage(img, Location_X, Location_Y);
  343. }
  344. else
  345. {
  346. //qrPathList[cPageCount].DrawBarCode(e.Graphics, Location_X, Location_Y, fscaling, PaperWidth: PaperWidth);
  347. Image img = qrPathList[cPageCount].DrawImage;
  348. e.Graphics.DrawImage(img, new Rectangle(Location_X, Location_Y, 100, 100));
  349. }
  350. e.Graphics.DrawString(qrPathList[cPageCount].Name, new Font("黑体", 9f), System.Drawing.Brushes.Black, new System.Drawing.PointF(5, 103));
  351. cPageCount++;
  352. if (cPageCount < PageSumCount)
  353. {
  354. e.HasMorePages = true;
  355. }
  356. }
  357. }
  358. }
  359. class QRCodeData
  360. {
  361. public string Name { get; set; }
  362. public string UrlPath { get; set; }
  363. public Image DrawImage { get; set; }
  364. }
  365. }