|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- using Learun.Application.Organization;
- using Learun.Cache.Base;
- using Learun.Cache.Factory;
- using Learun.Util;
- using System;
- using System.Collections.Generic;
- namespace Learun.Application.Base.SystemModule
- {
- /// <summary>
- /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
- /// Copyright (c) 2013-2018 北京泉江科技有限公司
- /// 创建人:陈彬彬
- /// 日 期:2017.04.17
- /// 描 述:编号规则
- /// </summary>
- public class CodeRuleBLL : CodeRuleIBLL
- {
- #region 属性
- private CodeRuleService codeRuleService = new CodeRuleService();
-
- // 组织单位
- DepartmentIBLL departmentIBLL = new DepartmentBLL();
- CompanyIBLL companyIBLL = new CompanyBLL();
-
- UserIBLL userIBLL = new UserBLL();
- #endregion
-
- #region 缓存定义
- /*缓存*/
- private ICache cache = CacheFactory.CaChe();
- private string cacheKey = "Learun_adms_cuderule_";//+规则编码
- private string cacheKeySeed = "Learun_adms_cuderule_seed_";//+规则主键
- #endregion
-
- #region 获取数据
- /// <summary>
- /// 规则列表
- /// </summary>
- /// <param name="pagination">分页</param>
- /// <param name="keyword">查询参数</param>
- /// <returns></returns>
- public IEnumerable<CodeRuleEntity> GetPageList(Pagination pagination, string keyword)
- {
- try
- {
- return codeRuleService.GetPageList(pagination, keyword);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 规则列表
- /// </summary>
- /// <returns></returns>
- public IEnumerable<CodeRuleEntity> GetList()
- {
- try
- {
- return codeRuleService.GetList();
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 规则实体
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <returns></returns>
- public CodeRuleEntity GetEntity(string keyValue)
- {
- try
- {
- return codeRuleService.GetEntity(keyValue); ;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 规则实体
- /// </summary>
- /// <param name="enCode">规则编码</param>
- /// <returns></returns>
- public CodeRuleEntity GetEntityByCode(string enCode)
- {
- try
- {
- CodeRuleEntity codeRuleEntity = cache.Read<CodeRuleEntity>(cacheKey + enCode, CacheId.codeRule);
- if (codeRuleEntity == null) {
- codeRuleEntity = codeRuleService.GetEntityByCode(enCode);
- cache.Write<CodeRuleEntity>(cacheKey + enCode, codeRuleEntity, CacheId.codeRule);
- }
- return codeRuleEntity;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- #endregion
-
- #region 提交数据
- /// <summary>
- /// 删除规则
- /// </summary>
- /// <param name="keyValue">主键</param>
- public void VirtualDelete(string keyValue)
- {
- try
- {
- CodeRuleEntity entity = codeRuleService.GetEntity(keyValue);
- cache.Remove(cacheKey + entity.F_EnCode, CacheId.codeRule);
- codeRuleService.VirtualDelete(keyValue);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 保存规则表单(新增、修改)
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <param name="codeRuleEntity">规则实体</param>
- /// <returns></returns>
- public void SaveEntity(string keyValue, CodeRuleEntity codeRuleEntity)
- {
- try
- {
- if (!string.IsNullOrEmpty(keyValue))
- {
- CodeRuleEntity entity = codeRuleService.GetEntity(keyValue);
- cache.Remove(cacheKey + entity.F_EnCode, CacheId.codeRule);
- }
- codeRuleService.SaveEntity(keyValue, codeRuleEntity);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
-
- /// <summary>
- /// 保存规则表单(新增、修改)
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <param name="codeRuleEntity">规则实体</param>
- /// <returns></returns>
- public void SaveEntity(string keyValue, CodeRuleEntity codeRuleEntity, UserInfo userInfo)
- {
- try
- {
- if (!string.IsNullOrEmpty(keyValue))
- {
- CodeRuleEntity entity = codeRuleService.GetEntity(keyValue);
- cache.Remove(cacheKey + entity.F_EnCode, CacheId.codeRule);
- }
- codeRuleService.SaveEntity(keyValue, codeRuleEntity,userInfo);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- #endregion
-
- #region 验证数据
- /// <summary>
- /// 规则编号不能重复
- /// </summary>
- /// <param name="enCode">编号</param>
- /// <param name="keyValue">主键</param>
- /// <returns></returns>
- public bool ExistEnCode(string enCode, string keyValue)
- {
- try
- {
- return codeRuleService.ExistEnCode(enCode, keyValue);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 规则名称不能重复
- /// </summary>
- /// <param name="fullName">名称</param>
- /// <param name="keyValue">主键</param>
- /// <returns></returns>
- public bool ExistFullName(string fullName, string keyValue)
- {
- try
- {
- return codeRuleService.ExistFullName(fullName, keyValue);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- #endregion
-
- #region 单据编码处理
- /// <summary>
- /// 获取当前编号规则种子列表
- /// </summary>
- /// <param name="ruleId">编号规则主键</param>
- /// <returns></returns>
- public List<CodeRuleSeedEntity> GetSeedList(string ruleId, UserInfo userInfo)
- {
- try
- {
- List<CodeRuleSeedEntity> codeRuleSeedList = cache.Read<List<CodeRuleSeedEntity>>(cacheKeySeed + ruleId, CacheId.codeRule);
- if (codeRuleSeedList == null) {
- codeRuleSeedList = codeRuleService.GetSeedList(ruleId, userInfo);
- cache.Write<List<CodeRuleSeedEntity>>(cacheKeySeed + ruleId, codeRuleSeedList, CacheId.codeRule);
- }
- return codeRuleSeedList;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 保存单据编号规则种子
- /// </summary>
- /// <param name="keyValue">主键</param>
- /// <param name="codeRuleSeedEntity">种子实体</param>
- public void SaveSeed(string keyValue, CodeRuleSeedEntity codeRuleSeedEntity, UserInfo userInfo)
- {
- try
- {
- codeRuleService.SaveSeed(keyValue, codeRuleSeedEntity,userInfo);
- cache.Remove(cacheKeySeed + codeRuleSeedEntity.F_RuleId, CacheId.codeRule);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 获得指定模块或者编号的单据号
- /// </summary>
- /// <param name="enCode">编码</param>
- /// <param name="userId">用户ID</param>
- /// <returns>单据号</returns>
- public string GetBillCode(string enCode,string userId = "")
- {
- try
- {
- string billCode = ""; //单据号
- string nextBillCode = "";//单据号
- bool isOutTime = false; //是否已过期
-
-
- CodeRuleEntity coderuleentity = GetEntityByCode(enCode);
- if (coderuleentity != null)
- {
- UserInfo userInfo = null;
- if (string.IsNullOrEmpty(userId))
- {
- userInfo = LoginUserInfo.Get();
- }
- else {
- UserEntity userEntity = userIBLL.GetEntityByUserId(userId);
- userInfo = new UserInfo
- {
- userId = userEntity.F_UserId,
- enCode = userEntity.F_EnCode,
- password = userEntity.F_Password,
- secretkey = userEntity.F_Secretkey,
- realName = userEntity.F_RealName,
- nickName = userEntity.F_NickName,
- headIcon = userEntity.F_HeadIcon,
- gender = userEntity.F_Gender,
- mobile = userEntity.F_Mobile,
- telephone = userEntity.F_Telephone,
- email = userEntity.F_Email,
- oICQ = userEntity.F_OICQ,
- weChat = userEntity.F_WeChat,
- companyId = userEntity.F_CompanyId,
- departmentId = userEntity.F_DepartmentId,
- openId = userEntity.F_OpenId,
- isSystem = userEntity.F_SecurityLevel == 1 ? true : false
- };
- }
-
- int nowSerious = 0;
- List<CodeRuleFormatModel> codeRuleFormatList = coderuleentity.F_RuleFormatJson.ToList<CodeRuleFormatModel>();
- string dateFormatStr = "";
- foreach (CodeRuleFormatModel codeRuleFormatEntity in codeRuleFormatList)
- {
- switch (codeRuleFormatEntity.itemType.ToString())
- {
- //自定义项
- case "0":
- billCode = billCode + codeRuleFormatEntity.formatStr;
- nextBillCode = nextBillCode + codeRuleFormatEntity.formatStr;
- break;
- //日期
- case "1":
- //日期字符串类型
- dateFormatStr = codeRuleFormatEntity.formatStr;
- billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.formatStr.Replace("m", "M"));
- nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.formatStr.Replace("m", "M"));
- break;
- //流水号
- case "2":
- CodeRuleSeedEntity maxSeed = null;
- CodeRuleSeedEntity codeRuleSeedEntity = null;
- List<CodeRuleSeedEntity> seedList = GetSeedList(coderuleentity.F_RuleId, userInfo);
- maxSeed = seedList.Find(t => t.F_UserId.IsEmpty());
- #region 处理流水号归0
- // 首先确定最大种子是否未归0的
- if (dateFormatStr.Contains("dd"))
- {
- if ((maxSeed.F_ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd"))
- {
- isOutTime = true;
- nowSerious = 1;
- maxSeed.F_SeedValue = 2;
- maxSeed.F_ModifyDate = DateTime.Now;
- }
- }
- else if (dateFormatStr.Contains("mm"))
- {
- if (((DateTime)maxSeed.F_ModifyDate).ToString("yyyy-MM") != DateTime.Now.ToString("yyyy-MM"))
- {
- isOutTime = true;
- nowSerious = 1;
- maxSeed.F_SeedValue = 2;
- maxSeed.F_ModifyDate = DateTime.Now;
- }
- }
- else if (dateFormatStr.Contains("yy"))
- {
- if (((DateTime)maxSeed.F_ModifyDate).ToString("yyyy") != DateTime.Now.ToString("yyyy"))
- {
- isOutTime = true;
- nowSerious = 1;
- maxSeed.F_SeedValue = 2;
- maxSeed.F_ModifyDate = DateTime.Now;
- }
- }
- #endregion
- // 查找当前用户是否已有之前未用掉的种子做更新
- codeRuleSeedEntity = seedList.Find(t => t.F_UserId == userInfo.userId && t.F_RuleId == coderuleentity.F_RuleId&& (t.F_CreateDate).ToDateString() == DateTime.Now.ToString("yyyy-MM-dd"));
- string keyvalue = codeRuleSeedEntity == null ? "" : codeRuleSeedEntity.F_RuleSeedId;
- if (isOutTime)
- {
- SaveSeed(maxSeed.F_RuleSeedId, maxSeed, userInfo);
- }
- else if (codeRuleSeedEntity == null)
- {
- nowSerious = (int)maxSeed.F_SeedValue;
- maxSeed.F_SeedValue += 1;
- maxSeed.Modify(maxSeed.F_RuleSeedId, userInfo);
- SaveSeed(maxSeed.F_RuleSeedId, maxSeed, userInfo);
- }
- else
- {
- nowSerious = (int)codeRuleSeedEntity.F_SeedValue;
- }
-
- codeRuleSeedEntity = new CodeRuleSeedEntity();
- codeRuleSeedEntity.Create(userInfo);
- codeRuleSeedEntity.F_SeedValue = nowSerious;
- codeRuleSeedEntity.F_UserId = userInfo.userId;
- codeRuleSeedEntity.F_RuleId = coderuleentity.F_RuleId;
- SaveSeed(keyvalue, codeRuleSeedEntity, userInfo);
- // 最大种子已经过期
- string seriousStr = new string('0', (int)(codeRuleFormatEntity.formatStr.Length - nowSerious.ToString().Length)) + nowSerious.ToString();
- string NextSeriousStr = new string('0', (int)(codeRuleFormatEntity.formatStr.Length - nowSerious.ToString().Length)) + maxSeed.F_SeedValue.ToString();
- billCode = billCode + seriousStr;
- nextBillCode = nextBillCode + NextSeriousStr;
- break;
- //部门
- case "3":
- DepartmentEntity departmentEntity = departmentIBLL.GetEntity(userInfo.companyId, userInfo.departmentId);
- if (codeRuleFormatEntity.formatStr == "code")
- {
- billCode = billCode + departmentEntity.F_EnCode;
- nextBillCode = nextBillCode + departmentEntity.F_EnCode;
- }
- else
- {
- billCode = billCode + departmentEntity.F_FullName;
- nextBillCode = nextBillCode + departmentEntity.F_FullName;
-
- }
- break;
- //公司
- case "4":
- CompanyEntity companyEntity = companyIBLL.GetEntity(userInfo.companyId);
- if (codeRuleFormatEntity.formatStr == "code")
- {
- billCode = billCode + companyEntity.F_EnCode;
- nextBillCode = nextBillCode + companyEntity.F_EnCode;
- }
- else
- {
- billCode = billCode + companyEntity.F_FullName;
- nextBillCode = nextBillCode + companyEntity.F_FullName;
- }
- break;
- //用户
- case "5":
- if (codeRuleFormatEntity.formatStr == "code")
- {
- billCode = billCode + userInfo.enCode;
- nextBillCode = nextBillCode + userInfo.enCode;
- }
- else
- {
- billCode = billCode + userInfo.account;
- nextBillCode = nextBillCode + userInfo.account;
- }
- break;
- default:
- break;
- }
- }
- coderuleentity.F_CurrentNumber = nextBillCode;
- SaveEntity(coderuleentity.F_RuleId, coderuleentity, userInfo);
- }
- return billCode;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 占用单据号
- /// </summary>
- /// <param name="enCode">单据编码</param>
- /// <param name="userId">用户ID</param>
- /// <returns>true/false</returns>
- public void UseRuleSeed(string enCode,string userId = "")
- {
- try
- {
- CodeRuleEntity codeRuleSeedEntity = GetEntityByCode(enCode);
- if (codeRuleSeedEntity != null)
- {
- if (string.IsNullOrEmpty(userId))
- {
- UserInfo userInfo = LoginUserInfo.Get();
- //删除用户已经用掉的种子
- codeRuleService.DeleteSeed(userInfo.userId, codeRuleSeedEntity.F_RuleId);
- }
- else {
- codeRuleService.DeleteSeed(userId, codeRuleSeedEntity.F_RuleId);
- }
- cache.Remove(cacheKeySeed + codeRuleSeedEntity.F_RuleId, CacheId.codeRule);
- }
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- #endregion
- }
- }
|