GoodsRepository.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. using AutoMapper;
  2. using EyeSoft.Reflection;
  3. using Newtonsoft.Json;
  4. using OASystem.Domain.Dtos.PersonnelModule;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.Entities.PersonnelModule;
  7. using OASystem.Domain.ViewModels.PersonnelModule;
  8. using System.Drawing;
  9. namespace OASystem.Infrastructure.Repositories.PersonnelModule
  10. {
  11. /// <summary>
  12. /// 物品进销存
  13. /// 仓储
  14. /// </summary>
  15. public class GoodsRepository : BaseRepository<Pm_GoodsInfo, GoodsInfoView>
  16. {
  17. private readonly IMapper _mapper;
  18. private JsonView _jv;
  19. public GoodsRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  20. {
  21. _mapper = mapper;
  22. _jv = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  23. }
  24. /// <summary>
  25. /// 基础数据
  26. /// </summary>
  27. /// <returns></returns>
  28. public async Task<JsonView> InitDataSource()
  29. {
  30. var typeData = await _sqlSugar.Queryable<GoodsTypeView>()
  31. .Includes(x => x.SubTypeItems.Where(z => z.IsDel == 0)
  32. //.Select(z => new {
  33. // z.Id,
  34. // z.STid,
  35. // z.Name
  36. //})
  37. .ToList())
  38. .Where(x => x.IsDel == 0 &&
  39. x.Remark.Equals("GoodsType"))
  40. //.Select(x => new {
  41. // x.Id,
  42. // x.Name,
  43. // x.SubTypeItems
  44. //})
  45. .ToListAsync();
  46. var groupData = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  47. .Where(x => x.IsDel == 0)
  48. .Select(x => new
  49. {
  50. id = x.Id,
  51. groupName = x.TeamName
  52. })
  53. .OrderByDescending(x => x.id)
  54. .ToListAsync();
  55. var userData = await _sqlSugar.Queryable<Sys_Users>()
  56. .Where(x => x.IsDel == 0)
  57. .Select(x => new
  58. {
  59. x.Id,
  60. UserName = x.CnName,
  61. })
  62. .ToListAsync();
  63. _jv.Code = StatusCodes.Status200OK;
  64. _jv.Data = new { goodsTypeData = typeData, groupNameData = groupData, userNameData = userData };
  65. _jv.Msg = $"操作成功";
  66. return _jv;
  67. }
  68. /// <summary>
  69. /// 物品列表
  70. /// </summary>
  71. /// <param name="dto"></param>
  72. /// <returns></returns>
  73. public async Task<JsonView> GoodsList(GoodsListDto dto)
  74. {
  75. var ids = new List<int>();
  76. if (!string.IsNullOrEmpty(dto.TypeIds))
  77. {
  78. var strArray = dto.TypeIds.Split(',');
  79. foreach (var str in strArray)
  80. {
  81. if (int.TryParse(str, out int id))
  82. {
  83. ids.Add(id);
  84. }
  85. }
  86. }
  87. RefAsync<int> total = 0;
  88. var data = await _sqlSugar.Queryable<GoodsListView>()
  89. .Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && z1.AuditStatus == GoodsAuditEnum.Pending).ToList())
  90. .Includes(glv => glv.TypeData)
  91. .Includes(glv => glv.UserData)
  92. .LeftJoin<Sys_SetData>((glv, sd) => glv.Type == sd.Id)
  93. .LeftJoin<Sys_Users>((glv, sd, u) => glv.LastUpdateUserId == u.Id)
  94. .Where(glv => glv.IsDel == 0)
  95. .WhereIF(ids.Count > 0, glv => ids.Contains(glv.Type))
  96. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), glv => glv.Name.Contains(dto.GoodsName))
  97. .OrderByDescending(glv => glv.LastUpdateTime)
  98. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  99. var view = data.Select(x => new
  100. {
  101. x.Id,
  102. x.Name,
  103. x.Type,
  104. TypeName = x.TypeData?.Name ?? string.Empty,
  105. LastUpdateUserName = x.UserData?.CnName ?? string.Empty,
  106. x.LastUpdateTime,
  107. StockQuantity = x.StockQuantity - x.WaitAuditQuantity,
  108. x.Unit,
  109. x.StockQuantityLabel,
  110. x.Remark
  111. }).ToList();
  112. _jv.Code = StatusCodes.Status200OK;
  113. _jv.Data = view;
  114. _jv.Count = total;
  115. _jv.Msg = $"操作成功";
  116. return _jv;
  117. }
  118. /// <summary>
  119. /// 物品Info
  120. /// </summary>
  121. /// <param name="portType"></param>
  122. /// <param name="id"></param>
  123. /// <returns></returns>
  124. public async Task<JsonView> GoodsInfo(int portType, int id)
  125. {
  126. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  127. .LeftJoin<Sys_SetData>((gi, sd) => gi.Type == sd.Id)
  128. .LeftJoin<Sys_Users>((gi, sd, u1) => gi.LastUpdateUserId == u1.Id)
  129. .LeftJoin<Sys_Users>((gi, sd, u1, u2) => gi.CreateUserId == u2.Id)
  130. .Where((gi, sd, u1, u2) => gi.IsDel == 0 && gi.Id == id)
  131. .Select((gi, sd, u1, u2) => new
  132. {
  133. gi.Id,
  134. gi.Name,
  135. ParentType = sd.STid,
  136. gi.Type,
  137. TypeName = sd.Name,
  138. gi.SQ_Total,
  139. gi.OQ_Total,
  140. gi.PriceTotal,
  141. gi.StockQuantity,
  142. gi.Unit,
  143. gi.Remark,
  144. LastUpdateUserName = u1.CnName,
  145. gi.LastUpdateTime,
  146. CreateUserName = u2.CnName,
  147. gi.CreateTime,
  148. })
  149. .FirstAsync();
  150. _jv.Code = StatusCodes.Status200OK;
  151. _jv.Data = data;
  152. _jv.Msg = $"操作成功";
  153. return _jv;
  154. }
  155. /// <summary>
  156. /// 物品 OP(Create Or Edit)
  157. /// </summary>
  158. /// <param name="dto"></param>
  159. /// <param name="currUserId"></param>
  160. /// <returns></returns>
  161. public async Task<JsonView> GoodsOp(GoodsOpDto dto, int currUserId)
  162. {
  163. var info = new Pm_GoodsInfo()
  164. {
  165. Id = dto.Id,
  166. Name = dto.Name,
  167. Type = dto.Type,
  168. SQ_Total = 0,
  169. OQ_Total = 0,
  170. PriceTotal = 0,
  171. StockQuantity = 0,
  172. Unit = dto.Unit,
  173. Remark = dto.Remark,
  174. LastUpdateUserId = currUserId,
  175. LastUpdateTime = DateTime.Now,
  176. CreateUserId = currUserId
  177. };
  178. if (dto.Id > 0) //Edit
  179. {
  180. var upd = await _sqlSugar.Updateable(info)
  181. .UpdateColumns(x => new
  182. {
  183. x.Name,
  184. x.Type,
  185. x.Unit,
  186. x.Remark,
  187. x.LastUpdateUserId,
  188. x.LastUpdateTime,
  189. })
  190. .ExecuteCommandAsync();
  191. if (upd > 0)
  192. {
  193. _jv.Msg = $"修改成功!";
  194. _jv.Code = StatusCodes.Status200OK;
  195. return _jv;
  196. }
  197. }
  198. else if (dto.Id < 1) //添加
  199. {
  200. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Name.Equals(info.Name));
  201. if (selectInfo != null)
  202. {
  203. _jv.Msg = $"“{info.Name}”该物品已存在,请勿重新添加!";
  204. return _jv;
  205. }
  206. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  207. if (add > 0)
  208. {
  209. _jv.Msg = $"添加成功!";
  210. _jv.Code = StatusCodes.Status200OK;
  211. return _jv;
  212. }
  213. }
  214. return _jv;
  215. }
  216. /// <summary>
  217. /// 物品 Del
  218. /// </summary>
  219. /// <param name="id"></param>
  220. /// <param name="currUserId"></param>
  221. /// <returns></returns>
  222. public async Task<JsonView> GoodsDel(int id, int currUserId)
  223. {
  224. _sqlSugar.BeginTran();
  225. var goods = await _sqlSugar.Updateable<Pm_GoodsInfo>()
  226. .SetColumns(x => new Pm_GoodsInfo()
  227. {
  228. IsDel = 1,
  229. DeleteUserId = currUserId,
  230. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  231. })
  232. .Where(x => x.Id == id)
  233. .ExecuteCommandAsync();
  234. if (goods < 1)
  235. {
  236. _sqlSugar.RollbackTran();
  237. _jv.Msg = $"操作失败";
  238. return _jv;
  239. }
  240. var goodsStorage = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  241. .SetColumns(x => new Pm_GoodsStorage()
  242. {
  243. IsDel = 1,
  244. DeleteUserId = currUserId,
  245. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  246. })
  247. .Where(x => x.Id == id)
  248. .ExecuteCommandAsync();
  249. var goodsReceive = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  250. .SetColumns(x => new Pm_GoodsReceive()
  251. {
  252. IsDel = 1,
  253. DeleteUserId = currUserId,
  254. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  255. })
  256. .Where(x => x.Id == id)
  257. .ExecuteCommandAsync();
  258. _sqlSugar.CommitTran();
  259. _jv.Code = StatusCodes.Status200OK;
  260. _jv.Msg = $"操作成功!";
  261. return _jv;
  262. }
  263. /// <summary>
  264. /// 物品入库列表
  265. /// </summary>
  266. /// <param name="dto"></param>
  267. /// <returns></returns>
  268. public async Task<JsonView> GoodsStorageList(GoodsStorageListDto dto)
  269. {
  270. RefAsync<int> total = 0;
  271. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  272. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  273. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  274. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  275. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  276. .WhereIF(dto.GoodsId > 0, (gs, gi, u, u1) => gs.GoodsId == dto.GoodsId)
  277. .WhereIF(!string.IsNullOrEmpty(dto.BatchNo), (gs, gi, u, u1) => gs.BatchNo.Contains(dto.BatchNo))
  278. .Select((gs, gi, u, u1) => new
  279. {
  280. gs.Id,
  281. gs.GoodsId,
  282. gs.BatchNo,
  283. GoodsName = gi.Name,
  284. gs.Quantity,
  285. gs.UnitPrice,
  286. gs.TotalPrice,
  287. gs.SupplierName,
  288. gs.SupplierTel,
  289. gs.SupplierAddress,
  290. gs.SupplierSource,
  291. StorageUserName = u1.CnName,
  292. gs.StorageTime,
  293. CreateUserName = u.CnName,
  294. gs.CreateTime,
  295. })
  296. .OrderByDescending(gs => gs.CreateTime)
  297. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  298. _jv.Code = StatusCodes.Status200OK;
  299. _jv.Data = data;
  300. _jv.Count = total;
  301. _jv.Msg = $"操作成功";
  302. return _jv;
  303. }
  304. /// <summary>
  305. /// 物品入库详情
  306. /// </summary>
  307. /// <param name="id"></param>
  308. /// <returns></returns>
  309. public async Task<JsonView> GoodsStorageInfo(int portType, int id)
  310. {
  311. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  312. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  313. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  314. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  315. .Where((gs, gi, u) => gs.IsDel == 0)
  316. .WhereIF(id > 0, (gs, gi, u) => gs.Id == id)
  317. .Select((gs, gi, u, u1) => new
  318. {
  319. gs.Id,
  320. gs.GoodsId,
  321. GoodsName = gi.Name,
  322. gs.Quantity,
  323. gs.UnitPrice,
  324. gs.TotalPrice,
  325. gs.SupplierName,
  326. gs.SupplierTel,
  327. gs.SupplierAddress,
  328. gs.SupplierSource,
  329. gs.ReceiveQuantity,
  330. gs.StorageUserId,
  331. StorageUser = u1.CnName,
  332. gs.StorageTime,
  333. CreateUserName = u.CnName,
  334. gs.CreateUserId,
  335. gs.CreateTime,
  336. gs.Remark
  337. })
  338. .FirstAsync();
  339. _jv.Msg = $"操作成功!";
  340. _jv.Code = StatusCodes.Status200OK;
  341. _jv.Data = data;
  342. return _jv;
  343. }
  344. /// <summary>
  345. /// 物品入库 操作(Create Or Edit)
  346. /// </summary>
  347. /// <param name="dto"></param>
  348. /// <param name="currUserId"></param>
  349. /// <returns></returns>
  350. public async Task<JsonView> GoodsStorageOp(GoodsStorageOpDto dto, int currUserId)
  351. {
  352. var info = _mapper.Map<Pm_GoodsStorage>(dto);
  353. info.CreateUserId = currUserId;
  354. info.BatchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff");
  355. decimal editAgoQuantity = 0.00M,
  356. editAgoTotalPrice = 0.00M;
  357. _sqlSugar.BeginTran();
  358. if (info.Id > 0) //修改
  359. {
  360. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  361. .Where(x => x.Id == dto.Id)
  362. .FirstAsync();
  363. editAgoQuantity = selectInfo.Quantity;
  364. editAgoTotalPrice = selectInfo.TotalPrice;
  365. var storageEdit = await _sqlSugar.Updateable(info)
  366. .UpdateColumns(x => new
  367. {
  368. x.Quantity,
  369. x.UnitPrice,
  370. x.TotalPrice,
  371. x.SupplierName,
  372. x.SupplierTel,
  373. x.SupplierAddress,
  374. x.SupplierSource,
  375. x.StorageUserId,
  376. x.StorageTime
  377. })
  378. .Where(x => x.Id == dto.Id)
  379. .ExecuteCommandAsync();
  380. if (storageEdit < 1)
  381. {
  382. _sqlSugar.RollbackTran();
  383. _jv.Msg = $"修改失败!";
  384. return _jv;
  385. }
  386. }
  387. else if (info.Id < 1) //添加
  388. {
  389. var storageAdd = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  390. if (storageAdd < 1)
  391. {
  392. _sqlSugar.RollbackTran();
  393. _jv.Msg = $"添加失败!";
  394. return _jv;
  395. }
  396. }
  397. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == info.GoodsId);
  398. goodsInfo.SQ_Total = goodsInfo.SQ_Total - editAgoQuantity + info.Quantity;
  399. goodsInfo.StockQuantity = goodsInfo.StockQuantity - editAgoQuantity + info.Quantity;
  400. goodsInfo.PriceTotal = goodsInfo.PriceTotal - editAgoTotalPrice + info.TotalPrice;
  401. goodsInfo.LastUpdateUserId = currUserId;
  402. goodsInfo.LastUpdateTime = DateTime.Now;
  403. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  404. .UpdateColumns(x => new
  405. {
  406. x.SQ_Total,
  407. x.StockQuantity,
  408. x.PriceTotal,
  409. x.LastUpdateUserId,
  410. x.LastUpdateTime,
  411. })
  412. .Where(x => x.Id == info.GoodsId)
  413. .ExecuteCommandAsync();
  414. if (goodsEdit > 0)
  415. {
  416. _sqlSugar.CommitTran();
  417. _jv.Msg = $"操作成功!";
  418. _jv.Code = StatusCodes.Status200OK;
  419. return _jv;
  420. }
  421. _sqlSugar.RollbackTran();
  422. _jv.Msg = $"操作失败!";
  423. return _jv;
  424. }
  425. /// <summary>
  426. /// 物品入库 Del
  427. /// </summary>
  428. /// <param name="id"></param>
  429. /// <returns></returns>
  430. public async Task<JsonView> GoodsStorageDel(int id, int userId)
  431. {
  432. var storageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  433. .Where(x => x.Id == id)
  434. .FirstAsync();
  435. if (storageInfo == null) return _jv;
  436. decimal delAgoQuantity = storageInfo.Quantity,
  437. delAgoTotalPrice = storageInfo.TotalPrice;
  438. var goodsId = storageInfo.GoodsId;
  439. _sqlSugar.BeginTran();
  440. var storageDel = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  441. .SetColumns(x => new Pm_GoodsStorage
  442. {
  443. DeleteUserId = userId,
  444. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  445. IsDel = 1
  446. })
  447. .Where(x => x.Id == id)
  448. .ExecuteCommandAsync();
  449. if (storageDel < 1)
  450. {
  451. _sqlSugar.RollbackTran();
  452. _jv.Msg = $"操作失败!";
  453. return _jv;
  454. }
  455. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == goodsId);
  456. goodsInfo.SQ_Total = goodsInfo.SQ_Total - delAgoQuantity;
  457. goodsInfo.StockQuantity = goodsInfo.StockQuantity - delAgoQuantity;
  458. goodsInfo.PriceTotal = goodsInfo.PriceTotal - delAgoTotalPrice;
  459. goodsInfo.LastUpdateUserId = userId;
  460. goodsInfo.LastUpdateTime = DateTime.Now;
  461. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  462. .UpdateColumns(x => new
  463. {
  464. x.SQ_Total,
  465. x.StockQuantity,
  466. x.PriceTotal,
  467. x.LastUpdateUserId,
  468. x.LastUpdateTime,
  469. })
  470. .Where(x => x.Id == goodsId)
  471. .ExecuteCommandAsync();
  472. if (goodsEdit > 0)
  473. {
  474. _sqlSugar.CommitTran();
  475. _jv.Msg = $"操作成功!";
  476. _jv.Code = StatusCodes.Status200OK;
  477. return _jv;
  478. }
  479. _sqlSugar.RollbackTran();
  480. _jv.Msg = $"操作失败!";
  481. return _jv;
  482. }
  483. /// <summary>
  484. /// 物品领用列表
  485. /// </summary>
  486. /// <param name="dto"></param>
  487. /// <returns></returns>
  488. public async Task<JsonView> GoodsReceiveList(GoodsReceiveListDTO dto)
  489. {
  490. //参数处理
  491. int[] typeLabel = Array.Empty<int>(),
  492. userLabel = Array.Empty<int>(),
  493. auditLabel = Array.Empty<int>(),
  494. groupLabel = Array.Empty<int>();
  495. if (!string.IsNullOrEmpty(dto.TypeLabel))
  496. {
  497. typeLabel = dto.TypeLabel
  498. .Split(',')
  499. .Select(x =>
  500. {
  501. if (int.TryParse(x, out var id)) return id;
  502. return id;
  503. })
  504. .ToArray();
  505. }
  506. if (!string.IsNullOrEmpty(dto.UserLabel))
  507. {
  508. userLabel = dto.UserLabel
  509. .Split(',')
  510. .Select(x =>
  511. {
  512. if (int.TryParse(x, out var id)) return id;
  513. return id;
  514. })
  515. .ToArray();
  516. }
  517. if (!string.IsNullOrEmpty(dto.AuditLabel))
  518. {
  519. auditLabel = dto.AuditLabel
  520. .Split(',')
  521. .Select(x =>
  522. {
  523. if (int.TryParse(x, out var id)) return id;
  524. return id;
  525. })
  526. .ToArray();
  527. }
  528. if (!string.IsNullOrEmpty(dto.GroupLabel))
  529. {
  530. groupLabel = dto.GroupLabel
  531. .Split(',')
  532. .Select(x =>
  533. {
  534. if (int.TryParse(x, out var id)) return id;
  535. return id;
  536. })
  537. .ToArray();
  538. }
  539. //物品ID和物品名称只能传一个
  540. if (dto.GoodsId > 0) dto.GoodsName = string.Empty;
  541. if (!string.IsNullOrEmpty(dto.GoodsName)) dto.GoodsId = 0;
  542. var beginBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.BeginDt) ? $"{dto.BeginDt} 00:00:00" : string.Empty, out var begin);
  543. var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
  544. RefAsync<int> total = 0;
  545. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  546. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  547. .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
  548. .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
  549. .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  550. .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  551. .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
  552. .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
  553. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
  554. .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
  555. .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
  556. .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
  557. .WhereIF(groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
  558. .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
  559. .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  560. {
  561. Id = gr.Id,
  562. GroupId = gr.GroupId,
  563. GroupName = di.TeamName,
  564. GoodsId = gr.GoodsId,
  565. GoodsName = gi.Name,
  566. GoodsType = sd.Name,
  567. Quantity = gr.Quantity,
  568. Reason = gr.Reason,
  569. Remark = gr.Remark,
  570. AuditStatus = gr.AuditStatus,
  571. //AuditStatusText = gr.AuditStatus.GetEnumDescription(),
  572. AuditUserId = gr.AuditUserId,
  573. AuditUserName = u1.CnName,
  574. AuditTime = gr.AuditTime,
  575. CreateUserName = u2.CnName,
  576. CreateTime = gr.CreateTime
  577. })
  578. .OrderByDescending(gr => gr.CreateTime)
  579. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  580. if (dto.PortType == 2 || dto.PortType == 3)
  581. {
  582. _jv.Data = data;
  583. }
  584. else if (dto.PortType == 1)
  585. {
  586. _jv.Data = _mapper.Map<List<GoodsReceiveListView>>(data);
  587. }
  588. _jv.Code = StatusCodes.Status200OK;
  589. _jv.Count = total;
  590. _jv.Msg = $"操作成功";
  591. return _jv;
  592. }
  593. /// <summary>
  594. /// 物品领用详情
  595. /// </summary>
  596. /// <param name="portType"></param>
  597. /// <param name="id"></param>
  598. /// <returns></returns>
  599. public async Task<JsonView> GoodsReceiveInfo(int portType, int id)
  600. {
  601. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  602. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  603. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  604. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  605. .LeftJoin<Grp_DelegationInfo>((gr, gi, u1, u2,di) => gr.GroupId == di.Id)
  606. .Where((gr, gi, u1, u2, di) => gr.IsDel == 0)
  607. .WhereIF(id > 0, (gr, gi, u1, u2, di) => gr.Id == id)
  608. .Select((gr, gi, u1, u2, di) => new GoodsReceiveInfoMobileView
  609. {
  610. Id = gr.Id,
  611. GroupId = gr.GroupId,
  612. GroupName = di.TeamName,
  613. GoodsId = gr.GoodsId,
  614. GoodsName = gi.Name,
  615. Quantity = gr.Quantity,
  616. Reason = gr.Reason,
  617. Remark = gr.Remark,
  618. GoodsStorageInfo = gr.GoodsStorageInfo,
  619. AuditStatus = gr.AuditStatus,
  620. AuditUserId = gr.AuditUserId,
  621. AuditUserName = u1.CnName,
  622. AuditTime = gr.AuditTime,
  623. CreateUserName = u2.CnName,
  624. CreateTime = gr.CreateTime
  625. })
  626. .FirstAsync();
  627. if (!string.IsNullOrEmpty(data.GoodsStorageInfo))
  628. {
  629. var subData = new List<dynamic>();
  630. try
  631. {
  632. var subData1 = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(data.GoodsStorageInfo);
  633. if (subData1.Count > 0)
  634. {
  635. string goodsStorageInfoStr = string.Empty;
  636. var storageIds = subData1.Select(x => x.StorageId).ToList();
  637. var storages = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && storageIds.Contains(x.Id)).ToListAsync();
  638. foreach (var item in subData1)
  639. {
  640. var storageInfo = storages.Find(x => x.Id == item.StorageId);
  641. if (storageInfo != null)
  642. {
  643. subData.Add(new
  644. {
  645. StorageId = item.StorageId,
  646. BatchNo = storageInfo.BatchNo,
  647. RecsiveQuantity = item.Quantity
  648. });
  649. goodsStorageInfoStr += $"物品名称:{data.GoodsName} 批次号:{storageInfo.BatchNo} 领用数量:{item.Quantity} \r\n";
  650. }
  651. }
  652. data.QuantityInfos = subData;
  653. data.GoodsStorageInfoStr = goodsStorageInfoStr;
  654. }
  655. }
  656. catch (Exception e)
  657. {
  658. Console.WriteLine(e);
  659. }
  660. }
  661. _jv.Code = StatusCodes.Status200OK;
  662. _jv.Msg = $"操作成功";
  663. if (portType == 2 || portType == 3) //移动端
  664. {
  665. _jv.Data = data;
  666. }
  667. else if (portType == 1) //pc端
  668. {
  669. _jv.Data = _mapper.Map<GoodsReceiveInfoView>(data);
  670. }
  671. return _jv;
  672. }
  673. /// <summary>
  674. /// 物品领用 OP(Add Or Edit)
  675. /// </summary>
  676. /// <param name="dto"></param>
  677. /// <param name="currUserId"></param>
  678. /// <returns></returns>
  679. public async Task<JsonView> GoodsReceiveOp(GoodsReceiveOpDto dto, int currUserId)
  680. {
  681. var info = _mapper.Map<Pm_GoodsReceive>(dto);
  682. info.CreateUserId = currUserId;
  683. _sqlSugar.BeginTran();
  684. //物品现有库存
  685. var stockQuantity = _sqlSugar.Queryable<Pm_GoodsInfo>()
  686. .First(x => x.Id == info.GoodsId)
  687. ?.StockQuantity;
  688. //待审核 该物品数量
  689. var waitAuditQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  690. .Where(x => x.IsDel == 0 &&
  691. x.GoodsId == dto.GoodsId &&
  692. x.AuditStatus == GoodsAuditEnum.Pending
  693. )
  694. .SumAsync(x => x.Quantity);
  695. if (info.Id > 0) //修改
  696. {
  697. //审核验证
  698. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == info.Id);
  699. if (selectInfo.AuditStatus == GoodsAuditEnum.Approved)
  700. {
  701. _sqlSugar.RollbackTran();
  702. _jv.Msg = $"该条数据已通过审核,不可更改!";
  703. return _jv;
  704. }
  705. //物品数量验证
  706. var editAfterQuantity = waitAuditQuantity - selectInfo.Quantity + info.Quantity;
  707. if (editAfterQuantity > stockQuantity)
  708. {
  709. _sqlSugar.RollbackTran();
  710. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  711. return _jv;
  712. }
  713. var edit = await _sqlSugar.Updateable(info)
  714. .UpdateColumns(x => new
  715. {
  716. x.GroupId,
  717. x.Quantity,
  718. x.Reason,
  719. x.Remark,
  720. })
  721. .Where(x => x.Id == info.Id)
  722. .ExecuteCommandAsync();
  723. if (edit > 0)
  724. {
  725. _sqlSugar.CommitTran();
  726. _jv.Msg = $"操作成功!";
  727. _jv.Code = StatusCodes.Status200OK;
  728. return _jv;
  729. }
  730. }
  731. else if (info.Id < 1) //添加
  732. {
  733. //物品数量验证
  734. decimal addAgoQuantity = waitAuditQuantity + info.Quantity;
  735. if (addAgoQuantity > stockQuantity)
  736. {
  737. _sqlSugar.RollbackTran();
  738. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  739. return _jv;
  740. }
  741. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  742. if (add > 0)
  743. {
  744. _sqlSugar.CommitTran();
  745. _jv.Msg = $"操作成功!";
  746. _jv.Code = StatusCodes.Status200OK;
  747. return _jv;
  748. }
  749. }
  750. _sqlSugar.RollbackTran();
  751. return _jv;
  752. }
  753. /// <summary>
  754. /// 物品领用 Audit
  755. /// </summary>
  756. /// <param name="idArray"></param>
  757. /// <param name="userId"></param>
  758. /// <param name="auditEnum"></param>
  759. /// <returns></returns>
  760. public async Task<JsonView> GoodsReceiveAudit(int[] idArray, int userId, GoodsAuditEnum auditEnum)
  761. {
  762. if (idArray.Length < 1) return _jv;
  763. //TODO: 审核权限验证
  764. _sqlSugar.BeginTran();
  765. var receiveInfos = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  766. .Where(x => x.IsDel == 0 && idArray.Contains(x.Id))
  767. .ToListAsync();
  768. var status = true;
  769. foreach (var id in idArray)
  770. {
  771. //1.更改审核状态
  772. var currInfo = receiveInfos.Find(x => x.Id == id);
  773. if (currInfo == null) continue;
  774. var edit = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  775. .SetColumns(x => new Pm_GoodsReceive()
  776. {
  777. AuditStatus = auditEnum,
  778. AuditUserId = userId,
  779. AuditTime = DateTime.Now,
  780. })
  781. .Where(x => x.Id == id)
  782. .ExecuteCommandAsync();
  783. if (edit < 1) status = false;
  784. //if (auditEnum != GoodsAuditEnum.Approved) continue;
  785. //2.更改库存
  786. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == currInfo.GoodsId).FirstAsync();
  787. if (auditEnum == GoodsAuditEnum.Pending)
  788. {
  789. goodsInfo.StockQuantity += currInfo.Quantity;
  790. goodsInfo.OQ_Total -= currInfo.Quantity;
  791. }
  792. else if (auditEnum == GoodsAuditEnum.Approved)
  793. {
  794. goodsInfo.StockQuantity -= currInfo.Quantity;
  795. goodsInfo.OQ_Total += currInfo.Quantity;
  796. }else if(auditEnum == GoodsAuditEnum.UnApproved) continue;
  797. goodsInfo.LastUpdateTime = DateTime.Now;
  798. goodsInfo.LastUpdateUserId = userId;
  799. var editGoods = await _sqlSugar.Updateable<Pm_GoodsInfo>(goodsInfo)
  800. .UpdateColumns(x => new
  801. {
  802. x.StockQuantity,
  803. x.OQ_Total,
  804. x.LastUpdateUserId,
  805. x.LastUpdateTime,
  806. })
  807. .Where(x => x.Id == currInfo.GoodsId)
  808. .ExecuteCommandAsync();
  809. if (editGoods < 1) status = false;
  810. //3.入库批次关联领用人 更改批次库存
  811. var goodsStorages = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  812. .Where(x => x.IsDel == 0 &&
  813. x.GoodsId == currInfo.GoodsId &&
  814. (x.Quantity - x.ReceiveQuantity) > 0
  815. )
  816. .OrderBy(x => x.CreateTime)
  817. .ToListAsync();
  818. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  819. var batchStorageInfos = new List<Pm_GoodsStorage>();
  820. var receiveQuantity = 0.00M; //领用总数量
  821. if (auditEnum == GoodsAuditEnum.Approved)
  822. {
  823. foreach (var storage in goodsStorages)
  824. {
  825. if (currInfo.Quantity == receiveQuantity) break;
  826. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  827. if (thisBatchSurplusQuantity <= 0.00M) continue;
  828. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  829. const decimal unit = 0.50M;
  830. while (receiveQuantity < currInfo.Quantity)
  831. {
  832. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  833. thisBatchReceiveQuantity += unit;
  834. receiveQuantity += unit;
  835. }
  836. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  837. {
  838. StorageId = storage.Id,
  839. Quantity = thisBatchReceiveQuantity
  840. });
  841. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  842. var storageUpd = storage;
  843. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  844. batchStorageInfos.Add(storageUpd);
  845. }
  846. //3.1 更改批次库存
  847. if (goodsReceiveInfos.Count > 0)
  848. {
  849. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  850. .UpdateColumns(x => x.ReceiveQuantity)
  851. .WhereColumns(x => x.Id)
  852. .ExecuteCommandAsync();
  853. if (edit1 < 1) status = false;
  854. }
  855. //3.2 添加入库批次关联领用人
  856. if (goodsReceiveInfos.Count > 0)
  857. {
  858. var edit1 = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  859. .SetColumns(x => new Pm_GoodsReceive()
  860. {
  861. GoodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos)
  862. })
  863. .Where(x => x.Id == id)
  864. .ExecuteCommandAsync();
  865. if (edit1 < 1) status = false;
  866. }
  867. }
  868. else if (auditEnum == GoodsAuditEnum.Pending)
  869. {
  870. var goodsStorageInfo = currInfo.GoodsStorageInfo;
  871. if (!string.IsNullOrEmpty(goodsStorageInfo))
  872. {
  873. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  874. if (goodsStorageInfos.Count > 0)
  875. {
  876. foreach (var item in goodsStorageInfos)
  877. {
  878. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  879. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  880. .FirstAsync();
  881. if (newStorageInfo != null)
  882. {
  883. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  884. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  885. .ExecuteCommandAsync();
  886. if (newEdit < 1) status = false;
  887. }
  888. }
  889. }
  890. }
  891. }
  892. }
  893. if (status)
  894. {
  895. _sqlSugar.CommitTran();
  896. _jv.Msg = $"操作成功!";
  897. _jv.Code = StatusCodes.Status200OK;
  898. return _jv;
  899. }
  900. _sqlSugar.RollbackTran();
  901. return _jv;
  902. }
  903. /// <summary>
  904. /// 物品领用 Del
  905. /// </summary>
  906. /// <param name="id"></param>
  907. /// <param name="currUserId"></param>
  908. /// <returns></returns>
  909. public async Task<JsonView> GoodsReceiveDel(int id, int currUserId)
  910. {
  911. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  912. .Where(x => x.IsDel == 0 && x.Id == id)
  913. .FirstAsync();
  914. if (receiveInfo.AuditStatus == GoodsAuditEnum.Approved)
  915. {
  916. _jv.Msg = $"该条数据已通过审核,不可删除!";
  917. return _jv;
  918. }
  919. var edit = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  920. .SetColumns(x => new Pm_GoodsReceive()
  921. {
  922. IsDel = 1,
  923. DeleteUserId = currUserId,
  924. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  925. })
  926. .Where(x => x.Id == id)
  927. .ExecuteCommandAsync();
  928. if (edit > 0)
  929. {
  930. _jv.Msg = $"操作成功!";
  931. _jv.Code = StatusCodes.Status200OK;
  932. return _jv;
  933. }
  934. return _jv;
  935. }
  936. }
  937. }