using DapperORMCore.Context.DataContext;
using DapperORMCore.Model.BaseModel;
using DapperORMCore.Model.CoreModel;
using DapperORMCore.Repository.IRepositorys;
using DapperORMCore.String.Consts;
using DapperORMCore.String.Enums;
using Microsoft.Extensions.Configuration;
using NXWMS.Code;
using NXWMS.Code.Verifications;
using NXWMS.DataAccess.Entity;
using NXWMS.IService.NXWMS.Rule;
using NXWMS.Model.AppModels.Condition.Rule;
using NXWMS.Model.AppModels.Result.Rule;
using NXWMS.Model.Common;
using NXWMS.String.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WestDistance.DapperORM.Repository.Repositorys;
namespace NXWMS.Service.NXWMS.Rule
{
///
/// 打印模版
///
[AutoInject(typeof(IPrintTemplateService), InjectType.Scope)]
public class PrintTemplateService : ServiceBase, IPrintTemplateService
{
///
/// 系统操作仓储中转
///
private IDataRepositoryContext _dataContext;
///
/// SQL节点仓储
///
private ISQLNodeRepository _iSQLNodeRepository;
///
/// 配置
///
private IConfiguration _configuration;
public PrintTemplateService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration, ISQLNodeRepository iSQLNodeRepository)
{
this._dataContext = dataRepositoryContext;
this._configuration = configuration;
this._iSQLNodeRepository = iSQLNodeRepository;
}
public OperateResultInfo> GetList(PrintTemplateSearchCondition info)
{
var sqlAndBuilder = new StringBuilder();
var sqlOrBuilder = new StringBuilder();
var sql = $@"SELECT
CreateName = (SELECT USER_NAME FROM SYS_USER A WHERE A.USER_ID=CREATE_BY),
UpdateName = (SELECT USER_NAME FROM SYS_USER B WHERE B.USER_ID=UPDATE_BY),
UsedFlagName = {_iSQLNodeRepository.GetEnumIntCaseString("USED_FLAG")},
PrintModeName = {_iSQLNodeRepository.GetEnumIntCaseString("PRINT_MODE")},
IsPrintHeaderName = {_iSQLNodeRepository.GetEnumIntCaseString("PRINT_HEADER_FLAG")},
IsShowWatermarkName = {_iSQLNodeRepository.GetEnumIntCaseString("SHOW_WATERMARK_FLAG")},
IsShowLogoName = {_iSQLNodeRepository.GetEnumIntCaseString("SHOW_LOGO_FLAG")},
SourceTypeName = {_iSQLNodeRepository.GetEnumIntCaseString("SOURCE_TYPE")},
{info.ItemSQL} FROM BAS_PRINT_TEMPLATE WHERE 1=1 AND DEL_FLAG = 0 ";
sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("TEMPLATE_CODE", info.TemplateCode, DBOperationString._ContainIn));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("TEMPLATE_NAME", info.TemplateName, DBOperationString._ContainIn));
sqlAndBuilder = info.PrintMode ==null ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_MODE", info.PrintMode, DBOperationString._Equal));
sqlAndBuilder = info.IsUsed == null ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("USED_FLAG", info.IsUsed, DBOperationString._Equal));
sqlAndBuilder =string.IsNullOrWhiteSpace(info.SourceName) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("SOURCE_NAME", info.SourceName, DBOperationString._ContainIn));
sqlAndBuilder = info.IsPrintHeader == null ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_HEADER_FLAG", info.IsPrintHeader, DBOperationString._Equal));
sql = sql + (sqlAndBuilder.Length > 0 ? _iSQLNodeRepository.GetAndString(sqlAndBuilder, false) : "");
IEnumerable result;
IEnumerable totalResult;
totalResult = new DataRepository(_dataContext).Query(sql);
if (info.PageIndex == 0 || info.PageSize == 0)
{
result = totalResult.ToList();
}
else
{
result = new DataRepository(_dataContext).QueryPage(sql, "CREATE_TIME", info.PageSize, info.PageIndex, true);
}
return SuccessStatus(new PageQueryResultInfo
{
RowData = result,
PageConditionInfo = info,
TotalCount = totalResult.Count(),
TotalPageCount = (int)Math.Ceiling((double)totalResult.Count() / info.PageSize)
});
}
public OperateResultInfo Add(PrintTemplateCondition info)
{
if (new DataRepository(_dataContext).Query().Where(m => m.PRINT_TEMPLATE_CODE == info.TemplateCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).Any())
{
return FailMessageStatus("打印模版编码已经存在,请输入其它编码!");
}
var now = DateTime.Now;
var id =
new DataRepository(_dataContext).Query().Count() == 0 ? 1 :
new DataRepository(_dataContext).Query().Select(s => s.PRINT_TEMPLATE_ID).Max() + 1;
var entity = new BAS_PRINT_TEMPLATE
{
CREATE_BY = info.OperationUserId,
CREATE_TIME = now,
UPDATE_BY = info.OperationUserId,
UPDATE_TIME = now,
USED_FLAG = Convert.ToInt32(info.IsUsed),
DESCRIBE = info.Describe,
DEL_FLAG = 0,
PRINT_HEADER_FLAG = info.IsPrintHeader ? 1 : 0,
PRINT_MODE=info.PrintMode,
PRINT_TEMPLATE_CODE = info.TemplateCode,
PRINT_NUMBER=info.PrintNumber,
PRINT_TEMPLATE_NAME = info.TemplateName,
SOURCE_CODE=info.SourceCode,
SHOW_LOGO_FLAG = info.IsShowLogo ? 1 : 0,
SHOW_WATERMARK_FLAG = info.IsShowWatermark ? 1 : 0,
SOURCE_CONTENT = info.SourceContent,
SOURCE_NAME = info.SourceCode,
SOURCE_TYPE = info.SourceType,
PRINT_TEMPLATE_ID= id,
};
var affectedRows = new DataRepository(_dataContext).Add(entity);
return GetStatus(affectedRows);
}
public OperateResultInfo Deleted(PrintTemplateCondition info)
{
var sqlAndBuilder = new StringBuilder();
sqlAndBuilder = info.TemplateId != null ?
info.TemplateId > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_ID", info.TemplateId, DBOperationString._Equal)) :
sqlAndBuilder : sqlAndBuilder;
sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_CODE", info.TemplateCode, DBOperationString._Equal));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateIds) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_ID", info.TemplateIds, DBOperationString._In));
if (sqlAndBuilder.Length == 0)
{
return FailMessageStatus("参数错误!");
}
var now = DateTime.Now;
var sql = $@"UPDATE BAS_PRINT_TEMPLATE SET DEL_FLAG=1,UPDATE_BY={info.OperationUserId},UPDATE_TIME='{now}'
WHERE 1=1 {sqlAndBuilder}";
var affectedRows = new DataRepository(_dataContext).Execute(sql);
return GetStatus(affectedRows, info.TemplateIds.Split(',').Length);
}
public OperateResultInfo Edit(PrintTemplateCondition info)
{
if (info == null)
{
return FailStatus();
}
if (!UtilCheckString.GetDynamicParmaValidateResult(info.SourceCode, info.SourceType, info.PrintMode, info.SourceName, info.TemplateCode, info.TemplateName))
{
return FailMessageStatus("参数错误!");
}
var whereList = new List()
.AddFieldKeyInfo(nameof(BAS_PRINT_TEMPLATE.PRINT_TEMPLATE_CODE), info.TemplateCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
!string.IsNullOrWhiteSpace(info.TemplateCode))
.AddFieldKeyInfo(nameof(BAS_PRINT_TEMPLATE.PRINT_TEMPLATE_ID), info.TemplateId, EnumCSharpPropertyType.INT, DBOperationString._Equal,
info.TemplateId != null);
var result = new DataRepository(_dataContext).Query(whereList).FirstOrDefault();
if (result != null)
{
var now = DateTime.Now;
result.DESCRIBE = info.Describe;
result.PRINT_TEMPLATE_NAME = info.TemplateName;
result.PRINT_TEMPLATE_CODE = info.TemplateCode;
result.PRINT_TEMPLATE_ID = result.PRINT_TEMPLATE_ID;
result.PRINT_NUMBER = info.PrintNumber;
result.PRINT_MODE = info.PrintMode;
result.SOURCE_CODE = info.SourceCode;
result.SOURCE_NAME = info.SourceName;
result.SOURCE_TYPE = info.SourceType;
result.SOURCE_CONTENT = info.SourceContent;
result.PRINT_HEADER_FLAG = info.IsPrintHeader ? 1 : 0;
result.SHOW_LOGO_FLAG = info.IsShowLogo ? 1 : 0;
result.SHOW_WATERMARK_FLAG = info.IsShowWatermark ? 1 : 0;
result.UPDATE_BY = info.OperationUserId;
result.UPDATE_TIME = now;
result.USED_FLAG = info.IsUsed ? 1 : 0;
var affectedRows = new DataRepository(_dataContext).Update(result, "PRINT_TEMPLATE_ID", "NEWID");
return GetStatus(affectedRows);
}
return FailMessageStatus("未查找到数据!");
}
public OperateResultInfo Remove(PrintTemplateCondition info)
{
if (string.IsNullOrWhiteSpace(info.TemplateCode) && info.TemplateId == null && string.IsNullOrWhiteSpace(info.TemplateIds))
{
return FailMessageStatus("参数错误!");
}
var sqlAndBuilder = new StringBuilder();
var sqlOrBuilder = new StringBuilder();
var sql = $@"SELECT {info.ItemSQL} FROM BAS_RULE WHERE 1=1 AND DEL_FLAG = 0 ";
sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_CODE", info.TemplateCode, DBOperationString._Equal));
sqlAndBuilder = info.TemplateId != null ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_ID", info.TemplateId, DBOperationString._Equal));
sql = sql + (sqlAndBuilder.Length > 0 ? _iSQLNodeRepository.GetAndString(sqlAndBuilder, false) : "");
var result = new DataRepository(_dataContext).Query(sql);
if (result.Count() != 1)
{
return FailMessageStatus("参数错误!");
}
var affectedRows = new DataRepository(_dataContext).Remove("PRINT_TEMPLATE_ID", result.FirstOrDefault().PRINT_TEMPLATE_ID.ToString());
return GetStatus(affectedRows);
}
}
}