GoodsRepository.cs 168 KB

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