GoodsRepository.cs 46 KB

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