GoodsRepository.cs 161 KB

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