GoodsRepository.cs 34 KB

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