using Dapper; using System; using System.Collections.Generic; using System.Linq; using System.Web; using VueWebApi.Models; namespace VueWebApi.Tools { public class AppLableBarCode { public static ToMessage mes = new ToMessage(); //定义全局返回信息对象 #region [获取规则编码] /// /// 根据功能编码获取最新规则编码 /// /// 功能编码 /// 物料编码 /// 数量 /// 单标签数量 /// public static ToMessage EncodingSeach(string rightcode,string partcode, string qty, string onelabqty) { string sql, year="", month="", day="", fdate="", q="", cunm2 = "", Encode="", cunm = ""; int digit2 = 0, value2 = 0, digit3 = 0, digitdiff = 0; bool stf; List list = new List(); List listdt = new List(); List lab = new List(); var dynamicParams = new DynamicParameters(); //获取标签个数:向下取整 decimal labcum = Math.Floor(decimal.Parse(qty) / decimal.Parse(onelabqty)); try { sql = @"select prefix,filingdate,incbit,value from T_CodeRules where rightcode=@rightcode"; dynamicParams.Add("@rightcode", rightcode); var data = DapperHelper.selectdata(sql, dynamicParams); if (data.Rows.Count > 0) { string prefix = data.Rows[0]["PREFIX"].ToString(); //固定字段 string filingdate = data.Rows[0]["FILINGDATE"].ToString(); //提交日期 int incbit = int.Parse(data.Rows[0]["INCBIT"].ToString()); //自增位数 string value = data.Rows[0]["VALUE"].ToString(); //流水号 decimal sumqty = 0;//定义累计单标签数量 if (int.Parse(value) >=0) { switch (filingdate) { case "年月日": lab = Labcode(partcode,year, month, day, prefix, fdate, digit2, value2, digit3, digitdiff, q, cunm2, Encode, value, labcum, sumqty, onelabqty, rightcode, qty,out cunm); break; case "年月": lab = Labcode(partcode,year, month, day, prefix, fdate, digit2, value2, digit3, digitdiff, q, cunm2, Encode, value, labcum, sumqty, onelabqty, rightcode, qty, out cunm); break; case "年": lab = Labcode(partcode,year, month, day, prefix, fdate, digit2, value2, digit3, digitdiff, q, cunm2, Encode, value, labcum, sumqty, onelabqty, rightcode, qty, out cunm); break; default: break; } //更新规则表位数及流水号 sql = @"update T_CodeRules set incbit=@incbit,value=@cunm2 where rightcode=@rightcode"; dynamicParams.Add("@incbit", cunm.Length); dynamicParams.Add("@cunm2", cunm); dynamicParams.Add("@rightcode", rightcode); list.Add(new { str = sql, parm = dynamicParams }); stf = DapperHelper.DoTransaction(list); if (stf) { mes.code = "200"; mes.Message = "成功!"; mes.data = lab; } else { mes.code = "300"; mes.count = 0; mes.Message = "失败!"; mes.data = null; } } } else { mes.code = "300"; mes.count = 0; mes.Message = "未设置编码规则,生成单号失败,请设置编码为【" + rightcode + "】的编码规则!"; mes.data = null; } } catch (Exception e) { mes.code = "300"; mes.count = 0; mes.Message = e.Message; mes.data = null; } return mes; } #endregion #region[生成条码] /// /// 生成条码 /// /// 物料编码 /// 年 /// 月 /// 日 /// 固定字段 /// 年(2位)+月(2位)+日(2位) /// 流水号长度 /// 自增流水号 /// 自增流水号长度 /// 位数差 /// 补位(0) /// 最终流水号(包含补位) /// 最终标签码(固定字段+规则+最终流水号) /// 初始查询最新流水号 /// 标签个数 /// 累计单标签数量 /// 单标签数量 /// 功能编码 /// 数量 /// 输出最终流水号 /// public static List Labcode(string partcode,string year,string month,string day,string prefix, string fdate,int digit2,int value2,int digit3,int digitdiff,string q,string cunm2,string Encode, string value, decimal labcum, decimal sumqty,string onelabqty,string rightcode,string qty,out string cunm) { List lab = new List(); year = DateTime.Now.Year.ToString().Substring(2, 2);//获取年(2位) month = DateTime.Now.Month.ToString().PadLeft(2, '0');//获取月(2位) day = DateTime.Now.Day.ToString().PadLeft(2, '0');//获取日(2位) fdate = year + month + day; //获取查询流水号的位数 digit2 = value.Length;//获取string类型位数 for (int i = 1; i <= labcum; i++) { sumqty += decimal.Parse(onelabqty); AppLabCode lb = new AppLabCode(); lb.rightcode = rightcode; if (i == labcum) //最后一张标签 { lb.labqty = (decimal.Parse(qty) - sumqty).ToString(); //单标签数量 } else { lb.labqty = onelabqty; //单标签数量 } //流水号循环自增 value2 = int.Parse(value) + i; //获取自增后流水号位数 digit3 = value2.ToString().Length; //获取位数之差 digitdiff = digit2 - digit3; //重新获取流水号位数 digit2 = digit3; q = ""; if (digitdiff < 0) //位数差为负数需要进补位 { cunm2 = value2.ToString();//最终流水号 } else { for (int j = 1; j <= digitdiff; j++) { q = q + "0"; } cunm2 = q + value2.ToString();//最终流水号 } Encode = prefix + fdate + cunm2; //最终编码 lb.labcode = Encode; lb.partnumber = partcode; lab.Add(lb); } cunm = cunm2; return lab; } #endregion } }