GoodsRepository.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. using OASystem.Domain.Entities.PersonnelModule;
  2. using OASystem.Domain.ViewModels.PersonnelModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using AutoMapper;
  9. using OASystem.Domain.Dtos.PersonnelModule;
  10. using SqlSugar;
  11. using NPOI.SS.Formula.Functions;
  12. using OASystem.Domain.Entities.Groups;
  13. namespace OASystem.Infrastructure.Repositories.PersonnelModule
  14. {
  15. /// <summary>
  16. /// 物品进销存
  17. /// 仓储
  18. /// </summary>
  19. public class GoodsRepository : BaseRepository<Pm_GoodsInfo, GoodsInfoView>
  20. {
  21. private readonly IMapper _mapper;
  22. private JsonView _jv;
  23. public GoodsRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  24. {
  25. _mapper = mapper;
  26. _jv = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  27. }
  28. /// <summary>
  29. /// 基础数据
  30. /// </summary>
  31. /// <returns></returns>
  32. public async Task<JsonView> InitDataSource()
  33. {
  34. var typeData = await _sqlSugar.Queryable<GoodsTypeView>()
  35. .Includes(x => x.SubTypeItems.Where(z => z.IsDel == 0)
  36. //.Select(z => new {
  37. // z.Id,
  38. // z.STid,
  39. // z.Name
  40. //})
  41. .ToList())
  42. .Where(x => x.IsDel == 0 &&
  43. x.Remark.Equals("GoodsType"))
  44. //.Select(x => new {
  45. // x.Id,
  46. // x.Name,
  47. // x.SubTypeItems
  48. //})
  49. .ToListAsync();
  50. var groupData = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  51. .Where(x => x.IsDel == 0)
  52. .Select(x => new
  53. {
  54. id = x.Id,
  55. groupName = x.TeamName
  56. })
  57. .OrderByDescending(x => x.id)
  58. .ToListAsync();
  59. _jv.Code = StatusCodes.Status200OK;
  60. _jv.Data = new { goodsTypeData = typeData, groupNameData = groupData };
  61. _jv.Msg = $"操作成功";
  62. return _jv;
  63. }
  64. /// <summary>
  65. /// 物品列表
  66. /// </summary>
  67. /// <param name="_dto"></param>
  68. /// <returns></returns>
  69. public async Task<JsonView> GoodsList(GoodsListDTO _dto)
  70. {
  71. var ids = new List<int>();
  72. if (!string.IsNullOrEmpty(_dto.TypeIds))
  73. {
  74. var strArray = _dto.TypeIds.Split(',');
  75. foreach (var str in strArray)
  76. {
  77. if (int.TryParse(str, out int id))
  78. {
  79. ids.Add(id);
  80. }
  81. }
  82. }
  83. RefAsync<int> total = 0;
  84. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  85. .LeftJoin<Sys_SetData>((gi, sd) => gi.Type == sd.Id)
  86. .LeftJoin<Sys_Users>((gi, sd, u) => gi.LastUpdateUserId == u.Id)
  87. .Where((gi, sd, u) => gi.IsDel == 0)
  88. .WhereIF(ids.Count > 0, (gi, sd, u) => ids.Contains(gi.Type))
  89. .WhereIF(!string.IsNullOrEmpty(_dto.GoodsName), (gi, sd, u) => gi.Name.Contains(_dto.GoodsName))
  90. .Select((gi, sd, u) => new
  91. {
  92. gi.Id,
  93. gi.Name,
  94. gi.Type,
  95. TypeName = sd.Name,
  96. gi.StockQuantity,
  97. LastUpdateUserName = u.CnName,
  98. gi.LastUpdateTime,
  99. gi.Remark,
  100. })
  101. .OrderByDescending(gi => gi.LastUpdateTime)
  102. .ToPageListAsync(_dto.PageIndex, _dto.PageSize, total);
  103. _jv.Code = StatusCodes.Status200OK;
  104. _jv.Data = data;
  105. _jv.Count = total;
  106. _jv.Msg = $"操作成功";
  107. return _jv;
  108. }
  109. /// <summary>
  110. /// 物品Info
  111. /// </summary>
  112. /// <param name="_dto"></param>
  113. /// <returns></returns>
  114. public async Task<JsonView> GoodsInfo(int portType, int id)
  115. {
  116. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  117. .LeftJoin<Sys_SetData>((gi, sd) => gi.Type == sd.Id)
  118. .LeftJoin<Sys_Users>((gi, sd, u1) => gi.LastUpdateUserId == u1.Id)
  119. .LeftJoin<Sys_Users>((gi, sd, u1, u2) => gi.CreateUserId == u2.Id)
  120. .Where((gi, sd, u1, u2) => gi.IsDel == 0 && gi.Id == id)
  121. .Select((gi, sd, u1, u2) => new
  122. {
  123. gi.Id,
  124. gi.Name,
  125. ParentType = sd.STid,
  126. gi.Type,
  127. TypeName = sd.Name,
  128. gi.SQ_Total,
  129. gi.OQ_Total,
  130. gi.PriceTotal,
  131. gi.StockQuantity,
  132. gi.Remark,
  133. LastUpdateUserName = u1.CnName,
  134. gi.LastUpdateTime,
  135. CreateUserName = u2.CnName,
  136. gi.CreateTime,
  137. })
  138. .FirstAsync();
  139. _jv.Code = StatusCodes.Status200OK;
  140. _jv.Data = data;
  141. _jv.Msg = $"操作成功";
  142. return _jv;
  143. }
  144. /// <summary>
  145. /// 物品 OP(Create Or Edit)
  146. /// </summary>
  147. /// <param name="_dto"></param>
  148. /// <returns></returns>
  149. public async Task<JsonView> GoodsOP(GoodsOPDTO _dto)
  150. {
  151. var info = new Pm_GoodsInfo()
  152. {
  153. Id = _dto.Id,
  154. Name = _dto.Name,
  155. Type = _dto.Type,
  156. SQ_Total = 0,
  157. OQ_Total = 0,
  158. PriceTotal = 0,
  159. StockQuantity = 0,
  160. Remark = _dto.Remark,
  161. LastUpdateUserId = _dto.CurrUserId,
  162. LastUpdateTime = DateTime.Now,
  163. CreateUserId = _dto.CurrUserId
  164. };
  165. if (_dto.Id > 0) //Edit
  166. {
  167. var upd = await _sqlSugar.Updateable(info)
  168. .UpdateColumns(x => new
  169. {
  170. x.Name,
  171. x.Type,
  172. x.Remark,
  173. x.LastUpdateUserId,
  174. x.LastUpdateTime,
  175. })
  176. .ExecuteCommandAsync();
  177. if (upd > 0)
  178. {
  179. _jv.Msg = $"修改成功!";
  180. _jv.Code = StatusCodes.Status200OK;
  181. return _jv;
  182. }
  183. }
  184. else if (_dto.Id < 1) //添加
  185. {
  186. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Name.Equals(info.Name));
  187. if (selectInfo != null)
  188. {
  189. _jv.Msg = $"“{info.Name}”该物品已存在,请勿重新添加!";
  190. return _jv;
  191. }
  192. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  193. if (add > 0)
  194. {
  195. _jv.Msg = $"添加成功!";
  196. _jv.Code = StatusCodes.Status200OK;
  197. return _jv;
  198. }
  199. }
  200. return _jv;
  201. }
  202. /// <summary>
  203. /// 物品 Del
  204. /// </summary>
  205. /// <param name="id"></param>
  206. /// <param name="currUserId"></param>
  207. /// <returns></returns>
  208. public async Task<JsonView> GoodsDel(int id, int currUserId)
  209. {
  210. _sqlSugar.BeginTran();
  211. var goods = await _sqlSugar.Updateable<Pm_GoodsInfo>()
  212. .SetColumns(x => new Pm_GoodsInfo()
  213. {
  214. IsDel = 1,
  215. DeleteUserId = currUserId,
  216. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  217. })
  218. .Where(x => x.Id == id)
  219. .ExecuteCommandAsync();
  220. if (goods < 1)
  221. {
  222. _sqlSugar.RollbackTran();
  223. _jv.Msg = $"操作失败";
  224. return _jv;
  225. }
  226. var goodsStorage = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  227. .SetColumns(x => new Pm_GoodsStorage()
  228. {
  229. IsDel = 1,
  230. DeleteUserId = currUserId,
  231. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  232. })
  233. .Where(x => x.Id == id)
  234. .ExecuteCommandAsync();
  235. var goodsReceive = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  236. .SetColumns(x => new Pm_GoodsReceive()
  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. _sqlSugar.CommitTran();
  245. _jv.Code = StatusCodes.Status200OK;
  246. _jv.Msg = $"操作成功!";
  247. return _jv;
  248. }
  249. /// <summary>
  250. /// 物品入库列表
  251. /// </summary>
  252. /// <param name="_dto"></param>
  253. /// <returns></returns>
  254. public async Task<JsonView> GoodsStorageList(GoodsStorageListDTO _dto)
  255. {
  256. RefAsync<int> total = 0;
  257. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  258. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  259. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  260. .Where((gs, gi, u) => gs.IsDel == 0)
  261. .WhereIF(_dto.GoodsId > 0, (gs, gi, u) => gs.GoodsId == _dto.GoodsId)
  262. .Select((gs, gi, u) => new
  263. {
  264. gs.Id,
  265. gs.GoodsId,
  266. GoodsName = gi.Name,
  267. gs.Quantity,
  268. gs.UnitPrice,
  269. gs.TotalPrice,
  270. gs.SupplierName,
  271. gs.SupplierTel,
  272. gs.SupplierAddress,
  273. gs.SupplierSource,
  274. CreateUserName = u.CnName,
  275. gs.CreateTime,
  276. })
  277. .OrderByDescending(gs => gs.CreateTime)
  278. .ToPageListAsync(_dto.PageIndex, _dto.PageSize, total);
  279. _jv.Code = StatusCodes.Status200OK;
  280. _jv.Data = data;
  281. _jv.Count = total;
  282. _jv.Msg = $"操作成功";
  283. return _jv;
  284. }
  285. /// <summary>
  286. /// 物品入库详情
  287. /// </summary>
  288. /// <param name="id"></param>
  289. /// <returns></returns>
  290. public async Task<JsonView> GoodsStorageInfo(int portType, int id)
  291. {
  292. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  293. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  294. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  295. .Where((gs, gi, u) => gs.IsDel == 0)
  296. .WhereIF(id > 0, (gs, gi, u) => gs.GoodsId == id)
  297. .Select((gs, gi, u) => new
  298. {
  299. gs.Id,
  300. gs.GoodsId,
  301. GoodsName = gi.Name,
  302. gs.Quantity,
  303. gs.UnitPrice,
  304. gs.TotalPrice,
  305. gs.SupplierName,
  306. gs.SupplierTel,
  307. gs.SupplierAddress,
  308. gs.SupplierSource,
  309. CreateUserName = u.CnName,
  310. gs.CreateTime,
  311. gs.Remark
  312. })
  313. .FirstAsync();
  314. _jv.Msg = $"操作成功!";
  315. _jv.Code = StatusCodes.Status200OK;
  316. _jv.Data = data;
  317. return _jv;
  318. }
  319. /// <summary>
  320. /// 物品入库 操作(Create Or Edit)
  321. /// </summary>
  322. /// <param name="id"></param>
  323. /// <returns></returns>
  324. public async Task<JsonView> GoodsStorageOP(GoodsStorageOPDTO _dto)
  325. {
  326. var info = _mapper.Map<Pm_GoodsStorage>(_dto);
  327. info.CreateUserId = _dto.CurrUserId;
  328. decimal editAgoQauntity = 0.00M,
  329. editAgoTotalPrice = 0.00M;
  330. _sqlSugar.BeginTran();
  331. if (info.Id > 0) //修改
  332. {
  333. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  334. .Where(x => x.Id == _dto.Id)
  335. .FirstAsync();
  336. editAgoQauntity = selectInfo.Quantity;
  337. editAgoTotalPrice = selectInfo.TotalPrice;
  338. var stoageEdit = await _sqlSugar.Updateable(info)
  339. .UpdateColumns(x => new
  340. {
  341. x.Quantity,
  342. x.UnitPrice,
  343. x.TotalPrice,
  344. x.SupplierName,
  345. x.SupplierTel,
  346. x.SupplierAddress,
  347. x.SupplierSource,
  348. })
  349. .Where(x => x.Id == _dto.Id)
  350. .ExecuteCommandAsync();
  351. if (stoageEdit < 1)
  352. {
  353. _sqlSugar.RollbackTran();
  354. _jv.Msg = $"修改失败!";
  355. return _jv;
  356. }
  357. }
  358. else if (info.Id < 1) //添加
  359. {
  360. var stoageAdd = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  361. if (stoageAdd < 1)
  362. {
  363. _sqlSugar.RollbackTran();
  364. _jv.Msg = $"添加失败!";
  365. return _jv;
  366. }
  367. }
  368. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == info.GoodsId);
  369. goodsInfo.SQ_Total = goodsInfo.SQ_Total - editAgoQauntity + info.Quantity;
  370. goodsInfo.StockQuantity = goodsInfo.StockQuantity - editAgoQauntity + info.Quantity;
  371. goodsInfo.PriceTotal = goodsInfo.PriceTotal - editAgoTotalPrice + info.TotalPrice;
  372. goodsInfo.LastUpdateUserId = _dto.CurrUserId;
  373. goodsInfo.LastUpdateTime = DateTime.Now;
  374. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  375. .UpdateColumns(x => new
  376. {
  377. x.SQ_Total,
  378. x.StockQuantity,
  379. x.PriceTotal,
  380. x.LastUpdateUserId,
  381. x.LastUpdateTime,
  382. })
  383. .Where(x => x.Id == info.GoodsId)
  384. .ExecuteCommandAsync();
  385. if (goodsEdit > 0)
  386. {
  387. _sqlSugar.CommitTran();
  388. _jv.Msg = $"操作成功!";
  389. _jv.Code = StatusCodes.Status200OK;
  390. return _jv;
  391. }
  392. _sqlSugar.RollbackTran();
  393. _jv.Msg = $"操作失败!";
  394. return _jv;
  395. }
  396. /// <summary>
  397. /// 物品入库 Del
  398. /// </summary>
  399. /// <param name="id"></param>
  400. /// <returns></returns>
  401. public async Task<JsonView> GoodsStorageDel(int id, int userId)
  402. {
  403. var storageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  404. .Where(x => x.Id == id)
  405. .FirstAsync();
  406. if (storageInfo == null) return _jv;
  407. decimal delAgoQauntity = storageInfo.Quantity,
  408. delAgoTotalPrice = storageInfo.TotalPrice;
  409. int goodsId = storageInfo.GoodsId;
  410. _sqlSugar.BeginTran();
  411. var storageDel = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  412. .SetColumns(x => new Pm_GoodsStorage
  413. {
  414. DeleteUserId = userId,
  415. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  416. IsDel = 1
  417. })
  418. .Where(x => x.Id == id)
  419. .ExecuteCommandAsync();
  420. if (storageDel < 1)
  421. {
  422. _sqlSugar.RollbackTran();
  423. _jv.Msg = $"操作失败!";
  424. return _jv;
  425. }
  426. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == goodsId);
  427. goodsInfo.SQ_Total = goodsInfo.SQ_Total - delAgoQauntity;
  428. goodsInfo.StockQuantity = goodsInfo.StockQuantity - delAgoQauntity;
  429. goodsInfo.PriceTotal = goodsInfo.PriceTotal - delAgoTotalPrice;
  430. goodsInfo.LastUpdateUserId = userId;
  431. goodsInfo.LastUpdateTime = DateTime.Now;
  432. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  433. .UpdateColumns(x => new
  434. {
  435. x.SQ_Total,
  436. x.StockQuantity,
  437. x.PriceTotal,
  438. x.LastUpdateUserId,
  439. x.LastUpdateTime,
  440. })
  441. .Where(x => x.Id == goodsId)
  442. .ExecuteCommandAsync();
  443. if (goodsEdit > 0)
  444. {
  445. _sqlSugar.CommitTran();
  446. _jv.Msg = $"操作成功!";
  447. _jv.Code = StatusCodes.Status200OK;
  448. return _jv;
  449. }
  450. _sqlSugar.RollbackTran();
  451. _jv.Msg = $"操作失败!";
  452. return _jv;
  453. }
  454. /// <summary>
  455. /// 物品领用列表
  456. /// </summary>
  457. /// <param name="_dto"></param>
  458. /// <returns></returns>
  459. public async Task<JsonView> GoodsReceiveList(GoodsReceiveListDTO _dto)
  460. {
  461. RefAsync<int> total = 0;
  462. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  463. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  464. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  465. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  466. .Where((gr, gi, u1, u2) => gr.IsDel == 0)
  467. .WhereIF(_dto.GoodsId > 0, (gr, gi, u1, u2) => gr.GoodsId == _dto.GoodsId)
  468. .WhereIF(_dto.GoodsId > 0, (gr, gi, u1, u2) => gr.CreateUserId == _dto.CurrUserId)
  469. .Select((gr, gi, u1, u2) => new GoodsReceiveListView
  470. {
  471. Id = gr.Id,
  472. GroupId = gr.GroupId,
  473. GoodsId = gr.GoodsId,
  474. GoodsName = gi.Name,
  475. Quantity = gr.Quantity,
  476. Reason = gr.Reason,
  477. Remark = gr.Remark,
  478. AuditStatus = gr.AuditStatus,
  479. //AuditStatusText = gr.AuditStatus.GetEnumDescription(),
  480. AuditUserId = gr.AuditUserId,
  481. AuditUserName = u1.CnName,
  482. AuditTime = gr.AuditTime,
  483. CreateUserName = u2.CnName,
  484. CreateTime = gr.CreateTime
  485. })
  486. .OrderByDescending(gr => gr.CreateTime)
  487. .ToPageListAsync(_dto.PageIndex, _dto.PageSize, total);
  488. _jv.Code = StatusCodes.Status200OK;
  489. _jv.Data = data;
  490. _jv.Count = total;
  491. _jv.Msg = $"操作成功";
  492. return _jv;
  493. }
  494. /// <summary>
  495. /// 物品领用详情
  496. /// </summary>
  497. /// <param name="portType"></param>
  498. /// <param name="id"></param>
  499. /// <returns></returns>
  500. public async Task<JsonView> GoodsReceiveInfo(int portType, int id)
  501. {
  502. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  503. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  504. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  505. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  506. .Where((gr, gi, u1, u2) => gr.IsDel == 0)
  507. .WhereIF(id > 0, (gr, gi, u1, u2u) => gr.GoodsId == id)
  508. .Select((gr, gi, u1, u2) => new
  509. {
  510. gr.Id,
  511. gr.GroupId,
  512. gr.GoodsId,
  513. GoodsName = gi.Name,
  514. gr.Quantity,
  515. gr.Reason,
  516. gr.Remark,
  517. gr.AuditStatus,
  518. AuditStatusText = gr.AuditStatus.GetDescription(),
  519. gr.AuditUserId,
  520. AuditUserName = u1.CnName,
  521. gr.AuditTime,
  522. CreateUserName = u2.CnName,
  523. gr.CreateTime
  524. })
  525. .FirstAsync();
  526. _jv.Code = StatusCodes.Status200OK;
  527. _jv.Data = data;
  528. _jv.Msg = $"操作成功";
  529. return _jv;
  530. }
  531. /// <summary>
  532. /// 物品领用 OP(Add Or Edit)
  533. /// </summary>
  534. /// <param name="id"></param>
  535. /// <returns></returns>
  536. public async Task<JsonView> GoodsReceiveOP(GoodsReceiveOPDTO _dto)
  537. {
  538. var info = _mapper.Map<Pm_GoodsReceive>(_dto);
  539. info.CreateUserId = _dto.CurrUserId;
  540. _sqlSugar.BeginTran();
  541. //物品现有库存
  542. var stockQuantity = _sqlSugar.Queryable<Pm_GoodsInfo>()
  543. .First(x => x.Id == info.GoodsId)
  544. ?.StockQuantity;
  545. //待审核 该物品数量
  546. var waitAuditQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  547. .Where(x => x.IsDel == 0 &&
  548. x.GoodsId == _dto.GoodsId &&
  549. x.AuditStatus == GoodsAuditEnum.Pending
  550. )
  551. .SumAsync(x => x.Quantity);
  552. if (info.Id > 0) //修改
  553. {
  554. //审核验证
  555. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == info.Id);
  556. if (selectInfo.AuditStatus == GoodsAuditEnum.Approved)
  557. {
  558. _sqlSugar.RollbackTran();
  559. _jv.Msg = $"该条数据已通过审核,不可更改!";
  560. return _jv;
  561. }
  562. //物品数量验证
  563. decimal editAfterQuantity = waitAuditQuantity - selectInfo.Quantity + info.Quantity;
  564. if (editAfterQuantity > stockQuantity)
  565. {
  566. _sqlSugar.RollbackTran();
  567. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  568. return _jv;
  569. }
  570. var edit = await _sqlSugar.Updateable(info)
  571. .UpdateColumns(x => new
  572. {
  573. x.GroupId,
  574. x.Quantity,
  575. x.Reason,
  576. x.Remark,
  577. })
  578. .Where(x => x.Id == info.Id)
  579. .ExecuteCommandAsync();
  580. if (edit > 0)
  581. {
  582. _sqlSugar.CommitTran();
  583. _jv.Msg = $"操作成功!";
  584. _jv.Code = StatusCodes.Status200OK;
  585. return _jv;
  586. }
  587. }
  588. else if (info.Id < 1) //添加
  589. {
  590. //物品数量验证
  591. decimal addAgoQuantity = waitAuditQuantity + info.Quantity;
  592. if (addAgoQuantity > stockQuantity)
  593. {
  594. _sqlSugar.RollbackTran();
  595. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  596. return _jv;
  597. }
  598. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  599. if (add > 0)
  600. {
  601. _sqlSugar.CommitTran();
  602. _jv.Msg = $"操作成功!";
  603. _jv.Code = StatusCodes.Status200OK;
  604. return _jv;
  605. }
  606. }
  607. _sqlSugar.RollbackTran();
  608. return _jv;
  609. }
  610. /// <summary>
  611. /// 物品领用 Audit
  612. /// </summary>
  613. /// <param name="idArray"></param>
  614. /// <param name="userId"></param>
  615. /// <param name="auditEnum"></param>
  616. /// <returns></returns>
  617. public async Task<JsonView> GoodsReceiveAudit(int[] idArray, int userId, GoodsAuditEnum auditEnum)
  618. {
  619. if (idArray.Length < 1) return _jv;
  620. //TODO: 审核权限验证
  621. _sqlSugar.BeginTran();
  622. var receiveInfos = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  623. .Where(x => x.IsDel == 0 && idArray.Contains(x.Id))
  624. .ToListAsync();
  625. bool status = true;
  626. foreach (var id in idArray)
  627. {
  628. var currInfo = receiveInfos.Find(x => x.Id == id);
  629. var edit = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  630. .SetColumns(x => new Pm_GoodsReceive()
  631. {
  632. AuditStatus = auditEnum,
  633. AuditUserId = userId,
  634. AuditTime = DateTime.Now,
  635. })
  636. .Where(x => x.Id == id)
  637. .ExecuteCommandAsync();
  638. if (edit < 1) status = false;
  639. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == currInfo.GoodsId).FirstAsync();
  640. goodsInfo.StockQuantity = goodsInfo.StockQuantity - currInfo.Quantity;
  641. goodsInfo.LastUpdateTime = DateTime.Now;
  642. goodsInfo.LastUpdateUserId = userId;
  643. var editGoods = await _sqlSugar.Updateable<Pm_GoodsInfo>(goodsInfo)
  644. .UpdateColumns(x => new
  645. {
  646. x.StockQuantity,
  647. x.LastUpdateUserId,
  648. x.LastUpdateTime,
  649. })
  650. .Where(x => x.Id == id)
  651. .ExecuteCommandAsync();
  652. if (editGoods < 1) status = false;
  653. }
  654. if (status)
  655. {
  656. _sqlSugar.CommitTran();
  657. _jv.Msg = $"操作成功!";
  658. _jv.Code = StatusCodes.Status200OK;
  659. return _jv;
  660. }
  661. _sqlSugar.RollbackTran();
  662. return _jv;
  663. }
  664. /// <summary>
  665. /// 物品领用 Del
  666. /// </summary>
  667. /// <param name="idArray"></param>
  668. /// <param name="userId"></param>
  669. /// <param name="auditEnum"></param>
  670. /// <returns></returns>
  671. public async Task<JsonView> GoodsReceiveDel(int id, int currUserId)
  672. {
  673. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  674. .Where(x => x.IsDel == 0 && x.Id == id)
  675. .FirstAsync();
  676. if (receiveInfo.AuditStatus == GoodsAuditEnum.Approved)
  677. {
  678. _jv.Msg = $"该条数据已通过审核,不可删除!";
  679. return _jv;
  680. }
  681. var edit = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  682. .UpdateColumns(x => new Pm_GoodsReceive()
  683. {
  684. IsDel = 1,
  685. DeleteUserId = currUserId,
  686. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  687. })
  688. .Where(x => x.Id == id)
  689. .ExecuteCommandAsync();
  690. if (edit > 0)
  691. {
  692. _jv.Msg = $"操作成功!";
  693. _jv.Code = StatusCodes.Status200OK;
  694. return _jv;
  695. }
  696. return _jv;
  697. }
  698. }
  699. }