GoodsRepository.cs 169 KB

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