GoodsRepository.cs 44 KB

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