GoodsRepository.cs 169 KB

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