You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
9.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.System.Method;
public class SequenceService : ISequenceService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public SequenceService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
}
/// <summary>
/// 列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<SequenceRes>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysSequence>()
.LeftJoin<SysPermission>((a, b) => a.PermissionId == b.Id)
.Where(whereList)
.Select<SequenceRes>().ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult EditSequence(SequenceReq req)
{
if (req.Id == 0)
{
if (db.Queryable<SysSequence>().Where(x => x.PermissionId == req.PermissionId && x.OrderNo == req.OrderNo).Any())
{
return DataResult.Failed("系统编码已存在!", MultiLanguageConst.SequenceExist);
}
var data = req.Adapt<SysSequence>();
var entity = db.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = db.Queryable<SysSequence>().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<SequenceRes> GetSequenceInfo(string id)
{
var data = db.Queryable<SysSequence>()
.LeftJoin<SysPermission>((a, b) => a.PermissionId == b.Id)
.Where(a => a.Id == long.Parse(id))
.Select<SequenceRes>()
.First();
return DataResult<SequenceRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
/// <summary>
/// 获取最新业务单据编码
/// </summary>
/// <returns></returns>
public DataResult<string> GetSequenceNextTest(string id)
{
//生成编号
string sequenceNewNo = "";
#region 获取序号生成器属性
//获取序号生成器属性
var sequence = db.Queryable<SysSequence>()
.First(u => u.Id == long.Parse(id));
if (sequence != null)
{
var ruleList = db.Queryable<SysSequenceRule>().OrderBy(x => x.OrderNo)
.Where(u => u.SequenceId == sequence.Id && u.Status == StatusEnum.Enable).ToList();
if (ruleList.Any())
{
int delimiterNum = 0;
for (global::System.Int32 i = 0; i < ruleList.Count; i++)
{
var item = ruleList[i];
delimiterNum++;
switch (item.RuleType)
{
case "const": //常量方式
sequenceNewNo += item.RuleValue;
break;
case "shortdate": //短日期 年2位月2位日期2位
sequenceNewNo += DateTime.Now.ToString("yyyyMMdd").Substring(2);
break;
case "date": //日期年4位
sequenceNewNo += DateTime.Now.ToString("yyyyMMdd");
break;
case "ydate": //年月年4位月2位
sequenceNewNo += DateTime.Now.ToString("yyyyMMdd").Substring(0, 6);
break;
case "sydate": //年月年2位月2位
sequenceNewNo += DateTime.Now.ToString("yyyyMMdd").Substring(2, 4);
break;
case "timestamp": //日期时间精确到毫秒
sequenceNewNo += DateTime.Now.ToString("yyyyMMddHHmmssffff");
break;
case "number": //计数,流水号
int num = CurrentReset(sequence, item);
//计数拼接
sequenceNewNo += NumberingSeqRule(item, num).ToString();
//更新当前序号,
sequence.CurrentNo = num;
break;
case "guid": //Guid
sequenceNewNo += GuidHelper.NewGuidFormatN();
break;
case "random": //随机数
Random random = new Random();
string strMax = "9".ToString().PadLeft(item.RuleValue.Length, '9');
string strRandom = random.Next(item.RuleValue.ToInt(), strMax.ToInt()).ToString(); //生成随机编号
sequenceNewNo += strRandom;
break;
}
if (!string.IsNullOrEmpty(sequence.SequenceDelimiter) && delimiterNum != ruleList.Count())
{
sequenceNewNo += sequence.SequenceDelimiter;
}
}
return DataResult<string>.Success(sequenceNewNo);
}
else
{
return DataResult<string>.Failed("未查询到业务编码对应的编码规则配置, 请检查编码规则配置!");
}
}
else
{
return DataResult<string>.Failed("不存在的单据编码!");
}
#endregion
}
/// <summary>
/// 计数 方式 重置规则
/// </summary>
/// <param name="seq"></param>
/// <param name="seqRule"></param>
/// <returns></returns>
private static int CurrentReset(SysSequence seq, SysSequenceRule seqRule)
{
int newNo = 0, ruleNo = 0;
try
{
ruleNo = seqRule.RuleValue.ToInt();
}
catch (Exception ex)
{
newNo = 1;
// Log4NetHelper.Error(ex.Message, ex);
}
switch (seq.SequenceReset)
{
case "D": //每天重置
if (!string.IsNullOrEmpty(seq.CurrentReset) && seq.CurrentReset != DateTime.Now.ToString("yyyyMMdd"))
{
newNo = 1;
}
break;
case "M": //每月重置
if (!string.IsNullOrWhiteSpace(seq.CurrentReset))
{
if (!seq.CurrentReset.Contains(DateTime.Now.ToString("yyyyMM")))
{
newNo = ruleNo;
}
}
else
{
newNo = 1;
}
break;
case "Y": //每年重置
if (!string.IsNullOrWhiteSpace(seq.CurrentReset))
{
if (!seq.CurrentReset.Contains(DateTime.Now.ToString("yyyy")))
{
newNo = ruleNo;
}
}
else
{
newNo = 1;
}
break;
}
if (newNo == 0)
{
if (seq.CurrentNo == 0)
{
newNo = ruleNo;
}
else
{
//当前序号+步长
newNo = seq.CurrentNo + seq.Step;
}
}
return newNo;
}
/// <summary>
/// 计数规则
/// </summary>
/// <param name="seqRule"></param>
/// <param name="code"></param>
/// <returns></returns>
private static string NumberingSeqRule(SysSequenceRule seqRule, int code)
{
string str = "";
if (seqRule.PaddingSide == "Left")
{
str += code.ToString().PadLeft(seqRule.PaddingWidth, seqRule.PaddingChar.ToChar());
}
if (seqRule.PaddingSide == "Right")
{
str += code.ToString().PadRight(seqRule.PaddingWidth, seqRule.PaddingChar.ToChar());
}
return str;
}
}