GoodsRepository.cs 113 KB

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