GoodsRepository.cs 150 KB

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