GoodsRepository.cs 45 KB

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