using LYFZ.Software.MainBusiness.Dresses; using LYFZ.WinAPI; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Printing; using System.Linq; using System.Text; using System.Windows.Forms; using ThoughtWorks.QRCode.Codec; namespace LYFZ.Software.MainBusiness.OAShopManagement { public class frmCloudProductInfo : Software.UI.OAShopManagement.frmCloudProductInfo { public string servicePath = "http://webapi.lyfz.net/"; string EnterpriseID = ""; Newtonsoft.Json.Linq.JArray jarray; PrintDocument printDocument1 = new PrintDocument(); int cPageCount = 0;//当前打印页 int PageSumCount = 1;//总页数 List qrPathList = new List(); Timer timer; public frmCloudProductInfo() { this.btn_Print.Click += btn_Print_Click; this.btn_Delete.Click += btn_Delete_Click; this.btn_Dress.Click += btn_Dress_Click; this.btn_Product.Click += btn_Product_Click; this.btn_Search.Click += btn_Search_Click; this.Load += frmCloudProductInfo_Load; GetEnterpriseID(); this.dataGridView_Data.CellDoubleClick += dataGridView_Data_CellDoubleClick; this.dataGridView_Data.CellClick += dataGridView_Data_CellClick; this.printDocument1.BeginPrint += printDocument1_BeginPrint; this.printDocument1.PrintPage += printDocument1_PrintPage; } void dataGridView_Data_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0 && e.RowIndex>-1) { System.Windows.Forms.DataGridViewRow dr = this.dataGridView_Data.Rows[e.RowIndex]; dr.Cells[0].Value = !Convert.ToBoolean(dr.Cells[0].Value); } } void dataGridView_Data_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e) { if (e.ColumnIndex > 0 && e.RowIndex>-1) { System.Windows.Forms.DataGridViewRow dr = this.dataGridView_Data.Rows[e.RowIndex]; frmSubmitDataToService frmDataToService = new frmSubmitDataToService(); frmDataToService.InitDataInfo(dr.Cells[4].Value.ToString(), dr.Cells[2].Tag.ToString()); frmDataToService.ShowDialog(); } } void frmCloudProductInfo_Load(object sender, EventArgs e) { timer = new Timer(); timer.Enabled = true; timer.Interval = 200; timer.Tick += timer_Tick; } void timer_Tick(object sender, EventArgs e) { timer.Enabled = false; GetServiceDataList(); } void GetServiceDataList() { if (!string.IsNullOrEmpty(EnterpriseID)) { dynamic dyObj = new System.Dynamic.ExpandoObject(); if (dyObj != null) { dyObj.type = "GetScanProductInfoList"; dyObj.EnterpriseID = EnterpriseID; try { string jsonData = HttpClientHelper.PostResponse(servicePath + "admin/ajax/AjaxScanProductInfoHandler.ashx", Newtonsoft.Json.JsonConvert.SerializeObject(dyObj), "application/x-www-form-urlencoded"); Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonData); if (Convert.ToBoolean(jobject.GetValue("status"))) { jarray = (Newtonsoft.Json.Linq.JArray)jobject.GetValue("Data"); BindServiceDataList(jarray); } } catch(Exception e) { MessageBoxCustom.Show(e.Message); } } } } void BindServiceDataList(Newtonsoft.Json.Linq.JArray _jarray) { this.dataGridView_Data.Rows.Clear(); foreach (Newtonsoft.Json.Linq.JObject jObj in _jarray) { int indx = this.dataGridView_Data.Rows.Add(); this.dataGridView_Data.Rows[indx].Cells[0].Value = false; this.dataGridView_Data.Rows[indx].Cells[1].Value = jObj.GetValue("EnterpriseID").ToString(); this.dataGridView_Data.Rows[indx].Cells[2].Value = jObj.GetValue("ProType").ToString() == "Product"?"商品":"礼服"; this.dataGridView_Data.Rows[indx].Cells[2].Tag = jObj.GetValue("ProType").ToString(); this.dataGridView_Data.Rows[indx].Cells[3].Value = jObj.GetValue("ProTypeList").ToString(); this.dataGridView_Data.Rows[indx].Cells[4].Value = jObj.GetValue("ProNumber").ToString(); this.dataGridView_Data.Rows[indx].Cells[5].Value = jObj.GetValue("ProName").ToString(); this.dataGridView_Data.Rows[indx].Tag = jObj.GetValue("ID").ToString(); } } void GetEnterpriseID() { if (string.IsNullOrEmpty(EnterpriseID)) { LYFZ.BLL.BLL_ErpSystemConfigure config = new BLL.BLL_ErpSystemConfigure(); System.Data.DataSet ds = config.GetSystemConfigure("*", "AppInterface"); if (ds.Tables[0].Rows.Count > 0) { LYFZ.Model.Model_ErpSystemConfigure model = config.GetModel(Convert.ToInt32(ds.Tables[0].Rows[0]["ID"])); try { Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(model.Sconfig_Value); EnterpriseID = jobject.GetValue("AppEnId").ToString(); } catch { } } } } void btn_Search_Click(object sender, EventArgs e) { string searchText = tb_SearchText.Text; if (!string.IsNullOrEmpty(searchText)) { Newtonsoft.Json.Linq.JArray tempJarray = new Newtonsoft.Json.Linq.JArray(); foreach (Newtonsoft.Json.Linq.JObject jobj in jarray) { string ProNumber = jobj.GetValue("ProNumber").ToString(); string ProName = jobj.GetValue("ProName").ToString(); if (ProNumber.Contains(searchText) || ProName.Contains(searchText)) { tempJarray.Add(jobj); } } BindServiceDataList(tempJarray); } else { BindServiceDataList(jarray); } } void btn_Product_Click(object sender, EventArgs e) { LYFZ.Software.MainBusiness.InitialSet.FrmProductSet productSet = new InitialSet.FrmProductSet(); productSet.ShowDialog(); timer.Enabled = true; } void btn_Dress_Click(object sender, EventArgs e) { LYFZ.Software.MainBusiness.Dresses.DressEnteredManagedFormMain manageFromMain = new Dresses.DressEnteredManagedFormMain(); manageFromMain.ShowDialog(); timer.Enabled = true; } void btn_Delete_Click(object sender, EventArgs e) { if (this.dataGridView_Data.SelectedRows.Count > 0) { if (MessageBoxCustom.Show("是否确定删除?", "提示", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { System.Windows.Forms.DataGridViewRow dr = this.dataGridView_Data.SelectedRows[0]; dynamic dyObj = new System.Dynamic.ExpandoObject(); if (dyObj != null) { dyObj.type = "DeleteScanProductInfoById"; dyObj.ID = dr.Tag.ToString(); try { string jsonData = HttpClientHelper.PostResponse(servicePath + "admin/ajax/AjaxScanProductInfoHandler.ashx", Newtonsoft.Json.JsonConvert.SerializeObject(dyObj), "application/x-www-form-urlencoded"); Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonData); if (Convert.ToBoolean(jobject.GetValue("status"))) { GetServiceDataList(); } }catch(Exception ex) { MessageBoxCustom.Show(ex.Message); } } } } } void btn_Print_Click(object sender, EventArgs e) { var dataGridView = dataGridView_Data.Rows; qrPathList.Clear(); List rowList = new List(); foreach(System.Windows.Forms.DataGridViewRow row in dataGridView) { if(Convert.ToBoolean(row.Cells[0].Value)) { rowList.Add(row); qrPathList.Add(new QRCodeData() { Name = row.Cells["商品名称"].Value.ToString(), UrlPath = servicePath + "WebApp/Product/ProductInfo.aspx?enterpriseID=" + row.Cells["企业编号"].Value.ToString() + "&proNumber=" + row.Cells["商品编号"].Value.ToString() + "&proType=" + row.Cells["商品类型"].Tag.ToString() + "" }); } } foreach(QRCodeData mod in qrPathList) { mod.DrawImage = CreateQRCode(mod.UrlPath); } if (qrPathList.Count==0) { MessageBoxCustom.Show("无打印的数据"); return; } cPageCount = 0; PageSumCount = qrPathList.Count; this.printDocument1.Print(); } Image CreateQRImage(string path,string Name) { string LoginQrCodeImageValue = path;//LYFZ.WinAPI.SDKSecurity.EncodeBase64(Encoding.UTF8, path); System.Drawing.Image qrBmp = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.CreateCode_Choose(LoginQrCodeImageValue, "Byte", "M", 8, 4); Bitmap LoginQrCodeImage = new System.Drawing.Bitmap(150, 150); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(LoginQrCodeImage)) { g.Clear(System.Drawing.Color.White); 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); g.DrawString(Name, new Font("黑体",9f), System.Drawing.Brushes.Black, new System.Drawing.PointF(80, 20)); } qrBmp.Dispose(); Image image = qrBmp; return image; } /// /// 生成二维码,如果有Logo,则在二维码中添加Logo /// /// /// public Bitmap CreateQRCode(string content) { QRCodeEncoder qrEncoder = new QRCodeEncoder(); qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //编码方式:BYTE 能支持中文,ALPHA_NUMERIC 扫描出来的都是数字 qrEncoder.QRCodeScale = Convert.ToInt32(4); //大小:值越大生成的二维码图片像素越高 qrEncoder.QRCodeVersion = Convert.ToInt32(8);//版本 qrEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //错误校验、更正等级 try { Bitmap qrcode = qrEncoder.Encode(content, Encoding.UTF8); /*if (!logoImagepath.Equals(string.Empty)) { Graphics g = Graphics.FromImage(qrcode); Bitmap bitmapLogo = new Bitmap(logoImagepath); int logoSize = Convert.ToInt32(txtLogoSize.Text); bitmapLogo = new Bitmap(bitmapLogo, new System.Drawing.Size(logoSize, logoSize)); PointF point = new PointF(qrcode.Width / 2 - logoSize / 2, qrcode.Height / 2 - logoSize / 2); g.DrawImage(bitmapLogo, point); }*/ return qrcode; } catch (IndexOutOfRangeException ex) { MessageBox.Show("超出当前二维码版本的容量上限,请选择更高的二维码版本!", "系统提示"); return new Bitmap(100, 100); } catch (Exception ex) { MessageBox.Show("生成二维码出错!", "系统提示"); return new Bitmap(100, 100); } } /// /// 打印开始重置为初始 /// /// /// void printDocument1_BeginPrint(object sender, PrintEventArgs e) { cPageCount = 0; } /// /// 打印2358865 /// /// /// void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { float fscaling = 1f; try { fscaling = float.Parse("0.58"); } catch (Exception ex) { e.Cancel = true; MessageBox.Show(ex.Message); return; } //e.PageSettings.Margins.Left = absCode128.MMToHundredthsInch(1); //e.PageSettings.Margins.Right = absCode128.MMToHundredthsInch(1); e.Graphics.Clear(Color.White); if (this.qrPathList.Count == 0) { e.Cancel = true; MessageBox.Show("请先生成条二维码"); return; } QRCodeData myBarcode = null; //是按图片打印还是按模板 bool IsPrintphotomode = false; int PaperWidth = e.PageSettings.PaperSize.Width; bool IsLandscape = e.PageSettings.Landscape; int Location_X = 1; int Location_Y = 2; if (IsLandscape) { Location_X = 13; Location_Y = 2; } if (PageSumCount <= 0) { if (IsPrintphotomode) { Image img = qrPathList[cPageCount].DrawImage; e.Graphics.DrawImage(img, new Rectangle(Location_X, Location_Y, 100, 100)); } else { Image img = qrPathList[cPageCount].DrawImage; e.Graphics.DrawImage(img, new Rectangle(Location_X, Location_Y,100,100)); //myBarcode.DrawBarCode(e.Graphics, Location_X, Location_Y, fscaling, PaperWidth: PaperWidth); } e.Graphics.DrawString(qrPathList[cPageCount].Name, new Font("黑体", 9f), System.Drawing.Brushes.Black, new System.Drawing.PointF(5, 103)); } else { if (IsPrintphotomode) { Image img = qrPathList[cPageCount].DrawImage; e.Graphics.DrawImage(img, new Rectangle(Location_X, Location_Y, 100, 100)); //Image img = qrPathList[cPageCount].GetBarCodeImage(e.Graphics.DpiX, e.Graphics.DpiY); //e.Graphics.DrawImage(img, Location_X, Location_Y); } else { //qrPathList[cPageCount].DrawBarCode(e.Graphics, Location_X, Location_Y, fscaling, PaperWidth: PaperWidth); Image img = qrPathList[cPageCount].DrawImage; e.Graphics.DrawImage(img, new Rectangle(Location_X, Location_Y, 100, 100)); } e.Graphics.DrawString(qrPathList[cPageCount].Name, new Font("黑体", 9f), System.Drawing.Brushes.Black, new System.Drawing.PointF(5, 103)); cPageCount++; if (cPageCount < PageSumCount) { e.HasMorePages = true; } } } } class QRCodeData { public string Name { get; set; } public string UrlPath { get; set; } public Image DrawImage { get; set; } } }