using Dapper;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Threading.Tasks;
|
using VueWebCoreApi.Models;
|
|
namespace VueWebCoreApi.Tools
|
{
|
public class AppLableBarCode
|
{
|
public static ToMessage mes = new ToMessage(); //定义全局返回信息对象
|
|
#region [获取规则编码]
|
/// <summary>
|
/// 根据功能编码获取最新规则编码
|
/// </summary>
|
/// <param name="rightcode">功能编码</param>
|
/// <param name="partcode">物料编码</param>
|
/// <param name="qty">数量</param>
|
/// <param name="onelabqty">单标签数量</param>
|
/// <returns></returns>
|
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;
|
decimal labcum = 0;
|
bool stf;
|
List<object> list = new List<object>();
|
List<string> listdt = new List<string>();
|
List<AppLabCode> lab = new List<AppLabCode>();
|
var dynamicParams = new DynamicParameters();
|
//获取标签个数:进一法
|
if (qty == "0" && onelabqty == "0")
|
{
|
labcum = 1;
|
}
|
else
|
{
|
labcum = Math.Ceiling(decimal.Parse(qty) / decimal.Parse(onelabqty));
|
}
|
try
|
{
|
sql = @"select prefix,filingdate,incbit,value from TCodeRules where menucode=@menucode";
|
dynamicParams.Add("@menucode", 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 TCodeRules set incbit=@incbit,value=@cunm2 where menucode=@menucode";
|
dynamicParams.Add("@incbit", cunm.Length);
|
dynamicParams.Add("@cunm2", cunm);
|
dynamicParams.Add("@menucode", 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[生成条码]
|
/// <summary>
|
/// 生成条码
|
/// </summary>
|
/// <param name="partcode">物料编码</param>
|
/// <param name="year">年</param>
|
/// <param name="month">月</param>
|
/// <param name="day">日</param>
|
/// <param name="prefix">固定字段</param>
|
/// <param name="fdate">年(2位)+月(2位)+日(2位)</param>
|
/// <param name="digit2">流水号长度</param>
|
/// <param name="value2">自增流水号</param>
|
/// <param name="digit3">自增流水号长度</param>
|
/// <param name="digitdiff">位数差</param>
|
/// <param name="q">补位(0)</param>
|
/// <param name="cunm2">最终流水号(包含补位)</param>
|
/// <param name="Encode">最终标签码(固定字段+规则+最终流水号)</param>
|
/// <param name="value">初始查询最新流水号</param>
|
/// <param name="labcum">标签个数</param>
|
/// <param name="sumqty">累计单标签数量</param>
|
/// <param name="onelabqty">单标签数量</param>
|
/// <param name="rightcode">功能编码</param>
|
/// <param name="qty">数量</param>
|
/// <param name="cunm">输出最终流水号</param>
|
/// <returns></returns>
|
public static List<AppLabCode> 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<AppLabCode> lab = new List<AppLabCode>();
|
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++)
|
{
|
AppLabCode lb = new AppLabCode();
|
lb.rightcode = rightcode;
|
if (i == labcum) //最后一张标签
|
{
|
lb.labqty = (decimal.Parse(qty) - sumqty).ToString(); //单标签数量
|
}
|
else
|
{
|
lb.labqty = onelabqty; //单标签数量
|
sumqty += decimal.Parse(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
|
}
|
}
|