GoodsRepository.cs 46 KB

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