GoodsRepository.cs 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215
  1. using Aspose.Cells;
  2. using AutoMapper;
  3. using EyeSoft.Collections.Generic;
  4. using EyeSoft.Reflection;
  5. using Newtonsoft.Json;
  6. using OASystem.Domain;
  7. using OASystem.Domain.Dtos.PersonnelModule;
  8. using OASystem.Domain.Entities.Financial;
  9. using OASystem.Domain.Entities.Groups;
  10. using OASystem.Domain.Entities.PersonnelModule;
  11. using OASystem.Domain.ViewModels.Financial;
  12. using OASystem.Domain.ViewModels.PersonnelModule;
  13. using OASystem.Infrastructure.Repositories.Financial;
  14. using OASystem.Infrastructure.Repositories.System;
  15. using OASystem.Infrastructure.Tools;
  16. namespace OASystem.Infrastructure.Repositories.PersonnelModule
  17. {
  18. /// <summary>
  19. /// 物品进销存
  20. /// 仓储
  21. /// </summary>
  22. public class GoodsRepository : BaseRepository<Pm_GoodsInfo, GoodsInfoView>
  23. {
  24. private readonly IMapper _mapper;
  25. private JsonView _jv;
  26. private string _url;
  27. private string _excelPath;
  28. private List<int> _goodsTypeIds; //多部门审核物品类型Id
  29. private readonly ApprovalProcessRepository _approvalProcessRep;
  30. public GoodsRepository(SqlSugarClient sqlSugar,
  31. IMapper mapper,
  32. ApprovalProcessRepository approvalProcessRep)
  33. : base(sqlSugar)
  34. {
  35. _mapper = mapper;
  36. _jv = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  37. _url = AppSettingsHelper.Get("ExcelBaseUrl");
  38. _excelPath = $"{AppSettingsHelper.Get("ExcelBasePath")}";
  39. if (!Directory.Exists(_excelPath))
  40. {
  41. Directory.CreateDirectory(_excelPath);
  42. }
  43. _goodsTypeIds = new List<int>() {
  44. 1423, //1423 贵重物品
  45. };
  46. _approvalProcessRep = approvalProcessRep;
  47. }
  48. /// <summary>
  49. /// 基础数据
  50. /// </summary>
  51. /// <returns></returns>
  52. public async Task<JsonView> InitDataSource()
  53. {
  54. var typeData = await _sqlSugar.Queryable<GoodsTypeView>()
  55. .Includes(x => x.SubTypeItems.Where(z => z.IsDel == 0)
  56. //.Select(z => new {
  57. // z.Id,
  58. // z.STid,
  59. // z.Name
  60. //})
  61. .ToList())
  62. .Where(x => x.IsDel == 0 &&
  63. x.Remark.Equals("GoodsType"))
  64. //.Select(x => new {
  65. // x.Id,
  66. // x.Name,
  67. // x.SubTypeItems
  68. //})
  69. .ToListAsync();
  70. var groupData = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  71. .Where(x => x.IsDel == 0)
  72. .Select(x => new
  73. {
  74. id = x.Id,
  75. groupName = x.TeamName
  76. })
  77. .OrderByDescending(x => x.id)
  78. .ToListAsync();
  79. groupData.Insert(0, new { id = 0, groupName = "其他物资(公司内部物资)" });
  80. groupData.Insert(0, new { id = -1, groupName = "拜访客户所使用的物资" });
  81. groupData.Insert(0, new { id = -2, groupName = "库存调整" });
  82. groupData.Insert(0, new { id = -3, groupName = "全部团组" });
  83. var userData = await _sqlSugar.Queryable<Sys_Users>()
  84. .Where(x => x.IsDel == 0)
  85. .Select(x => new
  86. {
  87. x.Id,
  88. UserName = x.CnName,
  89. })
  90. .ToListAsync();
  91. //库存状态
  92. var stockStatus = new List<dynamic>() {
  93. new { Value = -1, Text = "全部" },
  94. new { Value = 0, Text = "待确认" },
  95. new { Value = 1, Text = "部分确认" },
  96. new { Value = 2, Text = "已确认" },
  97. new { Value = 3, Text = "已拒绝" },
  98. };
  99. //领用出库状态
  100. var receiveStatus = new List<dynamic>() {
  101. new { Value = -1, Text = "全部" },
  102. new { Value = GoodsAuditEnum.Pending, Text = GoodsAuditEnum.Pending.GetEnumDescription() },
  103. //new { Value = GoodsAuditEnum.Approved, Text = GoodsAuditEnum.Approved.GetEnumDescription() },
  104. //new { Value = GoodsAuditEnum.UnApproved, Text = GoodsAuditEnum.UnApproved.GetEnumDescription() },
  105. //new { Value = GoodsAuditEnum.OutPending, Text = GoodsAuditEnum.OutPending.GetEnumDescription() },
  106. new { Value = GoodsAuditEnum.OutConfirming, Text = GoodsAuditEnum.OutConfirming.GetEnumDescription() },
  107. new { Value = GoodsAuditEnum.OutConfirmed, Text = GoodsAuditEnum.OutConfirmed.GetEnumDescription() },
  108. new { Value = GoodsAuditEnum.OutRejected, Text = GoodsAuditEnum.OutRejected.GetEnumDescription() },
  109. };
  110. _jv.Code = StatusCodes.Status200OK;
  111. _jv.Data = new { goodsTypeData = typeData, stockStatus, receiveStatus, groupNameData = groupData, userNameData = userData };
  112. _jv.Msg = $"操作成功";
  113. return _jv;
  114. }
  115. /// <summary>
  116. /// 物品列表
  117. /// </summary>
  118. /// <param name="dto"></param>
  119. /// <returns></returns>
  120. public async Task<JsonView> GoodsList(GoodsListDto dto)
  121. {
  122. var ids = new List<int>();
  123. if (!string.IsNullOrEmpty(dto.TypeIds))
  124. {
  125. var strArray = dto.TypeIds.Split(',');
  126. foreach (var str in strArray)
  127. {
  128. if (int.TryParse(str, out int id))
  129. {
  130. ids.Add(id);
  131. }
  132. }
  133. }
  134. var auditEnums = new List<GoodsAuditEnum>() {
  135. GoodsAuditEnum.Pending,
  136. GoodsAuditEnum.OutPending,
  137. GoodsAuditEnum.OutConfirming
  138. };
  139. RefAsync<int> total = 0;
  140. var data = await _sqlSugar.Queryable<GoodsListView>()
  141. //.Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && z1.AuditStatus == GoodsAuditEnum.Pending).ToList())
  142. .Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && auditEnums.Contains(z1.AuditStatus)).ToList())
  143. .Includes(glv => glv.TypeData)
  144. .Includes(glv => glv.UserData)
  145. .LeftJoin<Sys_SetData>((glv, sd) => glv.Type == sd.Id)
  146. .LeftJoin<Sys_Users>((glv, sd, u) => glv.LastUpdateUserId == u.Id)
  147. .Where(glv => glv.IsDel == 0)
  148. .WhereIF(ids.Count > 0, glv => ids.Contains(glv.Type))
  149. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), glv => glv.Name.Contains(dto.GoodsName))
  150. .OrderByDescending(glv => glv.LastUpdateTime)
  151. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  152. var view = data.Select(x => new
  153. {
  154. x.Id,
  155. x.Name,
  156. x.Type,
  157. TypeName = x.TypeData?.Name ?? string.Empty,
  158. LastUpdateUserName = x.UserData?.CnName ?? string.Empty,
  159. x.LastUpdateTime,
  160. StockQuantity = x.StockQuantity - x.WaitAuditQuantity,
  161. x.Unit,
  162. x.StockQuantityLabel,
  163. x.Remark
  164. }).ToList();
  165. _jv.Code = StatusCodes.Status200OK;
  166. _jv.Data = view;
  167. _jv.Count = total;
  168. _jv.Msg = $"操作成功";
  169. return _jv;
  170. }
  171. /// <summary>
  172. /// 物品Info
  173. /// </summary>
  174. /// <param name="portType"></param>
  175. /// <param name="id"></param>
  176. /// <returns></returns>
  177. public async Task<JsonView> GoodsInfo(int portType, int id)
  178. {
  179. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  180. .LeftJoin<Sys_SetData>((gi, sd) => gi.Type == sd.Id)
  181. .LeftJoin<Sys_Users>((gi, sd, u1) => gi.LastUpdateUserId == u1.Id)
  182. .LeftJoin<Sys_Users>((gi, sd, u1, u2) => gi.CreateUserId == u2.Id)
  183. .Where((gi, sd, u1, u2) => gi.IsDel == 0 && gi.Id == id)
  184. .Select((gi, sd, u1, u2) => new
  185. {
  186. gi.Id,
  187. gi.Name,
  188. ParentType = sd.STid,
  189. gi.Type,
  190. TypeName = sd.Name,
  191. gi.SQ_Total,
  192. gi.OQ_Total,
  193. gi.PriceTotal,
  194. gi.StockQuantity,
  195. gi.Unit,
  196. gi.Remark,
  197. LastUpdateUserName = u1.CnName,
  198. gi.LastUpdateTime,
  199. CreateUserName = u2.CnName,
  200. gi.CreateTime,
  201. })
  202. .FirstAsync();
  203. _jv.Code = StatusCodes.Status200OK;
  204. _jv.Data = data;
  205. _jv.Msg = $"操作成功";
  206. return _jv;
  207. }
  208. /// <summary>
  209. /// 物品 OP(Create Or Edit)
  210. /// </summary>
  211. /// <param name="dto"></param>
  212. /// <param name="currUserId"></param>
  213. /// <returns></returns>
  214. public async Task<JsonView> GoodsOp(GoodsOpDto dto, int currUserId)
  215. {
  216. var info = new Pm_GoodsInfo()
  217. {
  218. Id = dto.Id,
  219. Name = dto.Name,
  220. Type = dto.Type,
  221. SQ_Total = 0,
  222. OQ_Total = 0,
  223. PriceTotal = 0,
  224. StockQuantity = 0,
  225. Unit = dto.Unit,
  226. Remark = dto.Remark,
  227. LastUpdateUserId = currUserId,
  228. LastUpdateTime = DateTime.Now,
  229. CreateUserId = currUserId
  230. };
  231. if (dto.Id > 0) //Edit
  232. {
  233. var upd = await _sqlSugar.Updateable(info)
  234. .UpdateColumns(x => new
  235. {
  236. x.Name,
  237. x.Type,
  238. x.Unit,
  239. x.Remark,
  240. x.LastUpdateUserId,
  241. x.LastUpdateTime,
  242. })
  243. .ExecuteCommandAsync();
  244. if (upd > 0)
  245. {
  246. _jv.Msg = $"修改成功!";
  247. _jv.Code = StatusCodes.Status200OK;
  248. return _jv;
  249. }
  250. }
  251. else if (dto.Id < 1) //添加
  252. {
  253. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Name.Equals(info.Name));
  254. if (selectInfo != null)
  255. {
  256. _jv.Msg = $"“{info.Name}”该物品已存在,请勿重新添加!";
  257. return _jv;
  258. }
  259. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  260. if (add > 0)
  261. {
  262. _jv.Msg = $"添加成功!";
  263. _jv.Code = StatusCodes.Status200OK;
  264. return _jv;
  265. }
  266. }
  267. return _jv;
  268. }
  269. /// <summary>
  270. /// 物品 Del
  271. /// </summary>
  272. /// <param name="id"></param>
  273. /// <param name="currUserId"></param>
  274. /// <returns></returns>
  275. public async Task<JsonView> GoodsDel(int id, int currUserId)
  276. {
  277. _sqlSugar.BeginTran();
  278. var goods = await _sqlSugar.Updateable<Pm_GoodsInfo>()
  279. .SetColumns(x => new Pm_GoodsInfo()
  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. if (goods < 1)
  288. {
  289. _sqlSugar.RollbackTran();
  290. _jv.Msg = $"操作失败";
  291. return _jv;
  292. }
  293. var goodsStorage = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  294. .SetColumns(x => new Pm_GoodsStorage()
  295. {
  296. IsDel = 1,
  297. DeleteUserId = currUserId,
  298. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  299. })
  300. .Where(x => x.Id == id)
  301. .ExecuteCommandAsync();
  302. var goodsReceive = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  303. .SetColumns(x => new Pm_GoodsReceive()
  304. {
  305. IsDel = 1,
  306. DeleteUserId = currUserId,
  307. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  308. })
  309. .Where(x => x.Id == id)
  310. .ExecuteCommandAsync();
  311. _sqlSugar.CommitTran();
  312. _jv.Code = StatusCodes.Status200OK;
  313. _jv.Msg = $"操作成功!";
  314. return _jv;
  315. }
  316. /// <summary>
  317. /// 入库/出库 审核类型
  318. /// 根据物品类型 处理审核是否需要多部门审核
  319. /// </summary>
  320. /// <param name="goodsTypeId">物品类型Id </param>
  321. /// <returns></returns>
  322. public bool GoodsAuditType(int goodsTypeId)
  323. {
  324. if (_goodsTypeIds.Contains(goodsTypeId))
  325. {
  326. return true;
  327. }
  328. return false;
  329. }
  330. /// <summary>
  331. /// 物品入库列表(带审核)
  332. /// </summary>
  333. /// <param name="dto"></param>
  334. /// <returns></returns>
  335. public async Task<JsonView> GoodsStorageList(GoodsStorageListDto dto)
  336. {
  337. string reqAuditLabel = dto.AuditLabel;
  338. var auditLabel = Array.Empty<int>();
  339. int userId = dto.CurrUserId;
  340. if (!string.IsNullOrEmpty(reqAuditLabel))
  341. {
  342. if (!reqAuditLabel.Contains("-1"))
  343. {
  344. auditLabel = reqAuditLabel
  345. .Split(',')
  346. .Select(x =>
  347. {
  348. if (int.TryParse(x, out var id)) return id;
  349. return id;
  350. })
  351. .ToArray();
  352. }
  353. }
  354. var auditList = GoodsStorageConfirmAuditDep(1);
  355. var hrAuditPer = false;
  356. var finAuditPer = false;
  357. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  358. var finAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Financial);
  359. if (hrAuditInfo != null)
  360. {
  361. if (hrAuditInfo.AuditorIds.Any(x => x == userId)) hrAuditPer = true;
  362. }
  363. if (finAuditInfo != null)
  364. {
  365. if (finAuditInfo.AuditorIds.Any(x => x == userId)) finAuditPer = true;
  366. }
  367. RefAsync<int> total = 0;
  368. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  369. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  370. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  371. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  372. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  373. .WhereIF(dto.GoodsId > 0, (gs, gi, u, u1) => gs.GoodsId == dto.GoodsId)
  374. .WhereIF(auditLabel.Length > 0, (gs, gi, u, u1) => auditLabel.Contains((int)gs.ConfirmStatus))
  375. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gs, gi, u, u1) => gi.Name.Contains(dto.GoodsName))
  376. .WhereIF(!string.IsNullOrEmpty(dto.BatchNo), (gs, gi, u, u1) => gs.BatchNo.Contains(dto.BatchNo))
  377. .WhereIF(finAuditPer, (gs, gi, u, u1) => _goodsTypeIds.Contains(gi.Type))
  378. .Select((gs, gi, u, u1) => new GoodsStorageListView()
  379. {
  380. Id = gs.Id,
  381. GoodsId = gs.GoodsId,
  382. GoodsType = gi.Type,
  383. BatchNo = gs.BatchNo,
  384. GoodsName = gi.Name,
  385. Quantity = gs.Quantity,
  386. UnitPrice = gs.UnitPrice,
  387. TotalPrice = gs.TotalPrice,
  388. SupplierName = gs.SupplierName,
  389. SupplierTel = gs.SupplierTel,
  390. SupplierAddress = gs.SupplierAddress,
  391. SupplierSource = gs.SupplierSource,
  392. StorageUserName = u1.CnName,
  393. StorageTime = gs.StorageTime,
  394. CreateUserName = u.CnName,
  395. ConfirmStatus = gs.ConfirmStatus,
  396. StatusDesc = gs.StatusDesc,
  397. CreateTime = gs.CreateTime,
  398. Remark = gs.Remark
  399. })
  400. .OrderByDescending(gs => gs.CreateTime)
  401. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  402. foreach (var item in data)
  403. {
  404. var auditDeps = new List<GoodsStorageAuditPerView>
  405. {
  406. new() { AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = GoodsAuditDepEnum.Hr.GetEnumDescription() }
  407. };
  408. var auditPer = GoodsAuditType(item.GoodsType);
  409. if (auditPer)
  410. {
  411. auditDeps.Add(new() { AuditPer = finAuditPer, AuditDep = GoodsAuditDepEnum.Financial, ButtonText = GoodsAuditDepEnum.Financial.GetEnumDescription() });
  412. }
  413. item.AuditPers = auditDeps.ToArray();
  414. }
  415. _jv.Code = StatusCodes.Status200OK;
  416. _jv.Data = data;
  417. _jv.Count = total;
  418. _jv.Msg = $"操作成功";
  419. return _jv;
  420. }
  421. /// <summary>
  422. /// 物品入库详情
  423. /// </summary>
  424. /// <param name="id"></param>
  425. /// <returns></returns>
  426. public async Task<JsonView> GoodsStorageInfo(int portType, int id)
  427. {
  428. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  429. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  430. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  431. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  432. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  433. .WhereIF(id > 0, (gs, gi, u, u1) => gs.Id == id)
  434. .Select((gs, gi, u, u1) => new
  435. {
  436. gs.Id,
  437. gs.GoodsId,
  438. GoodsName = gi.Name,
  439. gs.Quantity,
  440. gs.UnitPrice,
  441. gs.TotalPrice,
  442. gs.SupplierName,
  443. gs.SupplierTel,
  444. gs.SupplierAddress,
  445. gs.SupplierSource,
  446. gs.ReceiveQuantity,
  447. gs.StorageUserId,
  448. StorageUser = u1.CnName,
  449. gs.StorageTime,
  450. CreateUserName = u.CnName,
  451. gs.CreateUserId,
  452. gs.CreateTime,
  453. gs.Remark,
  454. //gs.IsInConfirm
  455. })
  456. .FirstAsync();
  457. _jv.Msg = $"操作成功!";
  458. _jv.Code = StatusCodes.Status200OK;
  459. _jv.Data = data;
  460. return _jv;
  461. }
  462. /// <summary>
  463. /// 物品入库 操作(Create Or Edit)
  464. /// </summary>
  465. /// <param name="dto"></param>
  466. /// <param name="currUserId"></param>
  467. /// <returns></returns>
  468. public async Task<JsonView> GoodsStorageOp(GoodsStorageOpDto dto, int currUserId)
  469. {
  470. var info = _mapper.Map<Pm_GoodsStorage>(dto);
  471. info.CreateUserId = currUserId;
  472. info.BatchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff");
  473. var sign = dto.Id;
  474. _sqlSugar.BeginTran();
  475. if (info.Id > 0) //修改
  476. {
  477. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>().FirstAsync(x => x.Id == dto.Id);
  478. var auditStatus = selectInfo.ConfirmStatus;
  479. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  480. {
  481. _sqlSugar.RollbackTran();
  482. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可更改!如若更改请取消已确认或部分确认审核状态!";
  483. return _jv;
  484. }
  485. var storageEdit = await _sqlSugar.Updateable(info)
  486. .UpdateColumns(x => new
  487. {
  488. x.Quantity,
  489. x.UnitPrice,
  490. x.TotalPrice,
  491. x.SupplierName,
  492. x.SupplierTel,
  493. x.SupplierAddress,
  494. x.SupplierSource,
  495. x.StorageUserId,
  496. x.StorageTime
  497. })
  498. .Where(x => x.Id == dto.Id)
  499. .ExecuteCommandAsync();
  500. if (storageEdit < 1)
  501. {
  502. _sqlSugar.RollbackTran();
  503. _jv.Msg = $"修改失败!";
  504. return _jv;
  505. }
  506. //入库确认默认状态 多条 or 单条
  507. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.Type == 1 && x.DataId == dto.Id).ToListAsync();
  508. if (auditInfos.Any())
  509. {
  510. var isAuditPer = false;
  511. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.GoodsId);
  512. if (goodsInfo != null)
  513. {
  514. isAuditPer = GoodsAuditType(goodsInfo.Type);
  515. }
  516. if (!isAuditPer && auditInfos.Count == 1)
  517. {
  518. var auditInfo = new Pm_GoodsAudit(1, GoodsAuditDepEnum.Financial, dto.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  519. await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  520. }
  521. }
  522. }
  523. else if (info.Id < 1) //添加
  524. {
  525. info.ConfirmStatus = GoodsConfirmEnum.WaitConfirm;
  526. sign = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  527. if (sign < 1)
  528. {
  529. _sqlSugar.RollbackTran();
  530. _jv.Msg = $"添加失败!";
  531. return _jv;
  532. }
  533. //入库确认默认状态
  534. List<Pm_GoodsAudit> auditInfos = GoodsStorageConfirm(1, sign, info.CreateUserId);
  535. if (auditInfos.Any())
  536. {
  537. //验证添加多条或者单条审核状态
  538. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.GoodsId);
  539. if (goodsInfo != null)
  540. {
  541. var isAuditPer = GoodsAuditType(goodsInfo.Type);
  542. if (!isAuditPer) auditInfos = auditInfos.Where(x => x.Dep == GoodsAuditDepEnum.Hr).ToList();
  543. }
  544. await _sqlSugar.Insertable(auditInfos).ExecuteCommandAsync();
  545. }
  546. }
  547. _sqlSugar.CommitTran();
  548. _jv.Data = new { sign };
  549. _jv.Msg = $"操作成功!";
  550. _jv.Code = StatusCodes.Status200OK;
  551. return _jv;
  552. }
  553. /// <summary>
  554. /// 物品入库 确认操作-默认审核部门
  555. /// </summary>
  556. /// <param name="auditType">
  557. /// 审核类型
  558. /// 1.入库 2.出库
  559. /// </param>
  560. /// <param name="dataId">dataId</param>
  561. /// <param name="currUserId">审核人</param>
  562. /// <returns></returns>
  563. public List<Pm_GoodsAudit> GoodsStorageConfirm(int auditType, int dataId, int currUserId)
  564. {
  565. var goodsAuditList = new List<Pm_GoodsAudit>
  566. {
  567. new(auditType, GoodsAuditDepEnum.Hr, dataId, GoodsConfirmEnum.WaitConfirm, currUserId),
  568. new(auditType, GoodsAuditDepEnum.Financial, dataId, GoodsConfirmEnum.WaitConfirm, currUserId)
  569. };
  570. return goodsAuditList;
  571. }
  572. /// <summary>
  573. /// 物品入库 确认操作
  574. /// </summary>
  575. /// <param name="dto"></param>
  576. /// <returns></returns>
  577. public async Task<JsonView> GoodsStorageConfirmStatusChange(GoodsStorageConfirmDto dto, int currUserId)
  578. {
  579. int gsId = dto.Id;
  580. var auditDep = dto.AuditDep;
  581. if (gsId < 1)
  582. {
  583. _jv.Msg = string.Format("{0}", MsgTips.Id);
  584. return _jv;
  585. }
  586. if (currUserId < 1)
  587. {
  588. _jv.Msg = string.Format("{0}", MsgTips.UserId);
  589. return _jv;
  590. }
  591. //验证审核部门
  592. (bool auditPer, GoodsAuditDepEnum goodsAuditDep) = GoodsAuditDep(currUserId, auditDep, 1);
  593. if (!auditPer)
  594. {
  595. _jv.Msg = string.Format("未分配入库确认权限!");
  596. return _jv;
  597. }
  598. //入库确认 更改审核状态
  599. _sqlSugar.BeginTran();
  600. var info = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && x.Id == gsId).FirstAsync();
  601. if (info == null)
  602. {
  603. _jv.Msg = string.Format("入库信息不存在!");
  604. return _jv;
  605. }
  606. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.IsDel == 0 && x.Id == info.GoodsId).FirstAsync();
  607. if (goodsInfo == null)
  608. {
  609. _jv.Msg = string.Format("物品信息不存在!");
  610. return _jv;
  611. }
  612. int goodsTypeId = goodsInfo.Type;
  613. var preChangeStatus = info.ConfirmStatus;
  614. if (preChangeStatus == dto.ConfirmStatus)
  615. {
  616. _jv.Msg = string.Format("“{0}”已操作,请勿重复该操作!", dto.ConfirmStatus.GetEnumDescription());
  617. return _jv;
  618. }
  619. var preInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.IsDel == 0 && x.DataId == gsId).ToListAsync();
  620. //验证是否需要多级确认
  621. var isAuditPer = GoodsAuditType(goodsTypeId);
  622. //单部门审核时默认人事部审核
  623. if (!isAuditPer) goodsAuditDep = GoodsAuditDepEnum.Hr;
  624. var auditInfo = preInfos.FirstOrDefault(x => x.Dep == goodsAuditDep);
  625. if (auditInfo != null)
  626. {
  627. //移除部门审核状态
  628. preInfos.Remove(auditInfo);
  629. auditInfo.AuditStatus = dto.ConfirmStatus;
  630. auditInfo.AuditUserId = currUserId;
  631. auditInfo.AuditTime = DateTime.Now;
  632. var updStatus = await _sqlSugar.Updateable(auditInfo)
  633. .UpdateColumns(x => new { x.AuditStatus, x.AuditUserId, x.AuditTime })
  634. .ExecuteCommandAsync();
  635. if (updStatus < 1)
  636. {
  637. _sqlSugar.RollbackTran();
  638. _jv.Msg = string.Format("入库确认失败!");
  639. return _jv;
  640. }
  641. }
  642. else
  643. {
  644. auditInfo = new Pm_GoodsAudit(1, goodsAuditDep, gsId, dto.ConfirmStatus, currUserId, currUserId);
  645. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  646. if (addStatus < 1)
  647. {
  648. _sqlSugar.RollbackTran();
  649. _jv.Msg = string.Format("入库确认失败!");
  650. return _jv;
  651. }
  652. }
  653. preInfos.Add(auditInfo);
  654. //入库确认 更改入库状态及扣或增加除库存数、金额
  655. var confirmStatus = GoodsConfirmEnum.WaitConfirm;
  656. if (isAuditPer) //多级审核确认
  657. {
  658. if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 2)
  659. {
  660. confirmStatus = GoodsConfirmEnum.Confirmed;
  661. }
  662. else if (preInfos.Count(x => x.AuditStatus == GoodsConfirmEnum.Confirmed) >= 1)
  663. {
  664. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  665. }
  666. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.UnApproved).Count() > 0)
  667. {
  668. confirmStatus = GoodsConfirmEnum.UnApproved;
  669. }
  670. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.PartConfirmed).Count() > 0)
  671. {
  672. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  673. }
  674. }
  675. else //人事部审核确认
  676. {
  677. confirmStatus = GoodsConfirmEnum.Confirmed;
  678. confirmStatus = dto.ConfirmStatus;
  679. preInfos = preInfos.Where(x => x.Dep == GoodsAuditDepEnum.Hr).ToList();
  680. }
  681. //入库状态描述
  682. var statusDesc = new StringBuilder();
  683. foreach (var preInfo in preInfos)
  684. {
  685. string depName = preInfo.Dep.GetEnumDescription(),
  686. auditStatus = preInfo.AuditStatus.GetEnumDescription(),
  687. auditUserName = preInfo.AuditUserId > 0 ? _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == preInfo.AuditUserId)?.CnName ?? "-" : "-",
  688. auditTime = preInfo.AuditUserId > 0 ? preInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  689. statusDesc.Append(string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>", depName, auditStatus, auditUserName, auditTime));
  690. }
  691. //更改入库状态及描述
  692. info.ConfirmStatus = confirmStatus;
  693. info.StatusDesc = statusDesc.ToString();
  694. var goodsStorageUpd = await _sqlSugar.Updateable(info)
  695. .UpdateColumns(x => new
  696. {
  697. x.ConfirmStatus,
  698. x.StatusDesc,
  699. })
  700. .Where(x => x.Id == info.Id)
  701. .ExecuteCommandAsync();
  702. if (goodsStorageUpd < 1)
  703. {
  704. _sqlSugar.RollbackTran();
  705. _jv.Msg = $"入库确认状态更改失败!";
  706. return _jv;
  707. }
  708. //入库审核通过数量、金额
  709. decimal auditQuantity = info.Quantity,
  710. auditTotalPrice = info.TotalPrice;
  711. goodsInfo.LastUpdateUserId = currUserId;
  712. goodsInfo.LastUpdateTime = DateTime.Now;
  713. if (confirmStatus == GoodsConfirmEnum.Confirmed) // 确认状态
  714. {
  715. //更改后的状态和更改前的状态不一致时 更改库存数、金额
  716. if (preChangeStatus != confirmStatus) //
  717. {
  718. goodsInfo.SQ_Total += auditQuantity;
  719. goodsInfo.StockQuantity += auditQuantity;
  720. goodsInfo.PriceTotal += auditTotalPrice;
  721. }
  722. }
  723. else //其他状态 拒绝、部分确认、等待确认
  724. {
  725. //更改前状态为确认状态时,减库存数、金额
  726. if (preChangeStatus == GoodsConfirmEnum.Confirmed)
  727. {
  728. goodsInfo.SQ_Total -= auditQuantity;
  729. goodsInfo.StockQuantity -= auditQuantity;
  730. goodsInfo.PriceTotal -= auditTotalPrice;
  731. }
  732. }
  733. var goodsUpd = await _sqlSugar.Updateable(goodsInfo)
  734. .UpdateColumns(x => new
  735. {
  736. x.SQ_Total,
  737. x.StockQuantity,
  738. x.PriceTotal,
  739. x.LastUpdateUserId,
  740. x.LastUpdateTime,
  741. })
  742. .Where(x => x.Id == info.GoodsId)
  743. .ExecuteCommandAsync();
  744. if (goodsUpd > 0)
  745. {
  746. _sqlSugar.CommitTran();
  747. _jv.Msg = $"操作成功!";
  748. _jv.Code = StatusCodes.Status200OK;
  749. return _jv;
  750. }
  751. _sqlSugar.RollbackTran();
  752. _jv.Msg = $"操作失败!";
  753. return _jv;
  754. }
  755. /// <summary>
  756. /// 获取物品审核部门
  757. /// </summary>
  758. /// <param name="userId"> userId </param>
  759. /// <param name="auditDepEnum"> 审核部门枚举 </param>
  760. /// <param name="auditType">
  761. /// 指定审核类型(入库/出库)
  762. /// 1:入库审核
  763. /// 2:出库审核
  764. /// </param>
  765. /// <returns></returns>
  766. public static (bool, GoodsAuditDepEnum) GoodsAuditDep(int userId, GoodsAuditDepEnum auditDepEnum, int auditType = 1)
  767. {
  768. if (userId < 1) return (false, GoodsAuditDepEnum.Hr);
  769. var auditList = GoodsStorageConfirmAuditDep(1);
  770. if (auditType == 1)
  771. {
  772. if (auditList.Any(x => x.AuditDep == auditDepEnum && x.AuditorIds.Contains(userId)))
  773. {
  774. return (true, auditDepEnum);
  775. }
  776. }
  777. return (false, GoodsAuditDepEnum.Hr);
  778. }
  779. /// <summary>
  780. /// 物品审核部门列表
  781. /// </summary>
  782. /// <param name="auditType">
  783. /// 指定审核类型(入库/出库)
  784. /// 1:入库审核
  785. /// 2:出库审核
  786. /// </param>
  787. /// <returns></returns>
  788. public static List<GoodsAuditDepView> GoodsStorageConfirmAuditDep(int auditType = 1)
  789. {
  790. var auditList = new List<GoodsAuditDepView>();
  791. var hrAuditorIds = new GoodsAuditDepView()
  792. {
  793. AuditDep = GoodsAuditDepEnum.Hr,
  794. AuditorIds = new int[] {
  795. 309, // 赖红燕
  796. 343, // 陈湘
  797. //374, // 罗颖
  798. 208, // 雷怡
  799. }
  800. };
  801. var finAuditorIds = new GoodsAuditDepView()
  802. {
  803. AuditDep = GoodsAuditDepEnum.Financial,
  804. AuditorIds = new int[] {
  805. 187, // 曾艳
  806. 281, // 伏虹瑾
  807. 208, // 雷怡
  808. }
  809. };
  810. if (auditType == 1)//入库
  811. {
  812. hrAuditorIds.AuditorIds = new int[] {
  813. //343, // 陈湘
  814. 374, // 罗颖
  815. 208, // 雷怡
  816. };
  817. auditList.Add(hrAuditorIds);
  818. auditList.Add(finAuditorIds);
  819. }
  820. else if (auditType == 2) //出库
  821. {
  822. auditList.Add(hrAuditorIds);
  823. auditList.Add(finAuditorIds);
  824. }
  825. return auditList;
  826. }
  827. /// <summary>
  828. /// 物品入库 Del
  829. /// </summary>
  830. /// <param name="id"></param>
  831. /// <returns></returns>
  832. public async Task<JsonView> GoodsStorageDel(int id, int userId)
  833. {
  834. var storageInfo = await _sqlSugar
  835. .Queryable<Pm_GoodsStorage>()
  836. .Where(x => x.Id == id)
  837. .FirstAsync();
  838. if (storageInfo == null) return _jv;
  839. var auditStatus = storageInfo.ConfirmStatus;
  840. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  841. {
  842. _sqlSugar.RollbackTran();
  843. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可删除!如若删除请取消已确认或部分确认审核状态!";
  844. return _jv;
  845. }
  846. decimal delAgoQuantity = storageInfo.Quantity,
  847. delAgoTotalPrice = storageInfo.TotalPrice;
  848. var goodsId = storageInfo.GoodsId;
  849. _sqlSugar.BeginTran();
  850. var storageDel = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  851. .SetColumns(x => new Pm_GoodsStorage
  852. {
  853. DeleteUserId = userId,
  854. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  855. IsDel = 1
  856. })
  857. .Where(x => x.Id == id)
  858. .ExecuteCommandAsync();
  859. if (storageDel < 1)
  860. {
  861. _sqlSugar.RollbackTran();
  862. _jv.Msg = $"操作失败!";
  863. return _jv;
  864. }
  865. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == goodsId);
  866. goodsInfo.SQ_Total -= delAgoQuantity;
  867. goodsInfo.StockQuantity -= delAgoQuantity;
  868. goodsInfo.PriceTotal -= delAgoTotalPrice;
  869. goodsInfo.LastUpdateUserId = userId;
  870. goodsInfo.LastUpdateTime = DateTime.Now;
  871. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  872. .UpdateColumns(x => new
  873. {
  874. x.SQ_Total,
  875. x.StockQuantity,
  876. x.PriceTotal,
  877. x.LastUpdateUserId,
  878. x.LastUpdateTime,
  879. })
  880. .Where(x => x.Id == goodsId)
  881. .ExecuteCommandAsync();
  882. if (goodsEdit > 0)
  883. {
  884. _sqlSugar.CommitTran();
  885. _jv.Msg = $"操作成功!";
  886. _jv.Code = StatusCodes.Status200OK;
  887. return _jv;
  888. }
  889. _sqlSugar.RollbackTran();
  890. _jv.Msg = $"操作失败!";
  891. return _jv;
  892. }
  893. /// <summary>
  894. /// 物品入库
  895. /// excelDownload
  896. /// </summary>
  897. /// <param name="dto"></param>
  898. /// <returns></returns>
  899. public async Task<JsonView> GoodsStorageExcelDownload()
  900. {
  901. var fileName = $"物资入库{Guid.NewGuid()}.xlsx";
  902. var excelTempPath = $"{_excelPath}Template/物资入库Temp.xlsx";
  903. if (!File.Exists(excelTempPath))
  904. {
  905. _jv.Code = StatusCodes.Status204NoContent;
  906. _jv.Msg = $"该模板文件不存在!";
  907. return _jv;
  908. }
  909. _url = $"{_url}Office/Excel/GoodsFiles/";
  910. _excelPath = $"{_excelPath}GoodsFiles";
  911. if (!Directory.Exists(_excelPath))
  912. {
  913. Directory.CreateDirectory(_excelPath);
  914. }
  915. //入库记录
  916. var storageData = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  917. .LeftJoin<Sys_Users>((gs, su) => gs.StorageUserId == su.Id)
  918. .Where((gs, su) => gs.IsDel == 0)
  919. .Select((gs, su) => new
  920. {
  921. gs.GoodsId,
  922. gs.Quantity,
  923. su.CnName,
  924. gs.StorageTime
  925. })
  926. .ToListAsync();
  927. //出库记录
  928. var receiveData = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  929. .LeftJoin<Sys_Users>((gr, su) => gr.CreateUserId == su.Id)
  930. .Where((gr, su) => gr.IsDel == 0)
  931. .Select((gr, su) => new
  932. {
  933. gr.GoodsId,
  934. gr.Quantity,
  935. su.CnName,
  936. gr.CreateTime,
  937. gr.Reason
  938. })
  939. .ToListAsync();
  940. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  941. .Where(gi => gi.IsDel == 0)
  942. .Select(gi => new GoodsStorageExcelDownloadView
  943. {
  944. Id = gi.Id,
  945. Name = gi.Name,
  946. SQ_Total = gi.SQ_Total,
  947. OQ_Total = gi.OQ_Total,
  948. StockQuantity = gi.StockQuantity,
  949. Unit = gi.Unit
  950. })
  951. .ToListAsync();
  952. foreach (var item in data)
  953. {
  954. var storageData1 = storageData.Where(x => x.GoodsId == item.Id).ToList();
  955. if (storageData1.Any())
  956. {
  957. item.SQ_Total1 = storageData1.Sum(x => x.Quantity);
  958. item.SQ_Total -= item.SQ_Total1;
  959. item.StorageLabel = string.Join("\r\n", storageData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】 入库人:【" + x.CnName + "】 入库时间:【" + x.StorageTime + "】").ToList());
  960. }
  961. var receiveData1 = receiveData.Where(x => x.GoodsId == item.Id).ToList();
  962. if (receiveData1.Any())
  963. {
  964. 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());
  965. }
  966. }
  967. //载入模板
  968. WorkbookDesigner designer = new()
  969. {
  970. Workbook = new Workbook(excelTempPath)
  971. };
  972. designer.SetDataSource("Export", data);
  973. designer.Process();
  974. #region 渲染Cell批注
  975. var sheet = designer.Workbook.Worksheets[0];
  976. for (int i = 0; i < data.Count; i++)
  977. {
  978. string storageComment = $"C{i + 2}",
  979. receiveComment = $"D{i + 2}",
  980. storageCommentText = data[i].StorageLabel,
  981. receiveCommentText = data[i].ReceiveLabel;
  982. if (!string.IsNullOrEmpty(storageCommentText))
  983. {
  984. int storageIndex = sheet.Comments.Add(storageComment);
  985. Aspose.Cells.Comment comment = sheet.Comments[storageIndex];
  986. comment.Note = storageCommentText;
  987. comment.Width = 500;
  988. comment.Height = 200;
  989. //comment.Font.Color = Color.Red;
  990. }
  991. if (!string.IsNullOrEmpty(receiveCommentText))
  992. {
  993. int receiveIndex = sheet.Comments.Add(receiveComment);
  994. Aspose.Cells.Comment comment = sheet.Comments[receiveIndex];
  995. comment.Note = receiveCommentText;
  996. comment.Width = 800;
  997. comment.Height = 200;
  998. //comment.Font.Color = Color.Red;
  999. }
  1000. }
  1001. #endregion
  1002. string serverPath = $"{_url}{fileName}";
  1003. designer.Workbook.Save($"{_excelPath}/{fileName}");
  1004. _jv.Code = StatusCodes.Status200OK;
  1005. _jv.Data = new { url = serverPath };
  1006. _jv.Msg = $"操作成功";
  1007. return _jv;
  1008. }
  1009. /// <summary>
  1010. /// 物品领用列表
  1011. /// </summary>
  1012. /// <param name="dto"></param>
  1013. /// <returns></returns>
  1014. public async Task<JsonView> GoodsReceiveList(GoodsReceiveListDTO dto)
  1015. {
  1016. //参数处理
  1017. int[] typeLabel = Array.Empty<int>(),
  1018. userLabel = Array.Empty<int>(),
  1019. auditLabel = Array.Empty<int>(),
  1020. groupLabel = Array.Empty<int>();
  1021. string[] userNameLabel = Array.Empty<string>();
  1022. int currUserId = dto.CurrUserId;
  1023. if (!string.IsNullOrEmpty(dto.TypeLabel))
  1024. {
  1025. typeLabel = dto.TypeLabel
  1026. .Split(',')
  1027. .Select(x =>
  1028. {
  1029. if (int.TryParse(x, out var id)) return id;
  1030. return id;
  1031. })
  1032. .ToArray();
  1033. }
  1034. if (!string.IsNullOrEmpty(dto.UserLabel))
  1035. {
  1036. userLabel = dto.UserLabel
  1037. .Split(',')
  1038. .Select(x =>
  1039. {
  1040. if (int.TryParse(x, out var id)) return id;
  1041. return id;
  1042. })
  1043. .ToArray();
  1044. if (userLabel.Any())
  1045. {
  1046. userNameLabel = await _sqlSugar.Queryable<Sys_Users>().Where(x => userLabel.Contains(x.Id)).Select(x => x.CnName).ToArrayAsync();
  1047. }
  1048. }
  1049. if (!string.IsNullOrEmpty(dto.AuditLabel))
  1050. {
  1051. auditLabel = dto.AuditLabel
  1052. .Split(',')
  1053. .Select(x =>
  1054. {
  1055. if (int.TryParse(x, out var id)) return id;
  1056. return id;
  1057. })
  1058. .ToArray();
  1059. if (auditLabel.Any(x => x == -1))
  1060. {
  1061. auditLabel = auditLabel.Where(x => x != -1).ToArray();
  1062. }
  1063. }
  1064. if (!string.IsNullOrEmpty(dto.GroupLabel))
  1065. {
  1066. groupLabel = dto.GroupLabel
  1067. .Split(',')
  1068. .Select(x =>
  1069. {
  1070. if (int.TryParse(x, out var id)) return id;
  1071. return id;
  1072. })
  1073. .ToArray();
  1074. }
  1075. //全部团组
  1076. bool isAllGroups = false;
  1077. if (groupLabel.Contains(-3)) isAllGroups = true;
  1078. //物品ID和物品名称只能传一个
  1079. if (dto.GoodsId > 0) dto.GoodsName = string.Empty;
  1080. if (!string.IsNullOrEmpty(dto.GoodsName)) dto.GoodsId = 0;
  1081. if (currUserId == 343) //陈湘OAId登录 只显示贵重物品审核信息
  1082. {
  1083. if (_goodsTypeIds.Any())
  1084. {
  1085. var newArray = typeLabel.ToList();
  1086. newArray.AddRange(_goodsTypeIds);
  1087. typeLabel = newArray.ToArray();
  1088. }
  1089. }
  1090. var beginBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.BeginDt) ? $"{dto.BeginDt} 00:00:00" : string.Empty, out var begin);
  1091. var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
  1092. //新旧领用数据整合
  1093. var oldDatas = _sqlSugar.Queryable<Pm_GoodsReceive>()
  1094. .InnerJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1095. .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
  1096. .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
  1097. .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  1098. .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  1099. .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0 && gr.GoodsId > 0)
  1100. .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  1101. {
  1102. Id = gr.Id,
  1103. GroupId = gr.GroupId,
  1104. GroupName = di.TeamName,
  1105. GoodsId = gr.GoodsId,
  1106. GoodsName = gi.Name,
  1107. GoodsTypeId = gi.Type,
  1108. GoodsType = sd.Name,
  1109. Quantity = gr.Quantity,
  1110. Unit = gi.Unit,
  1111. Reason = gr.Reason,
  1112. Remark = gr.Remark,
  1113. AuditStatus = gr.AuditStatus,
  1114. StatusDesc = gr.StatusDesc,
  1115. AuditUserId = gr.AuditUserId,
  1116. AuditUserName = u1.CnName,
  1117. AuditTime = gr.AuditTime,
  1118. CreateUserName = u2.CnName,
  1119. CreateTime = gr.CreateTime
  1120. });
  1121. var newDatas = _sqlSugar.Queryable<Pm_GoodsReceive>()
  1122. .LeftJoin<Pm_GoodsReceiveDetails>((gr, grd) => gr.Id == grd.GoodsReceiveId)
  1123. .LeftJoin<Pm_GoodsInfo>((gr, grd, gi) => grd.GoodsId == gi.Id)
  1124. .LeftJoin<Sys_SetData>((gr, grd, gi, sd) => gi.Type == sd.Id)
  1125. .LeftJoin<Sys_Users>((gr, grd, gi, sd, u1) => gr.AuditUserId == u1.Id)
  1126. .LeftJoin<Sys_Users>((gr, grd, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  1127. .LeftJoin<Grp_DelegationInfo>((gr, grd, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  1128. .Where((gr, grd, gi, sd, u1, u2, di) => gr.IsDel == 0 && grd.IsDel == 0 && gr.GoodsId < 1)
  1129. .Select((gr, grd, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  1130. {
  1131. Id = gr.Id,
  1132. GroupId = gr.GroupId,
  1133. GroupName = di.TeamName,
  1134. GoodsId = grd.GoodsId,
  1135. GoodsName = gi.Name,
  1136. GoodsTypeId = gi.Type,
  1137. GoodsType = sd.Name,
  1138. Quantity = grd.Quantity,
  1139. Unit = gi.Unit,
  1140. Reason = gr.Reason,
  1141. Remark = gr.Remark,
  1142. AuditStatus = gr.AuditStatus,
  1143. StatusDesc = gr.StatusDesc,
  1144. AuditUserId = gr.AuditUserId,
  1145. AuditUserName = u1.CnName,
  1146. AuditTime = gr.AuditTime,
  1147. CreateUserName = u2.CnName,
  1148. CreateTime = gr.CreateTime
  1149. });
  1150. var isIdSelect = false;
  1151. var isNameSelect = false;
  1152. if (dto.GoodsId > 0)
  1153. {
  1154. isIdSelect = true;
  1155. dto.GoodsName = string.Empty;
  1156. }
  1157. if (!string.IsNullOrEmpty(dto.GoodsName))
  1158. {
  1159. isIdSelect = false;
  1160. isNameSelect = true;
  1161. }
  1162. RefAsync<int> total = 0;
  1163. var data = _sqlSugar.UnionAll(oldDatas, newDatas)
  1164. .WhereIF(isIdSelect && !isNameSelect, x => x.GoodsId == dto.GoodsId)
  1165. .WhereIF(!isIdSelect && isNameSelect, x => x.GoodsName.Contains(dto.GoodsName))
  1166. .WhereIF(auditLabel.Length > 0, x => auditLabel.Contains((int)x.AuditStatus))
  1167. .WhereIF(typeLabel.Length > 0, x => typeLabel.Contains(x.GoodsTypeId))
  1168. .WhereIF(userNameLabel.Length > 0, x => userNameLabel.Contains(x.CreateUserName))
  1169. .WhereIF(!isAllGroups && groupLabel.Length > 0, x => groupLabel.Contains(x.GroupId))
  1170. .WhereIF(isAllGroups, x => x.GroupId > 0)
  1171. .WhereIF(beginBool && endBool, x => x.CreateTime >= begin && x.CreateTime <= end)
  1172. .OrderByDescending(x => x.CreateTime);
  1173. //excel导出
  1174. if (dto.IsExcelDownload)
  1175. {
  1176. var fileName = $"物资领用{Guid.NewGuid()}.xlsx";
  1177. var excelTempPath = $"{_excelPath}Template/物资领用Temp.xlsx";
  1178. if (!File.Exists(excelTempPath))
  1179. {
  1180. _jv.Code = StatusCodes.Status204NoContent;
  1181. _jv.Msg = $"该模板文件不存在!";
  1182. return _jv;
  1183. }
  1184. _url = $"{_url}Office/Excel/GoodsFiles/";
  1185. _excelPath = $"{_excelPath}GoodsFiles";
  1186. if (!Directory.Exists(_excelPath))
  1187. {
  1188. Directory.CreateDirectory(_excelPath);
  1189. }
  1190. //载入模板
  1191. WorkbookDesigner designer = new()
  1192. {
  1193. Workbook = new Workbook(excelTempPath)
  1194. };
  1195. var tableData = await data.ToListAsync();
  1196. foreach (var item in tableData)
  1197. {
  1198. var oldGroupName = item.GroupName;
  1199. item.GroupName = item.GroupId switch
  1200. {
  1201. 0 => "其他物资(公司内部物资)",
  1202. -1 => "拜访客户所使用的物资",
  1203. -2 => "库存调整",
  1204. _ => oldGroupName
  1205. };
  1206. //审核相关处理
  1207. if (string.IsNullOrEmpty(item.AuditUserName))
  1208. {
  1209. var oldAuditDatas = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1210. .LeftJoin<Sys_Users>((x, u) => x.AuditUserId == u.Id)
  1211. .Where((x, u) => x.IsDel == 0 && x.Type == 2 && x.DataId == item.Id)
  1212. .Select((x, u) => new
  1213. {
  1214. x.AuditTime,
  1215. u.CnName
  1216. })
  1217. .ToListAsync();
  1218. if (oldAuditDatas.Any())
  1219. {
  1220. item.AuditUserName = string.Join("、", oldAuditDatas.Select(x => x.CnName).ToArray());
  1221. if (!string.IsNullOrEmpty(item.AuditUserName))
  1222. {
  1223. item.AuditTime = oldAuditDatas.Last().AuditTime;
  1224. }
  1225. else item.AuditTime = DateTime.Now;
  1226. }
  1227. else
  1228. {
  1229. var newAuditDatas = await _sqlSugar.Queryable<Sys_AuditRecord>()
  1230. .InnerJoin<Sys_AuditFlow>((x, y) => x.FlowId == y.Id)
  1231. .Where((x, y) => x.IsDel == 0 && y.BusinessType == 1 && y.BusinessId == item.Id)
  1232. .Select((x, y) => new
  1233. {
  1234. x.AuditTime,
  1235. x.AuditorName
  1236. })
  1237. .ToListAsync();
  1238. if (newAuditDatas.Any())
  1239. {
  1240. item.AuditUserName = string.Join("、", newAuditDatas.Select(x => x.AuditorName).Distinct().ToArray());
  1241. if (!string.IsNullOrEmpty(item.AuditUserName))
  1242. {
  1243. item.AuditTime = newAuditDatas.Last().AuditTime;
  1244. }
  1245. else item.AuditTime = DateTime.Now;
  1246. }
  1247. }
  1248. }
  1249. }
  1250. designer.SetDataSource("Export", tableData);
  1251. designer.Process();
  1252. string serverPath = $"{_url}{fileName}";
  1253. designer.Workbook.Save($"{_excelPath}/{fileName}");
  1254. _jv.Code = StatusCodes.Status200OK;
  1255. _jv.Data = new { url = serverPath };
  1256. _jv.Msg = $"操作成功";
  1257. return _jv;
  1258. }
  1259. //返回分页数据
  1260. var view = await data.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  1261. foreach (var item in view)
  1262. {
  1263. //团组名称处理
  1264. var oldGroupName = item.GroupName;
  1265. item.GroupName = item.GroupId switch
  1266. {
  1267. 0 => "其他物资(公司内部物资)",
  1268. -1 => "拜访客户所使用的物资",
  1269. -2 => "库存调整",
  1270. _ => oldGroupName
  1271. };
  1272. //审核相关处理
  1273. if (string.IsNullOrEmpty(item.AuditUserName))
  1274. {
  1275. var oldAuditDatas = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1276. .LeftJoin<Sys_Users>((x, u) => x.AuditUserId == u.Id)
  1277. .Where((x, u) => x.IsDel == 0 && x.Type == 2 && x.DataId == item.Id)
  1278. .Select((x, u) => new
  1279. {
  1280. x.AuditTime,
  1281. u.CnName
  1282. })
  1283. .ToListAsync();
  1284. if (oldAuditDatas.Any())
  1285. {
  1286. item.AuditUserName = string.Join("、", oldAuditDatas.Select(x => x.CnName).ToArray());
  1287. if (!string.IsNullOrEmpty(item.AuditUserName))
  1288. {
  1289. item.AuditTime = oldAuditDatas.Last().AuditTime;
  1290. }
  1291. else item.AuditTime = DateTime.Now;
  1292. }
  1293. else
  1294. {
  1295. var newAuditDatas = await _sqlSugar.Queryable<Sys_AuditRecord>()
  1296. .InnerJoin<Sys_AuditFlow>((x, y) => x.FlowId == y.Id)
  1297. .Where((x, y) => x.IsDel == 0 && y.BusinessType == 1 && y.BusinessId == item.Id)
  1298. .Select((x, y) => new
  1299. {
  1300. x.AuditTime,
  1301. x.AuditorName
  1302. })
  1303. .ToListAsync();
  1304. if (newAuditDatas.Any())
  1305. {
  1306. item.AuditUserName = string.Join("、", newAuditDatas.Select(x => x.AuditorName).Distinct().ToArray());
  1307. if (!string.IsNullOrEmpty(item.AuditUserName))
  1308. {
  1309. item.AuditTime = newAuditDatas.Last().AuditTime;
  1310. }
  1311. else item.AuditTime = DateTime.Now;
  1312. }
  1313. }
  1314. }
  1315. //权限前台验证
  1316. var auditPers = new List<GoodsStorageAuditPerView>
  1317. {
  1318. new() { AuditPer = true, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = GoodsAuditDepEnum.Hr_Reception.GetDescription() }
  1319. };
  1320. if (_goodsTypeIds.Contains(item.GoodsTypeId))
  1321. {
  1322. auditPers.Add(new() { AuditPer = true, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = GoodsAuditDepEnum.Hr.GetDescription() });
  1323. }
  1324. item.AuditPers = auditPers.ToArray();
  1325. }
  1326. if (dto.PortType == 2 || dto.PortType == 3)
  1327. {
  1328. _jv.Data = view;
  1329. }
  1330. else if (dto.PortType == 1)
  1331. {
  1332. var view1 = _mapper.Map<List<GoodsReceiveListView>>(view);
  1333. _jv.Data = view1;
  1334. }
  1335. _jv.Code = StatusCodes.Status200OK;
  1336. _jv.Count = total;
  1337. _jv.Msg = $"操作成功";
  1338. return _jv;
  1339. }
  1340. /// <summary>
  1341. /// 物品领用详情
  1342. /// </summary>
  1343. /// <param name="portType"></param>
  1344. /// <param name="id"></param>
  1345. /// <returns></returns>
  1346. public async Task<JsonView> GoodsReceiveInfo(int portType, int id)
  1347. {
  1348. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  1349. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1350. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  1351. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  1352. .LeftJoin<Grp_DelegationInfo>((gr, gi, u1, u2, di) => gr.GroupId == di.Id)
  1353. .Where((gr, gi, u1, u2, di) => gr.IsDel == 0)
  1354. .WhereIF(id > 0, (gr, gi, u1, u2, di) => gr.Id == id)
  1355. .Select((gr, gi, u1, u2, di) => new GoodsReceiveInfoMobileView
  1356. {
  1357. Id = gr.Id,
  1358. GroupId = gr.GroupId,
  1359. GroupName = di.TeamName,
  1360. GoodsId = gr.GoodsId,
  1361. GoodsName = gi.Name,
  1362. Quantity = gr.Quantity,
  1363. Reason = gr.Reason,
  1364. Remark = gr.Remark,
  1365. GoodsStorageInfo = gr.GoodsStorageInfo,
  1366. AuditStatus = gr.AuditStatus,
  1367. AuditUserId = gr.AuditUserId,
  1368. AuditUserName = u1.CnName,
  1369. AuditTime = gr.AuditTime,
  1370. CreateUserName = u2.CnName,
  1371. CreateTime = gr.CreateTime
  1372. })
  1373. .FirstAsync();
  1374. if (!string.IsNullOrEmpty(data.GoodsStorageInfo))
  1375. {
  1376. var subData = new List<dynamic>();
  1377. try
  1378. {
  1379. var subData1 = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(data.GoodsStorageInfo);
  1380. if (subData1.Count > 0)
  1381. {
  1382. string goodsStorageInfoStr = string.Empty;
  1383. var storageIds = subData1.Select(x => x.StorageId).ToList();
  1384. var storages = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && storageIds.Contains(x.Id)).ToListAsync();
  1385. foreach (var item in subData1)
  1386. {
  1387. var storageInfo = storages.Find(x => x.Id == item.StorageId);
  1388. if (storageInfo != null)
  1389. {
  1390. subData.Add(new
  1391. {
  1392. item.StorageId,
  1393. storageInfo.BatchNo,
  1394. RecsiveQuantity = item.Quantity
  1395. });
  1396. goodsStorageInfoStr += $"物品名称:{data.GoodsName} 批次号:{storageInfo.BatchNo} 领用数量:{item.Quantity} \r\n";
  1397. }
  1398. }
  1399. data.QuantityInfos = subData;
  1400. data.GoodsStorageInfoStr = goodsStorageInfoStr;
  1401. }
  1402. }
  1403. catch (Exception e)
  1404. {
  1405. Console.WriteLine(e);
  1406. }
  1407. }
  1408. _jv.Code = StatusCodes.Status200OK;
  1409. _jv.Msg = $"操作成功";
  1410. if (portType == 2 || portType == 3) //移动端
  1411. {
  1412. _jv.Data = data;
  1413. }
  1414. else if (portType == 1) //pc端
  1415. {
  1416. _jv.Data = _mapper.Map<GoodsReceiveInfoView>(data);
  1417. }
  1418. return _jv;
  1419. }
  1420. /// <summary>
  1421. /// 物品领用 OP(Add Or Edit)
  1422. /// </summary>
  1423. /// <param name="dto"></param>
  1424. /// <param name="currUserId"></param>
  1425. /// <returns></returns>
  1426. public async Task<JsonView> GoodsReceiveOp(GoodsReceiveOpDto dto, int currUserId)
  1427. {
  1428. var info = _mapper.Map<Pm_GoodsReceive>(dto);
  1429. info.CreateUserId = currUserId;
  1430. var auditEnums = new List<GoodsAuditEnum>() {
  1431. GoodsAuditEnum.Approved, // 1
  1432. GoodsAuditEnum.UnApproved, // 2
  1433. GoodsAuditEnum.OutConfirmed,// 5
  1434. GoodsAuditEnum.OutRejected // 6
  1435. };
  1436. _sqlSugar.BeginTran();
  1437. //物品现有库存
  1438. var goodsInfo = _sqlSugar.Queryable<Pm_GoodsInfo>().First(x => x.Id == info.GoodsId);
  1439. var stockQuantity = goodsInfo?.StockQuantity ?? 0.00M;
  1440. //待审核 该物品数量
  1441. var waitAuditQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>().Where(x => x.IsDel == 0 &&
  1442. x.GoodsId == dto.GoodsId &&
  1443. !auditEnums.Contains(x.AuditStatus)
  1444. ).SumAsync(x => x.Quantity);
  1445. //验证默认审核 多条 or 单条
  1446. //出库确认默认审核状态
  1447. var isAuditPer = GoodsAuditType(goodsInfo?.Type ?? 0);
  1448. //验证默认审核 多条审核时添加
  1449. var goodsAuditInfo1 = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr_Reception, info.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  1450. var goodsAuditInfo2 = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr, info.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  1451. var goodsAuditInfos = new List<Pm_GoodsAudit>() { goodsAuditInfo1 };
  1452. //状态描述
  1453. StringBuilder stringBuilder = new();
  1454. stringBuilder.Append($"领用确认:状态:待确认 审核人:- 审核时间:-;<br/>");
  1455. if (isAuditPer)
  1456. {
  1457. goodsAuditInfos.Add(goodsAuditInfo2);
  1458. stringBuilder.Append($"人事部:状态:待确认 审核人:- 审核时间:-;<br/>");
  1459. }
  1460. info.StatusDesc = stringBuilder.ToString();
  1461. if (info.Id > 0) //修改
  1462. {
  1463. //审核验证
  1464. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == info.Id);
  1465. if (auditEnums.Contains(selectInfo.AuditStatus))
  1466. {
  1467. _sqlSugar.RollbackTran();
  1468. _jv.Msg = $"该条数据已执行操作({selectInfo.AuditStatus.GetEnumDescription()}),不可更改!";
  1469. return _jv;
  1470. }
  1471. //物品数量验证
  1472. var editAfterQuantity = waitAuditQuantity - selectInfo.Quantity + info.Quantity;
  1473. if (editAfterQuantity > stockQuantity)
  1474. {
  1475. _sqlSugar.RollbackTran();
  1476. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1477. return _jv;
  1478. }
  1479. var edit = await _sqlSugar.Updateable(info)
  1480. .UpdateColumns(x => new
  1481. {
  1482. x.GroupId,
  1483. x.Quantity,
  1484. x.Reason,
  1485. x.Remark,
  1486. x.StatusDesc,
  1487. })
  1488. .Where(x => x.Id == info.Id)
  1489. .ExecuteCommandAsync();
  1490. if (edit > 0)
  1491. {
  1492. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.DataId == info.Id).ToListAsync();
  1493. if (!auditInfos.Any()) await _sqlSugar.Insertable(goodsAuditInfos).ExecuteCommandAsync();
  1494. _sqlSugar.CommitTran();
  1495. _jv.Msg = $"操作成功!";
  1496. _jv.Code = StatusCodes.Status200OK;
  1497. return _jv;
  1498. }
  1499. }
  1500. else if (info.Id < 1) //添加
  1501. {
  1502. //物品数量验证
  1503. decimal addAgoQuantity = waitAuditQuantity + info.Quantity;
  1504. if (addAgoQuantity > stockQuantity)
  1505. {
  1506. _sqlSugar.RollbackTran();
  1507. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1508. return _jv;
  1509. }
  1510. var add = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  1511. if (add > 0)
  1512. {
  1513. goodsAuditInfos.ForEach(x => x.DataId = add);
  1514. await _sqlSugar.Insertable(goodsAuditInfos).ExecuteCommandAsync();
  1515. _sqlSugar.CommitTran();
  1516. _jv.Msg = $"操作成功!";
  1517. _jv.Code = StatusCodes.Status200OK;
  1518. return _jv;
  1519. }
  1520. }
  1521. _sqlSugar.RollbackTran();
  1522. return _jv;
  1523. }
  1524. /// <summary>
  1525. /// 物品领用 Audit
  1526. /// </summary>
  1527. /// <param name="idArray"></param>
  1528. /// <param name="userId"></param>
  1529. /// <param name="auditEnum"></param>
  1530. /// <returns></returns>
  1531. public async Task<JsonView> GoodsReceiveAudit(int[] idArray, int userId, GoodsAuditEnum auditEnum)
  1532. {
  1533. if (idArray.Length < 1) return _jv;
  1534. if (!Enum.IsDefined(typeof(GoodsAuditEnum), (int)auditEnum))
  1535. {
  1536. _jv.Msg = $"出库确认状态超出可用范围!";
  1537. return _jv;
  1538. }
  1539. int receiveId = idArray[0];
  1540. var currUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == userId)?.CnName ?? "-";
  1541. _sqlSugar.BeginTran();
  1542. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.IsDel == 0 && receiveId == x.Id);
  1543. if (receiveInfo == null)
  1544. {
  1545. _sqlSugar.RollbackTran();
  1546. _jv.Msg = $"当前领用信息不存在!";
  1547. return _jv;
  1548. }
  1549. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  1550. int goodTypeId = goodsInfo?.Type ?? 0;
  1551. //审核前状态
  1552. var preChangeStatus = receiveInfo.AuditStatus;
  1553. //状态验证
  1554. //if (preChangeStatus == auditEnum)
  1555. //{
  1556. // _sqlSugar.RollbackTran();
  1557. // _jv.Msg = $"该条数据状态已设置,不可重复设置!";
  1558. // return _jv;
  1559. //}
  1560. var isAuditPer = GoodsAuditType(goodTypeId);
  1561. if (isAuditPer) //多人审核
  1562. {
  1563. var auditDep = GoodsAuditDepEnum.Hr_Reception;
  1564. var auditInfo = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1565. .FirstAsync(x => x.Type == 2 && x.DataId == receiveInfo.Id && x.Dep == GoodsAuditDepEnum.Hr_Reception);
  1566. if (auditInfo != null && auditInfo.AuditStatus == GoodsConfirmEnum.Confirmed)
  1567. {
  1568. auditDep = GoodsAuditDepEnum.Hr;
  1569. //多人审核的时候验证审核权限
  1570. var auditList = GoodsStorageConfirmAuditDep(2);
  1571. var auditDepInfo = auditList.Find(x => x.AuditorIds.Contains(userId));
  1572. if (auditDepInfo == null)
  1573. {
  1574. _jv.Msg = string.Format("未分配出库确认权限!");
  1575. _sqlSugar.RollbackTran();
  1576. return _jv;
  1577. }
  1578. }
  1579. // 确认中 确认 拒绝
  1580. _jv = await GoodsReceiveOutConfirmingMultiple(receiveInfo, userId, auditDep, auditEnum);
  1581. //switch (auditEnum)
  1582. //{
  1583. // case GoodsAuditEnum.Pending: //领用待确认(取消确认、取消拒绝)
  1584. // _jv = await GoodsReceivePending(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1585. // break;
  1586. // case GoodsAuditEnum.Approved: //领用确认
  1587. // _jv = await GoodsReceiveApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1588. // break;
  1589. // case GoodsAuditEnum.UnApproved: //领用拒绝
  1590. // _jv = await GoodsReceiveUnApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1591. // break;
  1592. // case GoodsAuditEnum.OutPending: //出库待确认(取消出库确认、取消出库拒绝)
  1593. // _jv = await GoodsReceiveOutPending(preChangeStatus, receiveInfo, userId, auditDep, auditEnum);
  1594. // break;
  1595. // case GoodsAuditEnum.OutConfirmed: //出库确认
  1596. // _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1597. // break;
  1598. // case GoodsAuditEnum.OutRejected: //出库拒绝
  1599. // _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1600. // break;
  1601. //}
  1602. }
  1603. else //单人审核
  1604. {
  1605. //领用确认 --> 已确认 / 领用拒绝 --> 已拒绝
  1606. _jv = await GoodsReceiveOutConfirmingSingle(receiveInfo, userId, currUserName, auditEnum);
  1607. }
  1608. if (_jv.Code == StatusCodes.Status200OK)
  1609. {
  1610. _sqlSugar.CommitTran();
  1611. _jv.Msg = $"操作成功!";
  1612. _jv.Data = auditEnum;
  1613. return _jv;
  1614. }
  1615. _sqlSugar.RollbackTran();
  1616. return _jv;
  1617. }
  1618. #region 多人审核状态变更
  1619. /// <summary>
  1620. /// 物品领用状态 - 领用待确认(取消确认、取消拒绝)
  1621. /// </summary>
  1622. /// <param name="preChangeStatus">更改前状态</param>
  1623. /// <param name="receiveId"></param>
  1624. /// <param name="userId"></param>
  1625. /// <param name="userName"></param>
  1626. /// <param name="auditEnum"></param>
  1627. /// <returns></returns>
  1628. public async Task<JsonView> GoodsReceivePending(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1629. {
  1630. _jv.Code = StatusCodes.Status400BadRequest;
  1631. //领用待确认 操作范围
  1632. var audirEnumPers = new List<GoodsAuditEnum>() {
  1633. GoodsAuditEnum.Approved,
  1634. GoodsAuditEnum.UnApproved
  1635. };
  1636. if (!audirEnumPers.Contains(preChangeStatus))
  1637. {
  1638. _jv.Msg = $"{GoodsAuditEnum.Approved.GetEnumDescription()}、{GoodsAuditEnum.UnApproved.GetEnumDescription()},状态下可取消操作!";
  1639. return _jv;
  1640. }
  1641. var currUserOpDt = DateTime.Now;
  1642. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1643. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1644. auditEnum = GoodsAuditEnum.Pending;
  1645. var changeStatus = await _sqlSugar
  1646. .Updateable<Pm_GoodsReceive>()
  1647. .SetColumns(x => new Pm_GoodsReceive()
  1648. {
  1649. AuditStatus = auditEnum,
  1650. AuditUserId = userId,
  1651. AuditTime = currUserOpDt,
  1652. StatusDesc = statusDesc
  1653. })
  1654. .Where(x => x.Id == receiveId)
  1655. .ExecuteCommandAsync();
  1656. if (changeStatus > 0)
  1657. {
  1658. _jv.Code = StatusCodes.Status200OK;
  1659. }
  1660. return _jv;
  1661. }
  1662. /// <summary>
  1663. /// 物品领用状态 - 领用确认
  1664. /// </summary>
  1665. /// <param name="preChangeStatus">更改前状态</param>
  1666. /// <param name="receiveId"></param>
  1667. /// <param name="userId"></param>
  1668. /// <param name="userName"></param>
  1669. /// <param name="auditEnum"></param>
  1670. /// <returns></returns>
  1671. public async Task<JsonView> GoodsReceiveApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1672. {
  1673. _jv.Code = StatusCodes.Status400BadRequest;
  1674. if (preChangeStatus != GoodsAuditEnum.Pending)
  1675. {
  1676. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1677. return _jv;
  1678. }
  1679. var currUserOpDt = DateTime.Now;
  1680. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1681. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1682. auditEnum = GoodsAuditEnum.OutPending;
  1683. var changeStatus = await _sqlSugar
  1684. .Updateable<Pm_GoodsReceive>()
  1685. .SetColumns(x => new Pm_GoodsReceive()
  1686. {
  1687. AuditStatus = auditEnum,
  1688. AuditUserId = userId,
  1689. AuditTime = currUserOpDt,
  1690. StatusDesc = statusDesc
  1691. })
  1692. .Where(x => x.Id == receiveId)
  1693. .ExecuteCommandAsync();
  1694. if (changeStatus > 0)
  1695. {
  1696. _jv.Code = StatusCodes.Status200OK;
  1697. }
  1698. return _jv;
  1699. }
  1700. /// <summary>
  1701. /// 物品领用状态 - 领用拒绝
  1702. /// </summary>
  1703. /// <param name="preChangeStatus">更改前状态</param>
  1704. /// <param name="receiveId"></param>
  1705. /// <param name="userId"></param>
  1706. /// <param name="userName"></param>
  1707. /// <param name="auditEnum"></param>
  1708. /// <returns></returns>
  1709. public async Task<JsonView> GoodsReceiveUnApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1710. {
  1711. _jv.Code = StatusCodes.Status400BadRequest;
  1712. if (preChangeStatus != GoodsAuditEnum.Pending)
  1713. {
  1714. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1715. return _jv;
  1716. }
  1717. auditEnum = GoodsAuditEnum.UnApproved;
  1718. var currUserOpDt = DateTime.Now;
  1719. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1720. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部认:状态:待确认 审核人:- 审核时间:-;<br/>", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1721. var changeStatus = await _sqlSugar
  1722. .Updateable<Pm_GoodsReceive>()
  1723. .SetColumns(x => new Pm_GoodsReceive()
  1724. {
  1725. AuditStatus = auditEnum,
  1726. AuditUserId = userId,
  1727. AuditTime = currUserOpDt,
  1728. StatusDesc = statusDesc
  1729. })
  1730. .Where(x => x.Id == receiveId)
  1731. .ExecuteCommandAsync();
  1732. if (changeStatus > 0)
  1733. {
  1734. _jv.Code = StatusCodes.Status200OK;
  1735. }
  1736. return _jv;
  1737. }
  1738. /// <summary>
  1739. /// 物品领用状态 - 出库待确认(出库确认、出库拒绝)
  1740. /// </summary>
  1741. /// <param name="preChangeStatus">更改前状态</param>
  1742. /// <param name="receiveId"></param>
  1743. /// <param name="userId"></param>
  1744. /// <param name="userName"></param>
  1745. /// <param name="auditEnum"></param>
  1746. /// <returns></returns>
  1747. public async Task<JsonView> GoodsReceiveOutPending(GoodsAuditEnum preChangeStatus, Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1748. {
  1749. _jv.Code = StatusCodes.Status400BadRequest;
  1750. //出库待确认 操作范围
  1751. var audirEnumPers = new List<GoodsAuditEnum>() {
  1752. GoodsAuditEnum.OutConfirmed,
  1753. GoodsAuditEnum.OutRejected
  1754. };
  1755. if (!audirEnumPers.Contains(preChangeStatus))
  1756. {
  1757. _jv.Msg = $"{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可取消操作!";
  1758. return _jv;
  1759. }
  1760. var currUserOpDt = DateTime.Now;
  1761. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1762. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1763. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1764. .ToListAsync();
  1765. auditEnum = GoodsAuditEnum.OutPending;
  1766. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1767. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1768. if (auditInfo == null)
  1769. {
  1770. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1771. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1772. if (addStatus < 1)
  1773. {
  1774. _jv.Msg = $"出库确认取消操作失败!";
  1775. return _jv;
  1776. }
  1777. auditInfos.Add(auditInfo);
  1778. }
  1779. else
  1780. {
  1781. //移除旧数据
  1782. auditInfos.Remove(auditInfo);
  1783. auditInfo.AuditStatus = auditStatus;
  1784. auditInfo.AuditUserId = userId;
  1785. auditInfo.AuditTime = currUserOpDt;
  1786. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1787. .SetColumns(x => new Pm_GoodsAudit()
  1788. {
  1789. AuditStatus = auditStatus,
  1790. AuditUserId = userId,
  1791. AuditTime = currUserOpDt
  1792. })
  1793. .Where(x => x.Id == auditInfo.Id)
  1794. .ExecuteCommandAsync();
  1795. if (updStatus < 1)
  1796. {
  1797. _jv.Msg = $"出库确认操作失败!";
  1798. return _jv;
  1799. }
  1800. //添加更新后的数据
  1801. auditInfos.Add(auditInfo);
  1802. }
  1803. //处理状态描述
  1804. StringBuilder statusDesc = new();
  1805. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1806. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1807. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1808. statusDesc.Append(receiveStatusDesc);
  1809. foreach (var auditInf in auditInfos)
  1810. {
  1811. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1812. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1813. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1814. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1815. statusDesc.Append(auditInfStatusDesc);
  1816. }
  1817. //批次库存信息
  1818. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1819. //出库确认取消是单独处理
  1820. if (preChangeStatus == GoodsAuditEnum.OutConfirming)
  1821. {
  1822. if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1823. {
  1824. auditEnum = GoodsAuditEnum.OutConfirming;
  1825. }
  1826. //2.1 更改库存
  1827. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1828. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1829. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1830. goodsInfo.LastUpdateTime = DateTime.Now;
  1831. goodsInfo.LastUpdateUserId = userId;
  1832. var editGoods = await _sqlSugar
  1833. .Updateable(goodsInfo)
  1834. .UpdateColumns(x => new
  1835. {
  1836. x.StockQuantity,
  1837. x.OQ_Total,
  1838. x.LastUpdateUserId,
  1839. x.LastUpdateTime,
  1840. })
  1841. .Where(x => x.Id == receiveInfo.GoodsId)
  1842. .ExecuteCommandAsync();
  1843. if (editGoods < 1)
  1844. {
  1845. _jv.Msg = $"库存更新失败!";
  1846. return _jv;
  1847. }
  1848. //2.2 入库批次关联领用人 更改批次库存
  1849. var goodsStorages = await _sqlSugar
  1850. .Queryable<Pm_GoodsStorage>()
  1851. .Where(x => x.IsDel == 0 &&
  1852. x.GoodsId == receiveInfo.GoodsId &&
  1853. (x.Quantity - x.ReceiveQuantity) > 0
  1854. )
  1855. .OrderBy(x => x.CreateTime)
  1856. .ToListAsync();
  1857. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1858. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1859. var receiveQuantity = 0.00M; //领用总数量
  1860. foreach (var storage in goodsStorages)
  1861. {
  1862. if (receiveInfo.Quantity == receiveQuantity) break;
  1863. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1864. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1865. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1866. const decimal unit = 0.50M;
  1867. while (receiveQuantity < receiveInfo.Quantity)
  1868. {
  1869. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1870. thisBatchReceiveQuantity += unit;
  1871. receiveQuantity += unit;
  1872. }
  1873. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1874. {
  1875. StorageId = storage.Id,
  1876. Quantity = thisBatchReceiveQuantity
  1877. });
  1878. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1879. var storageUpd = storage;
  1880. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1881. batchStorageInfos.Add(storageUpd);
  1882. }
  1883. //2.2.1 更改批次库存
  1884. if (goodsReceiveInfos.Count > 0)
  1885. {
  1886. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1887. .UpdateColumns(x => x.ReceiveQuantity)
  1888. .WhereColumns(x => x.Id)
  1889. .ExecuteCommandAsync();
  1890. if (edit1 < 1)
  1891. {
  1892. _jv.Msg = $"批次库存更新失败!";
  1893. return _jv;
  1894. }
  1895. }
  1896. //2.2.2 添加入库批次关联领用人
  1897. if (goodsReceiveInfos.Count > 0)
  1898. {
  1899. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1900. }
  1901. }
  1902. var statusDesc1 = statusDesc.ToString();
  1903. var changeStatus = await _sqlSugar
  1904. .Updateable<Pm_GoodsReceive>()
  1905. .SetColumns(x => new Pm_GoodsReceive()
  1906. {
  1907. AuditStatus = auditEnum,
  1908. StatusDesc = statusDesc1,
  1909. GoodsStorageInfo = goodsStorageInfo
  1910. })
  1911. .Where(x => x.Id == receiveInfo.Id)
  1912. .ExecuteCommandAsync();
  1913. if (changeStatus > 0)
  1914. {
  1915. _jv.Code = StatusCodes.Status200OK;
  1916. }
  1917. return _jv;
  1918. }
  1919. /// <summary>
  1920. /// 物品领用状态 - 出库确认中、出库确认完成、出库确认拒绝
  1921. /// </summary>
  1922. /// <param name="receiveInfo"></param>
  1923. /// <param name="userId"></param>
  1924. /// <param name="depEnum"></param>
  1925. /// <param name="auditEnum"></param>
  1926. /// <returns></returns>
  1927. public async Task<JsonView> GoodsReceiveOutConfirming(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1928. {
  1929. _jv.Code = StatusCodes.Status400BadRequest;
  1930. //更改前状态
  1931. var preChangeStatus = receiveInfo.AuditStatus;
  1932. if (preChangeStatus < GoodsAuditEnum.OutPending)
  1933. {
  1934. _jv.Msg = $"领用确认执行成功!才可执行出库确认或出库拒绝操作!";
  1935. return _jv;
  1936. }
  1937. var currUserOpDt = DateTime.Now;
  1938. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1939. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1940. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1941. .ToListAsync();
  1942. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1943. if (auditEnum == GoodsAuditEnum.OutRejected) auditStatus = GoodsConfirmEnum.UnApproved;
  1944. else if (auditEnum == GoodsAuditEnum.OutConfirmed) auditStatus = GoodsConfirmEnum.Confirmed;
  1945. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1946. if (auditInfo == null)
  1947. {
  1948. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1949. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1950. if (addStatus < 1)
  1951. {
  1952. _jv.Msg = $"出库确认操作失败!";
  1953. return _jv;
  1954. }
  1955. auditInfos.Add(auditInfo);
  1956. }
  1957. else
  1958. {
  1959. //移除旧数据
  1960. auditInfos.Remove(auditInfo);
  1961. auditInfo.AuditStatus = auditStatus;
  1962. auditInfo.AuditUserId = userId;
  1963. auditInfo.AuditTime = currUserOpDt;
  1964. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1965. .SetColumns(x => new Pm_GoodsAudit()
  1966. {
  1967. AuditStatus = auditStatus,
  1968. AuditUserId = userId,
  1969. AuditTime = currUserOpDt
  1970. })
  1971. .Where(x => x.Id == auditInfo.Id)
  1972. .ExecuteCommandAsync();
  1973. if (updStatus < 1)
  1974. {
  1975. _jv.Msg = $"出库确认操作失败!";
  1976. return _jv;
  1977. }
  1978. //添加更新后的数据
  1979. auditInfos.Add(auditInfo);
  1980. }
  1981. //处理状态描述
  1982. StringBuilder statusDesc = new();
  1983. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1984. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1985. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1986. statusDesc.Append(receiveStatusDesc);
  1987. foreach (var auditInf in auditInfos)
  1988. {
  1989. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1990. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1991. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1992. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1993. statusDesc.Append(auditInfStatusDesc);
  1994. }
  1995. if (preChangeStatus == auditEnum)
  1996. {
  1997. _jv.Msg = $"此操作已执行,不可重复操作!";
  1998. return _jv;
  1999. }
  2000. //批次库存信息
  2001. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2002. //更改领用状态及库存
  2003. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  2004. {
  2005. //出库确认拒绝
  2006. auditEnum = GoodsAuditEnum.OutRejected;
  2007. }
  2008. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  2009. {
  2010. //1.出库确认完成
  2011. auditEnum = GoodsAuditEnum.OutConfirmed;
  2012. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  2013. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  2014. {
  2015. //2.1 更改库存
  2016. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2017. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2018. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2019. goodsInfo.LastUpdateTime = DateTime.Now;
  2020. goodsInfo.LastUpdateUserId = userId;
  2021. var editGoods = await _sqlSugar
  2022. .Updateable(goodsInfo)
  2023. .UpdateColumns(x => new
  2024. {
  2025. x.StockQuantity,
  2026. x.OQ_Total,
  2027. x.LastUpdateUserId,
  2028. x.LastUpdateTime,
  2029. })
  2030. .Where(x => x.Id == receiveInfo.GoodsId)
  2031. .ExecuteCommandAsync();
  2032. if (editGoods < 1)
  2033. {
  2034. _jv.Msg = $"库存更新失败!";
  2035. return _jv;
  2036. }
  2037. //2.2 入库批次关联领用人 更改批次库存
  2038. var goodsStorages = await _sqlSugar
  2039. .Queryable<Pm_GoodsStorage>()
  2040. .Where(x => x.IsDel == 0 &&
  2041. x.GoodsId == receiveInfo.GoodsId &&
  2042. (x.Quantity - x.ReceiveQuantity) > 0
  2043. )
  2044. .OrderBy(x => x.CreateTime)
  2045. .ToListAsync();
  2046. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2047. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2048. var receiveQuantity = 0.00M; //领用总数量
  2049. foreach (var storage in goodsStorages)
  2050. {
  2051. if (receiveInfo.Quantity == receiveQuantity) break;
  2052. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2053. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2054. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2055. const decimal unit = 0.50M;
  2056. while (receiveQuantity < receiveInfo.Quantity)
  2057. {
  2058. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2059. thisBatchReceiveQuantity += unit;
  2060. receiveQuantity += unit;
  2061. }
  2062. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2063. {
  2064. StorageId = storage.Id,
  2065. Quantity = thisBatchReceiveQuantity
  2066. });
  2067. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2068. var storageUpd = storage;
  2069. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2070. batchStorageInfos.Add(storageUpd);
  2071. }
  2072. //2.2.1 更改批次库存
  2073. if (goodsReceiveInfos.Count > 0)
  2074. {
  2075. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2076. .UpdateColumns(x => x.ReceiveQuantity)
  2077. .WhereColumns(x => x.Id)
  2078. .ExecuteCommandAsync();
  2079. if (edit1 < 1)
  2080. {
  2081. _jv.Msg = $"批次库存更新失败!";
  2082. return _jv;
  2083. }
  2084. }
  2085. //2.2.2 添加入库批次关联领用人
  2086. if (goodsReceiveInfos.Count > 0)
  2087. {
  2088. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2089. }
  2090. }
  2091. }
  2092. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  2093. {
  2094. //出库确认中
  2095. auditEnum = GoodsAuditEnum.OutConfirming;
  2096. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2097. {
  2098. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  2099. if (!string.IsNullOrEmpty(goodsStorageInfo))
  2100. {
  2101. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2102. if (goodsStorageInfos.Any())
  2103. {
  2104. foreach (var item in goodsStorageInfos)
  2105. {
  2106. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2107. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2108. .FirstAsync();
  2109. if (newStorageInfo == null) continue;
  2110. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2111. .ReSetValue(x => x.ReceiveQuantity -= item.Quantity)
  2112. .ExecuteCommandAsync();
  2113. if (newEdit < 1)
  2114. {
  2115. _jv.Msg = $"批次领用库存更新失败!";
  2116. return _jv;
  2117. }
  2118. }
  2119. }
  2120. }
  2121. }
  2122. }
  2123. else
  2124. {
  2125. auditEnum = GoodsAuditEnum.OutPending;
  2126. }
  2127. var statusDesc1 = statusDesc.ToString();
  2128. var changeStatus = await _sqlSugar
  2129. .Updateable<Pm_GoodsReceive>()
  2130. .SetColumns(x => new Pm_GoodsReceive()
  2131. {
  2132. AuditStatus = auditEnum,
  2133. StatusDesc = statusDesc1,
  2134. GoodsStorageInfo = goodsStorageInfo,
  2135. })
  2136. .Where(x => x.Id == receiveInfo.Id)
  2137. .ExecuteCommandAsync();
  2138. if (changeStatus > 0)
  2139. {
  2140. _jv.Code = StatusCodes.Status200OK;
  2141. }
  2142. return _jv;
  2143. }
  2144. /// <summary>
  2145. ///
  2146. /// </summary>
  2147. /// <param name="receiveInfo"></param>
  2148. /// <param name="userId"></param>
  2149. /// <param name="depEnum"></param>
  2150. /// <param name="auditEnum"></param>
  2151. /// <returns></returns>
  2152. public async Task<JsonView> GoodsReceiveOutConfirmingMultiple(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  2153. {
  2154. _jv.Code = StatusCodes.Status400BadRequest;
  2155. //更改前状态
  2156. receiveInfo.CreateUserId = userId;
  2157. var preChangeStatus = receiveInfo.AuditStatus;
  2158. var currUserOpDt = DateTime.Now;
  2159. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  2160. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  2161. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  2162. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  2163. .ToListAsync();
  2164. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  2165. if (auditEnum == GoodsAuditEnum.UnApproved) auditStatus = GoodsConfirmEnum.UnApproved;
  2166. else if (auditEnum == GoodsAuditEnum.Approved) auditStatus = GoodsConfirmEnum.Confirmed;
  2167. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  2168. if (auditInfo == null)
  2169. {
  2170. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId)
  2171. {
  2172. AuditUserId = userId
  2173. };
  2174. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  2175. if (addStatus < 1)
  2176. {
  2177. _jv.Msg = $"出库确认操作失败!";
  2178. return _jv;
  2179. }
  2180. auditInfos.Add(auditInfo);
  2181. }
  2182. else
  2183. {
  2184. //移除旧数据
  2185. auditInfos.Remove(auditInfo);
  2186. auditInfo.AuditStatus = auditStatus;
  2187. auditInfo.AuditUserId = userId;
  2188. auditInfo.AuditTime = currUserOpDt;
  2189. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  2190. .SetColumns(x => new Pm_GoodsAudit()
  2191. {
  2192. AuditStatus = auditStatus,
  2193. AuditUserId = userId,
  2194. AuditTime = currUserOpDt
  2195. })
  2196. .Where(x => x.Id == auditInfo.Id)
  2197. .ExecuteCommandAsync();
  2198. if (updStatus < 1)
  2199. {
  2200. _jv.Msg = $"出库确认操作失败!";
  2201. return _jv;
  2202. }
  2203. //添加更新后的数据
  2204. auditInfos.Add(auditInfo);
  2205. }
  2206. auditInfos = auditInfos.OrderBy(x => x.Dep).ToList();
  2207. //处理状态描述
  2208. StringBuilder statusDesc = new();
  2209. foreach (var auditInf in auditInfos)
  2210. {
  2211. string auditType = auditInf.Dep == GoodsAuditDepEnum.Hr ? "人事部" : "领用确认";
  2212. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  2213. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  2214. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  2215. auditType, auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  2216. statusDesc.Append(auditInfStatusDesc);
  2217. }
  2218. if (preChangeStatus == auditEnum)
  2219. {
  2220. _jv.Msg = $"此操作已执行,不可重复操作!";
  2221. return _jv;
  2222. }
  2223. //批次库存信息
  2224. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2225. //更改领用状态及库存
  2226. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  2227. {
  2228. //出库确认拒绝
  2229. auditEnum = GoodsAuditEnum.OutRejected;
  2230. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2231. {
  2232. //回滚库存
  2233. _jv = await GoodsInventoryRollback(receiveInfo);
  2234. if (_jv.Code != StatusCodes.Status200OK) return _jv;
  2235. }
  2236. }
  2237. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  2238. {
  2239. //1.出库确认完成
  2240. auditEnum = GoodsAuditEnum.OutConfirmed;
  2241. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  2242. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  2243. {
  2244. //2.1 更改库存
  2245. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2246. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2247. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2248. goodsInfo.LastUpdateTime = DateTime.Now;
  2249. goodsInfo.LastUpdateUserId = userId;
  2250. var editGoods = await _sqlSugar
  2251. .Updateable(goodsInfo)
  2252. .UpdateColumns(x => new
  2253. {
  2254. x.StockQuantity,
  2255. x.OQ_Total,
  2256. x.LastUpdateUserId,
  2257. x.LastUpdateTime,
  2258. })
  2259. .Where(x => x.Id == receiveInfo.GoodsId)
  2260. .ExecuteCommandAsync();
  2261. if (editGoods < 1)
  2262. {
  2263. _jv.Msg = $"库存更新失败!";
  2264. return _jv;
  2265. }
  2266. //2.2 入库批次关联领用人 更改批次库存
  2267. var goodsStorages = await _sqlSugar
  2268. .Queryable<Pm_GoodsStorage>()
  2269. .Where(x => x.IsDel == 0 &&
  2270. x.GoodsId == receiveInfo.GoodsId &&
  2271. (x.Quantity - x.ReceiveQuantity) > 0
  2272. )
  2273. .OrderBy(x => x.CreateTime)
  2274. .ToListAsync();
  2275. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2276. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2277. var receiveQuantity = 0.00M; //领用总数量
  2278. foreach (var storage in goodsStorages)
  2279. {
  2280. if (receiveInfo.Quantity == receiveQuantity) break;
  2281. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2282. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2283. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2284. const decimal unit = 0.50M;
  2285. while (receiveQuantity < receiveInfo.Quantity)
  2286. {
  2287. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2288. thisBatchReceiveQuantity += unit;
  2289. receiveQuantity += unit;
  2290. }
  2291. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2292. {
  2293. StorageId = storage.Id,
  2294. Quantity = thisBatchReceiveQuantity
  2295. });
  2296. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2297. var storageUpd = storage;
  2298. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2299. batchStorageInfos.Add(storageUpd);
  2300. }
  2301. //2.2.1 更改批次库存
  2302. if (goodsReceiveInfos.Count > 0)
  2303. {
  2304. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2305. .UpdateColumns(x => x.ReceiveQuantity)
  2306. .WhereColumns(x => x.Id)
  2307. .ExecuteCommandAsync();
  2308. if (edit1 < 1)
  2309. {
  2310. _jv.Msg = $"批次库存更新失败!";
  2311. return _jv;
  2312. }
  2313. }
  2314. //2.2.2 添加入库批次关联领用人
  2315. if (goodsReceiveInfos.Count > 0)
  2316. {
  2317. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2318. }
  2319. }
  2320. }
  2321. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  2322. {
  2323. //出库确认中
  2324. auditEnum = GoodsAuditEnum.OutConfirming;
  2325. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2326. {
  2327. //回滚库存
  2328. _jv = await GoodsInventoryRollback(receiveInfo);
  2329. if (_jv.Code != StatusCodes.Status200OK) return _jv;
  2330. }
  2331. }
  2332. else auditEnum = GoodsAuditEnum.OutPending;
  2333. var statusDesc1 = statusDesc.ToString();
  2334. var changeStatus = await _sqlSugar
  2335. .Updateable<Pm_GoodsReceive>()
  2336. .SetColumns(x => new Pm_GoodsReceive()
  2337. {
  2338. AuditStatus = auditEnum,
  2339. StatusDesc = statusDesc1,
  2340. GoodsStorageInfo = goodsStorageInfo,
  2341. })
  2342. .Where(x => x.Id == receiveInfo.Id)
  2343. .ExecuteCommandAsync();
  2344. if (changeStatus > 0)
  2345. {
  2346. _jv.Code = StatusCodes.Status200OK;
  2347. }
  2348. return _jv;
  2349. }
  2350. /// <summary>
  2351. /// 物品库存回滚
  2352. /// </summary>
  2353. /// <param name="receiveInfo"></param>
  2354. /// <returns></returns>
  2355. public async Task<JsonView> GoodsInventoryRollback(Pm_GoodsReceive receiveInfo)
  2356. {
  2357. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  2358. //回滚库存
  2359. var changeGoods = await _sqlSugar
  2360. .Updateable(goodsInfo)
  2361. .ReSetValue(it =>
  2362. {
  2363. it.StockQuantity += receiveInfo.Quantity;
  2364. it.OQ_Total -= receiveInfo.Quantity;
  2365. it.LastUpdateTime = DateTime.Now;
  2366. it.LastUpdateUserId = receiveInfo.CreateUserId;
  2367. })
  2368. .ExecuteCommandAsync();
  2369. if (changeGoods < 1)
  2370. {
  2371. _jv.Msg = $"库存回滚失败!";
  2372. return _jv;
  2373. }
  2374. //批次领用记录
  2375. _jv.Code = StatusCodes.Status200OK;
  2376. var goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2377. if (string.IsNullOrEmpty(goodsStorageInfo)) return _jv;
  2378. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2379. if (!goodsStorageInfos.Any()) return _jv;
  2380. foreach (var item in goodsStorageInfos)
  2381. {
  2382. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2383. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2384. .FirstAsync();
  2385. if (newStorageInfo == null) continue;
  2386. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2387. .ReSetValue(x => x.ReceiveQuantity -= item.Quantity)
  2388. .ExecuteCommandAsync();
  2389. if (newEdit < 1)
  2390. {
  2391. _jv.Code = StatusCodes.Status400BadRequest;
  2392. _jv.Msg = $"批次领用库存更新失败!";
  2393. return _jv;
  2394. }
  2395. }
  2396. _jv.Code = StatusCodes.Status200OK;
  2397. return _jv;
  2398. }
  2399. #endregion
  2400. #region 单人审核状态变更
  2401. /// <summary>
  2402. /// 物品领用状态 - 出库确认完成、出库确认拒绝
  2403. /// </summary>
  2404. /// <param name="receiveInfo"></param>
  2405. /// <param name="userId"></param>
  2406. /// <param name="currUserName"></param>
  2407. /// <param name="auditEnum"></param>
  2408. /// <returns></returns>
  2409. public async Task<JsonView> GoodsReceiveOutConfirmingSingle(Pm_GoodsReceive receiveInfo, int userId, string currUserName, GoodsAuditEnum auditEnum)
  2410. {
  2411. _jv.Code = StatusCodes.Status400BadRequest;
  2412. //状态操作范围验证
  2413. var auditEnums = new List<GoodsAuditEnum>() {
  2414. GoodsAuditEnum.Pending,
  2415. GoodsAuditEnum.Approved,
  2416. GoodsAuditEnum.UnApproved,
  2417. };
  2418. if (!auditEnums.Contains(auditEnum))
  2419. {
  2420. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()}、{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可操作!";
  2421. return _jv;
  2422. }
  2423. var currUserOpDt = DateTime.Now;
  2424. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  2425. //更改前状态
  2426. var preChangeStatus = receiveInfo.AuditStatus;
  2427. //前台状态验证
  2428. var auditInfo = await _sqlSugar.Queryable<Pm_GoodsAudit>().FirstAsync(x => x.IsDel == 0 && x.Type == 2 && x.DataId == receiveInfo.Id);
  2429. if (auditInfo == null)
  2430. {
  2431. var addInfo = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr_Reception, receiveInfo.Id, GoodsConfirmEnum.WaitConfirm, userId);
  2432. auditInfo = await _sqlSugar.Insertable(addInfo).ExecuteReturnEntityAsync();
  2433. if (auditInfo == null)
  2434. {
  2435. _jv.Msg = $"确认操作失败!";
  2436. return _jv;
  2437. }
  2438. }
  2439. if (auditEnum == GoodsAuditEnum.Approved)
  2440. {
  2441. auditInfo.AuditStatus = GoodsConfirmEnum.Confirmed;
  2442. auditEnum = GoodsAuditEnum.OutConfirmed;
  2443. }
  2444. else if (auditEnum == GoodsAuditEnum.UnApproved)
  2445. {
  2446. auditInfo.AuditStatus = GoodsConfirmEnum.UnApproved;
  2447. auditEnum = GoodsAuditEnum.OutRejected;
  2448. }
  2449. if (preChangeStatus == auditEnum)
  2450. {
  2451. _jv.Msg = $"此操作已执行,不可重复操作!";
  2452. return _jv;
  2453. }
  2454. //更新子表审核状态
  2455. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  2456. .SetColumns(x => new Pm_GoodsAudit()
  2457. {
  2458. AuditStatus = auditInfo.AuditStatus,
  2459. AuditUserId = userId,
  2460. AuditTime = currUserOpDt
  2461. })
  2462. .Where(x => x.Id == auditInfo.Id)
  2463. .ExecuteCommandAsync();
  2464. if (updStatus < 1)
  2465. {
  2466. _jv.Msg = $"确认操作失败!";
  2467. return _jv;
  2468. }
  2469. //处理状态描述
  2470. StringBuilder statusDesc = new();
  2471. statusDesc.Append(string.Format("领用确认:状态:{0} 审核人:{1} 审核时间:{2};", auditInfo.AuditStatus.GetEnumDescription(), currUserName, currUserOpTime));
  2472. //批次库存信息
  2473. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2474. if (auditEnum == GoodsAuditEnum.OutConfirmed) //出库确认
  2475. {
  2476. //1 更改库存
  2477. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2478. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2479. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2480. goodsInfo.LastUpdateTime = DateTime.Now;
  2481. goodsInfo.LastUpdateUserId = userId;
  2482. var editGoods = await _sqlSugar.Updateable(goodsInfo)
  2483. .UpdateColumns(x => new
  2484. {
  2485. x.StockQuantity,
  2486. x.OQ_Total,
  2487. x.LastUpdateUserId,
  2488. x.LastUpdateTime,
  2489. })
  2490. .Where(x => x.Id == receiveInfo.GoodsId)
  2491. .ExecuteCommandAsync();
  2492. if (editGoods < 1)
  2493. {
  2494. _jv.Msg = $"库存更新失败!";
  2495. return _jv;
  2496. }
  2497. //2 入库批次关联领用人 更改批次库存
  2498. var goodsStorages = await _sqlSugar
  2499. .Queryable<Pm_GoodsStorage>()
  2500. .Where(x => x.IsDel == 0 &&
  2501. x.GoodsId == receiveInfo.GoodsId &&
  2502. (x.Quantity - x.ReceiveQuantity) > 0
  2503. )
  2504. .OrderBy(x => x.CreateTime)
  2505. .ToListAsync();
  2506. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2507. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2508. var receiveQuantity = 0.00M; //领用总数量
  2509. foreach (var storage in goodsStorages)
  2510. {
  2511. if (receiveInfo.Quantity == receiveQuantity) break;
  2512. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2513. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2514. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2515. const decimal unit = 0.50M;
  2516. while (receiveQuantity < receiveInfo.Quantity)
  2517. {
  2518. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2519. thisBatchReceiveQuantity += unit;
  2520. receiveQuantity += unit;
  2521. }
  2522. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2523. {
  2524. StorageId = storage.Id,
  2525. Quantity = thisBatchReceiveQuantity
  2526. });
  2527. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2528. var storageUpd = storage;
  2529. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2530. batchStorageInfos.Add(storageUpd);
  2531. }
  2532. //3 更改批次库存
  2533. if (goodsReceiveInfos.Count > 0)
  2534. {
  2535. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2536. .UpdateColumns(x => x.ReceiveQuantity)
  2537. .WhereColumns(x => x.Id)
  2538. .ExecuteCommandAsync();
  2539. if (edit1 < 1)
  2540. {
  2541. _jv.Msg = $"批次库存更新失败!";
  2542. return _jv;
  2543. }
  2544. }
  2545. //4 添加入库批次关联领用人
  2546. if (goodsReceiveInfos.Count > 0)
  2547. {
  2548. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2549. }
  2550. }
  2551. else if (auditEnum == GoodsAuditEnum.OutRejected) //出库拒绝
  2552. {
  2553. //拒绝之前的状态为已完成 回滚库存
  2554. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2555. {
  2556. //回滚库存
  2557. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  2558. var changeGoods = await _sqlSugar
  2559. .Updateable(goodsInfo)
  2560. .ReSetValue(it =>
  2561. {
  2562. it.StockQuantity += receiveInfo.Quantity;
  2563. it.OQ_Total -= receiveInfo.Quantity;
  2564. it.LastUpdateTime = DateTime.Now;
  2565. it.LastUpdateUserId = userId;
  2566. })
  2567. .ExecuteCommandAsync();
  2568. if (changeGoods < 1)
  2569. {
  2570. _jv.Msg = $"库存回滚失败!";
  2571. return _jv;
  2572. }
  2573. //批次号库存 信息更新
  2574. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  2575. if (!string.IsNullOrEmpty(goodsStorageInfo))
  2576. {
  2577. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2578. if (goodsStorageInfos.Any())
  2579. {
  2580. foreach (var item in goodsStorageInfos)
  2581. {
  2582. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2583. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2584. .FirstAsync();
  2585. if (newStorageInfo == null) continue;
  2586. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2587. .ReSetValue(x => x.ReceiveQuantity -= item.Quantity)
  2588. .ExecuteCommandAsync();
  2589. if (newEdit < 1)
  2590. {
  2591. _jv.Msg = $"批次领用库存更新失败!";
  2592. return _jv;
  2593. }
  2594. }
  2595. }
  2596. }
  2597. }
  2598. }
  2599. var statusDesc1 = statusDesc.ToString();
  2600. var changeStatus = await _sqlSugar
  2601. .Updateable<Pm_GoodsReceive>()
  2602. .SetColumns(x => new Pm_GoodsReceive()
  2603. {
  2604. AuditStatus = auditEnum,
  2605. StatusDesc = statusDesc1,
  2606. GoodsStorageInfo = goodsStorageInfo,
  2607. })
  2608. .Where(x => x.Id == receiveInfo.Id)
  2609. .ExecuteCommandAsync();
  2610. if (changeStatus > 0) _jv.Code = StatusCodes.Status200OK;
  2611. return _jv;
  2612. }
  2613. #endregion
  2614. /// <summary>
  2615. /// 物品领用 Del
  2616. /// </summary>
  2617. /// <param name="id"></param>
  2618. /// <param name="currUserId"></param>
  2619. /// <returns></returns>
  2620. public async Task<JsonView> GoodsReceiveDel(int id, int currUserId)
  2621. {
  2622. _jv.Msg = $"操作失败";
  2623. var receiveInfo = await _sqlSugar
  2624. .Queryable<Pm_GoodsReceive>()
  2625. .Where(x => x.IsDel == 0 && x.Id == id)
  2626. .FirstAsync();
  2627. if (receiveInfo.AuditStatus >= GoodsAuditEnum.Approved)
  2628. {
  2629. _jv.Msg = $"该条数据已进入审批流程,不可删除!";
  2630. return _jv;
  2631. }
  2632. _sqlSugar.BeginTran();
  2633. var edit = await _sqlSugar
  2634. .Updateable<Pm_GoodsReceive>()
  2635. .SetColumns(x => new Pm_GoodsReceive()
  2636. {
  2637. IsDel = 1,
  2638. DeleteUserId = currUserId,
  2639. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  2640. })
  2641. .Where(x => x.Id == id)
  2642. .ExecuteCommandAsync();
  2643. if (edit < 1)
  2644. {
  2645. _sqlSugar.RollbackTran();
  2646. return _jv;
  2647. }
  2648. var editSub = await _sqlSugar
  2649. .Updateable<Pm_GoodsReceiveDetails>()
  2650. .SetColumns(x => new Pm_GoodsReceiveDetails()
  2651. {
  2652. IsDel = 1,
  2653. DeleteUserId = currUserId,
  2654. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  2655. })
  2656. .Where(x => x.GoodsReceiveId == id)
  2657. .ExecuteCommandAsync();
  2658. if (editSub < 1)
  2659. {
  2660. _sqlSugar.RollbackTran();
  2661. return _jv;
  2662. }
  2663. _sqlSugar.CommitTran();
  2664. _jv.Msg = $"操作成功!";
  2665. _jv.Code = StatusCodes.Status200OK;
  2666. return _jv;
  2667. }
  2668. #region New 物品领用、审核
  2669. /// <summary>
  2670. /// 物品领用审核列表
  2671. /// </summary>
  2672. /// <param name="dto"></param>
  2673. /// <returns></returns>
  2674. public async Task<JsonView> GoodsReceiveAuditList(GoodsReceiveAuditListDTO dto)
  2675. {
  2676. var currUserId = dto.CurrUserId;
  2677. var auditStatus = dto.AuditStatus;
  2678. var goodsName = dto.GoodsName;
  2679. var sql = string.Format(@"SELECT
  2680. ROW_NUMBER() OVER (ORDER BY CreateTime DESC) AS RowNumber,
  2681. *
  2682. FROM
  2683. (
  2684. Select
  2685. gr.Id,
  2686. gr.GroupId,
  2687. CASE
  2688. WHEN gr.GroupId = 0 THEN '其他物资(公司内部物资)'
  2689. WHEN gr.GroupId = -1 THEN '拜访客户所使用的物资'
  2690. WHEN gr.GroupId = -2 THEN '库存调整'
  2691. ELSE di.TeamName
  2692. END [GroupName],
  2693. gr.GoodsId,
  2694. CASE
  2695. WHEN gr.GoodsId = 0 THEN gr.GoodsName
  2696. ELSE gi.Name
  2697. END [GoodsName],
  2698. gi.Type [GoodsTypeId],
  2699. sd.Name [GoodsType],
  2700. CASE
  2701. WHEN gi.Type = 0
  2702. OR gi.Type IS NULL THEN CASE
  2703. WHEN af.TemplateId = 3 THEN 1
  2704. ELSE 0
  2705. END
  2706. WHEN gi.Type = 1423 THEN 1
  2707. ELSE 0
  2708. END [IsValuable],
  2709. gr.Quantity,
  2710. gi.Unit,
  2711. gr.Reason,
  2712. gr.Remark,
  2713. gr.AuditStatus,
  2714. gr.StatusDesc,
  2715. gr.AuditUserId,
  2716. u1.CnName [AuditUserName],
  2717. gr.AuditTime,
  2718. u2.CnName [CreateUserName],
  2719. gr.CreateTime
  2720. FROM
  2721. OA2023DB.dbo.Pm_GoodsReceive gr
  2722. LEFT JOIN Pm_GoodsInfo gi ON gr.GoodsId = gi.Id
  2723. LEFT JOIN Grp_DelegationInfo di ON gr.GroupId = di.Id
  2724. LEFT JOIN Sys_Users u1 ON gr.AuditUserId = u1.Id
  2725. LEFT JOIN Sys_Users u2 ON gr.CreateUserId = u2.Id
  2726. LEFT JOIN Sys_AuditFlow af ON gr.Id = af.BusinessId
  2727. LEFT JOIN Sys_SetData sd ON gi.Type = sd.Id
  2728. WHERE
  2729. gr.IsDel = 0
  2730. ) Temp ");
  2731. var checkValuableUserIds = new List<int>() {
  2732. 343 , //陈湘OAId
  2733. 309 , //赖红燕AId
  2734. };
  2735. var isValueable = false;
  2736. if (checkValuableUserIds.Contains(currUserId)) //陈湘OAId登录 只显示贵重物品审核信息
  2737. {
  2738. isValueable = true;
  2739. }
  2740. RefAsync<int> total = 0;
  2741. var view = await _sqlSugar.SqlQueryable<GoodsReceiveListMobileView>(sql)
  2742. .WhereIF(isValueable, x => x.IsValuable == true)
  2743. .WhereIF(!string.IsNullOrEmpty(goodsName), x => x.GoodsName.Contains(goodsName))
  2744. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  2745. //普通物品领用审核模板
  2746. var normAuditTemps = await _approvalProcessRep.GetTemplateByBusinessTypeAsync(2); //普通物品
  2747. var normAuditUsers = new List<GoodsStorageAuditPerView>();
  2748. normAuditTemps?.TempNodes.ForEach(x =>
  2749. {
  2750. var auditDep = GoodsAuditDepEnum.Hr_Reception;
  2751. if (x.NodeName.Contains("人事部主管/经理审核")) auditDep = GoodsAuditDepEnum.Hr;
  2752. else if (x.NodeName.Contains("前台审核")) auditDep = GoodsAuditDepEnum.Hr;
  2753. x.NodeUsers.ForEach(y =>
  2754. {
  2755. var userFlang = false;
  2756. if (y.UserId == currUserId) userFlang = true;
  2757. var info = new GoodsStorageAuditPerView() { ButtonText = x.NodeName, AuditDep = auditDep, AuditPer = userFlang };
  2758. normAuditUsers.Add(info);
  2759. });
  2760. });
  2761. //贵重物品领用审核模板
  2762. var valuableAuditTemps = await _approvalProcessRep.GetTemplateByBusinessTypeAsync(3); //贵重物品
  2763. var valuableAuditUsers = new List<GoodsStorageAuditPerView>();
  2764. valuableAuditTemps?.TempNodes.ForEach(x =>
  2765. {
  2766. var auditDep = GoodsAuditDepEnum.Hr_Reception;
  2767. if (x.NodeName.Contains("人事部主管/经理审核")) auditDep = GoodsAuditDepEnum.Hr;
  2768. else if (x.NodeName.Contains("前台审核")) auditDep = GoodsAuditDepEnum.Hr;
  2769. x.NodeUsers.ForEach(y =>
  2770. {
  2771. var userFlang = false;
  2772. if (y.UserId == currUserId) userFlang = true;
  2773. var info = new GoodsStorageAuditPerView() { ButtonText = x.NodeName, AuditDep = auditDep, AuditPer = userFlang };
  2774. valuableAuditUsers.Add(info);
  2775. });
  2776. });
  2777. foreach (var item in view)
  2778. {
  2779. var details = $"暂无物品信息数据";
  2780. //设置领用详情
  2781. if (item.GoodsTypeId < 1)
  2782. {
  2783. var detailsData = await _sqlSugar.Queryable<Pm_GoodsReceiveDetails>()
  2784. .LeftJoin<Pm_GoodsInfo>((grd, gi) => grd.GoodsId == gi.Id)
  2785. .LeftJoin<Sys_SetData>((grd, gi, sd) => gi.Type == sd.Id)
  2786. .Where((grd, gi, sd) => grd.IsDel == 0 && grd.GoodsReceiveId == item.Id)
  2787. .Select((grd, gi, sd) => new
  2788. {
  2789. gi.Name,
  2790. TypeName = sd.Name,
  2791. grd.Quantity,
  2792. grd.Remark
  2793. })
  2794. .ToListAsync();
  2795. if (detailsData.Any())
  2796. {
  2797. //设置领用数量 多个物品 计算领用合计
  2798. item.Quantity = detailsData.Sum(x => x.Quantity);
  2799. var text = new StringBuilder();
  2800. text.Append($"归属团组:{item.GroupName} <br/><br/>");
  2801. detailsData.ForEach(x =>
  2802. {
  2803. var str = $"物品名称:{x.Name} 物品类型:{x.TypeName} 领用数量:{x.Quantity} 备注:{x.Remark}<br/>";
  2804. text.Append(str);
  2805. });
  2806. details = text.ToString();
  2807. }
  2808. }
  2809. else details = $"归属团组/类型:{item.GroupName} <br/><br/>物品名称:{item.GoodsName} 物品类型:{item.GoodsType} 领用数量:{item.Quantity} 备注:{item.Remark}<br/>";
  2810. item.GoodsDetails = details;
  2811. //设置领用状态描述
  2812. var auditRecords = await _sqlSugar.Queryable<Sys_AuditRecord>()
  2813. .InnerJoin<Sys_AuditFlow>((ar, af) => ar.FlowId == af.Id)
  2814. .Where((ar, af) => ar.IsDel == 0 && af.BusinessId == item.Id && af.BusinessType == 1)
  2815. .Select((ar, af) => new
  2816. {
  2817. ar.NodeId,
  2818. ar.NodeName,
  2819. ar.AuditResult,
  2820. ar.AuditorName,
  2821. ar.AuditTime
  2822. })
  2823. //.OrderBy((ar, af) => ar.AuditTime)
  2824. .ToListAsync();
  2825. if (auditRecords.Any())
  2826. {
  2827. var text = new StringBuilder();
  2828. auditRecords.ForEach(x =>
  2829. {
  2830. var statusText = x.AuditResult switch
  2831. {
  2832. 0 => "待审核",
  2833. 1 => "已审核",
  2834. 2 => "已拒绝",
  2835. 3 => "无需处理",
  2836. _ => "未知"
  2837. };
  2838. var auditTime = $"-";
  2839. if (x.AuditResult == 1 || x.AuditResult == 2 || x.AuditResult == 3) auditTime = x.AuditTime.ToString("yyyy-MM-dd HH:mm:ss");
  2840. var str = $"{x.NodeName}:状态:{statusText} 审核人:{x.AuditorName} 审核时间:{auditTime}<br/>";
  2841. text.Append(str);
  2842. });
  2843. item.StatusDesc = text.ToString();
  2844. }
  2845. //多情况下审核、操作权限验证
  2846. if (item.GoodsTypeId == 0)
  2847. {
  2848. int tempId = 2;
  2849. if (item.IsValuable) tempId = 3;
  2850. item.IsAuditPer = await _approvalProcessRep.VerifyAuditAuthAsync(tempId, 1, item.Id, currUserId);
  2851. }
  2852. else
  2853. {
  2854. if (item.AuditStatus == GoodsAuditEnum.Pending)
  2855. {
  2856. item.IsAuditPer = normAuditTemps?.TempNodes.FirstOrDefault()?.NodeUsers.Any(x => x.UserId == currUserId) ?? false;
  2857. }
  2858. else if (item.AuditStatus == GoodsAuditEnum.OutConfirming)
  2859. {
  2860. item.IsAuditPer = valuableAuditTemps?.TempNodes.FirstOrDefault(x => x.NodeOrder == 1)?.NodeUsers.Any(x => x.UserId == currUserId) ?? false;
  2861. }
  2862. }
  2863. //前端权限验证
  2864. if (item.IsValuable)
  2865. {
  2866. item.AuditPers = valuableAuditUsers.ToArray();
  2867. }
  2868. else item.AuditPers = normAuditUsers.ToArray();
  2869. }
  2870. if (dto.PortType == 2 || dto.PortType == 3)
  2871. {
  2872. _jv.Data = view;
  2873. }
  2874. else if (dto.PortType == 1)
  2875. {
  2876. var view1 = _mapper.Map<List<GoodsReceiveListView>>(view);
  2877. _jv.Data = view1;
  2878. }
  2879. _jv.Code = StatusCodes.Status200OK;
  2880. _jv.Count = total;
  2881. _jv.Msg = $"操作成功";
  2882. return _jv;
  2883. }
  2884. /// <summary>
  2885. /// 物品领用 可批量 OP(Add Or Edit)
  2886. /// </summary>
  2887. /// <param name="dto"></param>
  2888. /// <param name="currUserId"></param>
  2889. /// <returns></returns>
  2890. public async Task<JsonView> GoodsReceiveBatchOpAsync(GoodsReceiveBatchOpDto dto)
  2891. {
  2892. var goodsReceiveId = dto.Id;
  2893. var currUserId = dto.CurrUserId;
  2894. //请求参数处理
  2895. var receiveInfo = new Pm_GoodsReceive()
  2896. {
  2897. Id = goodsReceiveId,
  2898. GroupId = dto.GroupId,
  2899. Reason = dto.Reason,
  2900. Remark = dto.Remark,
  2901. AuditStatus = GoodsAuditEnum.Pending,
  2902. IsReplace = dto.IsReplace,
  2903. CreateUserId = currUserId,
  2904. };
  2905. var receiveDetails = _mapper.Map<Pm_GoodsReceiveDetails[]>(dto.ReceiveDetails);
  2906. //receiveDetails.ForEach(x => { x.CreateUserId = dto.CurrUserId; });
  2907. _sqlSugar.BeginTran();
  2908. //审核状态验证 false:其他物品 true:贵重物品
  2909. var isBatchVail = false;
  2910. //物品库存验证
  2911. int goodsIndex = 1;
  2912. var goodsNames = new List<string>();
  2913. foreach (var item in receiveDetails)
  2914. {
  2915. item.CreateUserId = currUserId;
  2916. int goodsId = item.GoodsId;
  2917. //物品验证
  2918. var goodsInfo = _sqlSugar.Queryable<Pm_GoodsInfo>().First(x => x.IsDel == 0 && x.Id == goodsId);
  2919. if (goodsInfo == null)
  2920. {
  2921. _jv.Msg = $"第{goodsIndex}项物品不存在!";
  2922. _sqlSugar.RollbackTran();
  2923. return _jv;
  2924. }
  2925. goodsNames.Add(goodsInfo.Name);
  2926. //物品库存验证
  2927. var stockQuantity = goodsInfo.StockQuantity;
  2928. var awaitAuditQuantity = await GoodsAwaitQuantityAsync(goodsId);
  2929. stockQuantity -= awaitAuditQuantity;
  2930. if (item.Quantity > stockQuantity)
  2931. {
  2932. _jv.Msg = $"“{goodsInfo.Name}”物品库存不足!剩余库存:{stockQuantity} {goodsInfo.Unit}(待审数量:{awaitAuditQuantity});";
  2933. _sqlSugar.RollbackTran();
  2934. return _jv;
  2935. }
  2936. //物品类型验证
  2937. if (_goodsTypeIds.Contains(goodsInfo.Type)) isBatchVail = true;
  2938. //入库批次关联领用人 更改批次库存
  2939. var goodsStorages = await _sqlSugar
  2940. .Queryable<Pm_GoodsStorage>()
  2941. .Where(x => x.IsDel == 0 &&
  2942. x.GoodsId == receiveInfo.GoodsId &&
  2943. (x.Quantity - x.ReceiveQuantity) > 0
  2944. )
  2945. .OrderBy(x => x.CreateTime)
  2946. .ToListAsync();
  2947. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2948. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2949. var receiveQuantity = 0.00M; //领用总数量
  2950. foreach (var storage in goodsStorages)
  2951. {
  2952. if (receiveInfo.Quantity == receiveQuantity) break;
  2953. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2954. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2955. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2956. const decimal unit = 0.50M;
  2957. while (receiveQuantity < receiveInfo.Quantity)
  2958. {
  2959. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2960. thisBatchReceiveQuantity += unit;
  2961. receiveQuantity += unit;
  2962. }
  2963. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2964. {
  2965. StorageId = storage.Id,
  2966. Quantity = thisBatchReceiveQuantity
  2967. });
  2968. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2969. var storageUpd = storage;
  2970. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2971. batchStorageInfos.Add(storageUpd);
  2972. }
  2973. goodsIndex++;
  2974. }
  2975. if (goodsNames.Any())
  2976. {
  2977. receiveInfo.GoodsName = string.Join("、", goodsNames);
  2978. }
  2979. //验证领用 添加OR编辑
  2980. var goodsReceiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.IsDel == 0 && x.Id == goodsReceiveId);
  2981. if (goodsReceiveInfo == null) //添加
  2982. {
  2983. goodsReceiveId = await _sqlSugar.Insertable(receiveInfo).ExecuteReturnIdentityAsync();
  2984. if (goodsReceiveId < 1)
  2985. {
  2986. _jv.Msg = $"领用添加失败!";
  2987. _sqlSugar.RollbackTran();
  2988. return _jv;
  2989. }
  2990. receiveDetails.ForEach(x => { x.GoodsReceiveId = goodsReceiveId; });
  2991. var receiveDetailsStatus = await _sqlSugar.Insertable(receiveDetails).ExecuteCommandAsync();
  2992. if (receiveDetailsStatus < 1)
  2993. {
  2994. _jv.Msg = $"领用明细添加失败!";
  2995. _sqlSugar.RollbackTran();
  2996. return _jv;
  2997. }
  2998. }
  2999. else //修改
  3000. {
  3001. //更改前状态验证
  3002. if (goodsReceiveInfo.AuditStatus != GoodsAuditEnum.Pending)
  3003. {
  3004. _jv.Msg = $"领用状态在“领用待确认”,即可修改!";
  3005. _sqlSugar.RollbackTran();
  3006. return _jv;
  3007. }
  3008. var receiveStatus = await _sqlSugar.Updateable(receiveInfo).ExecuteCommandAsync();
  3009. if (receiveStatus < 1)
  3010. {
  3011. _jv.Msg = $"领用更新失败!";
  3012. _sqlSugar.RollbackTran();
  3013. return _jv;
  3014. }
  3015. //更新子表数据
  3016. if (receiveDetails.Any())
  3017. {
  3018. receiveDetails.ForEach(x => { x.GoodsReceiveId = receiveInfo.Id; });
  3019. var db_receiveDetails = await _sqlSugar.Queryable<Pm_GoodsReceiveDetails>()
  3020. .Where(x => x.IsDel == 0 && x.GoodsReceiveId == goodsReceiveId)
  3021. .ToListAsync();
  3022. var toDelete = db_receiveDetails.Where(p => !receiveDetails.Any(np => np.Id == p.Id)).ToList();
  3023. if (toDelete.Any())
  3024. {
  3025. var delStatus = _sqlSugar.Deleteable(toDelete).ExecuteCommand();
  3026. if (delStatus < 1)
  3027. {
  3028. _jv.Msg = $"领用明细旧数据删除失败!";
  3029. _sqlSugar.RollbackTran();
  3030. return _jv;
  3031. }
  3032. }
  3033. var addOrUpdStatus = await _sqlSugar.Storageable<Pm_GoodsReceiveDetails>(receiveDetails.ToList()).ExecuteCommandAsync();
  3034. if (addOrUpdStatus < 1)
  3035. {
  3036. _jv.Msg = $"领用明细更新失败!";
  3037. _sqlSugar.RollbackTran();
  3038. return _jv;
  3039. }
  3040. }
  3041. }
  3042. #region 审批流程验证、创建
  3043. //审核验证 物品含有贵重物品 使用贵重物品审批流程
  3044. var auditTempInfo = new ApprovalProcessView();
  3045. if (isBatchVail) //贵重物品审核模板
  3046. {
  3047. auditTempInfo = await _approvalProcessRep.GetTemplateByBusinessTypeAsync(3);
  3048. }
  3049. else //其他物品审核模板
  3050. {
  3051. auditTempInfo = await _approvalProcessRep.GetTemplateByBusinessTypeAsync(2);
  3052. }
  3053. if (auditTempInfo == null)
  3054. {
  3055. _jv.Msg = $"未配置审核模板!";
  3056. _sqlSugar.RollbackTran();
  3057. return _jv;
  3058. }
  3059. if (auditTempInfo == null && !auditTempInfo.TempNodes.Any())
  3060. {
  3061. _jv.Msg = $"审核模板未配置节点!";
  3062. _sqlSugar.RollbackTran();
  3063. return _jv;
  3064. }
  3065. //创建审核流程
  3066. var firstNode = auditTempInfo.TempNodes.OrderBy(x => x.NodeOrder).First();
  3067. var flow = await _approvalProcessRep.GetFlowByBusinessAsync(goodsReceiveId, 1);
  3068. int flowId;
  3069. if (flow == null)
  3070. {
  3071. flow = new Sys_AuditFlow()
  3072. {
  3073. BusinessId = goodsReceiveId,
  3074. BusinessType = 1,
  3075. TemplateId = auditTempInfo.Id,
  3076. CurrentNodeId = firstNode.Id,
  3077. Status = 1,
  3078. };
  3079. flowId = await _sqlSugar.Insertable(flow).ExecuteReturnIdentityAsync();
  3080. if (flowId < 1)
  3081. {
  3082. _jv.Msg = $"审核流程添加失败!";
  3083. _sqlSugar.RollbackTran();
  3084. return _jv;
  3085. }
  3086. }
  3087. else flowId = flow.Id;
  3088. #endregion
  3089. //获取第一个节点的审核人员
  3090. var nodeUsers = firstNode.NodeUsers;
  3091. //删除旧的审核记录
  3092. var delRecords = await _sqlSugar.Deleteable<Sys_AuditRecord>()
  3093. .Where(x => x.FlowId == flowId && x.NodeId == firstNode.Id && x.NodeName == firstNode.NodeName)
  3094. .ExecuteCommandHasChangeAsync();
  3095. //创建审核记录
  3096. var records = nodeUsers.Select(user => new Sys_AuditRecord
  3097. {
  3098. FlowId = flowId,
  3099. NodeId = firstNode.Id,
  3100. NodeName = firstNode.NodeName,
  3101. AuditorId = user.UserId,
  3102. AuditorName = user.UserName,
  3103. AuditResult = 0, // 审核中
  3104. AuditTime = DateTime.Now
  3105. }).ToList();
  3106. var recordStatus = await _sqlSugar.Insertable(records).ExecuteCommandAsync();
  3107. if (recordStatus < 1)
  3108. {
  3109. _jv.Msg = $"审核记录创建失败!";
  3110. _sqlSugar.RollbackTran();
  3111. return _jv;
  3112. }
  3113. _sqlSugar.CommitTran();
  3114. _jv.Code = StatusCodes.Status200OK;
  3115. _jv.Data = new { sign = goodsReceiveId };
  3116. _jv.Msg = $"操作成功!";
  3117. return _jv;
  3118. }
  3119. /// <summary>
  3120. /// 物品领用 批量 List
  3121. /// </summary>
  3122. /// <param name="dto"></param>
  3123. /// <param name="currUserId"></param>
  3124. /// <returns></returns>
  3125. public async Task<JsonView> GoodsReceiveBatchListAsync(GoodsReceiveBatchListDto dto)
  3126. {
  3127. int currUserId = dto.CurrUserId;
  3128. string goodsName = dto.GoodsName;
  3129. var showAllPeopleIds = new List<int>() {
  3130. 374, // 罗颖
  3131. 233, // 刘华举
  3132. 208, // 雷怡
  3133. 343, // 陈湘
  3134. };
  3135. var isShowAllPeople = showAllPeopleIds.Any(x => x == currUserId);
  3136. RefAsync<int> total = 0;
  3137. var receiveList = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  3138. .LeftJoin<Sys_Users>((x, y) => x.CreateUserId == y.Id)
  3139. .Where((x, y) => x.IsDel == 0 && x.GoodsId == 0)
  3140. .WhereIF(!string.IsNullOrEmpty(goodsName), (x, y) => x.GoodsName.Contains(goodsName))
  3141. .WhereIF(!isShowAllPeople, (x, y) => x.CreateUserId == currUserId)
  3142. .OrderByDescending((x, y) => x.CreateTime)
  3143. .Select((x, y) => new GoodsReceiveBatchListView()
  3144. {
  3145. Id = x.Id,
  3146. GoodsName = x.GoodsName,
  3147. AccumQty = SqlFunc.Subqueryable<Pm_GoodsReceiveDetails>().Where(z => z.IsDel == 0 && z.GoodsReceiveId == x.Id).Sum(z => z.Quantity),
  3148. Reason = x.Reason,
  3149. Remark = x.Remark,
  3150. Status = x.AuditStatus,
  3151. Applicant = y.CnName,
  3152. ApplyTime = x.CreateTime,
  3153. })
  3154. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  3155. foreach (var item in receiveList)
  3156. {
  3157. var detailsText = new StringBuilder();
  3158. if (item.AccumQty > 0)
  3159. {
  3160. var goodsInfos = await _sqlSugar.Queryable<Pm_GoodsReceiveDetails>()
  3161. .InnerJoin<Pm_GoodsInfo>((x, y) => x.GoodsId == y.Id)
  3162. .LeftJoin<Sys_SetData>((x, y, z) => y.Type == z.Id)
  3163. .Where((x, y, z) => x.IsDel == 0 && x.GoodsReceiveId == item.Id)
  3164. .Select((x, y, z) => new
  3165. {
  3166. goodsId = x.GoodsId,
  3167. goodsName = y.Name,
  3168. goodsType = z.Name,
  3169. quantity = x.Quantity,
  3170. remark = x.Remark,
  3171. })
  3172. .ToListAsync();
  3173. if (goodsInfos.Any())
  3174. {
  3175. int index = 1;
  3176. goodsInfos.ForEach(x =>
  3177. {
  3178. detailsText.Append($"{index}.{x.goodsName}({x.goodsType}) 数量:{x.quantity:#0.00} 备注:{x.remark ?? "-"} <br/>");
  3179. index++;
  3180. });
  3181. }
  3182. }
  3183. else detailsText.Append($"暂无数据!");
  3184. item.GoodsDetails = detailsText.ToString();
  3185. }
  3186. _jv.Code = StatusCodes.Status200OK;
  3187. _jv.Msg = $"操作成功!";
  3188. _jv.Data = receiveList;
  3189. _jv.Count = total;
  3190. return _jv;
  3191. }
  3192. /// <summary>
  3193. /// 物品领用 批量 详情
  3194. /// </summary>
  3195. /// <param name="dto"></param>
  3196. /// <param name="currUserId"></param>
  3197. /// <returns></returns>
  3198. public async Task<JsonView> GoodsReceiveBatchInfoAsync(int id)
  3199. {
  3200. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  3201. .Where(x => x.IsDel == 0 && x.Id == id)
  3202. .FirstAsync();
  3203. if (receiveInfo == null)
  3204. {
  3205. _jv.Msg = "暂无数据!";
  3206. return _jv;
  3207. }
  3208. var info = new GoodsReceiveBatchView
  3209. {
  3210. Id = receiveInfo.Id,
  3211. GroupId = receiveInfo.GroupId,
  3212. Reason = receiveInfo.Reason,
  3213. IsReplace = receiveInfo.IsReplace,
  3214. Remark = receiveInfo.Remark,
  3215. };
  3216. info.ReceiveDetails = await _sqlSugar
  3217. .Queryable<Pm_GoodsReceiveDetails>()
  3218. .LeftJoin<Pm_GoodsInfo>((x, y) => x.GoodsId == y.Id)
  3219. .Where((x, y) => x.IsDel == 0 && x.GoodsReceiveId == receiveInfo.Id)
  3220. .Select((x, y) => new GoodsReceiveDetailsView()
  3221. {
  3222. Id = x.Id,
  3223. GoodsId = x.GoodsId,
  3224. GoodsName = y.Name,
  3225. Quantity = x.Quantity,
  3226. Remark = x.Remark,
  3227. })
  3228. .ToArrayAsync();
  3229. _jv.Code = StatusCodes.Status200OK;
  3230. _jv.Msg = $"操作成功!";
  3231. _jv.Data = info;
  3232. return _jv;
  3233. }
  3234. /// <summary>
  3235. /// 物品领用 审核 通过
  3236. /// </summary>
  3237. /// <param name="id"></param>
  3238. /// <param name="currUserId"></param>
  3239. /// <returns></returns>
  3240. public async Task<JsonView> GoodsReceiveApproveAsync(int id, int currUserId)
  3241. {
  3242. var appId = id;
  3243. //验证审核流程
  3244. var flow = await _approvalProcessRep.GetFlowByBusinessAsync(appId, 1);
  3245. if (flow == null)
  3246. {
  3247. _jv.Msg = $"审核流程不存在";
  3248. return _jv;
  3249. }
  3250. if (flow.Status != 1)
  3251. {
  3252. _jv.Msg = $"当前流程不在审核中";
  3253. return _jv;
  3254. }
  3255. //获取当前节点
  3256. var currNode = (await _approvalProcessRep.GetTemplateNodesAsync(flow.TemplateId)).FirstOrDefault(x => x.Id == flow.CurrentNodeId);
  3257. if (currNode == null)
  3258. {
  3259. _jv.Msg = $"当前审核节点不存在";
  3260. return _jv;
  3261. }
  3262. //检查用户是否有权审核
  3263. var canAudit = (await _approvalProcessRep.GetTemplateNodeUsersAsync(currNode.Id))
  3264. .Any(x => x.UserId == currUserId);
  3265. if (!canAudit)
  3266. {
  3267. _jv.Msg = $"您无权审核此领用";
  3268. return _jv;
  3269. }
  3270. //检查是否已审核过
  3271. var existingRecord = await _sqlSugar.Queryable<Sys_AuditRecord>()
  3272. .Where(x => x.FlowId == flow.Id && x.NodeId == currNode.Id && x.AuditorId == currUserId)
  3273. .FirstAsync();
  3274. if (existingRecord != null && existingRecord.AuditResult == 1)
  3275. {
  3276. _jv.Msg = $"您已审核过此领用";
  3277. return _jv;
  3278. }
  3279. // 创建或更新审核记录
  3280. var currUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == currUserId)?.CnName ?? "-";
  3281. var record = existingRecord ?? new Sys_AuditRecord
  3282. {
  3283. FlowId = flow.Id,
  3284. NodeId = currNode.Id,
  3285. NodeName = currNode.NodeName,
  3286. AuditorId = currUserId,
  3287. AuditorName = currUserName,
  3288. AuditResult = 1, // 通过
  3289. AuditTime = DateTime.Now,
  3290. AuditOpinion = "",
  3291. };
  3292. _sqlSugar.BeginTran();
  3293. if (existingRecord == null) await _sqlSugar.Insertable(record).ExecuteCommandAsync();
  3294. else
  3295. {
  3296. record.AuditResult = 1; // 通过
  3297. record.AuditTime = DateTime.Now;
  3298. await _sqlSugar.Updateable(record).ExecuteCommandAsync();
  3299. }
  3300. //检查节点是否完成
  3301. var nodeRecords = await _sqlSugar.Queryable<Sys_AuditRecord>()
  3302. .Where(x => x.FlowId == flow.Id && x.NodeId == currNode.Id)
  3303. .ToListAsync();
  3304. bool isNodeComplete = false;
  3305. if (currNode.ApproveType == 2) // 或签模式
  3306. {
  3307. //或签逻辑:任意一人通过即通过
  3308. if (nodeRecords.Any(x => x.AuditResult == 1))
  3309. {
  3310. isNodeComplete = true;
  3311. // 标记其他未审核的记录为"无需处理"
  3312. var unprocessed = nodeRecords
  3313. .Where(x => x.AuditResult == 0 && x.AuditorId != currUserId)
  3314. .ToList();
  3315. foreach (var r in unprocessed)
  3316. {
  3317. r.AuditResult = 3; // 无需处理
  3318. r.AuditTime = DateTime.Now;
  3319. r.AuditOpinion = "其他审核人已处理";
  3320. }
  3321. if (unprocessed.Any())
  3322. {
  3323. await _sqlSugar.Updateable(unprocessed).ExecuteCommandAsync();
  3324. }
  3325. }
  3326. }
  3327. else // 会签模式
  3328. {
  3329. // 会签逻辑:全部通过才通过
  3330. isNodeComplete = nodeRecords.All(x => x.AuditResult == 1);
  3331. }
  3332. if (isNodeComplete)
  3333. {
  3334. // 查找下一个节点
  3335. var nextNode = (await _approvalProcessRep.GetTemplateNodesAsync(flow.TemplateId))
  3336. .Where(x => x.NodeOrder > currNode.NodeOrder)
  3337. .OrderBy(x => x.NodeOrder)
  3338. .FirstOrDefault();
  3339. if (nextNode == null)
  3340. {
  3341. // 所有节点完成,审核通过
  3342. flow.Status = 2; // 已完成
  3343. flow.CurrentNodeId = 0;
  3344. // 更新业务状态为审核通过
  3345. var auditStatus = await UpdateStatusAsync(appId, GoodsAuditEnum.OutConfirmed);
  3346. if (!auditStatus)
  3347. {
  3348. _jv.Msg = $"审核失败!";
  3349. _sqlSugar.RollbackTran();
  3350. return _jv;
  3351. }
  3352. // 扣除库存
  3353. var deductStatus = await DeductStockAsync(appId);
  3354. if (!deductStatus)
  3355. {
  3356. _jv.Msg = $"库存扣除失败!";
  3357. _sqlSugar.RollbackTran();
  3358. return _jv;
  3359. }
  3360. }
  3361. else
  3362. {
  3363. // 进入下一个节点
  3364. flow.CurrentNodeId = nextNode.Id;
  3365. // 更新业务状态为审核中
  3366. var auditStatus = await UpdateStatusAsync(appId, GoodsAuditEnum.OutConfirming);
  3367. if (!auditStatus)
  3368. {
  3369. _jv.Msg = $"审核状态更新失败!";
  3370. _sqlSugar.RollbackTran();
  3371. return _jv;
  3372. }
  3373. // 为下一个节点创建审核记录
  3374. var nextNodeUsers = await _approvalProcessRep.GetTemplateNodeUsersAsync(nextNode.Id);
  3375. var records = nextNodeUsers.Select(user => new Sys_AuditRecord
  3376. {
  3377. FlowId = flow.Id,
  3378. NodeId = nextNode.Id,
  3379. NodeName = nextNode.NodeName,
  3380. AuditorId = user.UserId,
  3381. AuditorName = user.UserName,
  3382. AuditResult = 0,
  3383. AuditTime = DateTime.Now
  3384. }).ToList();
  3385. var recordStatus = await _sqlSugar.Insertable(records).ExecuteCommandAsync();
  3386. if (recordStatus < 1)
  3387. {
  3388. _jv.Msg = $"下一节点审批记录创建失败!";
  3389. _sqlSugar.RollbackTran();
  3390. return _jv;
  3391. }
  3392. }
  3393. var flowStatus = await _sqlSugar.Updateable(flow).ExecuteCommandAsync();
  3394. if (flowStatus < 1)
  3395. {
  3396. _jv.Msg = $"审批流程更新失败!";
  3397. _sqlSugar.RollbackTran();
  3398. return _jv;
  3399. }
  3400. }
  3401. _sqlSugar.CommitTran();
  3402. _jv.Code = StatusCodes.Status200OK;
  3403. _jv.Msg = $"操作成功!";
  3404. return _jv;
  3405. }
  3406. /// <summary>
  3407. /// 物品领用 审核 拒绝
  3408. /// </summary>
  3409. /// <param name="id"></param>
  3410. /// <param name="currUserId"></param>
  3411. /// <returns></returns>
  3412. public async Task<JsonView> GoodsReceiveRejectAsync(int id, int currUserId)
  3413. {
  3414. var appId = id;
  3415. //验证领用单
  3416. var receviveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == appId);
  3417. if (receviveInfo == null)
  3418. {
  3419. _jv.Msg = $"当前领用信息不存在";
  3420. return _jv;
  3421. }
  3422. //验证审核流程
  3423. var flow = await _approvalProcessRep.GetFlowByBusinessAsync(appId, 1);
  3424. if (flow == null)
  3425. {
  3426. _jv.Msg = $"审核流程不存在";
  3427. return _jv;
  3428. }
  3429. //if (flow.Status != 1)
  3430. //{
  3431. // _jv.Msg = $"当前流程不在审核中";
  3432. // return _jv;
  3433. //}
  3434. //获取当前节点
  3435. var currNode = (await _approvalProcessRep.GetTemplateNodesAsync(flow.TemplateId)).FirstOrDefault(x => x.Id == flow.CurrentNodeId);
  3436. if (currNode == null)
  3437. {
  3438. _jv.Msg = $"当前审核节点不存在";
  3439. return _jv;
  3440. }
  3441. //检查用户是否有权审核
  3442. var canAudit = (await _approvalProcessRep.GetTemplateNodeUsersAsync(currNode.Id))
  3443. .Any(x => x.UserId == currUserId);
  3444. if (!canAudit)
  3445. {
  3446. _jv.Msg = $"您无权审核此申请";
  3447. return _jv;
  3448. }
  3449. //检查是否已审核过
  3450. var existingRecord = await _sqlSugar.Queryable<Sys_AuditRecord>()
  3451. .Where(x => x.FlowId == flow.Id && x.NodeId == currNode.Id && x.AuditorId == currUserId)
  3452. .FirstAsync();
  3453. if (existingRecord != null && existingRecord.AuditResult == 2)
  3454. {
  3455. _jv.Msg = $"您已审核过此申请";
  3456. return _jv;
  3457. }
  3458. //验证是否已经审核通过
  3459. bool isApproved = receviveInfo.AuditStatus == GoodsAuditEnum.OutConfirmed;
  3460. _sqlSugar.BeginTran();
  3461. if (isApproved) //审核通过拒绝
  3462. {
  3463. //回滚库存
  3464. var rollbackStatus = await RollbackStockAsync(appId);
  3465. if (!rollbackStatus)
  3466. {
  3467. _jv.Msg = $"物品库存回滚失败!";
  3468. _sqlSugar.RollbackTran();
  3469. return _jv;
  3470. }
  3471. }
  3472. //更新流程状态为已拒绝
  3473. flow.Status = 3; // 已拒绝
  3474. flow.CurrentNodeId = currNode.Id;
  3475. var flowStatus = await _sqlSugar.Updateable(flow).ExecuteCommandAsync();
  3476. if (flowStatus < 1)
  3477. {
  3478. _jv.Msg = $"流程状态更新失败!";
  3479. _sqlSugar.RollbackTran();
  3480. return _jv;
  3481. }
  3482. // 更新领用状态为已拒绝
  3483. var receviveStatus = await UpdateStatusAsync(appId, GoodsAuditEnum.OutRejected); // 已拒绝
  3484. if (!receviveStatus)
  3485. {
  3486. _jv.Msg = $"领用物品状态更新失败!";
  3487. _sqlSugar.RollbackTran();
  3488. return _jv;
  3489. }
  3490. // 创建拒绝记录
  3491. var currUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == currUserId)?.CnName ?? "-";
  3492. var record = new Sys_AuditRecord
  3493. {
  3494. FlowId = flow.Id,
  3495. NodeId = flow.CurrentNodeId,
  3496. NodeName = $"{currNode.NodeName}-撤销",
  3497. AuditorId = currUserId,
  3498. AuditorName = currUserName,
  3499. AuditResult = 2, // 拒绝
  3500. AuditTime = DateTime.Now,
  3501. AuditOpinion = $"用户操作撤销",
  3502. };
  3503. var recordStatus = await _sqlSugar.Insertable(record).ExecuteCommandAsync();
  3504. if (recordStatus < 1)
  3505. {
  3506. _jv.Msg = $"领用物品拒绝记录创建失败!";
  3507. _sqlSugar.RollbackTran();
  3508. return _jv;
  3509. }
  3510. _sqlSugar.CommitTran();
  3511. _jv.Code = StatusCodes.Status200OK;
  3512. _jv.Msg = $"操作成功!";
  3513. return _jv;
  3514. }
  3515. /// <summary>
  3516. /// 物品待审核数量
  3517. /// </summary>
  3518. /// <param name="goodsId"></param>
  3519. /// <returns></returns>
  3520. public async Task<decimal> GoodsAwaitQuantityAsync(int goodsId)
  3521. {
  3522. decimal quantity = 0.00M;
  3523. //单条领用 待审核、确认中 物品数量
  3524. var waitAuditQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>().Where(x => x.IsDel == 0 &&
  3525. x.GoodsId == goodsId &&
  3526. (x.AuditStatus == GoodsAuditEnum.Pending || x.AuditStatus == GoodsAuditEnum.OutConfirming)
  3527. ).SumAsync(x => x.Quantity);
  3528. //批量领用 待审核、确认中 物品数量
  3529. var waitAuditBatchQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  3530. .InnerJoin<Pm_GoodsReceiveDetails>((gr, grd) => gr.Id == grd.GoodsReceiveId)
  3531. .Where((gr, grd) => gr.IsDel == 0 &&
  3532. (gr.AuditStatus == GoodsAuditEnum.Pending || gr.AuditStatus == GoodsAuditEnum.OutConfirming) &&
  3533. grd.GoodsId == goodsId
  3534. )
  3535. .Select((gr, grd) => new
  3536. {
  3537. grd.GoodsReceiveId,
  3538. gr.AuditStatus,
  3539. grd.Quantity
  3540. })
  3541. .SumAsync(gr => gr.Quantity);
  3542. quantity = waitAuditQuantity + waitAuditBatchQuantity;
  3543. return quantity;
  3544. }
  3545. /// <summary>
  3546. /// 更改领用状态
  3547. /// </summary>
  3548. /// <param name="id"></param>
  3549. /// <param name="status"></param>
  3550. /// <returns></returns>
  3551. public async Task<bool> UpdateStatusAsync(int id, GoodsAuditEnum status)
  3552. {
  3553. return await _sqlSugar.Updateable<Pm_GoodsReceive>()
  3554. .SetColumns(x => x.AuditStatus == status)
  3555. .Where(x => x.Id == id)
  3556. .ExecuteCommandHasChangeAsync();
  3557. }
  3558. /// <summary>
  3559. /// 领用状态完成扣除库存
  3560. /// </summary>
  3561. /// <param name="id"></param>
  3562. /// <param name="status"></param>
  3563. /// <returns></returns>
  3564. public async Task<bool> DeductStockAsync(int id)
  3565. {
  3566. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == id);
  3567. if (receiveInfo == null) return false;
  3568. var receiveDetails = await _sqlSugar.Queryable<Pm_GoodsReceiveDetails>()
  3569. .Where(x => x.IsDel == 0 && x.GoodsReceiveId == id)
  3570. .ToListAsync();
  3571. if (!receiveDetails.Any()) return false;
  3572. var goodsInfos = new List<Pm_GoodsInfo>();
  3573. foreach (var item in receiveDetails)
  3574. {
  3575. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == item.GoodsId);
  3576. if (goodsInfo == null) return false;
  3577. //1、物品库存扣除
  3578. goodsInfo.StockQuantity -= item.Quantity;
  3579. goodsInfo.OQ_Total += item.Quantity;
  3580. //2、入库批次关联领用人 更改批次库存
  3581. var goodsStorages = await _sqlSugar
  3582. .Queryable<Pm_GoodsStorage>()
  3583. .Where(x => x.IsDel == 0 &&
  3584. x.GoodsId == item.GoodsId &&
  3585. x.ConfirmStatus == GoodsConfirmEnum.Confirmed &&
  3586. (x.Quantity - x.ReceiveQuantity) > 0
  3587. )
  3588. .OrderBy(x => x.CreateTime)
  3589. .ToListAsync();
  3590. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  3591. var batchStorageInfos = new List<Pm_GoodsStorage>();
  3592. var receiveQuantity = 0.00M; //领用总数量
  3593. foreach (var storage in goodsStorages)
  3594. {
  3595. if (item.Quantity == receiveQuantity) break;
  3596. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  3597. if (thisBatchSurplusQuantity <= 0.00M) continue;
  3598. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  3599. const decimal unit = 0.50M;
  3600. while (receiveQuantity < storage.Quantity)
  3601. {
  3602. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity || receiveQuantity == item.Quantity) break;
  3603. thisBatchReceiveQuantity += unit;
  3604. receiveQuantity += unit;
  3605. }
  3606. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  3607. {
  3608. StorageId = storage.Id,
  3609. Quantity = thisBatchReceiveQuantity
  3610. });
  3611. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  3612. batchStorageInfos.Add(storage);
  3613. }
  3614. //3 更改批次库存
  3615. if (goodsReceiveInfos.Count > 0)
  3616. {
  3617. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  3618. .UpdateColumns(x => x.ReceiveQuantity)
  3619. .WhereColumns(x => x.Id)
  3620. .ExecuteCommandAsync();
  3621. if (edit1 < 1) return false;
  3622. }
  3623. //4 添加入库批次关联领用人
  3624. if (goodsReceiveInfos.Count > 0)
  3625. {
  3626. item.GoodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  3627. }
  3628. goodsInfos.Add(goodsInfo);
  3629. }
  3630. //库存更改
  3631. var editGoods = await _sqlSugar.Updateable(goodsInfos)
  3632. .UpdateColumns(x => new
  3633. {
  3634. x.StockQuantity,
  3635. x.OQ_Total,
  3636. })
  3637. .Where(x => x.Id == x.Id)
  3638. .ExecuteCommandAsync();
  3639. if (editGoods < 1) return false;
  3640. //领用明细更改
  3641. var editDetails = await _sqlSugar.Updateable(receiveDetails)
  3642. .UpdateColumns(x => new
  3643. {
  3644. x.GoodsStorageInfo,
  3645. })
  3646. .Where(x => x.GoodsReceiveId == x.GoodsReceiveId)
  3647. .ExecuteCommandAsync();
  3648. if (editDetails < 1) return false;
  3649. return true;
  3650. }
  3651. /// <summary>
  3652. /// 领用状态拒绝回滚所有物资的库存
  3653. /// </summary>
  3654. /// <param name="id"></param>
  3655. /// <param name="status"></param>
  3656. /// <returns></returns>
  3657. public async Task<bool> RollbackStockAsync(int id)
  3658. {
  3659. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == id);
  3660. if (receiveInfo == null) return false;
  3661. var receiveDetails = await _sqlSugar.Queryable<Pm_GoodsReceiveDetails>()
  3662. .Where(x => x.IsDel == 0 && x.GoodsReceiveId == id)
  3663. .ToListAsync();
  3664. if (!receiveDetails.Any()) return false;
  3665. var goodsInfos = new List<Pm_GoodsInfo>();
  3666. foreach (var item in receiveDetails)
  3667. {
  3668. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == item.GoodsId);
  3669. if (goodsInfo == null) return false;
  3670. //1、物品表库存回滚
  3671. var receiveQuantity = item.Quantity;
  3672. goodsInfo.StockQuantity += receiveQuantity;
  3673. goodsInfo.OQ_Total -= receiveQuantity;
  3674. //2、入库批次关联领用人 更改批次库存 回滚
  3675. var goodsStorages = await _sqlSugar
  3676. .Queryable<Pm_GoodsStorage>()
  3677. .Where(x => x.IsDel == 0 &&
  3678. x.GoodsId == receiveInfo.GoodsId &&
  3679. (x.Quantity - x.ReceiveQuantity) > 0
  3680. )
  3681. .OrderBy(x => x.CreateTime)
  3682. .ToListAsync();
  3683. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  3684. var batchStorageInfos = new List<Pm_GoodsStorage>();
  3685. foreach (var storage in goodsStorages)
  3686. {
  3687. if (receiveInfo.Quantity == receiveQuantity) break;
  3688. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  3689. if (thisBatchSurplusQuantity <= 0.00M) continue;
  3690. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  3691. const decimal unit = 0.50M;
  3692. while (receiveQuantity < receiveInfo.Quantity)
  3693. {
  3694. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  3695. thisBatchReceiveQuantity -= unit;
  3696. receiveQuantity -= unit;
  3697. }
  3698. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  3699. {
  3700. StorageId = storage.Id,
  3701. Quantity = thisBatchReceiveQuantity
  3702. });
  3703. storage.ReceiveQuantity -= thisBatchReceiveQuantity;
  3704. batchStorageInfos.Add(storage);
  3705. }
  3706. //3 批次库存 回滚
  3707. if (goodsReceiveInfos.Count > 0)
  3708. {
  3709. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  3710. .UpdateColumns(x => x.ReceiveQuantity)
  3711. .WhereColumns(x => x.Id)
  3712. .ExecuteCommandAsync();
  3713. if (edit1 < 1) return false;
  3714. }
  3715. item.GoodsStorageInfo = null;
  3716. goodsInfos.Add(goodsInfo);
  3717. }
  3718. //库存 回滚
  3719. var editGoods = await _sqlSugar.Updateable(goodsInfos)
  3720. .UpdateColumns(x => new
  3721. {
  3722. x.StockQuantity,
  3723. x.OQ_Total,
  3724. })
  3725. .Where(x => x.Id == x.Id)
  3726. .ExecuteCommandAsync();
  3727. if (editGoods < 1) return false;
  3728. //领用明细 回滚
  3729. var editDetails = await _sqlSugar.Updateable(receiveDetails)
  3730. .UpdateColumns(x => new
  3731. {
  3732. x.GoodsStorageInfo,
  3733. })
  3734. .Where(x => x.GoodsReceiveId == x.GoodsReceiveId)
  3735. .ExecuteCommandAsync();
  3736. if (editDetails < 1) return false;
  3737. return true;
  3738. }
  3739. #endregion
  3740. }
  3741. }