RmEnterStoresApplicationService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Web.Mvc;
  8. using Abp.Application.Services.Dto;
  9. using Abp.Auditing;
  10. using Abp.Authorization;
  11. using Abp.Domain.Repositories;
  12. using Abp.Runtime.Caching;
  13. using Abp.Timing;
  14. using IwbZero.Auditing;
  15. using IwbZero.AppServiceBase;
  16. using IwbZero.Helper;
  17. using NPOI.HPSF;
  18. using ShwasherSys.Authorization.Permissions;
  19. using ShwasherSys.RmStore.Dto;
  20. namespace ShwasherSys.RmStore
  21. {
  22. [AbpAuthorize]
  23. public class RmEnterStoreAppService : IwbZeroAsyncCrudAppService<RmEnterStore, RmEnterStoreDto, string, IwbPagedRequestDto, RmEnterStoreCreateDto, RmEnterStoreUpdateDto >, IRmEnterStoreAppService
  24. {
  25. public RmEnterStoreAppService(
  26. ICacheManager cacheManager,
  27. IRepository<RmEnterStore, string> repository, IRepository<ViewRmEnterStore, string> viewRmEnterStoreRepository, IRepository<CurrentRmStoreHouse, string> currentRmStoreHouseRepository) : base(repository, "Id")
  28. {
  29. ViewRmEnterStoreRepository = viewRmEnterStoreRepository;
  30. CurrentRmStoreHouseRepository = currentRmStoreHouseRepository;
  31. CacheManager = cacheManager;
  32. }
  33. protected override bool KeyIsAuto { get; set; } = false;
  34. protected IRepository<ViewRmEnterStore,string> ViewRmEnterStoreRepository { get; }
  35. protected IRepository<CurrentRmStoreHouse,string> CurrentRmStoreHouseRepository { get; }
  36. #region GetSelect
  37. [DisableAuditing]
  38. public override async Task<List<SelectListItem>> GetSelectList()
  39. {
  40. var list = await Repository.GetAllListAsync();
  41. var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
  42. foreach (var l in list)
  43. {
  44. //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
  45. }
  46. return sList;
  47. }
  48. [DisableAuditing]
  49. public override async Task<string> GetSelectStr()
  50. {
  51. var list = await Repository.GetAllListAsync();
  52. string str = "<option value=\"\" selected>请选择...</option>";
  53. foreach (var l in list)
  54. {
  55. //str += $"<option value=\"{l.Id}\">{l.}</option>";
  56. }
  57. return str;
  58. }
  59. #endregion
  60. #region CURD
  61. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgCreate)]
  62. public override async Task Create(RmEnterStoreCreateDto input)
  63. {
  64. input.Id =Guid.NewGuid().ToString("N");
  65. input.ProductBatchNum = await GetBatchNum();
  66. input.ApplyEnterDate = Clock.Now;
  67. input.ApplyStatus = RmEnterOutStatusEnum.Applying.ToInt();
  68. await CreateEntity(input);
  69. }
  70. private async Task<string> GetBatchNum()
  71. {
  72. string startNo = $"{DateTime.Now:yyMM}";
  73. var lastEntity = await Repository.GetAll().Where(a => a.ProductBatchNum.StartsWith(startNo)).OrderByDescending(a=>a.ProductBatchNum).ThenByDescending(a => a.Id)
  74. .FirstOrDefaultAsync();
  75. int noLength = 3, index = 0;
  76. if (lastEntity != null)
  77. {
  78. var entityNo = lastEntity.ProductBatchNum;
  79. index = Convert.ToInt32(entityNo.Substring(entityNo.Length - noLength));
  80. }
  81. index++;
  82. string no = $"{startNo}{index.LeftPad(noLength)}";
  83. while ((await Repository.CountAsync(a=>a.ProductBatchNum==no)) > 0)
  84. {
  85. index++;
  86. no = $"{startNo}{index.LeftPad(noLength)}";
  87. Thread.Sleep(100);
  88. }
  89. return no;
  90. }
  91. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgUpdate)]
  92. public override async Task Update(RmEnterStoreUpdateDto input)
  93. {
  94. await UpdateEntity(input);
  95. }
  96. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgDelete)]
  97. public override Task Delete(EntityDto<string> input)
  98. {
  99. return Repository.DeleteAsync(input.Id);
  100. }
  101. [DisableAuditing]
  102. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgQuery)]
  103. public override async Task<PagedResultDto<RmEnterStoreDto>> GetAll(IwbPagedRequestDto input)
  104. {
  105. var query = CreateFilteredQuery(input);
  106. query = ApplyFilter(query, input);
  107. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  108. query = ApplySorting(query, input);
  109. query = ApplyPaging(query, input);
  110. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  111. var dtoList = new PagedResultDto<RmEnterStoreDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  112. return dtoList;
  113. }
  114. #region GetEntity/Dto
  115. /// <summary>
  116. /// 查询实体Dto
  117. /// </summary>
  118. /// <param name="input"></param>
  119. /// <returns></returns>
  120. [DisableAuditing]
  121. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgQuery)]
  122. public override async Task<RmEnterStoreDto> GetDto(EntityDto<string> input)
  123. {
  124. var entity = await GetEntity(input);
  125. return MapToEntityDto(entity);
  126. }
  127. /// <summary>
  128. /// 查询实体Dto
  129. /// </summary>
  130. /// <param name="id"></param>
  131. /// <returns></returns>
  132. [DisableAuditing]
  133. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgQuery)]
  134. public override async Task<RmEnterStoreDto> GetDtoById(string id)
  135. {
  136. var entity = await GetEntityById(id);
  137. return MapToEntityDto(entity);
  138. }
  139. /// <summary>
  140. /// 查询实体Dto(需指明自定义字段)
  141. /// </summary>
  142. /// <param name="no"></param>
  143. /// <returns></returns>
  144. [DisableAuditing]
  145. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgQuery)]
  146. public override async Task<RmEnterStoreDto> GetDtoByNo(string no)
  147. {
  148. var entity = await GetEntityByNo(no);
  149. return MapToEntityDto(entity);
  150. }
  151. /// <summary>
  152. /// 查询实体
  153. /// </summary>
  154. /// <param name="input"></param>
  155. /// <returns></returns>
  156. [DisableAuditing]
  157. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgQuery)]
  158. public override async Task<RmEnterStore> GetEntity(EntityDto<string> input)
  159. {
  160. var entity = await GetEntityById(input.Id);
  161. return entity;
  162. }
  163. /// <summary>
  164. /// 查询实体
  165. /// </summary>
  166. /// <param name="id"></param>
  167. /// <returns></returns>
  168. [DisableAuditing]
  169. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgQuery)]
  170. public override async Task<RmEnterStore> GetEntityById(string id)
  171. {
  172. return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
  173. }
  174. /// <summary>
  175. /// 查询实体(需指明自定义字段)
  176. /// </summary>
  177. /// <param name="no"></param>
  178. /// <returns></returns>
  179. [DisableAuditing]
  180. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgQuery)]
  181. public override async Task<RmEnterStore> GetEntityByNo(string no)
  182. {
  183. //CheckGetPermission();
  184. if (string.IsNullOrEmpty(KeyFiledName))
  185. {
  186. ThrowError("NoKeyFieldName");
  187. }
  188. return await base.GetEntityByNo(no);
  189. }
  190. #endregion
  191. #region Hide
  192. ///// <summary>
  193. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{RmEnterStore}"/>过滤查询.
  194. ///// </summary>
  195. ///// <param name="input">The input.</param>
  196. //protected override IQueryable<RmEnterStore> CreateFilteredQuery(IwbPagedRequestDto input)
  197. //{
  198. // var query = Repository.GetAll();
  199. // var pagedInput = input as IIwbPagedRequest;
  200. // if (pagedInput == null)
  201. // {
  202. // return query;
  203. // }
  204. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  205. // {
  206. // object keyWords = pagedInput.KeyWords;
  207. // LambdaObject obj = new LambdaObject()
  208. // {
  209. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  210. // FieldName = pagedInput.KeyField,
  211. // FieldValue = keyWords,
  212. // ExpType = (LambdaExpType)pagedInput.ExpType
  213. // };
  214. // var exp = obj.GetExp<RmEnterStore>();
  215. // query = exp != null ? query.Where(exp) : query;
  216. // }
  217. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  218. // {
  219. // List<LambdaObject> objList = new List<LambdaObject>();
  220. // foreach (var o in pagedInput.SearchList)
  221. // {
  222. // if (string.IsNullOrEmpty(o.KeyWords))
  223. // continue;
  224. // object keyWords = o.KeyWords;
  225. // objList.Add(new LambdaObject
  226. // {
  227. // FieldType = (LambdaFieldType)o.FieldType,
  228. // FieldName = o.KeyField,
  229. // FieldValue = keyWords,
  230. // ExpType = (LambdaExpType)o.ExpType
  231. // });
  232. // }
  233. // var exp = objList.GetExp<RmEnterStore>();
  234. // query = exp != null ? query.Where(exp) : query;
  235. // }
  236. // return query;
  237. //}
  238. //protected override IQueryable<RmEnterStore> ApplySorting(IQueryable<RmEnterStore> query, IwbPagedRequestDto input)
  239. //{
  240. // return query.OrderBy(a => a.No);
  241. //}
  242. //protected override IQueryable<RmEnterStore> ApplyPaging(IQueryable<RmEnterStore> query, IwbPagedRequestDto input)
  243. //{
  244. // if (input is IPagedResultRequest pagedInput)
  245. // {
  246. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  247. // }
  248. // return query;
  249. //}
  250. #endregion
  251. #endregion
  252. [DisableAuditing]
  253. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgQuery)]
  254. public async Task<PagedResultDto<ViewRmEnterStore>> GetAllView(IwbPagedRequestDto input)
  255. {
  256. var query = ViewRmEnterStoreRepository.GetAll();
  257. query = ApplyFilter(query, input);
  258. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  259. query = _ApplySorting(query, input);
  260. query = _ApplyPaging(query, input);
  261. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  262. var dtoList = new PagedResultDto<ViewRmEnterStore>(totalCount, entities);
  263. return dtoList;
  264. }
  265. [AbpAuthorize(PermissionNames.PagesRawMaterialStoreRmStoreEnterMgUpdate, PermissionNames.PagesRawMaterialStoreRmStoreEnterMgDelete)]
  266. public async Task UpdateState(RwEnterStatusUpdateDto input)
  267. {
  268. var entity = await Repository.GetAsync(input.Id);
  269. entity.ApplyStatus = input.ApplyStatus;
  270. if (entity.ApplyStatus == RmEnterOutStatusEnum.Stored.ToInt())
  271. {
  272. entity.Quantity = input.Quantity;
  273. entity.EnterStoreDate = Clock.Now;
  274. entity.EnterStoreUser = AbpSession.UserName;
  275. entity.AuditDate = Clock.Now;
  276. var isExist = CurrentRmStoreHouseRepository.FirstOrDefault(i =>
  277. i.ProductBatchNum == entity.ProductBatchNum && i.StoreLocationNo == entity.StoreLocationNo&&i.StoreHouseId == entity.StoreHouseId);
  278. if (isExist!=null)
  279. {
  280. isExist.Quantity += entity.Quantity;
  281. await CurrentRmStoreHouseRepository.UpdateAsync(isExist);
  282. }
  283. else
  284. {
  285. CurrentRmStoreHouse crsh = new CurrentRmStoreHouse()
  286. {
  287. Id = Guid.NewGuid().ToString("N"),
  288. RmProductNo = entity.RmProductNo,
  289. Quantity = entity.Quantity,
  290. StoreLocationNo = entity.StoreLocationNo,
  291. ProductionOrderNo = entity.ProductionOrderNo,
  292. ProductBatchNum = entity.ProductBatchNum,
  293. FreezeQuantity = 0,
  294. StoreHouseId = entity.StoreHouseId
  295. };
  296. CurrentRmStoreHouseRepository.Insert(crsh);
  297. }
  298. }
  299. await Repository.UpdateAsync(entity);
  300. }
  301. }
  302. }