GoodsRepository.cs 168 KB

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