GoodsRepository.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. using Aspose.Cells;
  2. using Aspose.Cells.Drawing;
  3. using Aspose.Words.Drawing;
  4. using AutoMapper;
  5. using EyeSoft.IO;
  6. using EyeSoft.Reflection;
  7. using Newtonsoft.Json;
  8. using NPOI.OpenXmlFormats.Vml;
  9. using NPOI.SS.UserModel;
  10. using NPOI.Util;
  11. using OASystem.Domain;
  12. using OASystem.Domain.Dtos.PersonnelModule;
  13. using OASystem.Domain.Entities.Groups;
  14. using OASystem.Domain.Entities.PersonnelModule;
  15. using OASystem.Domain.ViewModels.PersonnelModule;
  16. using OASystem.Domain.ViewModels.SmallFun;
  17. using OASystem.Infrastructure.Tools;
  18. using System.Collections.Specialized;
  19. using System.Drawing;
  20. using System.IO;
  21. using System.Security.Policy;
  22. namespace OASystem.Infrastructure.Repositories.PersonnelModule
  23. {
  24. /// <summary>
  25. /// 物品进销存
  26. /// 仓储
  27. /// </summary>
  28. public class GoodsRepository : BaseRepository<Pm_GoodsInfo, GoodsInfoView>
  29. {
  30. private readonly IMapper _mapper;
  31. private JsonView _jv;
  32. private string _url;
  33. private string _excelPath;
  34. public GoodsRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  35. {
  36. _mapper = mapper;
  37. _jv = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  38. _url = AppSettingsHelper.Get("ExcelBaseUrl");
  39. _excelPath = $"{AppSettingsHelper.Get("ExcelBasePath")}";
  40. if (!Directory.Exists(_excelPath))
  41. {
  42. Directory.CreateDirectory(_excelPath);
  43. }
  44. }
  45. /// <summary>
  46. /// 基础数据
  47. /// </summary>
  48. /// <returns></returns>
  49. public async Task<JsonView> InitDataSource()
  50. {
  51. var typeData = await _sqlSugar.Queryable<GoodsTypeView>()
  52. .Includes(x => x.SubTypeItems.Where(z => z.IsDel == 0)
  53. //.Select(z => new {
  54. // z.Id,
  55. // z.STid,
  56. // z.Name
  57. //})
  58. .ToList())
  59. .Where(x => x.IsDel == 0 &&
  60. x.Remark.Equals("GoodsType"))
  61. //.Select(x => new {
  62. // x.Id,
  63. // x.Name,
  64. // x.SubTypeItems
  65. //})
  66. .ToListAsync();
  67. var groupData = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  68. .Where(x => x.IsDel == 0)
  69. .Select(x => new
  70. {
  71. id = x.Id,
  72. groupName = x.TeamName
  73. })
  74. .OrderByDescending(x => x.id)
  75. .ToListAsync();
  76. groupData.Insert(0, new { id = 0, groupName = "其他物资(公司内部物资)" });
  77. groupData.Insert(0, new { id = -1, groupName = "拜访客户所使用的物资" });
  78. groupData.Insert(0, new { id = -2, groupName = "库存调整" });
  79. var userData = await _sqlSugar.Queryable<Sys_Users>()
  80. .Where(x => x.IsDel == 0)
  81. .Select(x => new
  82. {
  83. x.Id,
  84. UserName = x.CnName,
  85. })
  86. .ToListAsync();
  87. //库存状态
  88. var stockStatus = new List<dynamic>() {
  89. new { Value = -1, Text = "全部" },
  90. new { Value = 0, Text = "待确认" },
  91. new { Value = 1, Text = "部分确认" },
  92. new { Value = 2, Text = "已确认" },
  93. new { Value = 3, Text = "已拒绝" },
  94. };
  95. _jv.Code = StatusCodes.Status200OK;
  96. _jv.Data = new { goodsTypeData = typeData, stockStatus = stockStatus, groupNameData = groupData, userNameData = userData };
  97. _jv.Msg = $"操作成功";
  98. return _jv;
  99. }
  100. /// <summary>
  101. /// 物品列表
  102. /// </summary>
  103. /// <param name="dto"></param>
  104. /// <returns></returns>
  105. public async Task<JsonView> GoodsList(GoodsListDto dto)
  106. {
  107. var ids = new List<int>();
  108. if (!string.IsNullOrEmpty(dto.TypeIds))
  109. {
  110. var strArray = dto.TypeIds.Split(',');
  111. foreach (var str in strArray)
  112. {
  113. if (int.TryParse(str, out int id))
  114. {
  115. ids.Add(id);
  116. }
  117. }
  118. }
  119. RefAsync<int> total = 0;
  120. var data = await _sqlSugar.Queryable<GoodsListView>()
  121. .Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && z1.AuditStatus == GoodsAuditEnum.Pending).ToList())
  122. .Includes(glv => glv.TypeData)
  123. .Includes(glv => glv.UserData)
  124. .LeftJoin<Sys_SetData>((glv, sd) => glv.Type == sd.Id)
  125. .LeftJoin<Sys_Users>((glv, sd, u) => glv.LastUpdateUserId == u.Id)
  126. .Where(glv => glv.IsDel == 0)
  127. .WhereIF(ids.Count > 0, glv => ids.Contains(glv.Type))
  128. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), glv => glv.Name.Contains(dto.GoodsName))
  129. .OrderByDescending(glv => glv.LastUpdateTime)
  130. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  131. var view = data.Select(x => new
  132. {
  133. x.Id,
  134. x.Name,
  135. x.Type,
  136. TypeName = x.TypeData?.Name ?? string.Empty,
  137. LastUpdateUserName = x.UserData?.CnName ?? string.Empty,
  138. x.LastUpdateTime,
  139. StockQuantity = x.StockQuantity - x.WaitAuditQuantity,
  140. x.Unit,
  141. x.StockQuantityLabel,
  142. x.Remark
  143. }).ToList();
  144. _jv.Code = StatusCodes.Status200OK;
  145. _jv.Data = view;
  146. _jv.Count = total;
  147. _jv.Msg = $"操作成功";
  148. return _jv;
  149. }
  150. /// <summary>
  151. /// 物品Info
  152. /// </summary>
  153. /// <param name="portType"></param>
  154. /// <param name="id"></param>
  155. /// <returns></returns>
  156. public async Task<JsonView> GoodsInfo(int portType, int id)
  157. {
  158. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  159. .LeftJoin<Sys_SetData>((gi, sd) => gi.Type == sd.Id)
  160. .LeftJoin<Sys_Users>((gi, sd, u1) => gi.LastUpdateUserId == u1.Id)
  161. .LeftJoin<Sys_Users>((gi, sd, u1, u2) => gi.CreateUserId == u2.Id)
  162. .Where((gi, sd, u1, u2) => gi.IsDel == 0 && gi.Id == id)
  163. .Select((gi, sd, u1, u2) => new
  164. {
  165. gi.Id,
  166. gi.Name,
  167. ParentType = sd.STid,
  168. gi.Type,
  169. TypeName = sd.Name,
  170. gi.SQ_Total,
  171. gi.OQ_Total,
  172. gi.PriceTotal,
  173. gi.StockQuantity,
  174. gi.Unit,
  175. gi.Remark,
  176. LastUpdateUserName = u1.CnName,
  177. gi.LastUpdateTime,
  178. CreateUserName = u2.CnName,
  179. gi.CreateTime,
  180. })
  181. .FirstAsync();
  182. _jv.Code = StatusCodes.Status200OK;
  183. _jv.Data = data;
  184. _jv.Msg = $"操作成功";
  185. return _jv;
  186. }
  187. /// <summary>
  188. /// 物品 OP(Create Or Edit)
  189. /// </summary>
  190. /// <param name="dto"></param>
  191. /// <param name="currUserId"></param>
  192. /// <returns></returns>
  193. public async Task<JsonView> GoodsOp(GoodsOpDto dto, int currUserId)
  194. {
  195. var info = new Pm_GoodsInfo()
  196. {
  197. Id = dto.Id,
  198. Name = dto.Name,
  199. Type = dto.Type,
  200. SQ_Total = 0,
  201. OQ_Total = 0,
  202. PriceTotal = 0,
  203. StockQuantity = 0,
  204. Unit = dto.Unit,
  205. Remark = dto.Remark,
  206. LastUpdateUserId = currUserId,
  207. LastUpdateTime = DateTime.Now,
  208. CreateUserId = currUserId
  209. };
  210. if (dto.Id > 0) //Edit
  211. {
  212. var upd = await _sqlSugar.Updateable(info)
  213. .UpdateColumns(x => new
  214. {
  215. x.Name,
  216. x.Type,
  217. x.Unit,
  218. x.Remark,
  219. x.LastUpdateUserId,
  220. x.LastUpdateTime,
  221. })
  222. .ExecuteCommandAsync();
  223. if (upd > 0)
  224. {
  225. _jv.Msg = $"修改成功!";
  226. _jv.Code = StatusCodes.Status200OK;
  227. return _jv;
  228. }
  229. }
  230. else if (dto.Id < 1) //添加
  231. {
  232. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Name.Equals(info.Name));
  233. if (selectInfo != null)
  234. {
  235. _jv.Msg = $"“{info.Name}”该物品已存在,请勿重新添加!";
  236. return _jv;
  237. }
  238. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  239. if (add > 0)
  240. {
  241. _jv.Msg = $"添加成功!";
  242. _jv.Code = StatusCodes.Status200OK;
  243. return _jv;
  244. }
  245. }
  246. return _jv;
  247. }
  248. /// <summary>
  249. /// 物品 Del
  250. /// </summary>
  251. /// <param name="id"></param>
  252. /// <param name="currUserId"></param>
  253. /// <returns></returns>
  254. public async Task<JsonView> GoodsDel(int id, int currUserId)
  255. {
  256. _sqlSugar.BeginTran();
  257. var goods = await _sqlSugar.Updateable<Pm_GoodsInfo>()
  258. .SetColumns(x => new Pm_GoodsInfo()
  259. {
  260. IsDel = 1,
  261. DeleteUserId = currUserId,
  262. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  263. })
  264. .Where(x => x.Id == id)
  265. .ExecuteCommandAsync();
  266. if (goods < 1)
  267. {
  268. _sqlSugar.RollbackTran();
  269. _jv.Msg = $"操作失败";
  270. return _jv;
  271. }
  272. var goodsStorage = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  273. .SetColumns(x => new Pm_GoodsStorage()
  274. {
  275. IsDel = 1,
  276. DeleteUserId = currUserId,
  277. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  278. })
  279. .Where(x => x.Id == id)
  280. .ExecuteCommandAsync();
  281. var goodsReceive = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  282. .SetColumns(x => new Pm_GoodsReceive()
  283. {
  284. IsDel = 1,
  285. DeleteUserId = currUserId,
  286. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  287. })
  288. .Where(x => x.Id == id)
  289. .ExecuteCommandAsync();
  290. _sqlSugar.CommitTran();
  291. _jv.Code = StatusCodes.Status200OK;
  292. _jv.Msg = $"操作成功!";
  293. return _jv;
  294. }
  295. /// <summary>
  296. /// 物品入库列表(带审核)
  297. /// </summary>
  298. /// <param name="dto"></param>
  299. /// <returns></returns>
  300. public async Task<JsonView> GoodsStorageList(GoodsStorageListDto dto,int userId)
  301. {
  302. string reqAuditLabel = dto.AuditLabel;
  303. var auditLabel = Array.Empty<int>();
  304. if (!string.IsNullOrEmpty(reqAuditLabel))
  305. {
  306. if (!reqAuditLabel.Contains("-1"))
  307. {
  308. auditLabel = reqAuditLabel
  309. .Split(',')
  310. .Select(x =>
  311. {
  312. if (int.TryParse(x, out var id)) return id;
  313. return id;
  314. })
  315. .ToArray();
  316. }
  317. }
  318. RefAsync<int> total = 0;
  319. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  320. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  321. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  322. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  323. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  324. .WhereIF(dto.GoodsId > 0, (gs, gi, u, u1) => gs.GoodsId == dto.GoodsId)
  325. .WhereIF(auditLabel.Length > 0, (gs, gi, u, u1) => auditLabel.Contains((int)gs.ConfirmStatus))
  326. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gs, gi, u, u1) => gi.Name.Contains(dto.GoodsName))
  327. .WhereIF(!string.IsNullOrEmpty(dto.BatchNo), (gs, gi, u, u1) => gs.BatchNo.Contains(dto.BatchNo))
  328. .Select((gs, gi, u, u1) => new GoodsStorageListView()
  329. {
  330. Id = gs.Id,
  331. GoodsId = gs.GoodsId,
  332. BatchNo = gs.BatchNo,
  333. GoodsName = gi.Name,
  334. Quantity = gs.Quantity,
  335. UnitPrice = gs.UnitPrice,
  336. TotalPrice = gs.TotalPrice,
  337. SupplierName = gs.SupplierName,
  338. SupplierTel = gs.SupplierTel,
  339. SupplierAddress = gs.SupplierAddress,
  340. SupplierSource = gs.SupplierSource,
  341. StorageUserName = u1.CnName,
  342. StorageTime = gs.StorageTime,
  343. CreateUserName = u.CnName,
  344. ConfirmStatus = gs.ConfirmStatus,
  345. StatusDesc = gs.StatusDesc,
  346. CreateTime = gs.CreateTime,
  347. })
  348. .OrderByDescending(gs => gs.CreateTime)
  349. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  350. var auditList = GoodsStorageConfirmAuditDep(1);
  351. var hrAuditPer = false;
  352. var finAuditPer = false;
  353. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  354. var finAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Financial);
  355. if (hrAuditInfo != null)
  356. {
  357. if (hrAuditInfo.AuditorIds.Any(x => x == userId))
  358. {
  359. hrAuditPer = true;
  360. }
  361. }
  362. if (finAuditInfo != null)
  363. {
  364. if (finAuditInfo.AuditorIds.Any(x => x == userId))
  365. {
  366. finAuditPer = true;
  367. }
  368. }
  369. foreach (var item in data)
  370. {
  371. item.AuditPers = new GoodsStorageAuditPerView[] {
  372. new (){ AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = GoodsAuditDepEnum.Hr.GetEnumDescription()},
  373. new (){ AuditPer = finAuditPer, AuditDep = GoodsAuditDepEnum.Financial, ButtonText = GoodsAuditDepEnum.Financial.GetEnumDescription()}
  374. };
  375. }
  376. _jv.Code = StatusCodes.Status200OK;
  377. _jv.Data = data;
  378. _jv.Count = total;
  379. _jv.Msg = $"操作成功";
  380. return _jv;
  381. }
  382. /// <summary>
  383. /// 物品入库详情
  384. /// </summary>
  385. /// <param name="id"></param>
  386. /// <returns></returns>
  387. public async Task<JsonView> GoodsStorageInfo(int portType, int id)
  388. {
  389. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  390. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  391. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  392. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  393. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  394. .WhereIF(id > 0, (gs, gi, u, u1) => gs.Id == id)
  395. .Select((gs, gi, u, u1) => new
  396. {
  397. gs.Id,
  398. gs.GoodsId,
  399. GoodsName = gi.Name,
  400. gs.Quantity,
  401. gs.UnitPrice,
  402. gs.TotalPrice,
  403. gs.SupplierName,
  404. gs.SupplierTel,
  405. gs.SupplierAddress,
  406. gs.SupplierSource,
  407. gs.ReceiveQuantity,
  408. gs.StorageUserId,
  409. StorageUser = u1.CnName,
  410. gs.StorageTime,
  411. CreateUserName = u.CnName,
  412. gs.CreateUserId,
  413. gs.CreateTime,
  414. gs.Remark,
  415. //gs.IsInConfirm
  416. })
  417. .FirstAsync();
  418. _jv.Msg = $"操作成功!";
  419. _jv.Code = StatusCodes.Status200OK;
  420. _jv.Data = data;
  421. return _jv;
  422. }
  423. /// <summary>
  424. /// 物品入库 操作(Create Or Edit)
  425. /// </summary>
  426. /// <param name="dto"></param>
  427. /// <param name="currUserId"></param>
  428. /// <returns></returns>
  429. public async Task<JsonView> GoodsStorageOp(GoodsStorageOpDto dto, int currUserId)
  430. {
  431. var info = _mapper.Map<Pm_GoodsStorage>(dto);
  432. info.CreateUserId = currUserId;
  433. info.BatchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff");
  434. _sqlSugar.BeginTran();
  435. if (info.Id > 0) //修改
  436. {
  437. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  438. .Where(x => x.Id == dto.Id)
  439. .FirstAsync();
  440. var auditStatus = selectInfo.ConfirmStatus;
  441. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  442. {
  443. _sqlSugar.RollbackTran();
  444. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可更改!如若更改请取消已确认或部分确认审核状态!";
  445. return _jv;
  446. }
  447. var storageEdit = await _sqlSugar.Updateable(info)
  448. .UpdateColumns(x => new
  449. {
  450. x.Quantity,
  451. x.UnitPrice,
  452. x.TotalPrice,
  453. x.SupplierName,
  454. x.SupplierTel,
  455. x.SupplierAddress,
  456. x.SupplierSource,
  457. x.StorageUserId,
  458. x.StorageTime
  459. })
  460. .Where(x => x.Id == dto.Id)
  461. .ExecuteCommandAsync();
  462. if (storageEdit < 1)
  463. {
  464. _sqlSugar.RollbackTran();
  465. _jv.Msg = $"修改失败!";
  466. return _jv;
  467. }
  468. }
  469. else if (info.Id < 1) //添加
  470. {
  471. info.ConfirmStatus = GoodsConfirmEnum.WaitConfirm;
  472. var storageAddId = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  473. if (storageAddId < 1)
  474. {
  475. _sqlSugar.RollbackTran();
  476. _jv.Msg = $"添加失败!";
  477. return _jv;
  478. }
  479. //入库确认默认状态
  480. List<Pm_GoodsAudit> auditInfos = GoodsStorageConfirm(1, storageAddId, info.CreateUserId);
  481. if (auditInfos.Any())
  482. {
  483. await _sqlSugar.Insertable(auditInfos).ExecuteCommandAsync();
  484. }
  485. }
  486. _sqlSugar.CommitTran();
  487. _jv.Msg = $"操作成功!";
  488. _jv.Code = StatusCodes.Status200OK;
  489. return _jv;
  490. }
  491. /// <summary>
  492. /// 物品入库 确认操作-默认审核部门
  493. /// </summary>
  494. /// <param name="auditType">
  495. /// 审核类型
  496. /// 1.入库 2.出库
  497. /// </param>
  498. /// <param name="dataId">dataId</param>
  499. /// <param name="currUserId">审核人</param>
  500. /// <returns></returns>
  501. public List<Pm_GoodsAudit> GoodsStorageConfirm(int auditType,int dataId,int currUserId)
  502. {
  503. var goodsAuditList = new List<Pm_GoodsAudit>();
  504. goodsAuditList.Add(new Pm_GoodsAudit(auditType, GoodsAuditDepEnum.Hr, dataId, GoodsConfirmEnum.WaitConfirm, currUserId));
  505. goodsAuditList.Add(new Pm_GoodsAudit(auditType, GoodsAuditDepEnum.Financial, dataId, GoodsConfirmEnum.WaitConfirm, currUserId));
  506. return goodsAuditList;
  507. }
  508. /// <summary>
  509. /// 物品入库 确认操作
  510. /// </summary>
  511. /// <param name="dto"></param>
  512. /// <returns></returns>
  513. public async Task<JsonView> GoodsStorageConfirmStatusChange(GoodsStorageConfirmDto dto,int currUserId)
  514. {
  515. int gsId = dto.Id;
  516. var auditDep = dto.AuditDep;
  517. if (gsId < 1)
  518. {
  519. _jv.Msg = string.Format("{0}",MsgTips.Id);
  520. return _jv;
  521. }
  522. if (currUserId < 1)
  523. {
  524. _jv.Msg = string.Format("{0}", MsgTips.UserId);
  525. return _jv;
  526. }
  527. //验证审核部门
  528. (bool auditPer, GoodsAuditDepEnum goodsAuditDep) = GoodsAuditDep(currUserId, auditDep, 1);
  529. if (!auditPer)
  530. {
  531. _jv.Msg = string.Format("未分配入库确认权限!");
  532. return _jv;
  533. }
  534. //入库确认 更改审核状态
  535. _sqlSugar.BeginTran();
  536. var info = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x =>x.IsDel == 0 && x.Id == gsId).FirstAsync();
  537. if (info == null)
  538. {
  539. _jv.Msg = string.Format("入库信息不存在!");
  540. return _jv;
  541. }
  542. var preChangeStatus = info.ConfirmStatus;
  543. if (preChangeStatus == dto.ConfirmStatus)
  544. {
  545. _jv.Msg = string.Format("“{0}”已操作,请勿重复该操作!", dto.ConfirmStatus.GetEnumDescription());
  546. return _jv;
  547. }
  548. var preInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.IsDel == 0 && x.DataId == gsId).ToListAsync();
  549. var auditInfo = preInfos.FirstOrDefault(x => x.Dep == goodsAuditDep);
  550. if (auditInfo != null)
  551. {
  552. //移除部门审核状态
  553. preInfos.Remove(auditInfo);
  554. auditInfo.AuditStatus = dto.ConfirmStatus;
  555. auditInfo.AuditUserId = currUserId;
  556. auditInfo.AuditTime = DateTime.Now;
  557. var updStatus = await _sqlSugar.Updateable(auditInfo)
  558. .UpdateColumns(x => new {x.AuditStatus, x.AuditUserId, x.AuditTime })
  559. .ExecuteCommandAsync();
  560. if (updStatus < 1)
  561. {
  562. _sqlSugar.RollbackTran();
  563. _jv.Msg = string.Format("入库确认失败!");
  564. return _jv;
  565. }
  566. }
  567. else
  568. {
  569. auditInfo = new Pm_GoodsAudit(1, goodsAuditDep, gsId, dto.ConfirmStatus, currUserId, currUserId);
  570. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  571. if (addStatus < 1)
  572. {
  573. _sqlSugar.RollbackTran();
  574. _jv.Msg = string.Format("入库确认失败!");
  575. return _jv;
  576. }
  577. }
  578. preInfos.Add(auditInfo);
  579. //入库确认 更改入库状态及扣或增加除库存数、金额
  580. var confirmStatus = GoodsConfirmEnum.WaitConfirm;
  581. if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 2)
  582. {
  583. confirmStatus = GoodsConfirmEnum.Confirmed;
  584. }else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 1)
  585. {
  586. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  587. }
  588. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.UnApproved).Count() > 0)
  589. {
  590. confirmStatus = GoodsConfirmEnum.UnApproved;
  591. }
  592. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.PartConfirmed).Count() > 0)
  593. {
  594. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  595. }
  596. //入库状态描述
  597. var statusDesc = new StringBuilder();
  598. foreach (var preInfo in preInfos)
  599. {
  600. string depName = preInfo.Dep.GetEnumDescription(),
  601. auditStatus = preInfo.AuditStatus.GetEnumDescription(),
  602. auditUserName = preInfo.AuditUserId > 0 ? _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == preInfo.AuditUserId)?.CnName ?? "-" : "-",
  603. auditTime = preInfo.AuditUserId > 0 ? preInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  604. statusDesc.AppendLine(string.Format("{0}:状态:{1}\t审核人:{2}\t审核时间:{3};", depName, auditStatus, auditUserName, auditTime));
  605. }
  606. //更改入库状态及描述
  607. info.ConfirmStatus = confirmStatus;
  608. info.StatusDesc = statusDesc.ToString();
  609. var goodsStorageUpd = await _sqlSugar.Updateable(info)
  610. .UpdateColumns(x => new
  611. {
  612. x.ConfirmStatus,
  613. x.StatusDesc,
  614. })
  615. .Where(x => x.Id == info.Id)
  616. .ExecuteCommandAsync();
  617. if (goodsStorageUpd < 1)
  618. {
  619. _sqlSugar.RollbackTran();
  620. _jv.Msg = $"入库确认状态更改失败!";
  621. return _jv;
  622. }
  623. //入库审核通过数量、金额
  624. decimal auditQuantity = info.Quantity,
  625. auditTotalPrice = info.TotalPrice;
  626. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == info.GoodsId);
  627. goodsInfo.LastUpdateUserId = currUserId;
  628. goodsInfo.LastUpdateTime = DateTime.Now;
  629. if (confirmStatus == GoodsConfirmEnum.Confirmed) // 确认状态
  630. {
  631. //更改后的状态和更改前的状态不一致时 更改库存数、金额
  632. if (preChangeStatus != confirmStatus) //
  633. {
  634. goodsInfo.SQ_Total += auditQuantity;
  635. goodsInfo.StockQuantity += auditQuantity;
  636. goodsInfo.PriceTotal += auditTotalPrice;
  637. }
  638. }
  639. else //其他状态 拒绝、部分确认、等待确认
  640. {
  641. //更改前状态为确认状态时,减库存数、金额
  642. if (preChangeStatus == GoodsConfirmEnum.Confirmed)
  643. {
  644. goodsInfo.SQ_Total -= auditQuantity;
  645. goodsInfo.StockQuantity -= auditQuantity;
  646. goodsInfo.PriceTotal -= auditTotalPrice;
  647. }
  648. }
  649. var goodsUpd = await _sqlSugar.Updateable(goodsInfo)
  650. .UpdateColumns(x => new
  651. {
  652. x.SQ_Total,
  653. x.StockQuantity,
  654. x.PriceTotal,
  655. x.LastUpdateUserId,
  656. x.LastUpdateTime,
  657. })
  658. .Where(x => x.Id == info.GoodsId)
  659. .ExecuteCommandAsync();
  660. if (goodsUpd > 0)
  661. {
  662. _sqlSugar.CommitTran();
  663. _jv.Msg = $"操作成功!";
  664. _jv.Code = StatusCodes.Status200OK;
  665. return _jv;
  666. }
  667. _sqlSugar.RollbackTran();
  668. _jv.Msg = $"操作失败!";
  669. return _jv;
  670. }
  671. /// <summary>
  672. /// 获取物品审核部门
  673. /// </summary>
  674. /// <param name="userId"> userId </param>
  675. /// <param name="auditDepEnum"> 审核部门枚举 </param>
  676. /// <param name="auditType">
  677. /// 指定审核类型(入库/出库)
  678. /// 1:入库审核
  679. /// 2:出库审核
  680. /// </param>
  681. /// <returns></returns>
  682. public (bool, GoodsAuditDepEnum) GoodsAuditDep(int userId,GoodsAuditDepEnum auditDepEnum,int auditType = 1)
  683. {
  684. if (userId < 1) return (false, GoodsAuditDepEnum.Hr);
  685. var auditList = GoodsStorageConfirmAuditDep(1);
  686. if (auditType == 1)
  687. {
  688. if (auditList.Any(x => x.AuditDep == auditDepEnum && x.AuditorIds.Contains(userId)))
  689. {
  690. return (true, auditDepEnum);
  691. }
  692. }
  693. return (false, GoodsAuditDepEnum.Hr);
  694. }
  695. /// <summary>
  696. /// 物品审核部门列表
  697. /// </summary>
  698. /// <param name="auditType">
  699. /// 指定审核类型(入库/出库)
  700. /// 1:入库审核
  701. /// 2:出库审核
  702. /// </param>
  703. /// <returns></returns>
  704. public List<GoodsAuditDepView> GoodsStorageConfirmAuditDep(int auditType=1)
  705. {
  706. var auditList = new List<GoodsAuditDepView>();
  707. var hrAuditorIds = new GoodsAuditDepView()
  708. {
  709. AuditDep = GoodsAuditDepEnum.Hr,
  710. AuditorIds = new int[] {
  711. 343, // 陈湘
  712. 374, // 罗颖
  713. 208, // 雷怡
  714. }
  715. };
  716. var finAuditorIds = new GoodsAuditDepView()
  717. {
  718. AuditDep = GoodsAuditDepEnum.Financial,
  719. AuditorIds = new int[] {
  720. 187, // 曾艳 、
  721. 281, // 伏虹瑾
  722. 208, // 雷怡
  723. }
  724. };
  725. if (auditType == 1)//入库
  726. {
  727. hrAuditorIds.AuditorIds = new int[] {
  728. //343, // 陈湘
  729. 374, // 罗颖
  730. 208, // 雷怡
  731. };
  732. auditList.Add(hrAuditorIds);
  733. auditList.Add(finAuditorIds);
  734. }
  735. else if (auditType == 2) //出库
  736. {
  737. auditList.Add(hrAuditorIds);
  738. auditList.Add(finAuditorIds);
  739. }
  740. return auditList;
  741. }
  742. /// <summary>
  743. /// 物品入库 Del
  744. /// </summary>
  745. /// <param name="id"></param>
  746. /// <returns></returns>
  747. public async Task<JsonView> GoodsStorageDel(int id, int userId)
  748. {
  749. var storageInfo = await _sqlSugar
  750. .Queryable<Pm_GoodsStorage>()
  751. .Where(x => x.Id == id)
  752. .FirstAsync();
  753. if (storageInfo == null) return _jv;
  754. var auditStatus = storageInfo.ConfirmStatus;
  755. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  756. {
  757. _sqlSugar.RollbackTran();
  758. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可删除!如若删除请取消已确认或部分确认审核状态!";
  759. return _jv;
  760. }
  761. decimal delAgoQuantity = storageInfo.Quantity,
  762. delAgoTotalPrice = storageInfo.TotalPrice;
  763. var goodsId = storageInfo.GoodsId;
  764. _sqlSugar.BeginTran();
  765. var storageDel = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  766. .SetColumns(x => new Pm_GoodsStorage
  767. {
  768. DeleteUserId = userId,
  769. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  770. IsDel = 1
  771. })
  772. .Where(x => x.Id == id)
  773. .ExecuteCommandAsync();
  774. if (storageDel < 1)
  775. {
  776. _sqlSugar.RollbackTran();
  777. _jv.Msg = $"操作失败!";
  778. return _jv;
  779. }
  780. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == goodsId);
  781. goodsInfo.SQ_Total = goodsInfo.SQ_Total - delAgoQuantity;
  782. goodsInfo.StockQuantity = goodsInfo.StockQuantity - delAgoQuantity;
  783. goodsInfo.PriceTotal = goodsInfo.PriceTotal - delAgoTotalPrice;
  784. goodsInfo.LastUpdateUserId = userId;
  785. goodsInfo.LastUpdateTime = DateTime.Now;
  786. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  787. .UpdateColumns(x => new
  788. {
  789. x.SQ_Total,
  790. x.StockQuantity,
  791. x.PriceTotal,
  792. x.LastUpdateUserId,
  793. x.LastUpdateTime,
  794. })
  795. .Where(x => x.Id == goodsId)
  796. .ExecuteCommandAsync();
  797. if (goodsEdit > 0)
  798. {
  799. _sqlSugar.CommitTran();
  800. _jv.Msg = $"操作成功!";
  801. _jv.Code = StatusCodes.Status200OK;
  802. return _jv;
  803. }
  804. _sqlSugar.RollbackTran();
  805. _jv.Msg = $"操作失败!";
  806. return _jv;
  807. }
  808. /// <summary>
  809. /// 物品入库
  810. /// excelDownload
  811. /// </summary>
  812. /// <param name="dto"></param>
  813. /// <returns></returns>
  814. public async Task<JsonView> GoodsStorageExcelDownload()
  815. {
  816. var fileName = $"物资入库{Guid.NewGuid()}.xlsx";
  817. var excelTempPath = $"{_excelPath}Template/物资入库Temp.xlsx";
  818. if (!File.Exists(excelTempPath))
  819. {
  820. _jv.Code = StatusCodes.Status204NoContent;
  821. _jv.Msg = $"该模板文件不存在!";
  822. return _jv;
  823. }
  824. _url = $"{_url}Office/Excel/GoodsFiles/";
  825. _excelPath = $"{_excelPath}GoodsFiles";
  826. if (!Directory.Exists(_excelPath))
  827. {
  828. Directory.CreateDirectory(_excelPath);
  829. }
  830. //入库记录
  831. var storageData = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  832. .LeftJoin<Sys_Users>((gs, su) => gs.StorageUserId == su.Id)
  833. .Where((gs, su) => gs.IsDel == 0)
  834. .Select((gs, su) => new
  835. {
  836. gs.GoodsId,
  837. gs.Quantity,
  838. su.CnName,
  839. gs.StorageTime
  840. })
  841. .ToListAsync();
  842. //出库记录
  843. var receiveData = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  844. .LeftJoin<Sys_Users>((gr, su) => gr.CreateUserId == su.Id)
  845. .Where((gr, su) => gr.IsDel == 0)
  846. .Select((gr, su) => new
  847. {
  848. gr.GoodsId,
  849. gr.Quantity,
  850. su.CnName,
  851. gr.CreateTime,
  852. gr.Reason
  853. })
  854. .ToListAsync();
  855. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  856. .Where(gi => gi.IsDel == 0)
  857. .Select(gi => new GoodsStorageExcelDownloadView
  858. {
  859. Id = gi.Id,
  860. Name = gi.Name,
  861. SQ_Total = gi.SQ_Total,
  862. OQ_Total = gi.OQ_Total,
  863. StockQuantity = gi.StockQuantity,
  864. Unit = gi.Unit
  865. })
  866. .ToListAsync();
  867. foreach (var item in data)
  868. {
  869. var storageData1 = storageData.Where(x => x.GoodsId == item.Id).ToList();
  870. if (storageData1.Any())
  871. {
  872. item.SQ_Total1 = storageData1.Sum(x => x.Quantity);
  873. item.SQ_Total -= item.SQ_Total1;
  874. item.StorageLabel = string.Join("\r\n", storageData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】 入库人:【" + x.CnName + "】 入库时间:【" + x.StorageTime + "】").ToList());
  875. }
  876. var receiveData1 = receiveData.Where(x => x.GoodsId == item.Id).ToList();
  877. if (receiveData1.Any())
  878. {
  879. item.ReceiveLabel = string.Join("\r\n", receiveData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】 申请人:【" + x.CnName + "】 申请时间:【" + x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") + "】 原因:【" + x.Reason + "】").ToList());
  880. }
  881. }
  882. //载入模板
  883. WorkbookDesigner designer = new WorkbookDesigner();
  884. designer.Workbook = new Workbook(excelTempPath);
  885. designer.SetDataSource("Export", data);
  886. designer.Process();
  887. #region 渲染Cell批注
  888. var sheet = designer.Workbook.Worksheets[0];
  889. for (int i = 0; i < data.Count; i++)
  890. {
  891. string storageComment = $"C{i + 2}",
  892. receiveComment = $"D{i + 2}",
  893. storageCommentText = data[i].StorageLabel,
  894. receiveCommentText = data[i].ReceiveLabel;
  895. if (!string.IsNullOrEmpty(storageCommentText))
  896. {
  897. int storageIndex = sheet.Comments.Add(storageComment);
  898. Aspose.Cells.Comment comment = sheet.Comments[storageIndex];
  899. comment.Note = storageCommentText;
  900. comment.Width = 500;
  901. comment.Height = 200;
  902. //comment.Font.Color = Color.Red;
  903. }
  904. if (!string.IsNullOrEmpty(receiveCommentText))
  905. {
  906. int receiveIndex = sheet.Comments.Add(receiveComment);
  907. Aspose.Cells.Comment comment = sheet.Comments[receiveIndex];
  908. comment.Note = receiveCommentText;
  909. comment.Width = 800;
  910. comment.Height = 200;
  911. //comment.Font.Color = Color.Red;
  912. }
  913. }
  914. #endregion
  915. string serverPath = $"{_url}{fileName}";
  916. designer.Workbook.Save($"{_excelPath}/{fileName}");
  917. _jv.Code = StatusCodes.Status200OK;
  918. _jv.Data = new { url = serverPath };
  919. _jv.Msg = $"操作成功";
  920. return _jv;
  921. }
  922. /// <summary>
  923. /// 物品领用列表
  924. /// </summary>
  925. /// <param name="dto"></param>
  926. /// <returns></returns>
  927. public async Task<JsonView> GoodsReceiveList(GoodsReceiveListDTO dto)
  928. {
  929. //参数处理
  930. int[] typeLabel = Array.Empty<int>(),
  931. userLabel = Array.Empty<int>(),
  932. auditLabel = Array.Empty<int>(),
  933. groupLabel = Array.Empty<int>();
  934. if (!string.IsNullOrEmpty(dto.TypeLabel))
  935. {
  936. typeLabel = dto.TypeLabel
  937. .Split(',')
  938. .Select(x =>
  939. {
  940. if (int.TryParse(x, out var id)) return id;
  941. return id;
  942. })
  943. .ToArray();
  944. }
  945. if (!string.IsNullOrEmpty(dto.UserLabel))
  946. {
  947. userLabel = dto.UserLabel
  948. .Split(',')
  949. .Select(x =>
  950. {
  951. if (int.TryParse(x, out var id)) return id;
  952. return id;
  953. })
  954. .ToArray();
  955. }
  956. if (!string.IsNullOrEmpty(dto.AuditLabel))
  957. {
  958. auditLabel = dto.AuditLabel
  959. .Split(',')
  960. .Select(x =>
  961. {
  962. if (int.TryParse(x, out var id)) return id;
  963. return id;
  964. })
  965. .ToArray();
  966. }
  967. if (!string.IsNullOrEmpty(dto.GroupLabel))
  968. {
  969. groupLabel = dto.GroupLabel
  970. .Split(',')
  971. .Select(x =>
  972. {
  973. if (int.TryParse(x, out var id)) return id;
  974. return id;
  975. })
  976. .ToArray();
  977. }
  978. //物品ID和物品名称只能传一个
  979. if (dto.GoodsId > 0) dto.GoodsName = string.Empty;
  980. if (!string.IsNullOrEmpty(dto.GoodsName)) dto.GoodsId = 0;
  981. var beginBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.BeginDt) ? $"{dto.BeginDt} 00:00:00" : string.Empty, out var begin);
  982. var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
  983. RefAsync<int> total = 0;
  984. var data = _sqlSugar.Queryable<Pm_GoodsReceive>()
  985. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  986. .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
  987. .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
  988. .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  989. .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  990. .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
  991. .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
  992. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
  993. .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
  994. .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
  995. .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
  996. .WhereIF(groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
  997. .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
  998. .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  999. {
  1000. Id = gr.Id,
  1001. GroupId = gr.GroupId,
  1002. GroupName = di.TeamName,
  1003. GoodsId = gr.GoodsId,
  1004. GoodsName = gi.Name,
  1005. GoodsType = sd.Name,
  1006. Quantity = gr.Quantity,
  1007. Unit = gi.Unit,
  1008. Reason = gr.Reason,
  1009. Remark = gr.Remark,
  1010. AuditStatus = gr.AuditStatus,
  1011. //AuditStatusText = gr.AuditStatus.GetEnumDescription(),
  1012. AuditUserId = gr.AuditUserId,
  1013. AuditUserName = u1.CnName,
  1014. AuditTime = gr.AuditTime,
  1015. CreateUserName = u2.CnName,
  1016. CreateTime = gr.CreateTime
  1017. })
  1018. .OrderByDescending(gr => gr.CreateTime);
  1019. //excel导出
  1020. if (dto.IsExcelDownload)
  1021. {
  1022. var fileName = $"物资领用{Guid.NewGuid()}.xlsx";
  1023. var excelTempPath = $"{_excelPath}Template/物资领用Temp.xlsx";
  1024. if (!File.Exists(excelTempPath))
  1025. {
  1026. _jv.Code = StatusCodes.Status204NoContent;
  1027. _jv.Msg = $"该模板文件不存在!";
  1028. return _jv;
  1029. }
  1030. _url = $"{_url}Office/Excel/GoodsFiles/";
  1031. _excelPath = $"{_excelPath}GoodsFiles";
  1032. if (!Directory.Exists(_excelPath))
  1033. {
  1034. Directory.CreateDirectory(_excelPath);
  1035. }
  1036. //载入模板
  1037. WorkbookDesigner designer = new WorkbookDesigner();
  1038. designer.Workbook = new Workbook(excelTempPath);
  1039. var tableData = await data.ToListAsync();
  1040. designer.SetDataSource("Export", tableData);
  1041. designer.Process();
  1042. string serverPath = $"{_url}{fileName}";
  1043. designer.Workbook.Save($"{_excelPath}/{fileName}");
  1044. _jv.Code = StatusCodes.Status200OK;
  1045. _jv.Data = new { url = serverPath };
  1046. _jv.Msg = $"操作成功";
  1047. return _jv;
  1048. }
  1049. //返回分页数据
  1050. var view = await data.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  1051. if (dto.PortType == 2 || dto.PortType == 3)
  1052. {
  1053. _jv.Data = view;
  1054. }
  1055. else if (dto.PortType == 1)
  1056. {
  1057. _jv.Data = _mapper.Map<List<GoodsReceiveListView>>(view);
  1058. }
  1059. _jv.Code = StatusCodes.Status200OK;
  1060. _jv.Count = total;
  1061. _jv.Msg = $"操作成功";
  1062. return _jv;
  1063. }
  1064. /// <summary>
  1065. /// 物品领用详情
  1066. /// </summary>
  1067. /// <param name="portType"></param>
  1068. /// <param name="id"></param>
  1069. /// <returns></returns>
  1070. public async Task<JsonView> GoodsReceiveInfo(int portType, int id)
  1071. {
  1072. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  1073. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1074. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  1075. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  1076. .LeftJoin<Grp_DelegationInfo>((gr, gi, u1, u2,di) => gr.GroupId == di.Id)
  1077. .Where((gr, gi, u1, u2, di) => gr.IsDel == 0)
  1078. .WhereIF(id > 0, (gr, gi, u1, u2, di) => gr.Id == id)
  1079. .Select((gr, gi, u1, u2, di) => new GoodsReceiveInfoMobileView
  1080. {
  1081. Id = gr.Id,
  1082. GroupId = gr.GroupId,
  1083. GroupName = di.TeamName,
  1084. GoodsId = gr.GoodsId,
  1085. GoodsName = gi.Name,
  1086. Quantity = gr.Quantity,
  1087. Reason = gr.Reason,
  1088. Remark = gr.Remark,
  1089. GoodsStorageInfo = gr.GoodsStorageInfo,
  1090. AuditStatus = gr.AuditStatus,
  1091. AuditUserId = gr.AuditUserId,
  1092. AuditUserName = u1.CnName,
  1093. AuditTime = gr.AuditTime,
  1094. CreateUserName = u2.CnName,
  1095. CreateTime = gr.CreateTime
  1096. })
  1097. .FirstAsync();
  1098. if (!string.IsNullOrEmpty(data.GoodsStorageInfo))
  1099. {
  1100. var subData = new List<dynamic>();
  1101. try
  1102. {
  1103. var subData1 = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(data.GoodsStorageInfo);
  1104. if (subData1.Count > 0)
  1105. {
  1106. string goodsStorageInfoStr = string.Empty;
  1107. var storageIds = subData1.Select(x => x.StorageId).ToList();
  1108. var storages = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && storageIds.Contains(x.Id)).ToListAsync();
  1109. foreach (var item in subData1)
  1110. {
  1111. var storageInfo = storages.Find(x => x.Id == item.StorageId);
  1112. if (storageInfo != null)
  1113. {
  1114. subData.Add(new
  1115. {
  1116. StorageId = item.StorageId,
  1117. BatchNo = storageInfo.BatchNo,
  1118. RecsiveQuantity = item.Quantity
  1119. });
  1120. goodsStorageInfoStr += $"物品名称:{data.GoodsName} 批次号:{storageInfo.BatchNo} 领用数量:{item.Quantity} \r\n";
  1121. }
  1122. }
  1123. data.QuantityInfos = subData;
  1124. data.GoodsStorageInfoStr = goodsStorageInfoStr;
  1125. }
  1126. }
  1127. catch (Exception e)
  1128. {
  1129. Console.WriteLine(e);
  1130. }
  1131. }
  1132. _jv.Code = StatusCodes.Status200OK;
  1133. _jv.Msg = $"操作成功";
  1134. if (portType == 2 || portType == 3) //移动端
  1135. {
  1136. _jv.Data = data;
  1137. }
  1138. else if (portType == 1) //pc端
  1139. {
  1140. _jv.Data = _mapper.Map<GoodsReceiveInfoView>(data);
  1141. }
  1142. return _jv;
  1143. }
  1144. /// <summary>
  1145. /// 物品领用 OP(Add Or Edit)
  1146. /// </summary>
  1147. /// <param name="dto"></param>
  1148. /// <param name="currUserId"></param>
  1149. /// <returns></returns>
  1150. public async Task<JsonView> GoodsReceiveOp(GoodsReceiveOpDto dto, int currUserId)
  1151. {
  1152. var info = _mapper.Map<Pm_GoodsReceive>(dto);
  1153. info.CreateUserId = currUserId;
  1154. _sqlSugar.BeginTran();
  1155. //物品现有库存
  1156. var stockQuantity = _sqlSugar
  1157. .Queryable<Pm_GoodsInfo>()
  1158. .First(x => x.Id == info.GoodsId)
  1159. ?.StockQuantity;
  1160. //待审核 该物品数量
  1161. var waitAuditQuantity = await _sqlSugar
  1162. .Queryable<Pm_GoodsReceive>()
  1163. .Where(x => x.IsDel == 0 &&
  1164. x.GoodsId == dto.GoodsId &&
  1165. x.AuditStatus == GoodsAuditEnum.Pending
  1166. )
  1167. .SumAsync(x => x.Quantity);
  1168. if (info.Id > 0) //修改
  1169. {
  1170. //审核验证
  1171. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == info.Id);
  1172. if (selectInfo.AuditStatus == GoodsAuditEnum.Approved)
  1173. {
  1174. _sqlSugar.RollbackTran();
  1175. _jv.Msg = $"该条数据已通过审核,不可更改!";
  1176. return _jv;
  1177. }
  1178. //物品数量验证
  1179. var editAfterQuantity = waitAuditQuantity - selectInfo.Quantity + info.Quantity;
  1180. if (editAfterQuantity > stockQuantity)
  1181. {
  1182. _sqlSugar.RollbackTran();
  1183. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1184. return _jv;
  1185. }
  1186. var edit = await _sqlSugar.Updateable(info)
  1187. .UpdateColumns(x => new
  1188. {
  1189. x.GroupId,
  1190. x.Quantity,
  1191. x.Reason,
  1192. x.Remark,
  1193. })
  1194. .Where(x => x.Id == info.Id)
  1195. .ExecuteCommandAsync();
  1196. if (edit > 0)
  1197. {
  1198. _sqlSugar.CommitTran();
  1199. _jv.Msg = $"操作成功!";
  1200. _jv.Code = StatusCodes.Status200OK;
  1201. return _jv;
  1202. }
  1203. }
  1204. else if (info.Id < 1) //添加
  1205. {
  1206. //物品数量验证
  1207. decimal addAgoQuantity = waitAuditQuantity + info.Quantity;
  1208. if (addAgoQuantity > stockQuantity)
  1209. {
  1210. _sqlSugar.RollbackTran();
  1211. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1212. return _jv;
  1213. }
  1214. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  1215. if (add > 0)
  1216. {
  1217. _sqlSugar.CommitTran();
  1218. _jv.Msg = $"操作成功!";
  1219. _jv.Code = StatusCodes.Status200OK;
  1220. return _jv;
  1221. }
  1222. }
  1223. _sqlSugar.RollbackTran();
  1224. return _jv;
  1225. }
  1226. /// <summary>
  1227. /// 物品领用 Audit
  1228. /// </summary>
  1229. /// <param name="idArray"></param>
  1230. /// <param name="userId"></param>
  1231. /// <param name="auditEnum"></param>
  1232. /// <returns></returns>
  1233. public async Task<JsonView> GoodsReceiveAudit(int[] idArray, int userId, GoodsAuditEnum auditEnum)
  1234. {
  1235. if (idArray.Length < 1) return _jv;
  1236. //TODO: 审核权限验证
  1237. _sqlSugar.BeginTran();
  1238. var receiveInfos = await _sqlSugar
  1239. .Queryable<Pm_GoodsReceive>()
  1240. .Where(x => x.IsDel == 0 && idArray.Contains(x.Id))
  1241. .ToListAsync();
  1242. var status = true;
  1243. foreach (var id in idArray)
  1244. {
  1245. //1.更改审核状态
  1246. var currInfo = receiveInfos.Find(x => x.Id == id);
  1247. if (currInfo == null) continue;
  1248. var edit = await _sqlSugar
  1249. .Updateable<Pm_GoodsReceive>()
  1250. .SetColumns(x => new Pm_GoodsReceive()
  1251. {
  1252. AuditStatus = auditEnum,
  1253. AuditUserId = userId,
  1254. AuditTime = DateTime.Now,
  1255. })
  1256. .Where(x => x.Id == id)
  1257. .ExecuteCommandAsync();
  1258. if (edit < 1) status = false;
  1259. //if (auditEnum != GoodsAuditEnum.Approved) continue;
  1260. //2.更改库存
  1261. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == currInfo.GoodsId).FirstAsync();
  1262. if (auditEnum == GoodsAuditEnum.Pending)
  1263. {
  1264. goodsInfo.StockQuantity += currInfo.Quantity;
  1265. goodsInfo.OQ_Total -= currInfo.Quantity;
  1266. }
  1267. else if (auditEnum == GoodsAuditEnum.Approved)
  1268. {
  1269. goodsInfo.StockQuantity -= currInfo.Quantity;
  1270. goodsInfo.OQ_Total += currInfo.Quantity;
  1271. }else if(auditEnum == GoodsAuditEnum.UnApproved) continue;
  1272. goodsInfo.LastUpdateTime = DateTime.Now;
  1273. goodsInfo.LastUpdateUserId = userId;
  1274. var editGoods = await _sqlSugar
  1275. .Updateable<Pm_GoodsInfo>(goodsInfo)
  1276. .UpdateColumns(x => new
  1277. {
  1278. x.StockQuantity,
  1279. x.OQ_Total,
  1280. x.LastUpdateUserId,
  1281. x.LastUpdateTime,
  1282. })
  1283. .Where(x => x.Id == currInfo.GoodsId)
  1284. .ExecuteCommandAsync();
  1285. if (editGoods < 1) status = false;
  1286. //3.入库批次关联领用人 更改批次库存
  1287. var goodsStorages = await _sqlSugar
  1288. .Queryable<Pm_GoodsStorage>()
  1289. .Where(x => x.IsDel == 0 &&
  1290. x.GoodsId == currInfo.GoodsId &&
  1291. (x.Quantity - x.ReceiveQuantity) > 0
  1292. )
  1293. .OrderBy(x => x.CreateTime)
  1294. .ToListAsync();
  1295. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1296. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1297. var receiveQuantity = 0.00M; //领用总数量
  1298. if (auditEnum == GoodsAuditEnum.Approved)
  1299. {
  1300. foreach (var storage in goodsStorages)
  1301. {
  1302. if (currInfo.Quantity == receiveQuantity) break;
  1303. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1304. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1305. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1306. const decimal unit = 0.50M;
  1307. while (receiveQuantity < currInfo.Quantity)
  1308. {
  1309. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1310. thisBatchReceiveQuantity += unit;
  1311. receiveQuantity += unit;
  1312. }
  1313. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1314. {
  1315. StorageId = storage.Id,
  1316. Quantity = thisBatchReceiveQuantity
  1317. });
  1318. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1319. var storageUpd = storage;
  1320. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1321. batchStorageInfos.Add(storageUpd);
  1322. }
  1323. //3.1 更改批次库存
  1324. if (goodsReceiveInfos.Count > 0)
  1325. {
  1326. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1327. .UpdateColumns(x => x.ReceiveQuantity)
  1328. .WhereColumns(x => x.Id)
  1329. .ExecuteCommandAsync();
  1330. if (edit1 < 1) status = false;
  1331. }
  1332. //3.2 添加入库批次关联领用人
  1333. if (goodsReceiveInfos.Count > 0)
  1334. {
  1335. var edit1 = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  1336. .SetColumns(x => new Pm_GoodsReceive()
  1337. {
  1338. GoodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos)
  1339. })
  1340. .Where(x => x.Id == id)
  1341. .ExecuteCommandAsync();
  1342. if (edit1 < 1) status = false;
  1343. }
  1344. }
  1345. else if (auditEnum == GoodsAuditEnum.Pending)
  1346. {
  1347. var goodsStorageInfo = currInfo.GoodsStorageInfo;
  1348. if (!string.IsNullOrEmpty(goodsStorageInfo))
  1349. {
  1350. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  1351. if (goodsStorageInfos.Count > 0)
  1352. {
  1353. foreach (var item in goodsStorageInfos)
  1354. {
  1355. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  1356. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  1357. .FirstAsync();
  1358. if (newStorageInfo != null)
  1359. {
  1360. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  1361. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  1362. .ExecuteCommandAsync();
  1363. if (newEdit < 1) status = false;
  1364. }
  1365. }
  1366. }
  1367. }
  1368. }
  1369. }
  1370. if (status)
  1371. {
  1372. _sqlSugar.CommitTran();
  1373. _jv.Msg = $"操作成功!";
  1374. _jv.Code = StatusCodes.Status200OK;
  1375. return _jv;
  1376. }
  1377. _sqlSugar.RollbackTran();
  1378. return _jv;
  1379. }
  1380. /// <summary>
  1381. /// 物品领用 Del
  1382. /// </summary>
  1383. /// <param name="id"></param>
  1384. /// <param name="currUserId"></param>
  1385. /// <returns></returns>
  1386. public async Task<JsonView> GoodsReceiveDel(int id, int currUserId)
  1387. {
  1388. var receiveInfo = await _sqlSugar
  1389. .Queryable<Pm_GoodsReceive>()
  1390. .Where(x => x.IsDel == 0 && x.Id == id)
  1391. .FirstAsync();
  1392. if (receiveInfo.AuditStatus == GoodsAuditEnum.Approved)
  1393. {
  1394. _jv.Msg = $"该条数据已通过审核,不可删除!";
  1395. return _jv;
  1396. }
  1397. var edit = await _sqlSugar
  1398. .Updateable<Pm_GoodsReceive>()
  1399. .SetColumns(x => new Pm_GoodsReceive()
  1400. {
  1401. IsDel = 1,
  1402. DeleteUserId = currUserId,
  1403. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  1404. })
  1405. .Where(x => x.Id == id)
  1406. .ExecuteCommandAsync();
  1407. if (edit > 0)
  1408. {
  1409. _jv.Msg = $"操作成功!";
  1410. _jv.Code = StatusCodes.Status200OK;
  1411. return _jv;
  1412. }
  1413. return _jv;
  1414. }
  1415. }
  1416. }