GoodsRepository.cs 47 KB

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