using learun.iapplication;
using learun.util;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace learun.application
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.09.24
/// 描 述:编号规则
///
public class CodeRuleService : ServiceBase
{
#region 构造函数和属性
private readonly string fieldSql;
///
///
///
public CodeRuleService()
{
fieldSql = @"
t.F_RuleId,
t.F_EnCode,
t.F_FullName,
t.F_CurrentNumber,
t.F_RuleFormatJson,
t.F_SortCode,
t.F_DeleteMark,
t.F_EnabledMark,
t.F_Description,
t.F_CreateDate,
t.F_CreateUserId,
t.F_CreateUserName,
t.F_ModifyDate,
t.F_ModifyUserId,
t.F_ModifyUserName
";
}
#endregion 构造函数和属性
#region 获取数据
///
/// 规则列表
///
/// 分页
/// 查询参数
///
public Task> GetPageList(Pagination pagination, string keyword)
{
var strSql = new StringBuilder();
strSql.Append(" SELECT ");
strSql.Append(fieldSql);
strSql.Append(" FROM LR_Base_CodeRule t WHERE t.F_EnabledMark = 1 AND t.F_DeleteMark = 0 ");
if (!string.IsNullOrEmpty(keyword))
{
strSql.Append(" AND ( F_EnCode LIKE @keyword OR F_FullName LIKE @keyword ) ");
keyword = '%' + keyword + '%';
}
return BaseRepository().FindList(strSql.ToString(), new { keyword }, pagination);
}
///
/// 规则列表
///
///
public Task> GetList()
{
var strSql = new StringBuilder();
strSql.Append(" SELECT ");
strSql.Append(fieldSql);
strSql.Append(" FROM LR_Base_CodeRule t WHERE t.F_EnabledMark = 1 AND t.F_DeleteMark = 0 ");
return BaseRepository().FindList(strSql.ToString());
}
///
/// 规则实体
///
/// 主键值
///
public Task GetEntity(string keyValue)
{
return BaseRepository().FindEntityByKey(keyValue);
}
///
/// 规则实体
///
/// 规则编码
///
public Task GetEntityByCode(string enCode)
{
var strSql = new StringBuilder();
strSql.Append(" SELECT ");
strSql.Append(fieldSql);
strSql.Append(" FROM LR_Base_CodeRule t WHERE t.F_EnabledMark = 1 AND t.F_DeleteMark = 0 AND F_EnCode = @enCode");
return BaseRepository().FindEntity(strSql.ToString(), new { enCode });
}
///
/// 获取种子
///
///
///
public Task GetSeedEntity(string ruleId)
{
return BaseRepository().FindEntity(" select * from lr_base_coderuleseed where F_RuleId = @ruleId", new { ruleId });
}
#endregion 获取数据
#region 提交数据
///
/// 删除规则
///
/// 主键
public async Task Delete(string keyValue)
{
var db = BaseRepository().BeginTrans();
try
{
await db.DeleteAny(new { F_RuleId = keyValue });
await db.DeleteAny(new { F_RuleId = keyValue });
db.Commit();
}
catch (Exception)
{
db.Rollback();
throw;
}
}
///
/// 保存规则表单(新增、修改)
///
/// 主键值
/// 规则实体
///
public async Task SaveEntity(string keyValue, CodeRuleEntity codeRuleEntity)
{
if (!string.IsNullOrEmpty(keyValue))
{
codeRuleEntity.F_RuleId = keyValue;
codeRuleEntity.F_ModifyDate = DateTime.Now;
codeRuleEntity.F_ModifyUserId = this.GetUserId();
codeRuleEntity.F_ModifyUserName = this.GetUserName();
await BaseRepository().Update(codeRuleEntity);
}
else
{
codeRuleEntity.F_RuleId = Guid.NewGuid().ToString();
codeRuleEntity.F_CreateDate = DateTime.Now;
codeRuleEntity.F_DeleteMark = 0;
codeRuleEntity.F_EnabledMark = 1;
codeRuleEntity.F_CreateUserId = this.GetUserId();
codeRuleEntity.F_CreateUserName = this.GetUserName();
await BaseRepository().Insert(codeRuleEntity);
}
}
#endregion 提交数据
#region 单据编码处理
///
/// 获取当前编号规则种子列表
///
/// 编号规则主键
/// 当前登录者信息
///
public async Task> GetSeedList(string ruleId, UserEntity userInfo)
{
//获取当前最大种子
List codeRuleSeedList = (List)await BaseRepository().FindList(" select * from lr_base_coderuleseed where F_RuleId = @ruleId", new { ruleId });
if (codeRuleSeedList.Count == 0)
{
//说明没有种子,插入一条种子
CodeRuleSeedEntity codeRuleSeedEntity = new CodeRuleSeedEntity();
codeRuleSeedEntity.F_RuleSeedId = Guid.NewGuid().ToString();
codeRuleSeedEntity.F_CreateDate = DateTime.Now;
codeRuleSeedEntity.F_ModifyDate = DateTime.Now;
codeRuleSeedEntity.F_CreateUserId = userInfo.F_UserId;
codeRuleSeedEntity.F_CreateUserName = userInfo.F_RealName;
codeRuleSeedEntity.F_SeedValue = 1;
codeRuleSeedEntity.F_RuleId = ruleId;
await BaseRepository().Insert(codeRuleSeedEntity);
codeRuleSeedList.Add(codeRuleSeedEntity);
}
return codeRuleSeedList;
}
///
/// 保存单据编号规则种子
///
/// 主键
/// 种子实体
/// 当前登录者信息
public async Task SaveSeed(string keyValue, CodeRuleSeedEntity codeRuleSeedEntity, UserEntity userInfo)
{
if (string.IsNullOrEmpty(keyValue))
{
codeRuleSeedEntity.F_RuleSeedId = Guid.NewGuid().ToString();
codeRuleSeedEntity.F_CreateDate = DateTime.Now;
codeRuleSeedEntity.F_ModifyDate = DateTime.Now;
codeRuleSeedEntity.F_CreateUserId = userInfo.F_UserId;
codeRuleSeedEntity.F_CreateUserName = userInfo.F_RealName;
await BaseRepository().Insert(codeRuleSeedEntity);
}
else
{
codeRuleSeedEntity.F_RuleSeedId = keyValue;
codeRuleSeedEntity.F_ModifyDate = DateTime.Now;
codeRuleSeedEntity.F_ModifyUserId = userInfo.F_UserId;
codeRuleSeedEntity.F_ModifyUserName = userInfo.F_RealName;
await BaseRepository().Update(codeRuleSeedEntity);
}
}
///
/// 删除种子,表示被占用了
///
/// 用户主键
/// 规则主键
public async Task DeleteSeed(string userId, string ruleId)
{
await BaseRepository().DeleteAny(new { F_UserId = userId, F_RuleId = ruleId });
}
#endregion 单据编码处理
}
}