using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Text;
namespace Learun.Application.Base.SystemModule
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.04.01
/// 描 述:编号规则
///
public class CodeRuleService : RepositoryFactory
{
#region 构造函数和属性
private 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 IEnumerable GetPageList(Pagination pagination, string keyword)
{
try
{
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 this.BaseRepository().FindList(strSql.ToString(), new { keyword = keyword }, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 规则列表
///
///
public IEnumerable GetList()
{
try
{
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 this.BaseRepository().FindList(strSql.ToString());
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 规则实体
///
/// 主键值
///
public CodeRuleEntity GetEntity(string keyValue)
{
try
{
return this.BaseRepository().FindEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 规则实体
///
/// 规则编码
///
public CodeRuleEntity GetEntityByCode(string enCode) {
try
{
return this.BaseRepository().FindEntity(t => t.F_EnCode == enCode);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取种子
///
///
///
public CodeRuleSeedEntity GetSeedEntity(string ruleId)
{
try
{
return this.BaseRepository().FindEntity(t => t.F_RuleId == ruleId);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除规则
///
/// 主键
public void VirtualDelete(string keyValue)
{
try
{
CodeRuleEntity entity = new CodeRuleEntity()
{
F_RuleId = keyValue,
F_DeleteMark = 1
};
this.BaseRepository().Update(entity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存规则表单(新增、修改)
///
/// 主键值
/// 规则实体
///
public void SaveEntity(string keyValue, CodeRuleEntity codeRuleEntity, UserInfo userInfo = null)
{
try
{
if (!string.IsNullOrEmpty(keyValue))
{
codeRuleEntity.Modify(keyValue, userInfo);
this.BaseRepository().Update(codeRuleEntity);
}
else
{
codeRuleEntity.Create(userInfo);
this.BaseRepository().Insert(codeRuleEntity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 验证数据
///
/// 规则编号不能重复
///
/// 编号
/// 主键
///
public bool ExistEnCode(string enCode, string keyValue)
{
try
{
var strSql = new StringBuilder();
strSql.Append(" SELECT t.F_RuleId FROM LR_Base_CodeRule t WHERE t.F_DeleteMark = 0 AND t.F_EnCode = @enCode ");
if (!string.IsNullOrEmpty(keyValue))
{
strSql.Append(" AND t.F_RuleId != @keyValue ");
}
CodeRuleEntity entity = this.BaseRepository().FindEntity(strSql.ToString(), new { enCode = enCode, keyValue = keyValue });
return entity == null ? true : false;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 规则名称不能重复
///
/// 名称
/// 主键
///
public bool ExistFullName(string fullName, string keyValue)
{
try
{
var strSql = new StringBuilder();
strSql.Append(" SELECT t.F_RuleId FROM LR_Base_CodeRule t WHERE t.F_DeleteMark = 0 AND t.F_FullName = @fullName ");
if (!string.IsNullOrEmpty(keyValue))
{
strSql.Append(" AND t.F_RuleId != @keyValue ");
}
CodeRuleEntity entity = this.BaseRepository().FindEntity(strSql.ToString(), new { fullName = fullName, keyValue = keyValue });
return entity == null ? true : false;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 单据编码处理
///
/// 获取当前编号规则种子列表
///
/// 编号规则主键
///
public List GetSeedList(string ruleId, UserInfo userInfo)
{
try
{
//获取当前最大种子
List codeRuleSeedList = (List)this.BaseRepository().FindList(t => t.F_RuleId.Equals(ruleId));
if (codeRuleSeedList.Count == 0)
{
//说明没有种子,插入一条种子
CodeRuleSeedEntity codeRuleSeedEntity = new CodeRuleSeedEntity();
codeRuleSeedEntity.Create(userInfo);
codeRuleSeedEntity.F_SeedValue = 1;
codeRuleSeedEntity.F_RuleId = ruleId;
this.BaseRepository().Insert(codeRuleSeedEntity);
codeRuleSeedList.Add(codeRuleSeedEntity);
}
return codeRuleSeedList;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存单据编号规则种子
///
/// 主键
/// 种子实体
public void SaveSeed(string keyValue, CodeRuleSeedEntity codeRuleSeedEntity, UserInfo userInfo)
{
try
{
if (string.IsNullOrEmpty(keyValue))
{
codeRuleSeedEntity.Create(userInfo);
this.BaseRepository().Insert(codeRuleSeedEntity);
}
else
{
codeRuleSeedEntity.Modify(keyValue, userInfo);
this.BaseRepository().Update(codeRuleSeedEntity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 删除种子,表示被占用了
///
/// 用户主键
/// 规则主键
public void DeleteSeed(string userId, string ruleId)
{
try
{
this.BaseRepository().Delete(t => t.F_UserId == userId && t.F_RuleId == ruleId);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}