using LeaRun.Application.Entity.BaseManage;
using LeaRun.Application.Entity.SystemManage;
using LeaRun.Application.IService.SystemManage;
using LeaRun.Data.Repository;
using LeaRun.Util;
using LeaRun.Util.Extension;
using LeaRun.Util.WebControl;
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeaRun.Application.Service.SystemManage
{
///
/// 版 本 6.1
/// Copyright (c) 2013-2016 上海力软信息技术有限公司
/// 创建人:佘赐雄
/// 日 期:2015.12.21 16:19
/// 描 述:编号规则
///
public class CodeRuleService : RepositoryFactory, ICodeRuleService
{
#region 获取数据
///
/// 规则列表
///
/// 分页
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
var expression = LinqExtensions.True();
var queryParam = queryJson.ToJObject();
//查询条件
if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
{
string condition = queryParam["condition"].ToString();
string keyword = queryParam["keyword"].ToString();
switch (condition)
{
case "EnCode": //对象编号
expression = expression.And(t => t.EnCode.Contains(keyword));
break;
case "FullName": //对象名称
expression = expression.And(t => t.FullName.Contains(keyword));
break;
default:
break;
}
}
return this.BaseRepository().FindList(expression, pagination);
}
///
/// 规则实体
///
/// 主键值
///
public CodeRuleEntity GetEntity(string keyValue)
{
return this.BaseRepository().FindEntity(keyValue);
}
#endregion
#region 提交数据
///
/// 删除规则
///
/// 主键
public void RemoveForm(string keyValue)
{
this.BaseRepository().Delete(keyValue);
}
///
/// 保存规则表单(新增、修改)
///
/// 主键值
/// 规则实体
///
public void SaveForm(string keyValue, CodeRuleEntity codeRuleEntity)
{
if (!string.IsNullOrEmpty(keyValue))
{
codeRuleEntity.Modify(keyValue);
this.BaseRepository().Update(codeRuleEntity);
}
else
{
codeRuleEntity.Create();
this.BaseRepository().Insert(codeRuleEntity);
}
}
#endregion
#region 验证数据
///
/// 规则编号不能重复
///
/// 编号
/// 主键
///
public bool ExistEnCode(string enCode, string keyValue)
{
var expression = LinqExtensions.True();
expression = expression.And(t => t.EnCode == enCode);
if (!string.IsNullOrEmpty(keyValue))
{
expression = expression.And(t => t.RuleId != keyValue);
}
return this.BaseRepository().IQueryable(expression).Count() == 0 ? true : false;
}
///
/// 规则名称不能重复
///
/// 名称
/// 主键
///
public bool ExistFullName(string fullName, string keyValue)
{
var expression = LinqExtensions.True();
expression = expression.And(t => t.FullName == fullName);
if (!string.IsNullOrEmpty(keyValue))
{
expression = expression.And(t => t.RuleId != keyValue);
}
return this.BaseRepository().IQueryable(expression).Count() == 0 ? true : false;
}
#endregion
#region 单据编码处理
///
/// 获得指定模块或者编号的单据号
///
/// 用户ID
/// 模块ID
/// 模板编码
/// 单据号
public string GetBillCode(string userId, string moduleId, string enCode)
{
IRepository db = new RepositoryFactory().BaseRepository();
UserEntity userEntity = db.FindEntity(userId);
CodeRuleEntity coderuleentity = db.FindEntity(t => t.ModuleId == moduleId || t.EnCode == enCode);
//判断种子是否已经产生,如果没有产生种子先插入一条初始种子
CodeRuleSeedEntity initSeed = db.FindEntity(t => t.RuleId == coderuleentity.RuleId);
if (initSeed == null)
{
initSeed = new CodeRuleSeedEntity();
initSeed.Create();
initSeed.SeedValue = 1;
initSeed.RuleId = coderuleentity.RuleId;
db.Insert(initSeed);
}
else
{
db = new RepositoryFactory().BaseRepository().BeginTrans();
}
//获得模板ID
string billCode = "";//单据号
string nextBillCode = "";//单据号
bool isOutTime = false;//是否已过期
if (coderuleentity != null)
{
try
{
int nowSerious = 0;
//取得流水号种子
List codeRuleSeedlist = db.IQueryable(t => t.RuleId == coderuleentity.RuleId).ToList();
//取得最大种子
CodeRuleSeedEntity maxSeed = db.FindEntity(t => t.UserId == null);
#region 处理隔天流水号归0
//首先确定最大种子是否是隔天未归0的
if ((maxSeed.ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd"))
{
isOutTime = true;
maxSeed.SeedValue = 1;
maxSeed.ModifyDate = DateTime.Now;
}
#endregion
List codeRuleFormatList = coderuleentity.RuleFormatJson.ToList();
foreach (CodeRuleFormatEntity codeRuleFormatEntity in codeRuleFormatList)
{
switch (codeRuleFormatEntity.ItemType.ToString())
{
//自定义项
case "0":
billCode = billCode + codeRuleFormatEntity.FormatStr;
nextBillCode = nextBillCode + codeRuleFormatEntity.FormatStr;
break;
//日期
case "1":
//日期字符串类型
billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
break;
//流水号
case "2":
//查找当前用户是否已有之前未用掉的种子
CodeRuleSeedEntity codeRuleSeedEntity = codeRuleSeedlist.Find(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
//删除已过期的用户未用掉的种子
if (codeRuleSeedEntity != null && isOutTime)
{
db.Delete(codeRuleSeedEntity);
codeRuleSeedEntity = null;
}
//如果没有就取当前最大的种子
if (codeRuleSeedEntity != null)
{
nowSerious = (int)codeRuleSeedEntity.SeedValue;
}
else
{
//取得系统最大的种子
int maxSerious = (int)maxSeed.SeedValue;
nowSerious = maxSerious;
codeRuleSeedEntity = new CodeRuleSeedEntity();
codeRuleSeedEntity.Create();
codeRuleSeedEntity.SeedValue = maxSerious;
codeRuleSeedEntity.UserId = userId;
codeRuleSeedEntity.RuleId = coderuleentity.RuleId;
db.Insert(codeRuleSeedEntity);
//处理种子更新
maxSeed.SeedValue += 1;
maxSeed.Modify(maxSeed.RuleSeedId);
db.Update(maxSeed);
}
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.SeedValue.ToString();
billCode = billCode + seriousStr;
nextBillCode = nextBillCode + NextSeriousStr;
break;
//部门
case "3":
DepartmentEntity departmentEntity = db.FindEntity(userEntity.DepartmentId);
if (codeRuleFormatEntity.FormatStr == "code")
{
billCode = billCode + departmentEntity.EnCode;
nextBillCode = nextBillCode + departmentEntity.EnCode;
}
else
{
billCode = billCode + departmentEntity.FullName;
nextBillCode = nextBillCode + departmentEntity.FullName;
}
break;
//公司
case "4":
OrganizeEntity organizeEntity = db.FindEntity(userEntity.OrganizeId);
if (codeRuleFormatEntity.FormatStr == "code")
{
billCode = billCode + organizeEntity.EnCode;
nextBillCode = nextBillCode + organizeEntity.EnCode;
}
else
{
billCode = billCode + organizeEntity.FullName;
nextBillCode = nextBillCode + organizeEntity.FullName;
}
break;
//用户
case "5":
if (codeRuleFormatEntity.FormatStr == "code")
{
billCode = billCode + userEntity.EnCode;
nextBillCode = nextBillCode + userEntity.EnCode;
}
else
{
billCode = billCode + userEntity.Account;
nextBillCode = nextBillCode + userEntity.Account;
}
break;
default:
break;
}
}
coderuleentity.CurrentNumber = nextBillCode;
db.Update(coderuleentity);
}
catch (Exception)
{
db.Rollback();
return billCode;
}
db.Commit();
}
return billCode;
}
///
/// 获得指定模块或者编号的单据号(直接使用)
///
/// 用户ID
/// 模块ID
/// 模板编码
/// 单据号
public string SetBillCode(string userId, string moduleId, string enCode, IRepository db = null)
{
IRepository dbc = null;
if (db == null)
{
dbc = new RepositoryFactory().BaseRepository();
}
else
{
dbc = db;
}
UserEntity userEntity = db.FindEntity(userId);
CodeRuleEntity coderuleentity = db.FindEntity(t => t.ModuleId == moduleId || t.EnCode == enCode);
//判断种子是否已经产生,如果没有产生种子先插入一条初始种子
CodeRuleSeedEntity initSeed = db.FindEntity(t => t.RuleId == coderuleentity.RuleId);
if (initSeed == null)
{
initSeed = new CodeRuleSeedEntity();
initSeed.Create();
initSeed.SeedValue = 1;
initSeed.RuleId = coderuleentity.RuleId;
initSeed.CreateDate = null;
//db.Insert(initSeed);
}
//获得模板ID
string billCode = "";//单据号
string nextBillCode = "";//单据号
bool isOutTime = false;//是否已过期
if (coderuleentity != null)
{
try
{
int nowSerious = 0;
//取得流水号种子
List codeRuleSeedlist = db.IQueryable(t => t.RuleId == coderuleentity.RuleId).ToList();
//取得最大种子
CodeRuleSeedEntity maxSeed = db.FindEntity(t => t.UserId == null);
#region 处理隔天流水号归0
//首先确定最大种子是否是隔天未归0的
if ((maxSeed.ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd"))
{
isOutTime = true;
maxSeed.SeedValue = 1;
maxSeed.ModifyDate = DateTime.Now;
}
#endregion
if (maxSeed == null)
{
maxSeed = initSeed;
}
List codeRuleFormatList = coderuleentity.RuleFormatJson.ToList();
foreach (CodeRuleFormatEntity codeRuleFormatEntity in codeRuleFormatList)
{
switch (codeRuleFormatEntity.ItemType.ToString())
{
//自定义项
case "0":
billCode = billCode + codeRuleFormatEntity.FormatStr;
nextBillCode = nextBillCode + codeRuleFormatEntity.FormatStr;
break;
//日期
case "1":
//日期字符串类型
billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
break;
//流水号
case "2":
//查找当前用户是否已有之前未用掉的种子
CodeRuleSeedEntity codeRuleSeedEntity = codeRuleSeedlist.Find(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
//删除已过期的用户未用掉的种子
if (codeRuleSeedEntity != null && isOutTime)
{
db.Delete(codeRuleSeedEntity);
codeRuleSeedEntity = null;
}
//如果没有就取当前最大的种子
if (codeRuleSeedEntity == null)
{
//取得系统最大的种子
int maxSerious = (int)maxSeed.SeedValue;
nowSerious = maxSerious;
codeRuleSeedEntity = new CodeRuleSeedEntity();
codeRuleSeedEntity.Create();
codeRuleSeedEntity.SeedValue = maxSerious;
codeRuleSeedEntity.UserId = userId;
codeRuleSeedEntity.RuleId = coderuleentity.RuleId;
//db.Insert(codeRuleSeedEntity);
//处理种子更新
maxSeed.SeedValue += 1;
if (maxSeed.CreateDate != null)
{
db.Update(maxSeed);
}
else
{
maxSeed.CreateDate = DateTime.Now;
db.Insert(maxSeed);
}
}
else
{
nowSerious = (int)codeRuleSeedEntity.SeedValue;
}
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.SeedValue.ToString();
billCode = billCode + seriousStr;
nextBillCode = nextBillCode + NextSeriousStr;
break;
//部门
case "3":
DepartmentEntity departmentEntity = db.FindEntity(userEntity.DepartmentId);
if (codeRuleFormatEntity.FormatStr == "code")
{
billCode = billCode + departmentEntity.EnCode;
nextBillCode = nextBillCode + departmentEntity.EnCode;
}
else
{
billCode = billCode + departmentEntity.FullName;
nextBillCode = nextBillCode + departmentEntity.FullName;
}
break;
//公司
case "4":
OrganizeEntity organizeEntity = db.FindEntity(userEntity.OrganizeId);
if (codeRuleFormatEntity.FormatStr == "code")
{
billCode = billCode + organizeEntity.EnCode;
nextBillCode = nextBillCode + organizeEntity.EnCode;
}
else
{
billCode = billCode + organizeEntity.FullName;
nextBillCode = nextBillCode + organizeEntity.FullName;
}
break;
//用户
case "5":
if (codeRuleFormatEntity.FormatStr == "code")
{
billCode = billCode + userEntity.EnCode;
nextBillCode = nextBillCode + userEntity.EnCode;
}
else
{
billCode = billCode + userEntity.Account;
nextBillCode = nextBillCode + userEntity.Account;
}
break;
default:
break;
}
}
coderuleentity.CurrentNumber = nextBillCode;
db.Update(coderuleentity);
}
catch (Exception)
{
throw;
}
}
return billCode;
}
///
/// 占用单据号
///
/// 用户ID
/// 模块ID
/// 模板编码
/// true/false
public bool UseRuleSeed(string userId, string moduleId, string enCode, IRepository db = null)
{
IRepository dbc = null;
if (db == null)
{
dbc = new RepositoryFactory().BaseRepository();
}
else
{
dbc = db;
}
UserEntity userEntity = dbc.FindEntity(userId);
CodeRuleEntity coderuleentity = dbc.FindEntity(t => t.ModuleId == moduleId || t.EnCode == enCode);
try
{
if (coderuleentity != null)
{
//删除用户已经用掉的种子
dbc.Delete(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
}
}
catch (Exception)
{
return false;
}
return true;
}
#endregion
}
}