RmProductsApplicationService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web.Mvc;
  7. using Abp.Application.Services.Dto;
  8. using Abp.Auditing;
  9. using Abp.Authorization;
  10. using Abp.Domain.Repositories;
  11. using Abp.Runtime.Caching;
  12. using IwbZero.Auditing;
  13. using IwbZero.AppServiceBase;
  14. using ShwasherSys.Authorization.Permissions;
  15. using ShwasherSys.Lambda;
  16. using ShwasherSys.ProductInfo.Dto;
  17. using Abp.Extensions;
  18. using Abp.Timing;
  19. using IwbZero.IdentityFramework;
  20. using IwbZero.Setting;
  21. using ShwasherSys.BaseSysInfo.SysAttachFiles;
  22. using ShwasherSys.EntityFramework;
  23. using ShwasherSys.ProductInfo.Dto.FileUpload;
  24. namespace ShwasherSys.ProductInfo
  25. {
  26. [AbpAuthorize]
  27. public class RmProductAppService : IwbZeroAsyncCrudAppService<RmProduct, RmProductDto, string, IwbPagedRequestDto, RmProductCreateDto, RmProductUpdateDto >, IRmProductAppService
  28. {
  29. public RmProductAppService(
  30. ICacheManager cacheManager,
  31. IRepository<RmProduct, string> repository, ISqlExecuter sqlExecuter) : base(repository, "Id")
  32. {
  33. SqlExecuter = sqlExecuter;
  34. CacheManager = cacheManager;
  35. }
  36. protected override bool KeyIsAuto { get; set; } = false;
  37. protected ISqlExecuter SqlExecuter { get; }
  38. #region GetSelect
  39. [DisableAuditing]
  40. public override async Task<List<SelectListItem>> GetSelectList()
  41. {
  42. var list = await Repository.GetAllListAsync();
  43. var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
  44. foreach (var l in list)
  45. {
  46. //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
  47. }
  48. return sList;
  49. }
  50. [DisableAuditing]
  51. public override async Task<string> GetSelectStr()
  52. {
  53. var list = await Repository.GetAllListAsync();
  54. string str = "<option value=\"\" selected>请选择...</option>";
  55. foreach (var l in list)
  56. {
  57. //str += $"<option value=\"{l.Id}\">{l.}</option>";
  58. }
  59. return str;
  60. }
  61. #endregion
  62. #region CURD
  63. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductCreate)]
  64. public override async Task Create(RmProductCreateDto input)
  65. {
  66. await CreateEntity(input);
  67. }
  68. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductUpdate)]
  69. public override async Task Update(RmProductUpdateDto input)
  70. {
  71. await UpdateEntity(input);
  72. }
  73. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductDelete)]
  74. public override Task Delete(EntityDto<string> input)
  75. {
  76. return Repository.DeleteAsync(input.Id);
  77. }
  78. [DisableAuditing]
  79. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductQuery)]
  80. public override async Task<PagedResultDto<RmProductDto>> GetAll(IwbPagedRequestDto input)
  81. {
  82. var query = CreateFilteredQuery(input);
  83. query = ApplyFilter(query, input);
  84. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  85. query = ApplySorting(query, input);
  86. query = ApplyPaging(query, input);
  87. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  88. var dtoList = new PagedResultDto<RmProductDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  89. return dtoList;
  90. }
  91. #region GetEntity/Dto
  92. /// <summary>
  93. /// 查询实体Dto
  94. /// </summary>
  95. /// <param name="input"></param>
  96. /// <returns></returns>
  97. [DisableAuditing]
  98. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductQuery)]
  99. public override async Task<RmProductDto> GetDto(EntityDto<string> input)
  100. {
  101. var entity = await GetEntity(input);
  102. return MapToEntityDto(entity);
  103. }
  104. /// <summary>
  105. /// 查询实体Dto
  106. /// </summary>
  107. /// <param name="id"></param>
  108. /// <returns></returns>
  109. [DisableAuditing]
  110. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductQuery)]
  111. public override async Task<RmProductDto> GetDtoById(string id)
  112. {
  113. var entity = await GetEntityById(id);
  114. return MapToEntityDto(entity);
  115. }
  116. /// <summary>
  117. /// 查询实体Dto(需指明自定义字段)
  118. /// </summary>
  119. /// <param name="no"></param>
  120. /// <returns></returns>
  121. [DisableAuditing]
  122. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductQuery)]
  123. public override async Task<RmProductDto> GetDtoByNo(string no)
  124. {
  125. var entity = await GetEntityByNo(no);
  126. return MapToEntityDto(entity);
  127. }
  128. /// <summary>
  129. /// 查询实体
  130. /// </summary>
  131. /// <param name="input"></param>
  132. /// <returns></returns>
  133. [DisableAuditing]
  134. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductQuery)]
  135. public override async Task<RmProduct> GetEntity(EntityDto<string> input)
  136. {
  137. var entity = await GetEntityById(input.Id);
  138. return entity;
  139. }
  140. /// <summary>
  141. /// 查询实体
  142. /// </summary>
  143. /// <param name="id"></param>
  144. /// <returns></returns>
  145. [DisableAuditing]
  146. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductQuery)]
  147. public override async Task<RmProduct> GetEntityById(string id)
  148. {
  149. return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
  150. }
  151. /// <summary>
  152. /// 查询实体(需指明自定义字段)
  153. /// </summary>
  154. /// <param name="no"></param>
  155. /// <returns></returns>
  156. [DisableAuditing]
  157. [AbpAuthorize(PermissionNames.PagesProductInfoRmProductQuery)]
  158. public override async Task<RmProduct> GetEntityByNo(string no)
  159. {
  160. //CheckGetPermission();
  161. if (string.IsNullOrEmpty(KeyFiledName))
  162. {
  163. ThrowError("NoKeyFieldName");
  164. }
  165. return await base.GetEntityByNo(no);
  166. }
  167. #endregion
  168. #region Hide
  169. ///// <summary>
  170. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{RmProduct}"/>过滤查询.
  171. ///// </summary>
  172. ///// <param name="input">The input.</param>
  173. //protected override IQueryable<RmProduct> CreateFilteredQuery(IwbPagedRequestDto input)
  174. //{
  175. // var query = Repository.GetAll();
  176. // var pagedInput = input as IIwbPagedRequest;
  177. // if (pagedInput == null)
  178. // {
  179. // return query;
  180. // }
  181. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  182. // {
  183. // object keyWords = pagedInput.KeyWords;
  184. // LambdaObject obj = new LambdaObject()
  185. // {
  186. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  187. // FieldName = pagedInput.KeyField,
  188. // FieldValue = keyWords,
  189. // ExpType = (LambdaExpType)pagedInput.ExpType
  190. // };
  191. // var exp = obj.GetExp<RmProduct>();
  192. // query = exp != null ? query.Where(exp) : query;
  193. // }
  194. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  195. // {
  196. // List<LambdaObject> objList = new List<LambdaObject>();
  197. // foreach (var o in pagedInput.SearchList)
  198. // {
  199. // if (string.IsNullOrEmpty(o.KeyWords))
  200. // continue;
  201. // object keyWords = o.KeyWords;
  202. // objList.Add(new LambdaObject
  203. // {
  204. // FieldType = (LambdaFieldType)o.FieldType,
  205. // FieldName = o.KeyField,
  206. // FieldValue = keyWords,
  207. // ExpType = (LambdaExpType)o.ExpType
  208. // });
  209. // }
  210. // var exp = objList.GetExp<RmProduct>();
  211. // query = exp != null ? query.Where(exp) : query;
  212. // }
  213. // return query;
  214. //}
  215. //protected override IQueryable<RmProduct> ApplySorting(IQueryable<RmProduct> query, IwbPagedRequestDto input)
  216. //{
  217. // return query.OrderBy(a => a.No);
  218. //}
  219. //protected override IQueryable<RmProduct> ApplyPaging(IQueryable<RmProduct> query, IwbPagedRequestDto input)
  220. //{
  221. // if (input is IPagedResultRequest pagedInput)
  222. // {
  223. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  224. // }
  225. // return query;
  226. //}
  227. #endregion
  228. #endregion
  229. [AbpAuthorize(PermissionNames.PagesProductInfoSemiProductsImportExcel)]
  230. public bool ImportExcel(FileUploadInfoDto input)
  231. {
  232. if (!string.IsNullOrEmpty(input.FileInfo))
  233. {
  234. string filePath =
  235. $"{SettingManager.GetSettingValue(SettingNames.DownloadPath)}/tmpUpload";
  236. var lcRetVal = SysAttachFileAppService.Base64ToFile(input.FileInfo,
  237. $"{input.FileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}", input.FileExt,
  238. filePath);
  239. StringBuilder errStringBuilder = new StringBuilder();
  240. List<RmProduct> resultEntityList = ExcelHelper.ExcelToEntityList<RmProduct>(new Dictionary<string, string>() { { "Id", "编码" }, { "Material", "材质" }, { "Model", "原材料规格" } }, $"{AppDomain.CurrentDomain.BaseDirectory}{lcRetVal}",
  241. out errStringBuilder);
  242. int indexCount = 1;
  243. if (resultEntityList.Any())
  244. {
  245. StringBuilder sbSql = new StringBuilder();
  246. sbSql.Append("delete from RmProduct;");
  247. foreach (var p in resultEntityList)
  248. {
  249. p.CreationTime = Clock.Now;
  250. p.CreatorUserId = AbpSession.UserId;
  251. p.ProductName = p.Material+" "+p.Model;
  252. p.Model = p.Model;
  253. p.Material = p.Material;
  254. sbSql.Append(p.InsertSql() + ";\r\n");
  255. indexCount++;
  256. }
  257. if (SqlExecuter.Execute(sbSql.ToString()) > 0)
  258. {
  259. return true;
  260. }
  261. }
  262. else
  263. {
  264. CheckErrors(new IwbIdentityResult("文件内容为空(或不符合要求),请检查!"));
  265. }
  266. this.LogError(errStringBuilder.ToString());
  267. }
  268. else
  269. {
  270. CheckErrors(new IwbIdentityResult("请先上传文件!"));
  271. }
  272. return false;
  273. }
  274. }
  275. }