PrintTemplateService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using DapperORMCore.Context.DataContext;
  2. using DapperORMCore.Model.BaseModel;
  3. using DapperORMCore.Model.CoreModel;
  4. using DapperORMCore.Repository.IRepositorys;
  5. using DapperORMCore.String.Consts;
  6. using DapperORMCore.String.Enums;
  7. using Microsoft.Extensions.Configuration;
  8. using NXWMS.Code;
  9. using NXWMS.Code.Verifications;
  10. using NXWMS.DataAccess.Entity;
  11. using NXWMS.IService.NXWMS.Rule;
  12. using NXWMS.Model.AppModels.Condition.Rule;
  13. using NXWMS.Model.AppModels.Result.Rule;
  14. using NXWMS.Model.Common;
  15. using NXWMS.String.Enums;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using WestDistance.DapperORM.Repository.Repositorys;
  22. namespace NXWMS.Service.NXWMS.Rule
  23. {
  24. /// <summary>
  25. /// 打印模版
  26. /// </summary>
  27. [AutoInject(typeof(IPrintTemplateService), InjectType.Scope)]
  28. public class PrintTemplateService : ServiceBase, IPrintTemplateService
  29. {
  30. /// <summary>
  31. /// 系统操作仓储中转
  32. /// </summary>
  33. private IDataRepositoryContext _dataContext;
  34. /// <summary>
  35. /// SQL节点仓储
  36. /// </summary>
  37. private ISQLNodeRepository _iSQLNodeRepository;
  38. /// <summary>
  39. /// 配置
  40. /// </summary>
  41. private IConfiguration _configuration;
  42. public PrintTemplateService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration, ISQLNodeRepository iSQLNodeRepository)
  43. {
  44. this._dataContext = dataRepositoryContext;
  45. this._configuration = configuration;
  46. this._iSQLNodeRepository = iSQLNodeRepository;
  47. }
  48. public OperateResultInfo<PageQueryResultInfo<PrintTemplateResult>> GetList(PrintTemplateSearchCondition info)
  49. {
  50. var sqlAndBuilder = new StringBuilder();
  51. var sqlOrBuilder = new StringBuilder();
  52. var sql = $@"SELECT
  53. CreateName = (SELECT USER_NAME FROM SYS_USER A WHERE A.USER_ID=CREATE_BY),
  54. UpdateName = (SELECT USER_NAME FROM SYS_USER B WHERE B.USER_ID=UPDATE_BY),
  55. UsedFlagName = {_iSQLNodeRepository.GetEnumIntCaseString<UsedFlag>("USED_FLAG")},
  56. PrintModeName = {_iSQLNodeRepository.GetEnumIntCaseString<PrintTemplateMode>("PRINT_MODE")},
  57. IsPrintHeaderName = {_iSQLNodeRepository.GetEnumIntCaseString<YesNoType>("PRINT_HEADER_FLAG")},
  58. IsShowWatermarkName = {_iSQLNodeRepository.GetEnumIntCaseString<YesNoType>("SHOW_WATERMARK_FLAG")},
  59. IsShowLogoName = {_iSQLNodeRepository.GetEnumIntCaseString<YesNoType>("SHOW_LOGO_FLAG")},
  60. SourceTypeName = {_iSQLNodeRepository.GetEnumIntCaseString<SourceType>("SOURCE_TYPE")},
  61. {info.ItemSQL} FROM BAS_PRINT_TEMPLATE WHERE 1=1 AND DEL_FLAG = 0 ";
  62. sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateCode) ?
  63. sqlAndBuilder :
  64. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("TEMPLATE_CODE", info.TemplateCode, DBOperationString._ContainIn));
  65. sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateCode) ?
  66. sqlAndBuilder :
  67. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("TEMPLATE_NAME", info.TemplateName, DBOperationString._ContainIn));
  68. sqlAndBuilder = info.PrintMode ==null ?
  69. sqlAndBuilder :
  70. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_MODE", info.PrintMode, DBOperationString._Equal));
  71. sqlAndBuilder = info.IsUsed == null ?
  72. sqlAndBuilder :
  73. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("USED_FLAG", info.IsUsed, DBOperationString._Equal));
  74. sqlAndBuilder =string.IsNullOrWhiteSpace(info.SourceName) ?
  75. sqlAndBuilder :
  76. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("SOURCE_NAME", info.SourceName, DBOperationString._ContainIn));
  77. sqlAndBuilder = info.IsPrintHeader == null ?
  78. sqlAndBuilder :
  79. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_HEADER_FLAG", info.IsPrintHeader, DBOperationString._Equal));
  80. sql = sql + (sqlAndBuilder.Length > 0 ? _iSQLNodeRepository.GetAndString(sqlAndBuilder, false) : "");
  81. IEnumerable<PrintTemplateResult> result;
  82. IEnumerable<PrintTemplateResult> totalResult;
  83. totalResult = new DataRepository<PrintTemplateResult>(_dataContext).Query(sql);
  84. if (info.PageIndex == 0 || info.PageSize == 0)
  85. {
  86. result = totalResult.ToList();
  87. }
  88. else
  89. {
  90. result = new DataRepository<PrintTemplateResult>(_dataContext).QueryPage(sql, "CREATE_TIME", info.PageSize, info.PageIndex, true);
  91. }
  92. return SuccessStatus(new PageQueryResultInfo<PrintTemplateResult>
  93. {
  94. RowData = result,
  95. PageConditionInfo = info,
  96. TotalCount = totalResult.Count(),
  97. TotalPageCount = (int)Math.Ceiling((double)totalResult.Count() / info.PageSize)
  98. });
  99. }
  100. public OperateResultInfo Add(PrintTemplateCondition info)
  101. {
  102. if (new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Query().Where(m => m.PRINT_TEMPLATE_CODE == info.TemplateCode
  103. && m.DEL_FLAG == 0 && m.USED_FLAG == 1).Any())
  104. {
  105. return FailMessageStatus("打印模版编码已经存在,请输入其它编码!");
  106. }
  107. var now = DateTime.Now;
  108. var id =
  109. new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Query().Count() == 0 ? 1 :
  110. new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Query().Select(s => s.PRINT_TEMPLATE_ID).Max() + 1;
  111. var entity = new BAS_PRINT_TEMPLATE
  112. {
  113. CREATE_BY = info.OperationUserId,
  114. CREATE_TIME = now,
  115. UPDATE_BY = info.OperationUserId,
  116. UPDATE_TIME = now,
  117. USED_FLAG = Convert.ToInt32(info.IsUsed),
  118. DESCRIBE = info.Describe,
  119. DEL_FLAG = 0,
  120. PRINT_HEADER_FLAG = info.IsPrintHeader ? 1 : 0,
  121. PRINT_MODE=info.PrintMode,
  122. PRINT_TEMPLATE_CODE = info.TemplateCode,
  123. PRINT_NUMBER=info.PrintNumber,
  124. PRINT_TEMPLATE_NAME = info.TemplateName,
  125. SOURCE_CODE=info.SourceCode,
  126. SHOW_LOGO_FLAG = info.IsShowLogo ? 1 : 0,
  127. SHOW_WATERMARK_FLAG = info.IsShowWatermark ? 1 : 0,
  128. SOURCE_CONTENT = info.SourceContent,
  129. SOURCE_NAME = info.SourceCode,
  130. SOURCE_TYPE = info.SourceType,
  131. PRINT_TEMPLATE_ID= id,
  132. };
  133. var affectedRows = new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Add(entity);
  134. return GetStatus(affectedRows);
  135. }
  136. public OperateResultInfo Deleted(PrintTemplateCondition info)
  137. {
  138. var sqlAndBuilder = new StringBuilder();
  139. sqlAndBuilder = info.TemplateId != null ?
  140. info.TemplateId > 0 ?
  141. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_ID", info.TemplateId, DBOperationString._Equal)) :
  142. sqlAndBuilder : sqlAndBuilder;
  143. sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateCode) ?
  144. sqlAndBuilder :
  145. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_CODE", info.TemplateCode, DBOperationString._Equal));
  146. sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateIds) ?
  147. sqlAndBuilder :
  148. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_ID", info.TemplateIds, DBOperationString._In));
  149. if (sqlAndBuilder.Length == 0)
  150. {
  151. return FailMessageStatus("参数错误!");
  152. }
  153. var now = DateTime.Now;
  154. var sql = $@"UPDATE BAS_PRINT_TEMPLATE SET DEL_FLAG=1,UPDATE_BY={info.OperationUserId},UPDATE_TIME='{now}'
  155. WHERE 1=1 {sqlAndBuilder}";
  156. var affectedRows = new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Execute(sql);
  157. return GetStatus(affectedRows, info.TemplateIds.Split(',').Length);
  158. }
  159. public OperateResultInfo Edit(PrintTemplateCondition info)
  160. {
  161. if (info == null)
  162. {
  163. return FailStatus();
  164. }
  165. if (!UtilCheckString.GetDynamicParmaValidateResult(info.SourceCode, info.SourceType, info.PrintMode, info.SourceName, info.TemplateCode, info.TemplateName))
  166. {
  167. return FailMessageStatus("参数错误!");
  168. }
  169. var whereList = new List<FieldKeyInfo>()
  170. .AddFieldKeyInfo(nameof(BAS_PRINT_TEMPLATE.PRINT_TEMPLATE_CODE), info.TemplateCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
  171. !string.IsNullOrWhiteSpace(info.TemplateCode))
  172. .AddFieldKeyInfo(nameof(BAS_PRINT_TEMPLATE.PRINT_TEMPLATE_ID), info.TemplateId, EnumCSharpPropertyType.INT, DBOperationString._Equal,
  173. info.TemplateId != null);
  174. var result = new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Query(whereList).FirstOrDefault();
  175. if (result != null)
  176. {
  177. var now = DateTime.Now;
  178. result.DESCRIBE = info.Describe;
  179. result.PRINT_TEMPLATE_NAME = info.TemplateName;
  180. result.PRINT_TEMPLATE_CODE = info.TemplateCode;
  181. result.PRINT_TEMPLATE_ID = result.PRINT_TEMPLATE_ID;
  182. result.PRINT_NUMBER = info.PrintNumber;
  183. result.PRINT_MODE = info.PrintMode;
  184. result.SOURCE_CODE = info.SourceCode;
  185. result.SOURCE_NAME = info.SourceName;
  186. result.SOURCE_TYPE = info.SourceType;
  187. result.SOURCE_CONTENT = info.SourceContent;
  188. result.PRINT_HEADER_FLAG = info.IsPrintHeader ? 1 : 0;
  189. result.SHOW_LOGO_FLAG = info.IsShowLogo ? 1 : 0;
  190. result.SHOW_WATERMARK_FLAG = info.IsShowWatermark ? 1 : 0;
  191. result.UPDATE_BY = info.OperationUserId;
  192. result.UPDATE_TIME = now;
  193. result.USED_FLAG = info.IsUsed ? 1 : 0;
  194. var affectedRows = new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Update(result, "PRINT_TEMPLATE_ID", "NEWID");
  195. return GetStatus(affectedRows);
  196. }
  197. return FailMessageStatus("未查找到数据!");
  198. }
  199. public OperateResultInfo Remove(PrintTemplateCondition info)
  200. {
  201. if (string.IsNullOrWhiteSpace(info.TemplateCode) && info.TemplateId == null && string.IsNullOrWhiteSpace(info.TemplateIds))
  202. {
  203. return FailMessageStatus("参数错误!");
  204. }
  205. var sqlAndBuilder = new StringBuilder();
  206. var sqlOrBuilder = new StringBuilder();
  207. var sql = $@"SELECT {info.ItemSQL} FROM BAS_RULE WHERE 1=1 AND DEL_FLAG = 0 ";
  208. sqlAndBuilder = string.IsNullOrWhiteSpace(info.TemplateCode) ?
  209. sqlAndBuilder :
  210. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_CODE", info.TemplateCode, DBOperationString._Equal));
  211. sqlAndBuilder = info.TemplateId != null ?
  212. sqlAndBuilder :
  213. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("PRINT_TEMPLATE_ID", info.TemplateId, DBOperationString._Equal));
  214. sql = sql + (sqlAndBuilder.Length > 0 ? _iSQLNodeRepository.GetAndString(sqlAndBuilder, false) : "");
  215. var result = new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Query(sql);
  216. if (result.Count() != 1)
  217. {
  218. return FailMessageStatus("参数错误!");
  219. }
  220. var affectedRows = new DataRepository<BAS_PRINT_TEMPLATE>(_dataContext).Remove("PRINT_TEMPLATE_ID", result.FirstOrDefault().PRINT_TEMPLATE_ID.ToString());
  221. return GetStatus(affectedRows);
  222. }
  223. }
  224. }