GoodsRepository.cs 46 KB

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