FinshedEnterStoresApplicationService.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  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.Extensions;
  12. using Abp.Runtime.Caching;
  13. using Abp.Timing;
  14. using IwbZero.AppServiceBase;
  15. using IwbZero.Auditing;
  16. using IwbZero.IdentityFramework;
  17. using IwbZero.Setting;
  18. using Newtonsoft.Json;
  19. using ShwasherSys.Authorization.Permissions;
  20. using ShwasherSys.BaseSysInfo;
  21. using ShwasherSys.BaseSysInfo.States;
  22. using ShwasherSys.Common;
  23. using ShwasherSys.FinshedStoreInfo.Dto;
  24. using ShwasherSys.Lambda;
  25. using ShwasherSys.ProductionOrderInfo;
  26. using ShwasherSys.ProductStoreInfo;
  27. using ShwasherSys.SemiProductStoreInfo;
  28. using ShwasherSys.SemiProductStoreInfo.Dto;
  29. namespace ShwasherSys.FinshedStoreInfo
  30. {
  31. [AbpAuthorize, AuditLog("成品仓库维护")]
  32. public class FinshedEnterStoreAppService : ShwasherAsyncCrudAppService<FinshedEnterStore, FinshedEnterStoreDto, int, PagedRequestDto, FinshedEnterStoreCreateDto, FinshedEnterStoreUpdateDto>, IFinshedEnterStoreAppService
  33. {
  34. public FinshedEnterStoreAppService(
  35. IRepository<BusinessLog> logRepository,
  36. IIwbSettingManager settingManager,
  37. ICacheManager cacheManager,
  38. IRepository<FinshedEnterStore, int> repository, IRepository<ViewProductEnterStore> viewProductEnterStoreRepository, IRepository<CurrentProductStoreHouse> currentProductStoreHouseRepository, ICommonAppService commonAppService, IStatesAppService statesAppService) : base(repository, "")
  39. {
  40. SettingManager = settingManager;
  41. CacheManager = cacheManager;
  42. StatesAppService = statesAppService;
  43. LogRepository = logRepository;
  44. ViewProductEnterStoreRepository = viewProductEnterStoreRepository;
  45. CurrentProductStoreHouseRepository = currentProductStoreHouseRepository;
  46. CommonAppService = commonAppService;
  47. }
  48. protected IStatesAppService StatesAppService { get; set; }
  49. protected override bool KeyIsAuto { get; set; } = false;
  50. public IRepository<BusinessLog> LogRepository { get; }
  51. public IRepository<ViewProductEnterStore> ViewProductEnterStoreRepository { get; }
  52. public IRepository<CurrentProductStoreHouse> CurrentProductStoreHouseRepository { get; }
  53. protected ICommonAppService CommonAppService { get; }
  54. #region GetSelect
  55. [DisableAuditing]
  56. public async Task<List<SelectListItem>> GetSelectList()
  57. {
  58. var list = await Repository.GetAllListAsync();
  59. var slist = new List<SelectListItem> { new SelectListItem { Text = @"请选择...", Value = "", Selected = true } };
  60. foreach (var l in list)
  61. {
  62. //slist.Add(new SelectListItem { Text = l., Value = l. });
  63. }
  64. return slist;
  65. }
  66. [DisableAuditing]
  67. public async Task<string> GetSelectStr()
  68. {
  69. var list = await Repository.GetAllListAsync();
  70. string str = "<option value=\"\" selected>请选择...</option>";
  71. foreach (var l in list)
  72. {
  73. //str += $"<option value=\"{l.}\">{l.}</option>";
  74. }
  75. return str;
  76. }
  77. #endregion
  78. #region CURD
  79. #region Get
  80. [DisableAuditing]
  81. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMgQuery)]
  82. public Task<FinshedEnterStore> GetEntityById(int id)
  83. {
  84. return Repository.GetAsync(id);
  85. }
  86. [DisableAuditing]
  87. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMgQuery)]
  88. public Task<FinshedEnterStore> GetEntityByNo(string no)
  89. {
  90. if (KeyFiledName.IsNullOrEmpty())
  91. {
  92. CheckErrors(IwbIdentityResult.Failed("编码/编号字段不明确,请检查后再操作!"));
  93. }
  94. LambdaObject obj = new LambdaObject()
  95. {
  96. FieldType = LambdaFieldType.S,
  97. FieldName = KeyFiledName,
  98. FieldValue = no,
  99. ExpType = LambdaExpType.Equal
  100. };
  101. var exp = obj.GetExp<FinshedEnterStore>();
  102. return Repository.FirstOrDefaultAsync(exp);
  103. }
  104. [DisableAuditing]
  105. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMgQuery)]
  106. public async Task<FinshedEnterStoreDto> GetDtoById(int id)
  107. {
  108. var entity = await GetEntityById(id);
  109. return MapToEntityDto(entity);
  110. }
  111. [DisableAuditing]
  112. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMgQuery)]
  113. public async Task<FinshedEnterStoreDto> GetDtoByNo(string no)
  114. {
  115. var entity = await GetEntityByNo(no);
  116. return MapToEntityDto(entity);
  117. }
  118. #endregion
  119. [DisableAuditing]
  120. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMg)]
  121. public override async Task<PagedResultDto<FinshedEnterStoreDto>> GetAll(PagedRequestDto input)
  122. {
  123. CheckGetAllPermission();
  124. var query = CreateFilteredQuery(input);
  125. if (input.SearchList != null && input.SearchList.Count > 0)
  126. {
  127. List<LambdaObject> objList = new List<LambdaObject>();
  128. foreach (var o in input.SearchList)
  129. {
  130. if (o.KeyWords.IsNullOrEmpty())
  131. continue;
  132. object keyWords = o.KeyWords;
  133. objList.Add(new LambdaObject
  134. {
  135. FieldType = (LambdaFieldType)o.FieldType,
  136. FieldName = o.KeyField,
  137. FieldValue = keyWords,
  138. ExpType = (LambdaExpType)o.ExpType
  139. });
  140. }
  141. var exp = objList.GetExp<FinshedEnterStore>();
  142. query = query.Where(exp);
  143. }
  144. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  145. query = ApplySorting(query, input);
  146. query = ApplyPaging(query, input);
  147. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  148. var dtos = new PagedResultDto<FinshedEnterStoreDto>(
  149. totalCount,
  150. entities.Select(MapToEntityDto).ToList()
  151. );
  152. return dtos;
  153. }
  154. [DisableAuditing]
  155. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMg)]
  156. public async Task<PagedResultDto<ViewProductEnterStore>> GetAllView(PagedRequestDto input)
  157. {
  158. CheckGetAllPermission();
  159. var query = ViewProductEnterStoreRepository.GetAll();
  160. if (input.SearchList != null && input.SearchList.Count > 0)
  161. {
  162. List<LambdaObject> objList = new List<LambdaObject>();
  163. foreach (var o in input.SearchList)
  164. {
  165. if (o.KeyWords.IsNullOrEmpty())
  166. continue;
  167. object keyWords = o.KeyWords;
  168. //检查查询条件中带有规格且多个分割条件的
  169. ProductModelSearch.CheckModelGreaterOneFilter(o, ref keyWords);
  170. objList.Add(new LambdaObject
  171. {
  172. FieldType = (LambdaFieldType)o.FieldType,
  173. FieldName = o.KeyField,
  174. FieldValue = keyWords,
  175. ExpType = (LambdaExpType)o.ExpType
  176. });
  177. }
  178. var exp = objList.GetExp<ViewProductEnterStore>();
  179. query = exp!=null? query.Where(exp): query;
  180. }
  181. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  182. query = query.OrderByDescending(i => i.TimeCreated);
  183. string pattern = ProductModelSearch.GetModelGreaterOneReg(input.SearchList);
  184. List<ViewProductEnterStore> entities;
  185. //检查查询条件中带有规格且多个分割条件的
  186. if (!pattern.IsNullOrEmpty())
  187. {
  188. var es = query.ToList();
  189. es = es.Where(f => f.Model != null && Regex.IsMatch(f.Model, pattern)).ToList();
  190. totalCount = es.Count;
  191. entities = es.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
  192. }
  193. else
  194. {
  195. query = query.Skip(input.SkipCount).Take(input.MaxResultCount);
  196. entities = await AsyncQueryableExecuter.ToListAsync(query);
  197. }
  198. //query = query.Skip(input.SkipCount).Take(input.MaxResultCount);
  199. //var entities = await AsyncQueryableExecuter.ToListAsync(query);
  200. var dtos = new PagedResultDto<ViewProductEnterStore>(
  201. totalCount,
  202. entities
  203. );
  204. return dtos;
  205. }
  206. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMgUpdate), AuditLog("通过申请")]
  207. public async Task<FinshedEnterStoreDto> Audit(FinshedEnterStoreAuditDto input)
  208. {
  209. var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  210. if (entity.ApplyStatus == FinshedEnterStoreApplyStatusEnum.EnterStored.ToInt())
  211. {
  212. CheckErrors(IwbIdentityResult.Failed("已入库不能再操作!"));
  213. }
  214. //包含库位信息
  215. if (!input.StoreLocationNo.IsNullOrEmpty())
  216. {
  217. //检查当前库位是否存在盘点信息
  218. var isCanChange = CommonAppService.CheckStoreCanUpdateByLocationNo(input.StoreLocationNo, 1);
  219. if (!isCanChange)
  220. {
  221. CheckErrors(IwbIdentityResult.Failed("该库存库位处于退货或者正在盘点状态,不可进行出入库更新!"));
  222. }
  223. }
  224. var createSourceType = input.CreateSourceType??0;
  225. if (createSourceType == EnterStoreCreateSourceEnum.Balance.ToInt()|| createSourceType == EnterStoreCreateSourceEnum.AntiPlating.ToInt() || createSourceType == EnterStoreCreateSourceEnum.NormalReturnGood.ToInt() || createSourceType == EnterStoreCreateSourceEnum.Downgrade.ToInt())
  226. {
  227. entity.ApplyStatus = FinshedEnterStoreApplyStatusEnum.EnterStored.ToInt();
  228. var date = Clock.Now;
  229. var currentStore = CurrentProductStoreHouseRepository.GetAll().FirstOrDefault(i => i.ProductionOrderNo == entity.ProductionOrderNo && i.StoreHouseId == entity.StoreHouseId&&i.StoreLocationNo == input.StoreLocationNo && i.ProductNo == entity.ProductNo);
  230. decimal enterQuantity = input.ActualPackageCount * input.PackageSpecification;
  231. if (currentStore != null)
  232. {
  233. var isCanUpdate =
  234. CommonAppService.CheckStoreRecordCanUpdate(currentStore.CurrentProductStoreHouseNo);
  235. if (!isCanUpdate)
  236. CheckErrors(IwbIdentityResult.Failed("该库存处于退货或者正在盘点状态,不可进行出入库更新!"));
  237. currentStore.Quantity += enterQuantity;
  238. currentStore.TimeLastMod = date;
  239. currentStore.UserIDLastMod = AbpSession.UserName;
  240. currentStore.KgWeight = entity.KgWeight;
  241. await CurrentProductStoreHouseRepository.UpdateAsync(currentStore);
  242. }
  243. else
  244. {
  245. string lcJson = JsonConvert.SerializeObject(entity);
  246. currentStore = lcJson.GetModel<CurrentProductStoreHouse>();
  247. currentStore.CurrentProductStoreHouseNo = Guid.NewGuid().ToString("N");
  248. currentStore.TimeCreated = date;
  249. currentStore.UserIDLastMod = AbpSession.UserName;
  250. currentStore.Quantity = enterQuantity;
  251. currentStore.FreezeQuantity = 0;
  252. currentStore.StoreLocationNo = input.StoreLocationNo;
  253. await CurrentProductStoreHouseRepository.InsertAsync(currentStore);
  254. }
  255. entity.AuditDate = Clock.Now;
  256. entity.ActualPackageCount = input.ActualPackageCount;
  257. entity.PackageSpecification = input.PackageSpecification;
  258. entity.StoreLocationNo = input.StoreLocationNo;
  259. entity.Quantity = enterQuantity;
  260. entity.AuditUser = AbpSession.UserName;
  261. entity.EnterStoreDate = Clock.Now;
  262. entity.EnterStoreUser = AbpSession.UserName;
  263. //string enterCreateSourceName = "";
  264. string enterCreateSourceName =
  265. StatesAppService.GetDisplayValue("FinshedEnterStore", "CreateSourceType", createSourceType + "");
  266. BusinessLogTypeEnum.PStore.WriteLog(LogRepository, $"{enterCreateSourceName}入库确认",
  267. $"{enterCreateSourceName}入库确认通过,数量{entity.ActualPackageCount}",
  268. entity.ProductionOrderNo,logExt2: currentStore.CurrentProductStoreHouseNo);
  269. }
  270. else
  271. {
  272. entity.ApplyStatus = FinshedEnterStoreApplyStatusEnum.Audited.ToInt();
  273. entity.AuditDate = Clock.Now;
  274. entity.ActualPackageCount = input.ActualPackageCount;
  275. entity.StoreLocationNo = input.StoreLocationNo;
  276. entity.Quantity = input.PackageSpecification * input.ActualPackageCount;
  277. entity.AuditUser = AbpSession.UserName;
  278. BusinessLogTypeEnum.PStore.WriteLog(LogRepository, "成品入库申请审核",
  279. $"成品入库申请审核通过,审核数量{entity.ActualPackageCount}",
  280. entity.ProductionOrderNo);
  281. }
  282. await Repository.UpdateAsync(entity);
  283. return MapToEntityDto(entity);
  284. }
  285. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMgRefuse), AuditLog("拒绝申请")]
  286. public async Task<FinshedEnterStoreDto> Refuse(EntityDto<int> input)
  287. {
  288. var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  289. if (entity.ApplyStatus == FinshedEnterStoreApplyStatusEnum.EnterStored.ToInt())
  290. {
  291. CheckErrors(IwbIdentityResult.Failed("已入库不能再操作!"));
  292. }
  293. entity.AuditDate = Clock.Now;
  294. entity.AuditUser = AbpSession.UserName;
  295. entity.ApplyStatus = FinshedEnterStoreApplyStatusEnum.Refused.ToInt();
  296. await Repository.UpdateAsync(entity);
  297. BusinessLogTypeEnum.PStore.WriteLog(LogRepository, "成品入库申请审核",
  298. $"拒绝成品入库申请",
  299. entity.ProductionOrderNo);
  300. return MapToEntityDto(entity);
  301. }
  302. //public async Task AuditBatch(List<EntityDto> inputs)
  303. //{
  304. // if (inputs.Any())
  305. // {
  306. // var date = Clock.Now;
  307. // foreach (var input in inputs)
  308. // {
  309. // var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  310. // if (entity.IsClose|| entity.ApplyStatus != FinshedEnterStoreApplyStatusEnum.Applying.ToInt())
  311. // {
  312. // continue;
  313. // }
  314. // entity.AuditDate = date;
  315. // entity.ActualPackageCount = entity.PackageCount;
  316. // entity.StoreLocationNo = input.StoreLocationNo;
  317. // entity.Quantity = entity.PackageSpecification * entity.ActualPackageCount;
  318. // entity.AuditUser = AbpSession.UserName;
  319. // entity.ApplyStatus = FinshedEnterStoreApplyStatusEnum.Audited.ToInt();
  320. // entity.AuditDate = date;
  321. // entity.AuditUser = AbpSession.UserName;
  322. // await Repository.UpdateAsync(entity);
  323. // }
  324. // }
  325. //}
  326. [DisableAuditing]
  327. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMgQuery)]
  328. public async Task<PagedResultDto<ViewProductEnterStore>> GetViewAll(PagedRequestDto input)
  329. {
  330. var query = ViewProductEnterStoreRepository.GetAll().Where(i=>i.IsClose==false);
  331. if (input.SearchList != null && input.SearchList.Count > 0)
  332. {
  333. List<LambdaObject> objList = new List<LambdaObject>();
  334. foreach (var o in input.SearchList)
  335. {
  336. if (o.KeyWords.IsNullOrEmpty())
  337. continue;
  338. object keyWords = o.KeyWords;
  339. //检查查询条件中带有规格且多个分割条件的
  340. ProductModelSearch.CheckModelGreaterOneFilter(o, ref keyWords);
  341. objList.Add(new LambdaObject
  342. {
  343. FieldType = (LambdaFieldType)o.FieldType,
  344. FieldName = o.KeyField,
  345. FieldValue = keyWords,
  346. ExpType = (LambdaExpType)o.ExpType
  347. });
  348. }
  349. var exp = objList.GetExp<ViewProductEnterStore>();
  350. query = query.Where(exp);
  351. }
  352. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  353. query = query.OrderByDescending(i => i.ApplyEnterDate);
  354. string pattern = ProductModelSearch.GetModelGreaterOneReg(input.SearchList);
  355. List<ViewProductEnterStore> entities;
  356. //检查查询条件中带有规格且多个分割条件的
  357. if (!pattern.IsNullOrEmpty())
  358. {
  359. var es = query.ToList();
  360. es = es.Where(f => f.Model != null && Regex.IsMatch(f.Model, pattern)).ToList();
  361. totalCount = es.Count;
  362. entities = es.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
  363. }
  364. else
  365. {
  366. query = query.Skip(input.SkipCount).Take(input.MaxResultCount);
  367. entities = await AsyncQueryableExecuter.ToListAsync(query);
  368. }
  369. //query = query.Skip(input.SkipCount).Take(input.MaxResultCount);
  370. //var entities = await AsyncQueryableExecuter.ToListAsync(query);
  371. var dtos = new PagedResultDto<ViewProductEnterStore>(
  372. totalCount,
  373. entities
  374. );
  375. return dtos;
  376. }
  377. [AbpAuthorize(PermissionNames.PagesFinshedStoreInfoFinshedEnterStoreApplyMgUpdate), AuditLog("确认入库")]
  378. public async Task<FinshedEnterStore> ConfirmEnterStoreQuantity(EntityDto<int> input)
  379. {
  380. var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  381. if (entity.ApplyStatus == EnterStoreApplyStatusEnum.EnterStored.ToInt())
  382. {
  383. CheckErrors(IwbIdentityResult.Failed("已入库不能再操作!"));
  384. }
  385. entity.ApplyStatus = EnterStoreApplyStatusEnum.EnterStored.ToInt();
  386. var date = Clock.Now;
  387. entity.EnterStoreDate = date;
  388. entity.EnterStoreUser = AbpSession.UserName;
  389. await Repository.UpdateAsync(entity);
  390. var currentStore = CurrentProductStoreHouseRepository.GetAll().FirstOrDefault(i => i.ProductionOrderNo == entity.ProductionOrderNo && i.StoreLocationNo == entity.StoreLocationNo && i.ProductNo== entity.ProductNo);
  391. if (currentStore != null)
  392. {
  393. currentStore.Quantity += entity.Quantity;
  394. currentStore.TimeLastMod = date;
  395. currentStore.UserIDLastMod = AbpSession.UserName;
  396. currentStore.KgWeight = entity.KgWeight;
  397. await CurrentProductStoreHouseRepository.UpdateAsync(currentStore);
  398. }
  399. else
  400. {
  401. string lcJson = JsonConvert.SerializeObject(entity);
  402. currentStore = lcJson.GetModel<CurrentProductStoreHouse>();
  403. currentStore.CurrentProductStoreHouseNo = Guid.NewGuid().ToString("N");
  404. currentStore.TimeCreated = date;
  405. currentStore.UserIDLastMod = AbpSession.UserName;
  406. currentStore.Quantity = entity.Quantity;
  407. currentStore.FreezeQuantity = 0;
  408. await CurrentProductStoreHouseRepository.InsertAsync(currentStore);
  409. }
  410. //取消确认入库自动变更排查单状态
  411. /*var productionOrder =
  412. await Repository.FirstOrDefaultAsync(a => a.ProductionOrderNo == entity.ProductionOrderNo);
  413. if (productionOrder == null)
  414. {
  415. CheckErrors(IwbIdentityResult.Failed("未发现排产单!"));
  416. return null;
  417. }
  418. productionOrder.ProductionOrderStatus = ProductionOrderStatusEnum.EnterStore.ToInt();
  419. await Repository.UpdateAsync(productionOrder);*/
  420. BusinessLogTypeEnum.SStore.WriteLog(LogRepository, "成品入库",
  421. $"确认成品入库数量[{entity.Id}],排产单入库信息:[确认数量:{entity.Quantity},库存:{currentStore.Quantity},冻结:{currentStore.FreezeQuantity}]",
  422. entity.ProductionOrderNo);
  423. return entity;
  424. }
  425. #endregion
  426. }
  427. }