GoodsRepository.cs 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  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 EyeSoft.Runtime.InteropServices;
  8. using Newtonsoft.Json;
  9. using NPOI.OpenXmlFormats.Vml;
  10. using NPOI.SS.UserModel;
  11. using NPOI.Util;
  12. using OASystem.Domain;
  13. using OASystem.Domain.Dtos.PersonnelModule;
  14. using OASystem.Domain.Entities.Groups;
  15. using OASystem.Domain.Entities.PersonnelModule;
  16. using OASystem.Domain.ViewModels.PersonnelModule;
  17. using OASystem.Domain.ViewModels.SmallFun;
  18. using OASystem.Infrastructure.Tools;
  19. using System.Collections.Specialized;
  20. using System.Drawing;
  21. using System.IO;
  22. using System.Security.Policy;
  23. namespace OASystem.Infrastructure.Repositories.PersonnelModule
  24. {
  25. /// <summary>
  26. /// 物品进销存
  27. /// 仓储
  28. /// </summary>
  29. public class GoodsRepository : BaseRepository<Pm_GoodsInfo, GoodsInfoView>
  30. {
  31. private readonly IMapper _mapper;
  32. private JsonView _jv;
  33. private string _url;
  34. private string _excelPath;
  35. public GoodsRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  36. {
  37. _mapper = mapper;
  38. _jv = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  39. _url = AppSettingsHelper.Get("ExcelBaseUrl");
  40. _excelPath = $"{AppSettingsHelper.Get("ExcelBasePath")}";
  41. if (!Directory.Exists(_excelPath))
  42. {
  43. Directory.CreateDirectory(_excelPath);
  44. }
  45. }
  46. /// <summary>
  47. /// 基础数据
  48. /// </summary>
  49. /// <returns></returns>
  50. public async Task<JsonView> InitDataSource()
  51. {
  52. var typeData = await _sqlSugar.Queryable<GoodsTypeView>()
  53. .Includes(x => x.SubTypeItems.Where(z => z.IsDel == 0)
  54. //.Select(z => new {
  55. // z.Id,
  56. // z.STid,
  57. // z.Name
  58. //})
  59. .ToList())
  60. .Where(x => x.IsDel == 0 &&
  61. x.Remark.Equals("GoodsType"))
  62. //.Select(x => new {
  63. // x.Id,
  64. // x.Name,
  65. // x.SubTypeItems
  66. //})
  67. .ToListAsync();
  68. var groupData = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  69. .Where(x => x.IsDel == 0)
  70. .Select(x => new
  71. {
  72. id = x.Id,
  73. groupName = x.TeamName
  74. })
  75. .OrderByDescending(x => x.id)
  76. .ToListAsync();
  77. groupData.Insert(0, new { id = 0, groupName = "其他物资(公司内部物资)" });
  78. groupData.Insert(0, new { id = -1, groupName = "拜访客户所使用的物资" });
  79. groupData.Insert(0, new { id = -2, groupName = "库存调整" });
  80. var userData = await _sqlSugar.Queryable<Sys_Users>()
  81. .Where(x => x.IsDel == 0)
  82. .Select(x => new
  83. {
  84. x.Id,
  85. UserName = x.CnName,
  86. })
  87. .ToListAsync();
  88. //库存状态
  89. var stockStatus = new List<dynamic>() {
  90. new { Value = -1, Text = "全部" },
  91. new { Value = 0, Text = "待确认" },
  92. new { Value = 1, Text = "部分确认" },
  93. new { Value = 2, Text = "已确认" },
  94. new { Value = 3, Text = "已拒绝" },
  95. };
  96. _jv.Code = StatusCodes.Status200OK;
  97. _jv.Data = new { goodsTypeData = typeData, stockStatus = stockStatus, groupNameData = groupData, userNameData = userData };
  98. _jv.Msg = $"操作成功";
  99. return _jv;
  100. }
  101. /// <summary>
  102. /// 物品列表
  103. /// </summary>
  104. /// <param name="dto"></param>
  105. /// <returns></returns>
  106. public async Task<JsonView> GoodsList(GoodsListDto dto)
  107. {
  108. var ids = new List<int>();
  109. if (!string.IsNullOrEmpty(dto.TypeIds))
  110. {
  111. var strArray = dto.TypeIds.Split(',');
  112. foreach (var str in strArray)
  113. {
  114. if (int.TryParse(str, out int id))
  115. {
  116. ids.Add(id);
  117. }
  118. }
  119. }
  120. var auditEnums = new List<GoodsAuditEnum>() {
  121. GoodsAuditEnum.Pending,
  122. GoodsAuditEnum.OutPending,
  123. GoodsAuditEnum.OutConfirming
  124. };
  125. RefAsync<int> total = 0;
  126. var data = await _sqlSugar.Queryable<GoodsListView>()
  127. //.Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && z1.AuditStatus == GoodsAuditEnum.Pending).ToList())
  128. .Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && auditEnums.Contains(z1.AuditStatus)).ToList())
  129. .Includes(glv => glv.TypeData)
  130. .Includes(glv => glv.UserData)
  131. .LeftJoin<Sys_SetData>((glv, sd) => glv.Type == sd.Id)
  132. .LeftJoin<Sys_Users>((glv, sd, u) => glv.LastUpdateUserId == u.Id)
  133. .Where(glv => glv.IsDel == 0)
  134. .WhereIF(ids.Count > 0, glv => ids.Contains(glv.Type))
  135. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), glv => glv.Name.Contains(dto.GoodsName))
  136. .OrderByDescending(glv => glv.LastUpdateTime)
  137. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  138. var view = data.Select(x => new
  139. {
  140. x.Id,
  141. x.Name,
  142. x.Type,
  143. TypeName = x.TypeData?.Name ?? string.Empty,
  144. LastUpdateUserName = x.UserData?.CnName ?? string.Empty,
  145. x.LastUpdateTime,
  146. StockQuantity = x.StockQuantity - x.WaitAuditQuantity,
  147. x.Unit,
  148. x.StockQuantityLabel,
  149. x.Remark
  150. }).ToList();
  151. _jv.Code = StatusCodes.Status200OK;
  152. _jv.Data = view;
  153. _jv.Count = total;
  154. _jv.Msg = $"操作成功";
  155. return _jv;
  156. }
  157. /// <summary>
  158. /// 物品Info
  159. /// </summary>
  160. /// <param name="portType"></param>
  161. /// <param name="id"></param>
  162. /// <returns></returns>
  163. public async Task<JsonView> GoodsInfo(int portType, int id)
  164. {
  165. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  166. .LeftJoin<Sys_SetData>((gi, sd) => gi.Type == sd.Id)
  167. .LeftJoin<Sys_Users>((gi, sd, u1) => gi.LastUpdateUserId == u1.Id)
  168. .LeftJoin<Sys_Users>((gi, sd, u1, u2) => gi.CreateUserId == u2.Id)
  169. .Where((gi, sd, u1, u2) => gi.IsDel == 0 && gi.Id == id)
  170. .Select((gi, sd, u1, u2) => new
  171. {
  172. gi.Id,
  173. gi.Name,
  174. ParentType = sd.STid,
  175. gi.Type,
  176. TypeName = sd.Name,
  177. gi.SQ_Total,
  178. gi.OQ_Total,
  179. gi.PriceTotal,
  180. gi.StockQuantity,
  181. gi.Unit,
  182. gi.Remark,
  183. LastUpdateUserName = u1.CnName,
  184. gi.LastUpdateTime,
  185. CreateUserName = u2.CnName,
  186. gi.CreateTime,
  187. })
  188. .FirstAsync();
  189. _jv.Code = StatusCodes.Status200OK;
  190. _jv.Data = data;
  191. _jv.Msg = $"操作成功";
  192. return _jv;
  193. }
  194. /// <summary>
  195. /// 物品 OP(Create Or Edit)
  196. /// </summary>
  197. /// <param name="dto"></param>
  198. /// <param name="currUserId"></param>
  199. /// <returns></returns>
  200. public async Task<JsonView> GoodsOp(GoodsOpDto dto, int currUserId)
  201. {
  202. var info = new Pm_GoodsInfo()
  203. {
  204. Id = dto.Id,
  205. Name = dto.Name,
  206. Type = dto.Type,
  207. SQ_Total = 0,
  208. OQ_Total = 0,
  209. PriceTotal = 0,
  210. StockQuantity = 0,
  211. Unit = dto.Unit,
  212. Remark = dto.Remark,
  213. LastUpdateUserId = currUserId,
  214. LastUpdateTime = DateTime.Now,
  215. CreateUserId = currUserId
  216. };
  217. if (dto.Id > 0) //Edit
  218. {
  219. var upd = await _sqlSugar.Updateable(info)
  220. .UpdateColumns(x => new
  221. {
  222. x.Name,
  223. x.Type,
  224. x.Unit,
  225. x.Remark,
  226. x.LastUpdateUserId,
  227. x.LastUpdateTime,
  228. })
  229. .ExecuteCommandAsync();
  230. if (upd > 0)
  231. {
  232. _jv.Msg = $"修改成功!";
  233. _jv.Code = StatusCodes.Status200OK;
  234. return _jv;
  235. }
  236. }
  237. else if (dto.Id < 1) //添加
  238. {
  239. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Name.Equals(info.Name));
  240. if (selectInfo != null)
  241. {
  242. _jv.Msg = $"“{info.Name}”该物品已存在,请勿重新添加!";
  243. return _jv;
  244. }
  245. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  246. if (add > 0)
  247. {
  248. _jv.Msg = $"添加成功!";
  249. _jv.Code = StatusCodes.Status200OK;
  250. return _jv;
  251. }
  252. }
  253. return _jv;
  254. }
  255. /// <summary>
  256. /// 物品 Del
  257. /// </summary>
  258. /// <param name="id"></param>
  259. /// <param name="currUserId"></param>
  260. /// <returns></returns>
  261. public async Task<JsonView> GoodsDel(int id, int currUserId)
  262. {
  263. _sqlSugar.BeginTran();
  264. var goods = await _sqlSugar.Updateable<Pm_GoodsInfo>()
  265. .SetColumns(x => new Pm_GoodsInfo()
  266. {
  267. IsDel = 1,
  268. DeleteUserId = currUserId,
  269. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  270. })
  271. .Where(x => x.Id == id)
  272. .ExecuteCommandAsync();
  273. if (goods < 1)
  274. {
  275. _sqlSugar.RollbackTran();
  276. _jv.Msg = $"操作失败";
  277. return _jv;
  278. }
  279. var goodsStorage = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  280. .SetColumns(x => new Pm_GoodsStorage()
  281. {
  282. IsDel = 1,
  283. DeleteUserId = currUserId,
  284. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  285. })
  286. .Where(x => x.Id == id)
  287. .ExecuteCommandAsync();
  288. var goodsReceive = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  289. .SetColumns(x => new Pm_GoodsReceive()
  290. {
  291. IsDel = 1,
  292. DeleteUserId = currUserId,
  293. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  294. })
  295. .Where(x => x.Id == id)
  296. .ExecuteCommandAsync();
  297. _sqlSugar.CommitTran();
  298. _jv.Code = StatusCodes.Status200OK;
  299. _jv.Msg = $"操作成功!";
  300. return _jv;
  301. }
  302. /// <summary>
  303. /// 物品入库列表(带审核)
  304. /// </summary>
  305. /// <param name="dto"></param>
  306. /// <returns></returns>
  307. public async Task<JsonView> GoodsStorageList(GoodsStorageListDto dto)
  308. {
  309. string reqAuditLabel = dto.AuditLabel;
  310. var auditLabel = Array.Empty<int>();
  311. int userId = dto.CurrUserId;
  312. if (!string.IsNullOrEmpty(reqAuditLabel))
  313. {
  314. if (!reqAuditLabel.Contains("-1"))
  315. {
  316. auditLabel = reqAuditLabel
  317. .Split(',')
  318. .Select(x =>
  319. {
  320. if (int.TryParse(x, out var id)) return id;
  321. return id;
  322. })
  323. .ToArray();
  324. }
  325. }
  326. RefAsync<int> total = 0;
  327. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  328. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  329. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  330. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  331. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  332. .WhereIF(dto.GoodsId > 0, (gs, gi, u, u1) => gs.GoodsId == dto.GoodsId)
  333. .WhereIF(auditLabel.Length > 0, (gs, gi, u, u1) => auditLabel.Contains((int)gs.ConfirmStatus))
  334. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gs, gi, u, u1) => gi.Name.Contains(dto.GoodsName))
  335. .WhereIF(!string.IsNullOrEmpty(dto.BatchNo), (gs, gi, u, u1) => gs.BatchNo.Contains(dto.BatchNo))
  336. .Select((gs, gi, u, u1) => new GoodsStorageListView()
  337. {
  338. Id = gs.Id,
  339. GoodsId = gs.GoodsId,
  340. BatchNo = gs.BatchNo,
  341. GoodsName = gi.Name,
  342. Quantity = gs.Quantity,
  343. UnitPrice = gs.UnitPrice,
  344. TotalPrice = gs.TotalPrice,
  345. SupplierName = gs.SupplierName,
  346. SupplierTel = gs.SupplierTel,
  347. SupplierAddress = gs.SupplierAddress,
  348. SupplierSource = gs.SupplierSource,
  349. StorageUserName = u1.CnName,
  350. StorageTime = gs.StorageTime,
  351. CreateUserName = u.CnName,
  352. ConfirmStatus = gs.ConfirmStatus,
  353. StatusDesc = gs.StatusDesc,
  354. CreateTime = gs.CreateTime,
  355. Remark = gs.Remark
  356. })
  357. .OrderByDescending(gs => gs.CreateTime)
  358. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  359. var auditList = GoodsStorageConfirmAuditDep(1);
  360. var hrAuditPer = false;
  361. var finAuditPer = false;
  362. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  363. var finAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Financial);
  364. if (hrAuditInfo != null)
  365. {
  366. if (hrAuditInfo.AuditorIds.Any(x => x == userId))
  367. {
  368. hrAuditPer = true;
  369. }
  370. }
  371. if (finAuditInfo != null)
  372. {
  373. if (finAuditInfo.AuditorIds.Any(x => x == userId))
  374. {
  375. finAuditPer = true;
  376. }
  377. }
  378. foreach (var item in data)
  379. {
  380. item.AuditPers = new GoodsStorageAuditPerView[] {
  381. new (){ AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = GoodsAuditDepEnum.Hr.GetEnumDescription()},
  382. new (){ AuditPer = finAuditPer, AuditDep = GoodsAuditDepEnum.Financial, ButtonText = GoodsAuditDepEnum.Financial.GetEnumDescription()}
  383. };
  384. }
  385. _jv.Code = StatusCodes.Status200OK;
  386. _jv.Data = data;
  387. _jv.Count = total;
  388. _jv.Msg = $"操作成功";
  389. return _jv;
  390. }
  391. /// <summary>
  392. /// 物品入库详情
  393. /// </summary>
  394. /// <param name="id"></param>
  395. /// <returns></returns>
  396. public async Task<JsonView> GoodsStorageInfo(int portType, int id)
  397. {
  398. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  399. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  400. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  401. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  402. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  403. .WhereIF(id > 0, (gs, gi, u, u1) => gs.Id == id)
  404. .Select((gs, gi, u, u1) => new
  405. {
  406. gs.Id,
  407. gs.GoodsId,
  408. GoodsName = gi.Name,
  409. gs.Quantity,
  410. gs.UnitPrice,
  411. gs.TotalPrice,
  412. gs.SupplierName,
  413. gs.SupplierTel,
  414. gs.SupplierAddress,
  415. gs.SupplierSource,
  416. gs.ReceiveQuantity,
  417. gs.StorageUserId,
  418. StorageUser = u1.CnName,
  419. gs.StorageTime,
  420. CreateUserName = u.CnName,
  421. gs.CreateUserId,
  422. gs.CreateTime,
  423. gs.Remark,
  424. //gs.IsInConfirm
  425. })
  426. .FirstAsync();
  427. _jv.Msg = $"操作成功!";
  428. _jv.Code = StatusCodes.Status200OK;
  429. _jv.Data = data;
  430. return _jv;
  431. }
  432. /// <summary>
  433. /// 物品入库 操作(Create Or Edit)
  434. /// </summary>
  435. /// <param name="dto"></param>
  436. /// <param name="currUserId"></param>
  437. /// <returns></returns>
  438. public async Task<JsonView> GoodsStorageOp(GoodsStorageOpDto dto, int currUserId)
  439. {
  440. var info = _mapper.Map<Pm_GoodsStorage>(dto);
  441. info.CreateUserId = currUserId;
  442. info.BatchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff");
  443. _sqlSugar.BeginTran();
  444. if (info.Id > 0) //修改
  445. {
  446. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  447. .Where(x => x.Id == dto.Id)
  448. .FirstAsync();
  449. var auditStatus = selectInfo.ConfirmStatus;
  450. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  451. {
  452. _sqlSugar.RollbackTran();
  453. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可更改!如若更改请取消已确认或部分确认审核状态!";
  454. return _jv;
  455. }
  456. var storageEdit = await _sqlSugar.Updateable(info)
  457. .UpdateColumns(x => new
  458. {
  459. x.Quantity,
  460. x.UnitPrice,
  461. x.TotalPrice,
  462. x.SupplierName,
  463. x.SupplierTel,
  464. x.SupplierAddress,
  465. x.SupplierSource,
  466. x.StorageUserId,
  467. x.StorageTime
  468. })
  469. .Where(x => x.Id == dto.Id)
  470. .ExecuteCommandAsync();
  471. if (storageEdit < 1)
  472. {
  473. _sqlSugar.RollbackTran();
  474. _jv.Msg = $"修改失败!";
  475. return _jv;
  476. }
  477. }
  478. else if (info.Id < 1) //添加
  479. {
  480. info.ConfirmStatus = GoodsConfirmEnum.WaitConfirm;
  481. var storageAddId = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  482. if (storageAddId < 1)
  483. {
  484. _sqlSugar.RollbackTran();
  485. _jv.Msg = $"添加失败!";
  486. return _jv;
  487. }
  488. //入库确认默认状态
  489. List<Pm_GoodsAudit> auditInfos = GoodsStorageConfirm(1, storageAddId, info.CreateUserId);
  490. if (auditInfos.Any())
  491. {
  492. await _sqlSugar.Insertable(auditInfos).ExecuteCommandAsync();
  493. }
  494. }
  495. _sqlSugar.CommitTran();
  496. _jv.Msg = $"操作成功!";
  497. _jv.Code = StatusCodes.Status200OK;
  498. return _jv;
  499. }
  500. /// <summary>
  501. /// 物品入库 确认操作-默认审核部门
  502. /// </summary>
  503. /// <param name="auditType">
  504. /// 审核类型
  505. /// 1.入库 2.出库
  506. /// </param>
  507. /// <param name="dataId">dataId</param>
  508. /// <param name="currUserId">审核人</param>
  509. /// <returns></returns>
  510. public List<Pm_GoodsAudit> GoodsStorageConfirm(int auditType,int dataId,int currUserId)
  511. {
  512. var goodsAuditList = new List<Pm_GoodsAudit>();
  513. goodsAuditList.Add(new Pm_GoodsAudit(auditType, GoodsAuditDepEnum.Hr, dataId, GoodsConfirmEnum.WaitConfirm, currUserId));
  514. goodsAuditList.Add(new Pm_GoodsAudit(auditType, GoodsAuditDepEnum.Financial, dataId, GoodsConfirmEnum.WaitConfirm, currUserId));
  515. return goodsAuditList;
  516. }
  517. /// <summary>
  518. /// 物品入库 确认操作
  519. /// </summary>
  520. /// <param name="dto"></param>
  521. /// <returns></returns>
  522. public async Task<JsonView> GoodsStorageConfirmStatusChange(GoodsStorageConfirmDto dto,int currUserId)
  523. {
  524. int gsId = dto.Id;
  525. var auditDep = dto.AuditDep;
  526. if (gsId < 1)
  527. {
  528. _jv.Msg = string.Format("{0}",MsgTips.Id);
  529. return _jv;
  530. }
  531. if (currUserId < 1)
  532. {
  533. _jv.Msg = string.Format("{0}", MsgTips.UserId);
  534. return _jv;
  535. }
  536. //验证审核部门
  537. (bool auditPer, GoodsAuditDepEnum goodsAuditDep) = GoodsAuditDep(currUserId, auditDep, 1);
  538. if (!auditPer)
  539. {
  540. _jv.Msg = string.Format("未分配入库确认权限!");
  541. return _jv;
  542. }
  543. //入库确认 更改审核状态
  544. _sqlSugar.BeginTran();
  545. var info = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x =>x.IsDel == 0 && x.Id == gsId).FirstAsync();
  546. if (info == null)
  547. {
  548. _jv.Msg = string.Format("入库信息不存在!");
  549. return _jv;
  550. }
  551. var preChangeStatus = info.ConfirmStatus;
  552. if (preChangeStatus == dto.ConfirmStatus)
  553. {
  554. _jv.Msg = string.Format("“{0}”已操作,请勿重复该操作!", dto.ConfirmStatus.GetEnumDescription());
  555. return _jv;
  556. }
  557. var preInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.IsDel == 0 && x.DataId == gsId).ToListAsync();
  558. var auditInfo = preInfos.FirstOrDefault(x => x.Dep == goodsAuditDep);
  559. if (auditInfo != null)
  560. {
  561. //移除部门审核状态
  562. preInfos.Remove(auditInfo);
  563. auditInfo.AuditStatus = dto.ConfirmStatus;
  564. auditInfo.AuditUserId = currUserId;
  565. auditInfo.AuditTime = DateTime.Now;
  566. var updStatus = await _sqlSugar.Updateable(auditInfo)
  567. .UpdateColumns(x => new {x.AuditStatus, x.AuditUserId, x.AuditTime })
  568. .ExecuteCommandAsync();
  569. if (updStatus < 1)
  570. {
  571. _sqlSugar.RollbackTran();
  572. _jv.Msg = string.Format("入库确认失败!");
  573. return _jv;
  574. }
  575. }
  576. else
  577. {
  578. auditInfo = new Pm_GoodsAudit(1, goodsAuditDep, gsId, dto.ConfirmStatus, currUserId, currUserId);
  579. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  580. if (addStatus < 1)
  581. {
  582. _sqlSugar.RollbackTran();
  583. _jv.Msg = string.Format("入库确认失败!");
  584. return _jv;
  585. }
  586. }
  587. preInfos.Add(auditInfo);
  588. //入库确认 更改入库状态及扣或增加除库存数、金额
  589. var confirmStatus = GoodsConfirmEnum.WaitConfirm;
  590. if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 2)
  591. {
  592. confirmStatus = GoodsConfirmEnum.Confirmed;
  593. }else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 1)
  594. {
  595. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  596. }
  597. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.UnApproved).Count() > 0)
  598. {
  599. confirmStatus = GoodsConfirmEnum.UnApproved;
  600. }
  601. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.PartConfirmed).Count() > 0)
  602. {
  603. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  604. }
  605. //入库状态描述
  606. var statusDesc = new StringBuilder();
  607. foreach (var preInfo in preInfos)
  608. {
  609. string depName = preInfo.Dep.GetEnumDescription(),
  610. auditStatus = preInfo.AuditStatus.GetEnumDescription(),
  611. auditUserName = preInfo.AuditUserId > 0 ? _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == preInfo.AuditUserId)?.CnName ?? "-" : "-",
  612. auditTime = preInfo.AuditUserId > 0 ? preInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  613. statusDesc.Append(string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>", depName, auditStatus, auditUserName, auditTime));
  614. }
  615. //更改入库状态及描述
  616. info.ConfirmStatus = confirmStatus;
  617. info.StatusDesc = statusDesc.ToString();
  618. var goodsStorageUpd = await _sqlSugar.Updateable(info)
  619. .UpdateColumns(x => new
  620. {
  621. x.ConfirmStatus,
  622. x.StatusDesc,
  623. })
  624. .Where(x => x.Id == info.Id)
  625. .ExecuteCommandAsync();
  626. if (goodsStorageUpd < 1)
  627. {
  628. _sqlSugar.RollbackTran();
  629. _jv.Msg = $"入库确认状态更改失败!";
  630. return _jv;
  631. }
  632. //入库审核通过数量、金额
  633. decimal auditQuantity = info.Quantity,
  634. auditTotalPrice = info.TotalPrice;
  635. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == info.GoodsId);
  636. goodsInfo.LastUpdateUserId = currUserId;
  637. goodsInfo.LastUpdateTime = DateTime.Now;
  638. if (confirmStatus == GoodsConfirmEnum.Confirmed) // 确认状态
  639. {
  640. //更改后的状态和更改前的状态不一致时 更改库存数、金额
  641. if (preChangeStatus != confirmStatus) //
  642. {
  643. goodsInfo.SQ_Total += auditQuantity;
  644. goodsInfo.StockQuantity += auditQuantity;
  645. goodsInfo.PriceTotal += auditTotalPrice;
  646. }
  647. }
  648. else //其他状态 拒绝、部分确认、等待确认
  649. {
  650. //更改前状态为确认状态时,减库存数、金额
  651. if (preChangeStatus == GoodsConfirmEnum.Confirmed)
  652. {
  653. goodsInfo.SQ_Total -= auditQuantity;
  654. goodsInfo.StockQuantity -= auditQuantity;
  655. goodsInfo.PriceTotal -= auditTotalPrice;
  656. }
  657. }
  658. var goodsUpd = await _sqlSugar.Updateable(goodsInfo)
  659. .UpdateColumns(x => new
  660. {
  661. x.SQ_Total,
  662. x.StockQuantity,
  663. x.PriceTotal,
  664. x.LastUpdateUserId,
  665. x.LastUpdateTime,
  666. })
  667. .Where(x => x.Id == info.GoodsId)
  668. .ExecuteCommandAsync();
  669. if (goodsUpd > 0)
  670. {
  671. _sqlSugar.CommitTran();
  672. _jv.Msg = $"操作成功!";
  673. _jv.Code = StatusCodes.Status200OK;
  674. return _jv;
  675. }
  676. _sqlSugar.RollbackTran();
  677. _jv.Msg = $"操作失败!";
  678. return _jv;
  679. }
  680. /// <summary>
  681. /// 获取物品审核部门
  682. /// </summary>
  683. /// <param name="userId"> userId </param>
  684. /// <param name="auditDepEnum"> 审核部门枚举 </param>
  685. /// <param name="auditType">
  686. /// 指定审核类型(入库/出库)
  687. /// 1:入库审核
  688. /// 2:出库审核
  689. /// </param>
  690. /// <returns></returns>
  691. public (bool, GoodsAuditDepEnum) GoodsAuditDep(int userId,GoodsAuditDepEnum auditDepEnum,int auditType = 1)
  692. {
  693. if (userId < 1) return (false, GoodsAuditDepEnum.Hr);
  694. var auditList = GoodsStorageConfirmAuditDep(1);
  695. if (auditType == 1)
  696. {
  697. if (auditList.Any(x => x.AuditDep == auditDepEnum && x.AuditorIds.Contains(userId)))
  698. {
  699. return (true, auditDepEnum);
  700. }
  701. }
  702. return (false, GoodsAuditDepEnum.Hr);
  703. }
  704. /// <summary>
  705. /// 物品审核部门列表
  706. /// </summary>
  707. /// <param name="auditType">
  708. /// 指定审核类型(入库/出库)
  709. /// 1:入库审核
  710. /// 2:出库审核
  711. /// </param>
  712. /// <returns></returns>
  713. public List<GoodsAuditDepView> GoodsStorageConfirmAuditDep(int auditType=1)
  714. {
  715. var auditList = new List<GoodsAuditDepView>();
  716. var hrAuditorIds = new GoodsAuditDepView()
  717. {
  718. AuditDep = GoodsAuditDepEnum.Hr,
  719. AuditorIds = new int[] {
  720. 343, // 陈湘
  721. 374, // 罗颖
  722. 208, // 雷怡
  723. }
  724. };
  725. var finAuditorIds = new GoodsAuditDepView()
  726. {
  727. AuditDep = GoodsAuditDepEnum.Financial,
  728. AuditorIds = new int[] {
  729. 187, // 曾艳 、
  730. 281, // 伏虹瑾
  731. 208, // 雷怡
  732. }
  733. };
  734. if (auditType == 1)//入库
  735. {
  736. hrAuditorIds.AuditorIds = new int[] {
  737. //343, // 陈湘
  738. 374, // 罗颖
  739. 208, // 雷怡
  740. };
  741. auditList.Add(hrAuditorIds);
  742. auditList.Add(finAuditorIds);
  743. }
  744. else if (auditType == 2) //出库
  745. {
  746. auditList.Add(hrAuditorIds);
  747. auditList.Add(finAuditorIds);
  748. }
  749. return auditList;
  750. }
  751. /// <summary>
  752. /// 物品入库 Del
  753. /// </summary>
  754. /// <param name="id"></param>
  755. /// <returns></returns>
  756. public async Task<JsonView> GoodsStorageDel(int id, int userId)
  757. {
  758. var storageInfo = await _sqlSugar
  759. .Queryable<Pm_GoodsStorage>()
  760. .Where(x => x.Id == id)
  761. .FirstAsync();
  762. if (storageInfo == null) return _jv;
  763. var auditStatus = storageInfo.ConfirmStatus;
  764. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  765. {
  766. _sqlSugar.RollbackTran();
  767. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可删除!如若删除请取消已确认或部分确认审核状态!";
  768. return _jv;
  769. }
  770. decimal delAgoQuantity = storageInfo.Quantity,
  771. delAgoTotalPrice = storageInfo.TotalPrice;
  772. var goodsId = storageInfo.GoodsId;
  773. _sqlSugar.BeginTran();
  774. var storageDel = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  775. .SetColumns(x => new Pm_GoodsStorage
  776. {
  777. DeleteUserId = userId,
  778. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  779. IsDel = 1
  780. })
  781. .Where(x => x.Id == id)
  782. .ExecuteCommandAsync();
  783. if (storageDel < 1)
  784. {
  785. _sqlSugar.RollbackTran();
  786. _jv.Msg = $"操作失败!";
  787. return _jv;
  788. }
  789. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == goodsId);
  790. goodsInfo.SQ_Total = goodsInfo.SQ_Total - delAgoQuantity;
  791. goodsInfo.StockQuantity = goodsInfo.StockQuantity - delAgoQuantity;
  792. goodsInfo.PriceTotal = goodsInfo.PriceTotal - delAgoTotalPrice;
  793. goodsInfo.LastUpdateUserId = userId;
  794. goodsInfo.LastUpdateTime = DateTime.Now;
  795. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  796. .UpdateColumns(x => new
  797. {
  798. x.SQ_Total,
  799. x.StockQuantity,
  800. x.PriceTotal,
  801. x.LastUpdateUserId,
  802. x.LastUpdateTime,
  803. })
  804. .Where(x => x.Id == goodsId)
  805. .ExecuteCommandAsync();
  806. if (goodsEdit > 0)
  807. {
  808. _sqlSugar.CommitTran();
  809. _jv.Msg = $"操作成功!";
  810. _jv.Code = StatusCodes.Status200OK;
  811. return _jv;
  812. }
  813. _sqlSugar.RollbackTran();
  814. _jv.Msg = $"操作失败!";
  815. return _jv;
  816. }
  817. /// <summary>
  818. /// 物品入库
  819. /// excelDownload
  820. /// </summary>
  821. /// <param name="dto"></param>
  822. /// <returns></returns>
  823. public async Task<JsonView> GoodsStorageExcelDownload()
  824. {
  825. var fileName = $"物资入库{Guid.NewGuid()}.xlsx";
  826. var excelTempPath = $"{_excelPath}Template/物资入库Temp.xlsx";
  827. if (!File.Exists(excelTempPath))
  828. {
  829. _jv.Code = StatusCodes.Status204NoContent;
  830. _jv.Msg = $"该模板文件不存在!";
  831. return _jv;
  832. }
  833. _url = $"{_url}Office/Excel/GoodsFiles/";
  834. _excelPath = $"{_excelPath}GoodsFiles";
  835. if (!Directory.Exists(_excelPath))
  836. {
  837. Directory.CreateDirectory(_excelPath);
  838. }
  839. //入库记录
  840. var storageData = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  841. .LeftJoin<Sys_Users>((gs, su) => gs.StorageUserId == su.Id)
  842. .Where((gs, su) => gs.IsDel == 0)
  843. .Select((gs, su) => new
  844. {
  845. gs.GoodsId,
  846. gs.Quantity,
  847. su.CnName,
  848. gs.StorageTime
  849. })
  850. .ToListAsync();
  851. //出库记录
  852. var receiveData = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  853. .LeftJoin<Sys_Users>((gr, su) => gr.CreateUserId == su.Id)
  854. .Where((gr, su) => gr.IsDel == 0)
  855. .Select((gr, su) => new
  856. {
  857. gr.GoodsId,
  858. gr.Quantity,
  859. su.CnName,
  860. gr.CreateTime,
  861. gr.Reason
  862. })
  863. .ToListAsync();
  864. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  865. .Where(gi => gi.IsDel == 0)
  866. .Select(gi => new GoodsStorageExcelDownloadView
  867. {
  868. Id = gi.Id,
  869. Name = gi.Name,
  870. SQ_Total = gi.SQ_Total,
  871. OQ_Total = gi.OQ_Total,
  872. StockQuantity = gi.StockQuantity,
  873. Unit = gi.Unit
  874. })
  875. .ToListAsync();
  876. foreach (var item in data)
  877. {
  878. var storageData1 = storageData.Where(x => x.GoodsId == item.Id).ToList();
  879. if (storageData1.Any())
  880. {
  881. item.SQ_Total1 = storageData1.Sum(x => x.Quantity);
  882. item.SQ_Total -= item.SQ_Total1;
  883. item.StorageLabel = string.Join("\r\n", storageData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】 入库人:【" + x.CnName + "】 入库时间:【" + x.StorageTime + "】").ToList());
  884. }
  885. var receiveData1 = receiveData.Where(x => x.GoodsId == item.Id).ToList();
  886. if (receiveData1.Any())
  887. {
  888. 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());
  889. }
  890. }
  891. //载入模板
  892. WorkbookDesigner designer = new WorkbookDesigner();
  893. designer.Workbook = new Workbook(excelTempPath);
  894. designer.SetDataSource("Export", data);
  895. designer.Process();
  896. #region 渲染Cell批注
  897. var sheet = designer.Workbook.Worksheets[0];
  898. for (int i = 0; i < data.Count; i++)
  899. {
  900. string storageComment = $"C{i + 2}",
  901. receiveComment = $"D{i + 2}",
  902. storageCommentText = data[i].StorageLabel,
  903. receiveCommentText = data[i].ReceiveLabel;
  904. if (!string.IsNullOrEmpty(storageCommentText))
  905. {
  906. int storageIndex = sheet.Comments.Add(storageComment);
  907. Aspose.Cells.Comment comment = sheet.Comments[storageIndex];
  908. comment.Note = storageCommentText;
  909. comment.Width = 500;
  910. comment.Height = 200;
  911. //comment.Font.Color = Color.Red;
  912. }
  913. if (!string.IsNullOrEmpty(receiveCommentText))
  914. {
  915. int receiveIndex = sheet.Comments.Add(receiveComment);
  916. Aspose.Cells.Comment comment = sheet.Comments[receiveIndex];
  917. comment.Note = receiveCommentText;
  918. comment.Width = 800;
  919. comment.Height = 200;
  920. //comment.Font.Color = Color.Red;
  921. }
  922. }
  923. #endregion
  924. string serverPath = $"{_url}{fileName}";
  925. designer.Workbook.Save($"{_excelPath}/{fileName}");
  926. _jv.Code = StatusCodes.Status200OK;
  927. _jv.Data = new { url = serverPath };
  928. _jv.Msg = $"操作成功";
  929. return _jv;
  930. }
  931. /// <summary>
  932. /// 物品领用列表
  933. /// </summary>
  934. /// <param name="dto"></param>
  935. /// <returns></returns>
  936. public async Task<JsonView> GoodsReceiveList(GoodsReceiveListDTO dto)
  937. {
  938. //参数处理
  939. int[] typeLabel = Array.Empty<int>(),
  940. userLabel = Array.Empty<int>(),
  941. auditLabel = Array.Empty<int>(),
  942. groupLabel = Array.Empty<int>();
  943. int currUserId = dto.CurrUserId;
  944. if (!string.IsNullOrEmpty(dto.TypeLabel))
  945. {
  946. typeLabel = dto.TypeLabel
  947. .Split(',')
  948. .Select(x =>
  949. {
  950. if (int.TryParse(x, out var id)) return id;
  951. return id;
  952. })
  953. .ToArray();
  954. }
  955. if (!string.IsNullOrEmpty(dto.UserLabel))
  956. {
  957. userLabel = dto.UserLabel
  958. .Split(',')
  959. .Select(x =>
  960. {
  961. if (int.TryParse(x, out var id)) return id;
  962. return id;
  963. })
  964. .ToArray();
  965. }
  966. if (!string.IsNullOrEmpty(dto.AuditLabel))
  967. {
  968. auditLabel = dto.AuditLabel
  969. .Split(',')
  970. .Select(x =>
  971. {
  972. if (int.TryParse(x, out var id)) return id;
  973. return id;
  974. })
  975. .ToArray();
  976. }
  977. if (!string.IsNullOrEmpty(dto.GroupLabel))
  978. {
  979. groupLabel = dto.GroupLabel
  980. .Split(',')
  981. .Select(x =>
  982. {
  983. if (int.TryParse(x, out var id)) return id;
  984. return id;
  985. })
  986. .ToArray();
  987. }
  988. //物品ID和物品名称只能传一个
  989. if (dto.GoodsId > 0) dto.GoodsName = string.Empty;
  990. if (!string.IsNullOrEmpty(dto.GoodsName)) dto.GoodsId = 0;
  991. var beginBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.BeginDt) ? $"{dto.BeginDt} 00:00:00" : string.Empty, out var begin);
  992. var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
  993. RefAsync<int> total = 0;
  994. var data = _sqlSugar.Queryable<Pm_GoodsReceive>()
  995. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  996. .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
  997. .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
  998. .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  999. .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  1000. .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
  1001. .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
  1002. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
  1003. .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
  1004. .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
  1005. .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
  1006. .WhereIF(groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
  1007. .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
  1008. .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  1009. {
  1010. Id = gr.Id,
  1011. GroupId = gr.GroupId,
  1012. GroupName = di.TeamName,
  1013. GoodsId = gr.GoodsId,
  1014. GoodsName = gi.Name,
  1015. GoodsType = sd.Name,
  1016. Quantity = gr.Quantity,
  1017. Unit = gi.Unit,
  1018. Reason = gr.Reason,
  1019. Remark = gr.Remark,
  1020. AuditStatus = gr.AuditStatus,
  1021. StatusDesc = gr.StatusDesc,
  1022. AuditUserId = gr.AuditUserId,
  1023. AuditUserName = u1.CnName,
  1024. AuditTime = gr.AuditTime,
  1025. CreateUserName = u2.CnName,
  1026. CreateTime = gr.CreateTime
  1027. })
  1028. .OrderByDescending(gr => gr.CreateTime);
  1029. //excel导出
  1030. if (dto.IsExcelDownload)
  1031. {
  1032. var fileName = $"物资领用{Guid.NewGuid()}.xlsx";
  1033. var excelTempPath = $"{_excelPath}Template/物资领用Temp.xlsx";
  1034. if (!File.Exists(excelTempPath))
  1035. {
  1036. _jv.Code = StatusCodes.Status204NoContent;
  1037. _jv.Msg = $"该模板文件不存在!";
  1038. return _jv;
  1039. }
  1040. _url = $"{_url}Office/Excel/GoodsFiles/";
  1041. _excelPath = $"{_excelPath}GoodsFiles";
  1042. if (!Directory.Exists(_excelPath))
  1043. {
  1044. Directory.CreateDirectory(_excelPath);
  1045. }
  1046. //载入模板
  1047. WorkbookDesigner designer = new WorkbookDesigner();
  1048. designer.Workbook = new Workbook(excelTempPath);
  1049. var tableData = await data.ToListAsync();
  1050. designer.SetDataSource("Export", tableData);
  1051. designer.Process();
  1052. string serverPath = $"{_url}{fileName}";
  1053. designer.Workbook.Save($"{_excelPath}/{fileName}");
  1054. _jv.Code = StatusCodes.Status200OK;
  1055. _jv.Data = new { url = serverPath };
  1056. _jv.Msg = $"操作成功";
  1057. return _jv;
  1058. }
  1059. //返回分页数据
  1060. var view = await data.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  1061. //财务人事审核权限处理
  1062. var auditList = GoodsStorageConfirmAuditDep(2);
  1063. bool hrAuditPer = false,
  1064. finAuditPer = false;
  1065. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  1066. var finAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Financial);
  1067. if (hrAuditInfo != null)
  1068. {
  1069. if (hrAuditInfo.AuditorIds.Any(x => x == currUserId))
  1070. {
  1071. hrAuditPer = true;
  1072. }
  1073. }
  1074. if (finAuditInfo != null)
  1075. {
  1076. if (finAuditInfo.AuditorIds.Any(x => x == currUserId))
  1077. {
  1078. finAuditPer = true;
  1079. }
  1080. }
  1081. foreach (var item in view)
  1082. {
  1083. item.AuditPers = new GoodsStorageAuditPerView[] {
  1084. new (){ AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = GoodsAuditDepEnum.Hr.GetEnumDescription()},
  1085. new (){ AuditPer = finAuditPer, AuditDep = GoodsAuditDepEnum.Financial, ButtonText = GoodsAuditDepEnum.Financial.GetEnumDescription()}
  1086. };
  1087. }
  1088. if (dto.PortType == 2 || dto.PortType == 3)
  1089. {
  1090. _jv.Data = view;
  1091. }
  1092. else if (dto.PortType == 1)
  1093. {
  1094. var view1 = _mapper.Map<List<GoodsReceiveListView>>(view);
  1095. _jv.Data = view1;
  1096. }
  1097. _jv.Code = StatusCodes.Status200OK;
  1098. _jv.Count = total;
  1099. _jv.Msg = $"操作成功";
  1100. return _jv;
  1101. }
  1102. /// <summary>
  1103. /// 物品领用详情
  1104. /// </summary>
  1105. /// <param name="portType"></param>
  1106. /// <param name="id"></param>
  1107. /// <returns></returns>
  1108. public async Task<JsonView> GoodsReceiveInfo(int portType, int id)
  1109. {
  1110. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  1111. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1112. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  1113. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  1114. .LeftJoin<Grp_DelegationInfo>((gr, gi, u1, u2,di) => gr.GroupId == di.Id)
  1115. .Where((gr, gi, u1, u2, di) => gr.IsDel == 0)
  1116. .WhereIF(id > 0, (gr, gi, u1, u2, di) => gr.Id == id)
  1117. .Select((gr, gi, u1, u2, di) => new GoodsReceiveInfoMobileView
  1118. {
  1119. Id = gr.Id,
  1120. GroupId = gr.GroupId,
  1121. GroupName = di.TeamName,
  1122. GoodsId = gr.GoodsId,
  1123. GoodsName = gi.Name,
  1124. Quantity = gr.Quantity,
  1125. Reason = gr.Reason,
  1126. Remark = gr.Remark,
  1127. GoodsStorageInfo = gr.GoodsStorageInfo,
  1128. AuditStatus = gr.AuditStatus,
  1129. AuditUserId = gr.AuditUserId,
  1130. AuditUserName = u1.CnName,
  1131. AuditTime = gr.AuditTime,
  1132. CreateUserName = u2.CnName,
  1133. CreateTime = gr.CreateTime
  1134. })
  1135. .FirstAsync();
  1136. if (!string.IsNullOrEmpty(data.GoodsStorageInfo))
  1137. {
  1138. var subData = new List<dynamic>();
  1139. try
  1140. {
  1141. var subData1 = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(data.GoodsStorageInfo);
  1142. if (subData1.Count > 0)
  1143. {
  1144. string goodsStorageInfoStr = string.Empty;
  1145. var storageIds = subData1.Select(x => x.StorageId).ToList();
  1146. var storages = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && storageIds.Contains(x.Id)).ToListAsync();
  1147. foreach (var item in subData1)
  1148. {
  1149. var storageInfo = storages.Find(x => x.Id == item.StorageId);
  1150. if (storageInfo != null)
  1151. {
  1152. subData.Add(new
  1153. {
  1154. StorageId = item.StorageId,
  1155. BatchNo = storageInfo.BatchNo,
  1156. RecsiveQuantity = item.Quantity
  1157. });
  1158. goodsStorageInfoStr += $"物品名称:{data.GoodsName} 批次号:{storageInfo.BatchNo} 领用数量:{item.Quantity} \r\n";
  1159. }
  1160. }
  1161. data.QuantityInfos = subData;
  1162. data.GoodsStorageInfoStr = goodsStorageInfoStr;
  1163. }
  1164. }
  1165. catch (Exception e)
  1166. {
  1167. Console.WriteLine(e);
  1168. }
  1169. }
  1170. _jv.Code = StatusCodes.Status200OK;
  1171. _jv.Msg = $"操作成功";
  1172. if (portType == 2 || portType == 3) //移动端
  1173. {
  1174. _jv.Data = data;
  1175. }
  1176. else if (portType == 1) //pc端
  1177. {
  1178. _jv.Data = _mapper.Map<GoodsReceiveInfoView>(data);
  1179. }
  1180. return _jv;
  1181. }
  1182. /// <summary>
  1183. /// 物品领用 OP(Add Or Edit)
  1184. /// </summary>
  1185. /// <param name="dto"></param>
  1186. /// <param name="currUserId"></param>
  1187. /// <returns></returns>
  1188. public async Task<JsonView> GoodsReceiveOp(GoodsReceiveOpDto dto, int currUserId)
  1189. {
  1190. var info = _mapper.Map<Pm_GoodsReceive>(dto);
  1191. info.CreateUserId = currUserId;
  1192. var auditEnums = new List<GoodsAuditEnum>() {
  1193. GoodsAuditEnum.Approved, // 1
  1194. GoodsAuditEnum.UnApproved, // 2
  1195. GoodsAuditEnum.OutConfirmed,// 5
  1196. GoodsAuditEnum.OutRejected // 6
  1197. };
  1198. _sqlSugar.BeginTran();
  1199. //物品现有库存
  1200. var stockQuantity = _sqlSugar.Queryable<Pm_GoodsInfo>().First(x => x.Id == info.GoodsId)?.StockQuantity;
  1201. //待审核 该物品数量
  1202. var waitAuditQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>().Where(x => x.IsDel == 0 &&
  1203. x.GoodsId == dto.GoodsId &&
  1204. !auditEnums.Contains(x.AuditStatus)
  1205. ).SumAsync(x => x.Quantity);
  1206. if (info.Id > 0) //修改
  1207. {
  1208. //审核验证
  1209. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == info.Id);
  1210. if (auditEnums.Contains(selectInfo.AuditStatus))
  1211. {
  1212. _sqlSugar.RollbackTran();
  1213. _jv.Msg = $"该条数据已执行操作({selectInfo.AuditStatus.GetEnumDescription()}),不可更改!";
  1214. return _jv;
  1215. }
  1216. //物品数量验证
  1217. var editAfterQuantity = waitAuditQuantity - selectInfo.Quantity + info.Quantity;
  1218. if (editAfterQuantity > stockQuantity)
  1219. {
  1220. _sqlSugar.RollbackTran();
  1221. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1222. return _jv;
  1223. }
  1224. var edit = await _sqlSugar.Updateable(info)
  1225. .UpdateColumns(x => new
  1226. {
  1227. x.GroupId,
  1228. x.Quantity,
  1229. x.Reason,
  1230. x.Remark,
  1231. })
  1232. .Where(x => x.Id == info.Id)
  1233. .ExecuteCommandAsync();
  1234. if (edit > 0)
  1235. {
  1236. _sqlSugar.CommitTran();
  1237. _jv.Msg = $"操作成功!";
  1238. _jv.Code = StatusCodes.Status200OK;
  1239. return _jv;
  1240. }
  1241. }
  1242. else if (info.Id < 1) //添加
  1243. {
  1244. //物品数量验证
  1245. decimal addAgoQuantity = waitAuditQuantity + info.Quantity;
  1246. if (addAgoQuantity > stockQuantity)
  1247. {
  1248. _sqlSugar.RollbackTran();
  1249. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1250. return _jv;
  1251. }
  1252. var add = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  1253. if (add < 1)
  1254. {
  1255. _sqlSugar.RollbackTran();
  1256. _jv.Msg = $"添加失败!";
  1257. _jv.Code = StatusCodes.Status400BadRequest;
  1258. return _jv;
  1259. }
  1260. //出库确认默认审核状态
  1261. var goodsAuditList = new List<Pm_GoodsAudit>() {
  1262. new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr, add, GoodsConfirmEnum.WaitConfirm, currUserId),
  1263. new Pm_GoodsAudit(2, GoodsAuditDepEnum.Financial, add, GoodsConfirmEnum.WaitConfirm, currUserId)
  1264. };
  1265. if (goodsAuditList.Any())
  1266. {
  1267. await _sqlSugar.Insertable(goodsAuditList).ExecuteCommandAsync();
  1268. }
  1269. _sqlSugar.CommitTran();
  1270. _jv.Msg = $"操作成功!";
  1271. _jv.Code = StatusCodes.Status200OK;
  1272. return _jv;
  1273. }
  1274. _sqlSugar.RollbackTran();
  1275. return _jv;
  1276. }
  1277. /// <summary>
  1278. /// 物品领用 Audit
  1279. /// </summary>
  1280. /// <param name="idArray"></param>
  1281. /// <param name="userId"></param>
  1282. /// <param name="auditEnum"></param>
  1283. /// <returns></returns>
  1284. public async Task<JsonView> GoodsReceiveAudit(int[] idArray, int userId, GoodsAuditEnum auditEnum)
  1285. {
  1286. if (idArray.Length < 1) return _jv;
  1287. var auditEnums = new List<GoodsAuditEnum>() {
  1288. GoodsAuditEnum.Pending,
  1289. GoodsAuditEnum.Approved,
  1290. GoodsAuditEnum.UnApproved,
  1291. GoodsAuditEnum.OutPending,
  1292. GoodsAuditEnum.OutConfirmed,
  1293. GoodsAuditEnum.OutRejected,
  1294. };
  1295. if (!auditEnums.Contains(auditEnum))
  1296. {
  1297. _jv.Msg = $"出库确认状态超出可用范围!";
  1298. return _jv;
  1299. }
  1300. int receiveId = idArray[0];
  1301. //验证审核部门 审核状态 大于等于 OutPending 时 验证
  1302. var auditDep = GoodsAuditDepEnum.Hr;
  1303. if (auditEnum >= GoodsAuditEnum.OutPending)
  1304. {
  1305. var auditList = GoodsStorageConfirmAuditDep(2);
  1306. var auditDepInfo = auditList.Find(x => x.AuditorIds.Contains(userId));
  1307. if (auditDepInfo == null)
  1308. {
  1309. _jv.Msg = string.Format("未分配出库确认权限!");
  1310. return _jv;
  1311. }
  1312. auditDep = auditDepInfo.AuditDep;
  1313. }
  1314. var currUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == userId)?.CnName ?? "-";
  1315. _sqlSugar.BeginTran();
  1316. var receiveInfo = await _sqlSugar
  1317. .Queryable<Pm_GoodsReceive>()
  1318. //.Where(x => x.IsDel == 0 && idArray.Contains(x.Id))
  1319. .Where(x => x.IsDel == 0 && receiveId == x.Id )
  1320. .FirstAsync();
  1321. if (receiveInfo == null)
  1322. {
  1323. _sqlSugar.RollbackTran();
  1324. _jv.Msg = $"当前领用信息不存在!";
  1325. return _jv;
  1326. }
  1327. //审核前状态
  1328. var preChangeStatus = receiveInfo.AuditStatus;
  1329. //状态验证
  1330. //if (preChangeStatus == auditEnum)
  1331. //{
  1332. // _sqlSugar.RollbackTran();
  1333. // _jv.Msg = $"该条数据状态已设置,不可重复设置!";
  1334. // return _jv;
  1335. //}
  1336. StringBuilder statusDesc = new StringBuilder();
  1337. //string.Format("领用确认:状态:待确认 审核人:- 审核时间:-;<br/>人事部:状态:待确认 审核人:- 审核时间:-;<br/>财务部:状态:待确认 审核人:- 审核时间:-;");
  1338. switch (auditEnum)
  1339. {
  1340. case GoodsAuditEnum.Pending: //领用待确认(取消确认、取消拒绝)
  1341. _jv = await GoodsReceivePending(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1342. break;
  1343. case GoodsAuditEnum.Approved: //领用确认
  1344. _jv = await GoodsReceiveApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1345. break;
  1346. case GoodsAuditEnum.UnApproved: //领用拒绝
  1347. _jv = await GoodsReceiveUnApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1348. break;
  1349. case GoodsAuditEnum.OutPending: //出库待确认(出库确认、出库拒绝)
  1350. _jv = await GoodsReceiveOutPending(preChangeStatus, receiveInfo, userId, auditDep, auditEnum);
  1351. break;
  1352. case GoodsAuditEnum.OutConfirmed: //出库确认
  1353. _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1354. break;
  1355. case GoodsAuditEnum.OutRejected: //出库拒绝
  1356. _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1357. break;
  1358. }
  1359. if (_jv.Code == StatusCodes.Status200OK)
  1360. {
  1361. _sqlSugar.CommitTran();
  1362. _jv.Msg = $"操作成功!";
  1363. return _jv;
  1364. }
  1365. _sqlSugar.RollbackTran();
  1366. return _jv;
  1367. }
  1368. /// <summary>
  1369. /// 物品领用状态 - 领用待确认(取消确认、取消拒绝)
  1370. /// </summary>
  1371. /// <param name="preChangeStatus">更改前状态</param>
  1372. /// <param name="receiveId"></param>
  1373. /// <param name="userId"></param>
  1374. /// <param name="userName"></param>
  1375. /// <param name="auditEnum"></param>
  1376. /// <returns></returns>
  1377. public async Task<JsonView> GoodsReceivePending(GoodsAuditEnum preChangeStatus,int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1378. {
  1379. _jv.Code = StatusCodes.Status400BadRequest;
  1380. //领用待确认 操作范围
  1381. var audirEnumPers = new List<GoodsAuditEnum>() {
  1382. GoodsAuditEnum.Approved,
  1383. GoodsAuditEnum.UnApproved
  1384. };
  1385. if (!audirEnumPers.Contains(preChangeStatus))
  1386. {
  1387. _jv.Msg = $"{GoodsAuditEnum.Approved.GetEnumDescription()}、{GoodsAuditEnum.UnApproved.GetEnumDescription()},状态下可取消操作!";
  1388. return _jv;
  1389. }
  1390. var currUserOpDt = DateTime.Now;
  1391. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1392. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1393. auditEnum = GoodsAuditEnum.Pending;
  1394. var changeStatus = await _sqlSugar
  1395. .Updateable<Pm_GoodsReceive>()
  1396. .SetColumns(x => new Pm_GoodsReceive()
  1397. {
  1398. AuditStatus = auditEnum,
  1399. AuditUserId = userId,
  1400. AuditTime = currUserOpDt,
  1401. StatusDesc = statusDesc
  1402. })
  1403. .Where(x => x.Id == receiveId)
  1404. .ExecuteCommandAsync();
  1405. if (changeStatus > 0)
  1406. {
  1407. _jv.Code = StatusCodes.Status200OK;
  1408. }
  1409. return _jv;
  1410. }
  1411. /// <summary>
  1412. /// 物品领用状态 - 领用确认
  1413. /// </summary>
  1414. /// <param name="preChangeStatus">更改前状态</param>
  1415. /// <param name="receiveId"></param>
  1416. /// <param name="userId"></param>
  1417. /// <param name="userName"></param>
  1418. /// <param name="auditEnum"></param>
  1419. /// <returns></returns>
  1420. public async Task<JsonView> GoodsReceiveApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId,string userName, GoodsAuditEnum auditEnum)
  1421. {
  1422. _jv.Code = StatusCodes.Status400BadRequest;
  1423. if (preChangeStatus != GoodsAuditEnum.Pending)
  1424. {
  1425. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1426. return _jv;
  1427. }
  1428. var currUserOpDt = DateTime.Now;
  1429. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1430. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1431. auditEnum = GoodsAuditEnum.OutPending;
  1432. var changeStatus = await _sqlSugar
  1433. .Updateable<Pm_GoodsReceive>()
  1434. .SetColumns(x => new Pm_GoodsReceive()
  1435. {
  1436. AuditStatus = auditEnum,
  1437. AuditUserId = userId,
  1438. AuditTime = currUserOpDt,
  1439. StatusDesc = statusDesc
  1440. })
  1441. .Where(x => x.Id == receiveId)
  1442. .ExecuteCommandAsync();
  1443. if (changeStatus > 0)
  1444. {
  1445. _jv.Code = StatusCodes.Status200OK;
  1446. }
  1447. return _jv;
  1448. }
  1449. /// <summary>
  1450. /// 物品领用状态 - 领用拒绝
  1451. /// </summary>
  1452. /// <param name="preChangeStatus">更改前状态</param>
  1453. /// <param name="receiveId"></param>
  1454. /// <param name="userId"></param>
  1455. /// <param name="userName"></param>
  1456. /// <param name="auditEnum"></param>
  1457. /// <returns></returns>
  1458. public async Task<JsonView> GoodsReceiveUnApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1459. {
  1460. _jv.Code = StatusCodes.Status400BadRequest;
  1461. if (preChangeStatus != GoodsAuditEnum.Pending)
  1462. {
  1463. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1464. return _jv;
  1465. }
  1466. auditEnum = GoodsAuditEnum.UnApproved;
  1467. var currUserOpDt = DateTime.Now;
  1468. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1469. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部认:状态:待确认 审核人:- 审核时间:-;<br/>", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1470. var changeStatus = await _sqlSugar
  1471. .Updateable<Pm_GoodsReceive>()
  1472. .SetColumns(x => new Pm_GoodsReceive()
  1473. {
  1474. AuditStatus = auditEnum,
  1475. AuditUserId = userId,
  1476. AuditTime = currUserOpDt,
  1477. StatusDesc = statusDesc
  1478. })
  1479. .Where(x => x.Id == receiveId)
  1480. .ExecuteCommandAsync();
  1481. if (changeStatus > 0)
  1482. {
  1483. _jv.Code = StatusCodes.Status200OK;
  1484. }
  1485. return _jv;
  1486. }
  1487. /// <summary>
  1488. /// 物品领用状态 - 出库待确认(出库确认、出库拒绝)
  1489. /// </summary>
  1490. /// <param name="preChangeStatus">更改前状态</param>
  1491. /// <param name="receiveId"></param>
  1492. /// <param name="userId"></param>
  1493. /// <param name="userName"></param>
  1494. /// <param name="auditEnum"></param>
  1495. /// <returns></returns>
  1496. public async Task<JsonView> GoodsReceiveOutPending(GoodsAuditEnum preChangeStatus, Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1497. {
  1498. _jv.Code = StatusCodes.Status400BadRequest;
  1499. //出库待确认 操作范围
  1500. var audirEnumPers = new List<GoodsAuditEnum>() {
  1501. GoodsAuditEnum.OutConfirmed,
  1502. GoodsAuditEnum.OutRejected
  1503. };
  1504. if (!audirEnumPers.Contains(preChangeStatus))
  1505. {
  1506. _jv.Msg = $"{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可取消操作!";
  1507. return _jv;
  1508. }
  1509. var currUserOpDt = DateTime.Now;
  1510. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1511. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1512. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1513. .ToListAsync();
  1514. auditEnum = GoodsAuditEnum.OutPending;
  1515. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1516. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1517. if (auditInfo == null)
  1518. {
  1519. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1520. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1521. if (addStatus < 1)
  1522. {
  1523. _jv.Msg = $"出库确认取消操作失败!";
  1524. return _jv;
  1525. }
  1526. auditInfos.Add(auditInfo);
  1527. }
  1528. else
  1529. {
  1530. //移除旧数据
  1531. auditInfos.Remove(auditInfo);
  1532. auditInfo.AuditStatus = auditStatus;
  1533. auditInfo.AuditUserId = userId;
  1534. auditInfo.AuditTime = currUserOpDt;
  1535. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1536. .SetColumns(x => new Pm_GoodsAudit()
  1537. {
  1538. AuditStatus = auditStatus,
  1539. AuditUserId = userId,
  1540. AuditTime = currUserOpDt
  1541. })
  1542. .Where(x => x.Id == auditInfo.Id)
  1543. .ExecuteCommandAsync();
  1544. if (updStatus < 1)
  1545. {
  1546. _jv.Msg = $"出库确认操作失败!";
  1547. return _jv;
  1548. }
  1549. //添加更新后的数据
  1550. auditInfos.Add(auditInfo);
  1551. }
  1552. //处理状态描述
  1553. StringBuilder statusDesc = new StringBuilder();
  1554. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1555. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1556. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1557. statusDesc.Append(receiveStatusDesc);
  1558. foreach (var auditInf in auditInfos)
  1559. {
  1560. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1561. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1562. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1563. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1564. statusDesc.Append(auditInfStatusDesc);
  1565. }
  1566. //批次库存信息
  1567. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1568. //出库确认取消是单独处理
  1569. if (preChangeStatus == GoodsAuditEnum.OutConfirming)
  1570. {
  1571. if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1572. {
  1573. auditEnum = GoodsAuditEnum.OutConfirming;
  1574. }
  1575. //2.1 更改库存
  1576. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1577. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1578. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1579. goodsInfo.LastUpdateTime = DateTime.Now;
  1580. goodsInfo.LastUpdateUserId = userId;
  1581. var editGoods = await _sqlSugar
  1582. .Updateable(goodsInfo)
  1583. .UpdateColumns(x => new
  1584. {
  1585. x.StockQuantity,
  1586. x.OQ_Total,
  1587. x.LastUpdateUserId,
  1588. x.LastUpdateTime,
  1589. })
  1590. .Where(x => x.Id == receiveInfo.GoodsId)
  1591. .ExecuteCommandAsync();
  1592. if (editGoods < 1)
  1593. {
  1594. _jv.Msg = $"库存更新失败!";
  1595. return _jv;
  1596. }
  1597. //2.2 入库批次关联领用人 更改批次库存
  1598. var goodsStorages = await _sqlSugar
  1599. .Queryable<Pm_GoodsStorage>()
  1600. .Where(x => x.IsDel == 0 &&
  1601. x.GoodsId == receiveInfo.GoodsId &&
  1602. (x.Quantity - x.ReceiveQuantity) > 0
  1603. )
  1604. .OrderBy(x => x.CreateTime)
  1605. .ToListAsync();
  1606. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1607. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1608. var receiveQuantity = 0.00M; //领用总数量
  1609. foreach (var storage in goodsStorages)
  1610. {
  1611. if (receiveInfo.Quantity == receiveQuantity) break;
  1612. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1613. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1614. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1615. const decimal unit = 0.50M;
  1616. while (receiveQuantity < receiveInfo.Quantity)
  1617. {
  1618. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1619. thisBatchReceiveQuantity += unit;
  1620. receiveQuantity += unit;
  1621. }
  1622. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1623. {
  1624. StorageId = storage.Id,
  1625. Quantity = thisBatchReceiveQuantity
  1626. });
  1627. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1628. var storageUpd = storage;
  1629. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1630. batchStorageInfos.Add(storageUpd);
  1631. }
  1632. //2.2.1 更改批次库存
  1633. if (goodsReceiveInfos.Count > 0)
  1634. {
  1635. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1636. .UpdateColumns(x => x.ReceiveQuantity)
  1637. .WhereColumns(x => x.Id)
  1638. .ExecuteCommandAsync();
  1639. if (edit1 < 1)
  1640. {
  1641. _jv.Msg = $"批次库存更新失败!";
  1642. return _jv;
  1643. }
  1644. }
  1645. //2.2.2 添加入库批次关联领用人
  1646. if (goodsReceiveInfos.Count > 0)
  1647. {
  1648. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1649. }
  1650. }
  1651. var statusDesc1 = statusDesc.ToString();
  1652. var changeStatus = await _sqlSugar
  1653. .Updateable<Pm_GoodsReceive>()
  1654. .SetColumns(x => new Pm_GoodsReceive()
  1655. {
  1656. AuditStatus = auditEnum,
  1657. StatusDesc = statusDesc1,
  1658. GoodsStorageInfo= goodsStorageInfo
  1659. })
  1660. .Where(x => x.Id == receiveInfo.Id)
  1661. .ExecuteCommandAsync();
  1662. if (changeStatus > 0)
  1663. {
  1664. _jv.Code = StatusCodes.Status200OK;
  1665. }
  1666. return _jv;
  1667. }
  1668. /// <summary>
  1669. /// 物品领用状态 - 出库确认中、出库确认完成、出库确认拒绝
  1670. /// </summary>
  1671. /// <param name="receiveInfo"></param>
  1672. /// <param name="userId"></param>
  1673. /// <param name="depEnum"></param>
  1674. /// <param name="auditEnum"></param>
  1675. /// <returns></returns>
  1676. public async Task<JsonView> GoodsReceiveOutConfirming(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum,GoodsAuditEnum auditEnum)
  1677. {
  1678. _jv.Code = StatusCodes.Status400BadRequest;
  1679. if (auditEnum < GoodsAuditEnum.OutPending)
  1680. {
  1681. _jv.Msg = $"领用确认执行成功!才可执行出库确认或出库拒绝操作!";
  1682. return _jv;
  1683. }
  1684. var currUserOpDt = DateTime.Now;
  1685. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1686. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1687. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1688. .ToListAsync();
  1689. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1690. if (auditEnum == GoodsAuditEnum.OutRejected) auditStatus = GoodsConfirmEnum.UnApproved;
  1691. else if (auditEnum == GoodsAuditEnum.OutConfirmed) auditStatus = GoodsConfirmEnum.Confirmed;
  1692. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1693. if (auditInfo == null)
  1694. {
  1695. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1696. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1697. if (addStatus < 1)
  1698. {
  1699. _jv.Msg = $"出库确认操作失败!";
  1700. return _jv;
  1701. }
  1702. auditInfos.Add(auditInfo);
  1703. }
  1704. else
  1705. {
  1706. //移除旧数据
  1707. auditInfos.Remove(auditInfo);
  1708. auditInfo.AuditStatus = auditStatus;
  1709. auditInfo.AuditUserId = userId;
  1710. auditInfo.AuditTime = currUserOpDt;
  1711. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1712. .SetColumns(x => new Pm_GoodsAudit()
  1713. {
  1714. AuditStatus = auditStatus,
  1715. AuditUserId = userId,
  1716. AuditTime = currUserOpDt
  1717. })
  1718. .Where(x => x.Id == auditInfo.Id)
  1719. .ExecuteCommandAsync();
  1720. if (updStatus < 1)
  1721. {
  1722. _jv.Msg = $"出库确认操作失败!";
  1723. return _jv;
  1724. }
  1725. //添加更新后的数据
  1726. auditInfos.Add(auditInfo);
  1727. }
  1728. //处理状态描述
  1729. StringBuilder statusDesc= new StringBuilder();
  1730. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1731. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1732. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1733. statusDesc.Append(receiveStatusDesc);
  1734. foreach (var auditInf in auditInfos)
  1735. {
  1736. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1737. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1738. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1739. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1740. statusDesc.Append(auditInfStatusDesc);
  1741. }
  1742. //更改前状态
  1743. var preChangeStatus = receiveInfo.AuditStatus;
  1744. if (preChangeStatus == auditEnum)
  1745. {
  1746. _jv.Msg = $"此操作已执行,不可重复操作!";
  1747. return _jv;
  1748. }
  1749. //批次库存信息
  1750. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1751. //更改领用状态及库存
  1752. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  1753. {
  1754. //出库确认拒绝
  1755. auditEnum = GoodsAuditEnum.OutRejected;
  1756. }
  1757. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  1758. {
  1759. //1.出库确认完成
  1760. auditEnum = GoodsAuditEnum.OutConfirmed;
  1761. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  1762. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  1763. {
  1764. //2.1 更改库存
  1765. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1766. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1767. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1768. goodsInfo.LastUpdateTime = DateTime.Now;
  1769. goodsInfo.LastUpdateUserId = userId;
  1770. var editGoods = await _sqlSugar
  1771. .Updateable(goodsInfo)
  1772. .UpdateColumns(x => new
  1773. {
  1774. x.StockQuantity,
  1775. x.OQ_Total,
  1776. x.LastUpdateUserId,
  1777. x.LastUpdateTime,
  1778. })
  1779. .Where(x => x.Id == receiveInfo.GoodsId)
  1780. .ExecuteCommandAsync();
  1781. if (editGoods < 1)
  1782. {
  1783. _jv.Msg = $"库存更新失败!";
  1784. return _jv;
  1785. }
  1786. //2.2 入库批次关联领用人 更改批次库存
  1787. var goodsStorages = await _sqlSugar
  1788. .Queryable<Pm_GoodsStorage>()
  1789. .Where(x => x.IsDel == 0 &&
  1790. x.GoodsId == receiveInfo.GoodsId &&
  1791. (x.Quantity - x.ReceiveQuantity) > 0
  1792. )
  1793. .OrderBy(x => x.CreateTime)
  1794. .ToListAsync();
  1795. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1796. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1797. var receiveQuantity = 0.00M; //领用总数量
  1798. foreach (var storage in goodsStorages)
  1799. {
  1800. if (receiveInfo.Quantity == receiveQuantity) break;
  1801. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1802. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1803. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1804. const decimal unit = 0.50M;
  1805. while (receiveQuantity < receiveInfo.Quantity)
  1806. {
  1807. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1808. thisBatchReceiveQuantity += unit;
  1809. receiveQuantity += unit;
  1810. }
  1811. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1812. {
  1813. StorageId = storage.Id,
  1814. Quantity = thisBatchReceiveQuantity
  1815. });
  1816. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1817. var storageUpd = storage;
  1818. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1819. batchStorageInfos.Add(storageUpd);
  1820. }
  1821. //2.2.1 更改批次库存
  1822. if (goodsReceiveInfos.Count > 0)
  1823. {
  1824. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1825. .UpdateColumns(x => x.ReceiveQuantity)
  1826. .WhereColumns(x => x.Id)
  1827. .ExecuteCommandAsync();
  1828. if (edit1 < 1)
  1829. {
  1830. _jv.Msg = $"批次库存更新失败!";
  1831. return _jv;
  1832. }
  1833. }
  1834. //2.2.2 添加入库批次关联领用人
  1835. if (goodsReceiveInfos.Count > 0)
  1836. {
  1837. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1838. }
  1839. }
  1840. }
  1841. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1842. {
  1843. //出库确认中
  1844. auditEnum = GoodsAuditEnum.OutConfirming;
  1845. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  1846. {
  1847. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  1848. if (!string.IsNullOrEmpty(goodsStorageInfo))
  1849. {
  1850. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  1851. if (goodsStorageInfos.Any())
  1852. {
  1853. foreach (var item in goodsStorageInfos)
  1854. {
  1855. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  1856. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  1857. .FirstAsync();
  1858. if (newStorageInfo == null) continue;
  1859. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  1860. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  1861. .ExecuteCommandAsync();
  1862. if (newEdit < 1)
  1863. {
  1864. _jv.Msg = $"批次领用库存更新失败!";
  1865. return _jv;
  1866. }
  1867. }
  1868. }
  1869. }
  1870. }
  1871. }
  1872. else
  1873. {
  1874. auditEnum = GoodsAuditEnum.OutPending;
  1875. }
  1876. var statusDesc1 =statusDesc.ToString();
  1877. var changeStatus = await _sqlSugar
  1878. .Updateable<Pm_GoodsReceive>()
  1879. .SetColumns(x => new Pm_GoodsReceive()
  1880. {
  1881. AuditStatus = auditEnum,
  1882. StatusDesc = statusDesc1,
  1883. GoodsStorageInfo = goodsStorageInfo,
  1884. })
  1885. .Where(x => x.Id == receiveInfo.Id)
  1886. .ExecuteCommandAsync();
  1887. if (changeStatus > 0)
  1888. {
  1889. _jv.Code = StatusCodes.Status200OK;
  1890. }
  1891. return _jv;
  1892. }
  1893. /// <summary>
  1894. /// 物品领用 Del
  1895. /// </summary>
  1896. /// <param name="id"></param>
  1897. /// <param name="currUserId"></param>
  1898. /// <returns></returns>
  1899. public async Task<JsonView> GoodsReceiveDel(int id, int currUserId)
  1900. {
  1901. var receiveInfo = await _sqlSugar
  1902. .Queryable<Pm_GoodsReceive>()
  1903. .Where(x => x.IsDel == 0 && x.Id == id)
  1904. .FirstAsync();
  1905. if (receiveInfo.AuditStatus >= GoodsAuditEnum.Approved)
  1906. {
  1907. _jv.Msg = $"该条数据已进入审批流程,不可删除!";
  1908. return _jv;
  1909. }
  1910. var edit = await _sqlSugar
  1911. .Updateable<Pm_GoodsReceive>()
  1912. .SetColumns(x => new Pm_GoodsReceive()
  1913. {
  1914. IsDel = 1,
  1915. DeleteUserId = currUserId,
  1916. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  1917. })
  1918. .Where(x => x.Id == id)
  1919. .ExecuteCommandAsync();
  1920. if (edit > 0)
  1921. {
  1922. _jv.Msg = $"操作成功!";
  1923. _jv.Code = StatusCodes.Status200OK;
  1924. return _jv;
  1925. }
  1926. return _jv;
  1927. }
  1928. }
  1929. }