GoodsRepository.cs 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786
  1. using Aspose.Cells;
  2. using AutoMapper;
  3. using EyeSoft.Runtime.InteropServices;
  4. using Newtonsoft.Json;
  5. using OASystem.Domain;
  6. using OASystem.Domain.Dtos.PersonnelModule;
  7. using OASystem.Domain.Entities.Groups;
  8. using OASystem.Domain.Entities.PersonnelModule;
  9. using OASystem.Domain.ViewModels.PersonnelModule;
  10. using OASystem.Infrastructure.Tools;
  11. namespace OASystem.Infrastructure.Repositories.PersonnelModule
  12. {
  13. /// <summary>
  14. /// 物品进销存
  15. /// 仓储
  16. /// </summary>
  17. public class GoodsRepository : BaseRepository<Pm_GoodsInfo, GoodsInfoView>
  18. {
  19. private readonly IMapper _mapper;
  20. private JsonView _jv;
  21. private string _url;
  22. private string _excelPath;
  23. private List<int> _goodsTypeIds; //多部门审核物品类型Id
  24. public GoodsRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  25. {
  26. _mapper = mapper;
  27. _jv = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  28. _url = AppSettingsHelper.Get("ExcelBaseUrl");
  29. _excelPath = $"{AppSettingsHelper.Get("ExcelBasePath")}";
  30. if (!Directory.Exists(_excelPath))
  31. {
  32. Directory.CreateDirectory(_excelPath);
  33. }
  34. _goodsTypeIds = new List<int>() {
  35. 1423, //1423 贵重物品
  36. };
  37. }
  38. /// <summary>
  39. /// 基础数据
  40. /// </summary>
  41. /// <returns></returns>
  42. public async Task<JsonView> InitDataSource()
  43. {
  44. var typeData = await _sqlSugar.Queryable<GoodsTypeView>()
  45. .Includes(x => x.SubTypeItems.Where(z => z.IsDel == 0)
  46. //.Select(z => new {
  47. // z.Id,
  48. // z.STid,
  49. // z.Name
  50. //})
  51. .ToList())
  52. .Where(x => x.IsDel == 0 &&
  53. x.Remark.Equals("GoodsType"))
  54. //.Select(x => new {
  55. // x.Id,
  56. // x.Name,
  57. // x.SubTypeItems
  58. //})
  59. .ToListAsync();
  60. var groupData = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  61. .Where(x => x.IsDel == 0)
  62. .Select(x => new
  63. {
  64. id = x.Id,
  65. groupName = x.TeamName
  66. })
  67. .OrderByDescending(x => x.id)
  68. .ToListAsync();
  69. groupData.Insert(0, new { id = 0, groupName = "其他物资(公司内部物资)" });
  70. groupData.Insert(0, new { id = -1, groupName = "拜访客户所使用的物资" });
  71. groupData.Insert(0, new { id = -2, groupName = "库存调整" });
  72. var userData = await _sqlSugar.Queryable<Sys_Users>()
  73. .Where(x => x.IsDel == 0)
  74. .Select(x => new
  75. {
  76. x.Id,
  77. UserName = x.CnName,
  78. })
  79. .ToListAsync();
  80. //库存状态
  81. var stockStatus = new List<dynamic>() {
  82. new { Value = -1, Text = "全部" },
  83. new { Value = 0, Text = "待确认" },
  84. new { Value = 1, Text = "部分确认" },
  85. new { Value = 2, Text = "已确认" },
  86. new { Value = 3, Text = "已拒绝" },
  87. };
  88. //领用出库状态
  89. var receiveStatus = new List<dynamic>() {
  90. new { Value = -1, Text = "全部" },
  91. new { Value = GoodsAuditEnum.Pending, Text = GoodsAuditEnum.Pending.GetEnumDescription() },
  92. new { Value = GoodsAuditEnum.Approved, Text = GoodsAuditEnum.Approved.GetEnumDescription() },
  93. new { Value = GoodsAuditEnum.UnApproved, Text = GoodsAuditEnum.UnApproved.GetEnumDescription() },
  94. new { Value = GoodsAuditEnum.OutPending, Text = GoodsAuditEnum.OutPending.GetEnumDescription() },
  95. new { Value = GoodsAuditEnum.OutConfirming, Text = GoodsAuditEnum.OutConfirming.GetEnumDescription() },
  96. new { Value = GoodsAuditEnum.OutConfirmed, Text = GoodsAuditEnum.OutConfirmed.GetEnumDescription() },
  97. new { Value = GoodsAuditEnum.OutRejected, Text = GoodsAuditEnum.OutRejected.GetEnumDescription() },
  98. };
  99. _jv.Code = StatusCodes.Status200OK;
  100. _jv.Data = new { goodsTypeData = typeData, stockStatus = stockStatus, receiveStatus = receiveStatus, groupNameData = groupData, userNameData = userData };
  101. _jv.Msg = $"操作成功";
  102. return _jv;
  103. }
  104. /// <summary>
  105. /// 物品列表
  106. /// </summary>
  107. /// <param name="dto"></param>
  108. /// <returns></returns>
  109. public async Task<JsonView> GoodsList(GoodsListDto dto)
  110. {
  111. var ids = new List<int>();
  112. if (!string.IsNullOrEmpty(dto.TypeIds))
  113. {
  114. var strArray = dto.TypeIds.Split(',');
  115. foreach (var str in strArray)
  116. {
  117. if (int.TryParse(str, out int id))
  118. {
  119. ids.Add(id);
  120. }
  121. }
  122. }
  123. var auditEnums = new List<GoodsAuditEnum>() {
  124. GoodsAuditEnum.Pending,
  125. GoodsAuditEnum.OutPending,
  126. GoodsAuditEnum.OutConfirming
  127. };
  128. RefAsync<int> total = 0;
  129. var data = await _sqlSugar.Queryable<GoodsListView>()
  130. //.Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && z1.AuditStatus == GoodsAuditEnum.Pending).ToList())
  131. .Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && auditEnums.Contains(z1.AuditStatus)).ToList())
  132. .Includes(glv => glv.TypeData)
  133. .Includes(glv => glv.UserData)
  134. .LeftJoin<Sys_SetData>((glv, sd) => glv.Type == sd.Id)
  135. .LeftJoin<Sys_Users>((glv, sd, u) => glv.LastUpdateUserId == u.Id)
  136. .Where(glv => glv.IsDel == 0)
  137. .WhereIF(ids.Count > 0, glv => ids.Contains(glv.Type))
  138. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), glv => glv.Name.Contains(dto.GoodsName))
  139. .OrderByDescending(glv => glv.LastUpdateTime)
  140. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  141. var view = data.Select(x => new
  142. {
  143. x.Id,
  144. x.Name,
  145. x.Type,
  146. TypeName = x.TypeData?.Name ?? string.Empty,
  147. LastUpdateUserName = x.UserData?.CnName ?? string.Empty,
  148. x.LastUpdateTime,
  149. StockQuantity = x.StockQuantity - x.WaitAuditQuantity,
  150. x.Unit,
  151. x.StockQuantityLabel,
  152. x.Remark
  153. }).ToList();
  154. _jv.Code = StatusCodes.Status200OK;
  155. _jv.Data = view;
  156. _jv.Count = total;
  157. _jv.Msg = $"操作成功";
  158. return _jv;
  159. }
  160. /// <summary>
  161. /// 物品Info
  162. /// </summary>
  163. /// <param name="portType"></param>
  164. /// <param name="id"></param>
  165. /// <returns></returns>
  166. public async Task<JsonView> GoodsInfo(int portType, int id)
  167. {
  168. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  169. .LeftJoin<Sys_SetData>((gi, sd) => gi.Type == sd.Id)
  170. .LeftJoin<Sys_Users>((gi, sd, u1) => gi.LastUpdateUserId == u1.Id)
  171. .LeftJoin<Sys_Users>((gi, sd, u1, u2) => gi.CreateUserId == u2.Id)
  172. .Where((gi, sd, u1, u2) => gi.IsDel == 0 && gi.Id == id)
  173. .Select((gi, sd, u1, u2) => new
  174. {
  175. gi.Id,
  176. gi.Name,
  177. ParentType = sd.STid,
  178. gi.Type,
  179. TypeName = sd.Name,
  180. gi.SQ_Total,
  181. gi.OQ_Total,
  182. gi.PriceTotal,
  183. gi.StockQuantity,
  184. gi.Unit,
  185. gi.Remark,
  186. LastUpdateUserName = u1.CnName,
  187. gi.LastUpdateTime,
  188. CreateUserName = u2.CnName,
  189. gi.CreateTime,
  190. })
  191. .FirstAsync();
  192. _jv.Code = StatusCodes.Status200OK;
  193. _jv.Data = data;
  194. _jv.Msg = $"操作成功";
  195. return _jv;
  196. }
  197. /// <summary>
  198. /// 物品 OP(Create Or Edit)
  199. /// </summary>
  200. /// <param name="dto"></param>
  201. /// <param name="currUserId"></param>
  202. /// <returns></returns>
  203. public async Task<JsonView> GoodsOp(GoodsOpDto dto, int currUserId)
  204. {
  205. var info = new Pm_GoodsInfo()
  206. {
  207. Id = dto.Id,
  208. Name = dto.Name,
  209. Type = dto.Type,
  210. SQ_Total = 0,
  211. OQ_Total = 0,
  212. PriceTotal = 0,
  213. StockQuantity = 0,
  214. Unit = dto.Unit,
  215. Remark = dto.Remark,
  216. LastUpdateUserId = currUserId,
  217. LastUpdateTime = DateTime.Now,
  218. CreateUserId = currUserId
  219. };
  220. if (dto.Id > 0) //Edit
  221. {
  222. var upd = await _sqlSugar.Updateable(info)
  223. .UpdateColumns(x => new
  224. {
  225. x.Name,
  226. x.Type,
  227. x.Unit,
  228. x.Remark,
  229. x.LastUpdateUserId,
  230. x.LastUpdateTime,
  231. })
  232. .ExecuteCommandAsync();
  233. if (upd > 0)
  234. {
  235. _jv.Msg = $"修改成功!";
  236. _jv.Code = StatusCodes.Status200OK;
  237. return _jv;
  238. }
  239. }
  240. else if (dto.Id < 1) //添加
  241. {
  242. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Name.Equals(info.Name));
  243. if (selectInfo != null)
  244. {
  245. _jv.Msg = $"“{info.Name}”该物品已存在,请勿重新添加!";
  246. return _jv;
  247. }
  248. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  249. if (add > 0)
  250. {
  251. _jv.Msg = $"添加成功!";
  252. _jv.Code = StatusCodes.Status200OK;
  253. return _jv;
  254. }
  255. }
  256. return _jv;
  257. }
  258. /// <summary>
  259. /// 物品 Del
  260. /// </summary>
  261. /// <param name="id"></param>
  262. /// <param name="currUserId"></param>
  263. /// <returns></returns>
  264. public async Task<JsonView> GoodsDel(int id, int currUserId)
  265. {
  266. _sqlSugar.BeginTran();
  267. var goods = await _sqlSugar.Updateable<Pm_GoodsInfo>()
  268. .SetColumns(x => new Pm_GoodsInfo()
  269. {
  270. IsDel = 1,
  271. DeleteUserId = currUserId,
  272. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  273. })
  274. .Where(x => x.Id == id)
  275. .ExecuteCommandAsync();
  276. if (goods < 1)
  277. {
  278. _sqlSugar.RollbackTran();
  279. _jv.Msg = $"操作失败";
  280. return _jv;
  281. }
  282. var goodsStorage = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  283. .SetColumns(x => new Pm_GoodsStorage()
  284. {
  285. IsDel = 1,
  286. DeleteUserId = currUserId,
  287. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  288. })
  289. .Where(x => x.Id == id)
  290. .ExecuteCommandAsync();
  291. var goodsReceive = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  292. .SetColumns(x => new Pm_GoodsReceive()
  293. {
  294. IsDel = 1,
  295. DeleteUserId = currUserId,
  296. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  297. })
  298. .Where(x => x.Id == id)
  299. .ExecuteCommandAsync();
  300. _sqlSugar.CommitTran();
  301. _jv.Code = StatusCodes.Status200OK;
  302. _jv.Msg = $"操作成功!";
  303. return _jv;
  304. }
  305. /// <summary>
  306. /// 入库/出库 审核类型
  307. /// 根据物品类型 处理审核是否需要多部门审核
  308. /// </summary>
  309. /// <param name="goodsTypeId">物品类型Id </param>
  310. /// <returns></returns>
  311. public bool GoodsAuditType(int goodsTypeId)
  312. {
  313. if (_goodsTypeIds.Contains(goodsTypeId))
  314. {
  315. return true;
  316. }
  317. return false;
  318. }
  319. /// <summary>
  320. /// 物品入库列表(带审核)
  321. /// </summary>
  322. /// <param name="dto"></param>
  323. /// <returns></returns>
  324. public async Task<JsonView> GoodsStorageList(GoodsStorageListDto dto)
  325. {
  326. string reqAuditLabel = dto.AuditLabel;
  327. var auditLabel = Array.Empty<int>();
  328. int userId = dto.CurrUserId;
  329. if (!string.IsNullOrEmpty(reqAuditLabel))
  330. {
  331. if (!reqAuditLabel.Contains("-1"))
  332. {
  333. auditLabel = reqAuditLabel
  334. .Split(',')
  335. .Select(x =>
  336. {
  337. if (int.TryParse(x, out var id)) return id;
  338. return id;
  339. })
  340. .ToArray();
  341. }
  342. }
  343. RefAsync<int> total = 0;
  344. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  345. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  346. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  347. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  348. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  349. .WhereIF(dto.GoodsId > 0, (gs, gi, u, u1) => gs.GoodsId == dto.GoodsId)
  350. .WhereIF(auditLabel.Length > 0, (gs, gi, u, u1) => auditLabel.Contains((int)gs.ConfirmStatus))
  351. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gs, gi, u, u1) => gi.Name.Contains(dto.GoodsName))
  352. .WhereIF(!string.IsNullOrEmpty(dto.BatchNo), (gs, gi, u, u1) => gs.BatchNo.Contains(dto.BatchNo))
  353. .Select((gs, gi, u, u1) => new GoodsStorageListView()
  354. {
  355. Id = gs.Id,
  356. GoodsId = gs.GoodsId,
  357. GoodsType = gi.Type,
  358. BatchNo = gs.BatchNo,
  359. GoodsName = gi.Name,
  360. Quantity = gs.Quantity,
  361. UnitPrice = gs.UnitPrice,
  362. TotalPrice = gs.TotalPrice,
  363. SupplierName = gs.SupplierName,
  364. SupplierTel = gs.SupplierTel,
  365. SupplierAddress = gs.SupplierAddress,
  366. SupplierSource = gs.SupplierSource,
  367. StorageUserName = u1.CnName,
  368. StorageTime = gs.StorageTime,
  369. CreateUserName = u.CnName,
  370. ConfirmStatus = gs.ConfirmStatus,
  371. StatusDesc = gs.StatusDesc,
  372. CreateTime = gs.CreateTime,
  373. Remark = gs.Remark
  374. })
  375. .OrderByDescending(gs => gs.CreateTime)
  376. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  377. var auditList = GoodsStorageConfirmAuditDep(1);
  378. var hrAuditPer = false;
  379. var finAuditPer = false;
  380. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  381. var finAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Financial);
  382. if (hrAuditInfo != null)
  383. {
  384. if (hrAuditInfo.AuditorIds.Any(x => x == userId)) hrAuditPer = true;
  385. }
  386. if (finAuditInfo != null)
  387. {
  388. if (finAuditInfo.AuditorIds.Any(x => x == userId)) finAuditPer = true;
  389. }
  390. foreach (var item in data)
  391. {
  392. var auditDeps = new List<GoodsStorageAuditPerView>();
  393. auditDeps.Add(new() { AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = GoodsAuditDepEnum.Hr.GetEnumDescription() });
  394. var auditPer = GoodsAuditType(item.GoodsType);
  395. if (auditPer)
  396. {
  397. auditDeps.Add(new() { AuditPer = finAuditPer, AuditDep = GoodsAuditDepEnum.Financial, ButtonText = GoodsAuditDepEnum.Financial.GetEnumDescription() });
  398. }
  399. item.AuditPers = auditDeps.ToArray();
  400. }
  401. _jv.Code = StatusCodes.Status200OK;
  402. _jv.Data = data;
  403. _jv.Count = total;
  404. _jv.Msg = $"操作成功";
  405. return _jv;
  406. }
  407. /// <summary>
  408. /// 物品入库详情
  409. /// </summary>
  410. /// <param name="id"></param>
  411. /// <returns></returns>
  412. public async Task<JsonView> GoodsStorageInfo(int portType, int id)
  413. {
  414. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  415. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  416. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  417. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  418. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  419. .WhereIF(id > 0, (gs, gi, u, u1) => gs.Id == id)
  420. .Select((gs, gi, u, u1) => new
  421. {
  422. gs.Id,
  423. gs.GoodsId,
  424. GoodsName = gi.Name,
  425. gs.Quantity,
  426. gs.UnitPrice,
  427. gs.TotalPrice,
  428. gs.SupplierName,
  429. gs.SupplierTel,
  430. gs.SupplierAddress,
  431. gs.SupplierSource,
  432. gs.ReceiveQuantity,
  433. gs.StorageUserId,
  434. StorageUser = u1.CnName,
  435. gs.StorageTime,
  436. CreateUserName = u.CnName,
  437. gs.CreateUserId,
  438. gs.CreateTime,
  439. gs.Remark,
  440. //gs.IsInConfirm
  441. })
  442. .FirstAsync();
  443. _jv.Msg = $"操作成功!";
  444. _jv.Code = StatusCodes.Status200OK;
  445. _jv.Data = data;
  446. return _jv;
  447. }
  448. /// <summary>
  449. /// 物品入库 操作(Create Or Edit)
  450. /// </summary>
  451. /// <param name="dto"></param>
  452. /// <param name="currUserId"></param>
  453. /// <returns></returns>
  454. public async Task<JsonView> GoodsStorageOp(GoodsStorageOpDto dto, int currUserId)
  455. {
  456. var info = _mapper.Map<Pm_GoodsStorage>(dto);
  457. info.CreateUserId = currUserId;
  458. info.BatchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff");
  459. _sqlSugar.BeginTran();
  460. if (info.Id > 0) //修改
  461. {
  462. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>().FirstAsync(x => x.Id == dto.Id);
  463. var auditStatus = selectInfo.ConfirmStatus;
  464. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  465. {
  466. _sqlSugar.RollbackTran();
  467. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可更改!如若更改请取消已确认或部分确认审核状态!";
  468. return _jv;
  469. }
  470. var storageEdit = await _sqlSugar.Updateable(info)
  471. .UpdateColumns(x => new
  472. {
  473. x.Quantity,
  474. x.UnitPrice,
  475. x.TotalPrice,
  476. x.SupplierName,
  477. x.SupplierTel,
  478. x.SupplierAddress,
  479. x.SupplierSource,
  480. x.StorageUserId,
  481. x.StorageTime
  482. })
  483. .Where(x => x.Id == dto.Id)
  484. .ExecuteCommandAsync();
  485. if (storageEdit < 1)
  486. {
  487. _sqlSugar.RollbackTran();
  488. _jv.Msg = $"修改失败!";
  489. return _jv;
  490. }
  491. //入库确认默认状态 多条 or 单条
  492. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.Type == 1 && x.DataId == dto.Id).ToListAsync();
  493. if (auditInfos.Any())
  494. {
  495. var isAuditPer = false;
  496. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.GoodsId);
  497. if (goodsInfo != null)
  498. {
  499. isAuditPer = GoodsAuditType(goodsInfo.Type);
  500. }
  501. if (!isAuditPer && auditInfos.Count == 1)
  502. {
  503. var auditInfo = new Pm_GoodsAudit(1, GoodsAuditDepEnum.Financial, dto.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  504. await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  505. }
  506. }
  507. }
  508. else if (info.Id < 1) //添加
  509. {
  510. info.ConfirmStatus = GoodsConfirmEnum.WaitConfirm;
  511. var storageAddId = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  512. if (storageAddId < 1)
  513. {
  514. _sqlSugar.RollbackTran();
  515. _jv.Msg = $"添加失败!";
  516. return _jv;
  517. }
  518. //入库确认默认状态
  519. List<Pm_GoodsAudit> auditInfos = GoodsStorageConfirm(1, storageAddId, info.CreateUserId);
  520. if (auditInfos.Any())
  521. {
  522. //验证添加多条或者单条审核状态
  523. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.GoodsId);
  524. if (goodsInfo != null)
  525. {
  526. var isAuditPer = GoodsAuditType(goodsInfo.Type);
  527. if (!isAuditPer) auditInfos = auditInfos.Where(x => x.Dep == GoodsAuditDepEnum.Hr).ToList();
  528. }
  529. await _sqlSugar.Insertable(auditInfos).ExecuteCommandAsync();
  530. }
  531. }
  532. _sqlSugar.CommitTran();
  533. _jv.Msg = $"操作成功!";
  534. _jv.Code = StatusCodes.Status200OK;
  535. return _jv;
  536. }
  537. /// <summary>
  538. /// 物品入库 确认操作-默认审核部门
  539. /// </summary>
  540. /// <param name="auditType">
  541. /// 审核类型
  542. /// 1.入库 2.出库
  543. /// </param>
  544. /// <param name="dataId">dataId</param>
  545. /// <param name="currUserId">审核人</param>
  546. /// <returns></returns>
  547. public List<Pm_GoodsAudit> GoodsStorageConfirm(int auditType, int dataId, int currUserId)
  548. {
  549. var goodsAuditList = new List<Pm_GoodsAudit>();
  550. goodsAuditList.Add(new Pm_GoodsAudit(auditType, GoodsAuditDepEnum.Hr, dataId, GoodsConfirmEnum.WaitConfirm, currUserId));
  551. goodsAuditList.Add(new Pm_GoodsAudit(auditType, GoodsAuditDepEnum.Financial, dataId, GoodsConfirmEnum.WaitConfirm, currUserId));
  552. return goodsAuditList;
  553. }
  554. /// <summary>
  555. /// 物品入库 确认操作
  556. /// </summary>
  557. /// <param name="dto"></param>
  558. /// <returns></returns>
  559. public async Task<JsonView> GoodsStorageConfirmStatusChange(GoodsStorageConfirmDto dto, int currUserId)
  560. {
  561. int gsId = dto.Id;
  562. var auditDep = dto.AuditDep;
  563. if (gsId < 1)
  564. {
  565. _jv.Msg = string.Format("{0}", MsgTips.Id);
  566. return _jv;
  567. }
  568. if (currUserId < 1)
  569. {
  570. _jv.Msg = string.Format("{0}", MsgTips.UserId);
  571. return _jv;
  572. }
  573. //验证审核部门
  574. (bool auditPer, GoodsAuditDepEnum goodsAuditDep) = GoodsAuditDep(currUserId, auditDep, 1);
  575. if (!auditPer)
  576. {
  577. _jv.Msg = string.Format("未分配入库确认权限!");
  578. return _jv;
  579. }
  580. //入库确认 更改审核状态
  581. _sqlSugar.BeginTran();
  582. var info = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && x.Id == gsId).FirstAsync();
  583. if (info == null)
  584. {
  585. _jv.Msg = string.Format("入库信息不存在!");
  586. return _jv;
  587. }
  588. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.IsDel == 0 && x.Id == info.GoodsId).FirstAsync();
  589. if (goodsInfo == null)
  590. {
  591. _jv.Msg = string.Format("物品信息不存在!");
  592. return _jv;
  593. }
  594. int goodsTypeId = goodsInfo.Type;
  595. var preChangeStatus = info.ConfirmStatus;
  596. if (preChangeStatus == dto.ConfirmStatus)
  597. {
  598. _jv.Msg = string.Format("“{0}”已操作,请勿重复该操作!", dto.ConfirmStatus.GetEnumDescription());
  599. return _jv;
  600. }
  601. var preInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.IsDel == 0 && x.DataId == gsId).ToListAsync();
  602. //验证是否需要多级确认
  603. var isAuditPer = GoodsAuditType(goodsTypeId);
  604. //单部门审核时默认人事部审核
  605. if (!isAuditPer) goodsAuditDep = GoodsAuditDepEnum.Hr;
  606. var auditInfo = preInfos.FirstOrDefault(x => x.Dep == goodsAuditDep);
  607. if (auditInfo != null)
  608. {
  609. //移除部门审核状态
  610. preInfos.Remove(auditInfo);
  611. auditInfo.AuditStatus = dto.ConfirmStatus;
  612. auditInfo.AuditUserId = currUserId;
  613. auditInfo.AuditTime = DateTime.Now;
  614. var updStatus = await _sqlSugar.Updateable(auditInfo)
  615. .UpdateColumns(x => new { x.AuditStatus, x.AuditUserId, x.AuditTime })
  616. .ExecuteCommandAsync();
  617. if (updStatus < 1)
  618. {
  619. _sqlSugar.RollbackTran();
  620. _jv.Msg = string.Format("入库确认失败!");
  621. return _jv;
  622. }
  623. }
  624. else
  625. {
  626. auditInfo = new Pm_GoodsAudit(1, goodsAuditDep, gsId, dto.ConfirmStatus, currUserId, currUserId);
  627. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  628. if (addStatus < 1)
  629. {
  630. _sqlSugar.RollbackTran();
  631. _jv.Msg = string.Format("入库确认失败!");
  632. return _jv;
  633. }
  634. }
  635. preInfos.Add(auditInfo);
  636. //入库确认 更改入库状态及扣或增加除库存数、金额
  637. var confirmStatus = GoodsConfirmEnum.WaitConfirm;
  638. if (isAuditPer) //多级审核确认
  639. {
  640. if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 2)
  641. {
  642. confirmStatus = GoodsConfirmEnum.Confirmed;
  643. }
  644. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 1)
  645. {
  646. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  647. }
  648. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.UnApproved).Count() > 0)
  649. {
  650. confirmStatus = GoodsConfirmEnum.UnApproved;
  651. }
  652. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.PartConfirmed).Count() > 0)
  653. {
  654. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  655. }
  656. }
  657. else //人事部审核确认
  658. {
  659. confirmStatus = GoodsConfirmEnum.Confirmed;
  660. confirmStatus = dto.ConfirmStatus;
  661. preInfos = preInfos.Where(x => x.Dep == GoodsAuditDepEnum.Hr).ToList();
  662. }
  663. //入库状态描述
  664. var statusDesc = new StringBuilder();
  665. foreach (var preInfo in preInfos)
  666. {
  667. string depName = preInfo.Dep.GetEnumDescription(),
  668. auditStatus = preInfo.AuditStatus.GetEnumDescription(),
  669. auditUserName = preInfo.AuditUserId > 0 ? _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == preInfo.AuditUserId)?.CnName ?? "-" : "-",
  670. auditTime = preInfo.AuditUserId > 0 ? preInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  671. statusDesc.Append(string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>", depName, auditStatus, auditUserName, auditTime));
  672. }
  673. //更改入库状态及描述
  674. info.ConfirmStatus = confirmStatus;
  675. info.StatusDesc = statusDesc.ToString();
  676. var goodsStorageUpd = await _sqlSugar.Updateable(info)
  677. .UpdateColumns(x => new
  678. {
  679. x.ConfirmStatus,
  680. x.StatusDesc,
  681. })
  682. .Where(x => x.Id == info.Id)
  683. .ExecuteCommandAsync();
  684. if (goodsStorageUpd < 1)
  685. {
  686. _sqlSugar.RollbackTran();
  687. _jv.Msg = $"入库确认状态更改失败!";
  688. return _jv;
  689. }
  690. //入库审核通过数量、金额
  691. decimal auditQuantity = info.Quantity,
  692. auditTotalPrice = info.TotalPrice;
  693. goodsInfo.LastUpdateUserId = currUserId;
  694. goodsInfo.LastUpdateTime = DateTime.Now;
  695. if (confirmStatus == GoodsConfirmEnum.Confirmed) // 确认状态
  696. {
  697. //更改后的状态和更改前的状态不一致时 更改库存数、金额
  698. if (preChangeStatus != confirmStatus) //
  699. {
  700. goodsInfo.SQ_Total += auditQuantity;
  701. goodsInfo.StockQuantity += auditQuantity;
  702. goodsInfo.PriceTotal += auditTotalPrice;
  703. }
  704. }
  705. else //其他状态 拒绝、部分确认、等待确认
  706. {
  707. //更改前状态为确认状态时,减库存数、金额
  708. if (preChangeStatus == GoodsConfirmEnum.Confirmed)
  709. {
  710. goodsInfo.SQ_Total -= auditQuantity;
  711. goodsInfo.StockQuantity -= auditQuantity;
  712. goodsInfo.PriceTotal -= auditTotalPrice;
  713. }
  714. }
  715. var goodsUpd = await _sqlSugar.Updateable(goodsInfo)
  716. .UpdateColumns(x => new
  717. {
  718. x.SQ_Total,
  719. x.StockQuantity,
  720. x.PriceTotal,
  721. x.LastUpdateUserId,
  722. x.LastUpdateTime,
  723. })
  724. .Where(x => x.Id == info.GoodsId)
  725. .ExecuteCommandAsync();
  726. if (goodsUpd > 0)
  727. {
  728. _sqlSugar.CommitTran();
  729. _jv.Msg = $"操作成功!";
  730. _jv.Code = StatusCodes.Status200OK;
  731. return _jv;
  732. }
  733. _sqlSugar.RollbackTran();
  734. _jv.Msg = $"操作失败!";
  735. return _jv;
  736. }
  737. /// <summary>
  738. /// 获取物品审核部门
  739. /// </summary>
  740. /// <param name="userId"> userId </param>
  741. /// <param name="auditDepEnum"> 审核部门枚举 </param>
  742. /// <param name="auditType">
  743. /// 指定审核类型(入库/出库)
  744. /// 1:入库审核
  745. /// 2:出库审核
  746. /// </param>
  747. /// <returns></returns>
  748. public (bool, GoodsAuditDepEnum) GoodsAuditDep(int userId, GoodsAuditDepEnum auditDepEnum, int auditType = 1)
  749. {
  750. if (userId < 1) return (false, GoodsAuditDepEnum.Hr);
  751. var auditList = GoodsStorageConfirmAuditDep(1);
  752. if (auditType == 1)
  753. {
  754. if (auditList.Any(x => x.AuditDep == auditDepEnum && x.AuditorIds.Contains(userId)))
  755. {
  756. return (true, auditDepEnum);
  757. }
  758. }
  759. return (false, GoodsAuditDepEnum.Hr);
  760. }
  761. /// <summary>
  762. /// 物品审核部门列表
  763. /// </summary>
  764. /// <param name="auditType">
  765. /// 指定审核类型(入库/出库)
  766. /// 1:入库审核
  767. /// 2:出库审核
  768. /// </param>
  769. /// <returns></returns>
  770. public List<GoodsAuditDepView> GoodsStorageConfirmAuditDep(int auditType = 1)
  771. {
  772. var auditList = new List<GoodsAuditDepView>();
  773. var hrAuditorIds = new GoodsAuditDepView()
  774. {
  775. AuditDep = GoodsAuditDepEnum.Hr,
  776. AuditorIds = new int[] {
  777. 309, // 赖红燕
  778. 343, // 陈湘
  779. //374, // 罗颖
  780. 208, // 雷怡
  781. }
  782. };
  783. var finAuditorIds = new GoodsAuditDepView()
  784. {
  785. AuditDep = GoodsAuditDepEnum.Financial,
  786. AuditorIds = new int[] {
  787. 187, // 曾艳
  788. 281, // 伏虹瑾
  789. 208, // 雷怡
  790. }
  791. };
  792. if (auditType == 1)//入库
  793. {
  794. hrAuditorIds.AuditorIds = new int[] {
  795. //343, // 陈湘
  796. 374, // 罗颖
  797. 208, // 雷怡
  798. };
  799. auditList.Add(hrAuditorIds);
  800. auditList.Add(finAuditorIds);
  801. }
  802. else if (auditType == 2) //出库
  803. {
  804. auditList.Add(hrAuditorIds);
  805. auditList.Add(finAuditorIds);
  806. }
  807. return auditList;
  808. }
  809. /// <summary>
  810. /// 物品入库 Del
  811. /// </summary>
  812. /// <param name="id"></param>
  813. /// <returns></returns>
  814. public async Task<JsonView> GoodsStorageDel(int id, int userId)
  815. {
  816. var storageInfo = await _sqlSugar
  817. .Queryable<Pm_GoodsStorage>()
  818. .Where(x => x.Id == id)
  819. .FirstAsync();
  820. if (storageInfo == null) return _jv;
  821. var auditStatus = storageInfo.ConfirmStatus;
  822. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  823. {
  824. _sqlSugar.RollbackTran();
  825. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可删除!如若删除请取消已确认或部分确认审核状态!";
  826. return _jv;
  827. }
  828. decimal delAgoQuantity = storageInfo.Quantity,
  829. delAgoTotalPrice = storageInfo.TotalPrice;
  830. var goodsId = storageInfo.GoodsId;
  831. _sqlSugar.BeginTran();
  832. var storageDel = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  833. .SetColumns(x => new Pm_GoodsStorage
  834. {
  835. DeleteUserId = userId,
  836. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  837. IsDel = 1
  838. })
  839. .Where(x => x.Id == id)
  840. .ExecuteCommandAsync();
  841. if (storageDel < 1)
  842. {
  843. _sqlSugar.RollbackTran();
  844. _jv.Msg = $"操作失败!";
  845. return _jv;
  846. }
  847. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == goodsId);
  848. goodsInfo.SQ_Total = goodsInfo.SQ_Total - delAgoQuantity;
  849. goodsInfo.StockQuantity = goodsInfo.StockQuantity - delAgoQuantity;
  850. goodsInfo.PriceTotal = goodsInfo.PriceTotal - delAgoTotalPrice;
  851. goodsInfo.LastUpdateUserId = userId;
  852. goodsInfo.LastUpdateTime = DateTime.Now;
  853. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  854. .UpdateColumns(x => new
  855. {
  856. x.SQ_Total,
  857. x.StockQuantity,
  858. x.PriceTotal,
  859. x.LastUpdateUserId,
  860. x.LastUpdateTime,
  861. })
  862. .Where(x => x.Id == goodsId)
  863. .ExecuteCommandAsync();
  864. if (goodsEdit > 0)
  865. {
  866. _sqlSugar.CommitTran();
  867. _jv.Msg = $"操作成功!";
  868. _jv.Code = StatusCodes.Status200OK;
  869. return _jv;
  870. }
  871. _sqlSugar.RollbackTran();
  872. _jv.Msg = $"操作失败!";
  873. return _jv;
  874. }
  875. /// <summary>
  876. /// 物品入库
  877. /// excelDownload
  878. /// </summary>
  879. /// <param name="dto"></param>
  880. /// <returns></returns>
  881. public async Task<JsonView> GoodsStorageExcelDownload()
  882. {
  883. var fileName = $"物资入库{Guid.NewGuid()}.xlsx";
  884. var excelTempPath = $"{_excelPath}Template/物资入库Temp.xlsx";
  885. if (!File.Exists(excelTempPath))
  886. {
  887. _jv.Code = StatusCodes.Status204NoContent;
  888. _jv.Msg = $"该模板文件不存在!";
  889. return _jv;
  890. }
  891. _url = $"{_url}Office/Excel/GoodsFiles/";
  892. _excelPath = $"{_excelPath}GoodsFiles";
  893. if (!Directory.Exists(_excelPath))
  894. {
  895. Directory.CreateDirectory(_excelPath);
  896. }
  897. //入库记录
  898. var storageData = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  899. .LeftJoin<Sys_Users>((gs, su) => gs.StorageUserId == su.Id)
  900. .Where((gs, su) => gs.IsDel == 0)
  901. .Select((gs, su) => new
  902. {
  903. gs.GoodsId,
  904. gs.Quantity,
  905. su.CnName,
  906. gs.StorageTime
  907. })
  908. .ToListAsync();
  909. //出库记录
  910. var receiveData = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  911. .LeftJoin<Sys_Users>((gr, su) => gr.CreateUserId == su.Id)
  912. .Where((gr, su) => gr.IsDel == 0)
  913. .Select((gr, su) => new
  914. {
  915. gr.GoodsId,
  916. gr.Quantity,
  917. su.CnName,
  918. gr.CreateTime,
  919. gr.Reason
  920. })
  921. .ToListAsync();
  922. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  923. .Where(gi => gi.IsDel == 0)
  924. .Select(gi => new GoodsStorageExcelDownloadView
  925. {
  926. Id = gi.Id,
  927. Name = gi.Name,
  928. SQ_Total = gi.SQ_Total,
  929. OQ_Total = gi.OQ_Total,
  930. StockQuantity = gi.StockQuantity,
  931. Unit = gi.Unit
  932. })
  933. .ToListAsync();
  934. foreach (var item in data)
  935. {
  936. var storageData1 = storageData.Where(x => x.GoodsId == item.Id).ToList();
  937. if (storageData1.Any())
  938. {
  939. item.SQ_Total1 = storageData1.Sum(x => x.Quantity);
  940. item.SQ_Total -= item.SQ_Total1;
  941. item.StorageLabel = string.Join("\r\n", storageData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】 入库人:【" + x.CnName + "】 入库时间:【" + x.StorageTime + "】").ToList());
  942. }
  943. var receiveData1 = receiveData.Where(x => x.GoodsId == item.Id).ToList();
  944. if (receiveData1.Any())
  945. {
  946. 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());
  947. }
  948. }
  949. //载入模板
  950. WorkbookDesigner designer = new WorkbookDesigner();
  951. designer.Workbook = new Workbook(excelTempPath);
  952. designer.SetDataSource("Export", data);
  953. designer.Process();
  954. #region 渲染Cell批注
  955. var sheet = designer.Workbook.Worksheets[0];
  956. for (int i = 0; i < data.Count; i++)
  957. {
  958. string storageComment = $"C{i + 2}",
  959. receiveComment = $"D{i + 2}",
  960. storageCommentText = data[i].StorageLabel,
  961. receiveCommentText = data[i].ReceiveLabel;
  962. if (!string.IsNullOrEmpty(storageCommentText))
  963. {
  964. int storageIndex = sheet.Comments.Add(storageComment);
  965. Aspose.Cells.Comment comment = sheet.Comments[storageIndex];
  966. comment.Note = storageCommentText;
  967. comment.Width = 500;
  968. comment.Height = 200;
  969. //comment.Font.Color = Color.Red;
  970. }
  971. if (!string.IsNullOrEmpty(receiveCommentText))
  972. {
  973. int receiveIndex = sheet.Comments.Add(receiveComment);
  974. Aspose.Cells.Comment comment = sheet.Comments[receiveIndex];
  975. comment.Note = receiveCommentText;
  976. comment.Width = 800;
  977. comment.Height = 200;
  978. //comment.Font.Color = Color.Red;
  979. }
  980. }
  981. #endregion
  982. string serverPath = $"{_url}{fileName}";
  983. designer.Workbook.Save($"{_excelPath}/{fileName}");
  984. _jv.Code = StatusCodes.Status200OK;
  985. _jv.Data = new { url = serverPath };
  986. _jv.Msg = $"操作成功";
  987. return _jv;
  988. }
  989. /// <summary>
  990. /// 物品领用列表
  991. /// </summary>
  992. /// <param name="dto"></param>
  993. /// <returns></returns>
  994. public async Task<JsonView> GoodsReceiveList(GoodsReceiveListDTO dto)
  995. {
  996. //参数处理
  997. int[] typeLabel = Array.Empty<int>(),
  998. userLabel = Array.Empty<int>(),
  999. auditLabel = Array.Empty<int>(),
  1000. groupLabel = Array.Empty<int>();
  1001. int currUserId = dto.CurrUserId;
  1002. if (!string.IsNullOrEmpty(dto.TypeLabel))
  1003. {
  1004. typeLabel = dto.TypeLabel
  1005. .Split(',')
  1006. .Select(x =>
  1007. {
  1008. if (int.TryParse(x, out var id)) return id;
  1009. return id;
  1010. })
  1011. .ToArray();
  1012. }
  1013. if (!string.IsNullOrEmpty(dto.UserLabel))
  1014. {
  1015. userLabel = dto.UserLabel
  1016. .Split(',')
  1017. .Select(x =>
  1018. {
  1019. if (int.TryParse(x, out var id)) return id;
  1020. return id;
  1021. })
  1022. .ToArray();
  1023. }
  1024. if (!string.IsNullOrEmpty(dto.AuditLabel))
  1025. {
  1026. auditLabel = dto.AuditLabel
  1027. .Split(',')
  1028. .Select(x =>
  1029. {
  1030. if (int.TryParse(x, out var id)) return id;
  1031. return id;
  1032. })
  1033. .ToArray();
  1034. if (auditLabel.Any(x => x == -1))
  1035. {
  1036. auditLabel = auditLabel.Where(x => x != -1).ToArray();
  1037. }
  1038. }
  1039. if (!string.IsNullOrEmpty(dto.GroupLabel))
  1040. {
  1041. groupLabel = dto.GroupLabel
  1042. .Split(',')
  1043. .Select(x =>
  1044. {
  1045. if (int.TryParse(x, out var id)) return id;
  1046. return id;
  1047. })
  1048. .ToArray();
  1049. }
  1050. //物品ID和物品名称只能传一个
  1051. if (dto.GoodsId > 0) dto.GoodsName = string.Empty;
  1052. if (!string.IsNullOrEmpty(dto.GoodsName)) dto.GoodsId = 0;
  1053. var beginBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.BeginDt) ? $"{dto.BeginDt} 00:00:00" : string.Empty, out var begin);
  1054. var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
  1055. RefAsync<int> total = 0;
  1056. var data = _sqlSugar.Queryable<Pm_GoodsReceive>()
  1057. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1058. .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
  1059. .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
  1060. .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  1061. .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  1062. .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
  1063. .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
  1064. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
  1065. .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
  1066. .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
  1067. .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
  1068. .WhereIF(groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
  1069. .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
  1070. .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  1071. {
  1072. Id = gr.Id,
  1073. GroupId = gr.GroupId,
  1074. GroupName = di.TeamName,
  1075. GoodsId = gr.GoodsId,
  1076. GoodsName = gi.Name,
  1077. GoodsTypeId = gi.Type,
  1078. GoodsType = sd.Name,
  1079. Quantity = gr.Quantity,
  1080. Unit = gi.Unit,
  1081. Reason = gr.Reason,
  1082. Remark = gr.Remark,
  1083. AuditStatus = gr.AuditStatus,
  1084. StatusDesc = gr.StatusDesc,
  1085. AuditUserId = gr.AuditUserId,
  1086. AuditUserName = u1.CnName,
  1087. AuditTime = gr.AuditTime,
  1088. CreateUserName = u2.CnName,
  1089. CreateTime = gr.CreateTime
  1090. })
  1091. .OrderByDescending(gr => gr.CreateTime);
  1092. //excel导出
  1093. if (dto.IsExcelDownload)
  1094. {
  1095. var fileName = $"物资领用{Guid.NewGuid()}.xlsx";
  1096. var excelTempPath = $"{_excelPath}Template/物资领用Temp.xlsx";
  1097. if (!File.Exists(excelTempPath))
  1098. {
  1099. _jv.Code = StatusCodes.Status204NoContent;
  1100. _jv.Msg = $"该模板文件不存在!";
  1101. return _jv;
  1102. }
  1103. _url = $"{_url}Office/Excel/GoodsFiles/";
  1104. _excelPath = $"{_excelPath}GoodsFiles";
  1105. if (!Directory.Exists(_excelPath))
  1106. {
  1107. Directory.CreateDirectory(_excelPath);
  1108. }
  1109. //载入模板
  1110. WorkbookDesigner designer = new WorkbookDesigner();
  1111. designer.Workbook = new Workbook(excelTempPath);
  1112. var tableData = await data.ToListAsync();
  1113. designer.SetDataSource("Export", tableData);
  1114. designer.Process();
  1115. string serverPath = $"{_url}{fileName}";
  1116. designer.Workbook.Save($"{_excelPath}/{fileName}");
  1117. _jv.Code = StatusCodes.Status200OK;
  1118. _jv.Data = new { url = serverPath };
  1119. _jv.Msg = $"操作成功";
  1120. return _jv;
  1121. }
  1122. //财务人事审核权限处理
  1123. var auditList = GoodsStorageConfirmAuditDep(2);
  1124. bool hrAuditPer = false;
  1125. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  1126. if (hrAuditInfo.AuditorIds.Any(x => x == currUserId))
  1127. {
  1128. hrAuditPer = true;
  1129. }
  1130. //返回分页数据
  1131. //领用审核List只显示多条审核数据 陈湘
  1132. var view = await data.WhereIF(hrAuditPer, x => _goodsTypeIds.Contains(x.GoodsTypeId))
  1133. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  1134. foreach (var item in view)
  1135. {
  1136. //默认审核验证 多条 or 单挑
  1137. var isAudit = GoodsAuditType(item.GoodsTypeId);
  1138. var auditPers = new List<GoodsStorageAuditPerView>()
  1139. {
  1140. new (){ AuditPer = true, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = $"领用确认"},
  1141. };
  1142. if (isAudit) auditPers.Add(new() { AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = $"出库确认" });
  1143. item.AuditPers = auditPers.ToArray();
  1144. }
  1145. if (dto.PortType == 2 || dto.PortType == 3)
  1146. {
  1147. _jv.Data = view;
  1148. }
  1149. else if (dto.PortType == 1)
  1150. {
  1151. var view1 = _mapper.Map<List<GoodsReceiveListView>>(view);
  1152. _jv.Data = view1;
  1153. }
  1154. _jv.Code = StatusCodes.Status200OK;
  1155. _jv.Count = total;
  1156. _jv.Msg = $"操作成功";
  1157. return _jv;
  1158. }
  1159. /// <summary>
  1160. /// 物品领用详情
  1161. /// </summary>
  1162. /// <param name="portType"></param>
  1163. /// <param name="id"></param>
  1164. /// <returns></returns>
  1165. public async Task<JsonView> GoodsReceiveInfo(int portType, int id)
  1166. {
  1167. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  1168. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1169. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  1170. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  1171. .LeftJoin<Grp_DelegationInfo>((gr, gi, u1, u2, di) => gr.GroupId == di.Id)
  1172. .Where((gr, gi, u1, u2, di) => gr.IsDel == 0)
  1173. .WhereIF(id > 0, (gr, gi, u1, u2, di) => gr.Id == id)
  1174. .Select((gr, gi, u1, u2, di) => new GoodsReceiveInfoMobileView
  1175. {
  1176. Id = gr.Id,
  1177. GroupId = gr.GroupId,
  1178. GroupName = di.TeamName,
  1179. GoodsId = gr.GoodsId,
  1180. GoodsName = gi.Name,
  1181. Quantity = gr.Quantity,
  1182. Reason = gr.Reason,
  1183. Remark = gr.Remark,
  1184. GoodsStorageInfo = gr.GoodsStorageInfo,
  1185. AuditStatus = gr.AuditStatus,
  1186. AuditUserId = gr.AuditUserId,
  1187. AuditUserName = u1.CnName,
  1188. AuditTime = gr.AuditTime,
  1189. CreateUserName = u2.CnName,
  1190. CreateTime = gr.CreateTime
  1191. })
  1192. .FirstAsync();
  1193. if (!string.IsNullOrEmpty(data.GoodsStorageInfo))
  1194. {
  1195. var subData = new List<dynamic>();
  1196. try
  1197. {
  1198. var subData1 = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(data.GoodsStorageInfo);
  1199. if (subData1.Count > 0)
  1200. {
  1201. string goodsStorageInfoStr = string.Empty;
  1202. var storageIds = subData1.Select(x => x.StorageId).ToList();
  1203. var storages = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && storageIds.Contains(x.Id)).ToListAsync();
  1204. foreach (var item in subData1)
  1205. {
  1206. var storageInfo = storages.Find(x => x.Id == item.StorageId);
  1207. if (storageInfo != null)
  1208. {
  1209. subData.Add(new
  1210. {
  1211. StorageId = item.StorageId,
  1212. BatchNo = storageInfo.BatchNo,
  1213. RecsiveQuantity = item.Quantity
  1214. });
  1215. goodsStorageInfoStr += $"物品名称:{data.GoodsName} 批次号:{storageInfo.BatchNo} 领用数量:{item.Quantity} \r\n";
  1216. }
  1217. }
  1218. data.QuantityInfos = subData;
  1219. data.GoodsStorageInfoStr = goodsStorageInfoStr;
  1220. }
  1221. }
  1222. catch (Exception e)
  1223. {
  1224. Console.WriteLine(e);
  1225. }
  1226. }
  1227. _jv.Code = StatusCodes.Status200OK;
  1228. _jv.Msg = $"操作成功";
  1229. if (portType == 2 || portType == 3) //移动端
  1230. {
  1231. _jv.Data = data;
  1232. }
  1233. else if (portType == 1) //pc端
  1234. {
  1235. _jv.Data = _mapper.Map<GoodsReceiveInfoView>(data);
  1236. }
  1237. return _jv;
  1238. }
  1239. /// <summary>
  1240. /// 物品领用 OP(Add Or Edit)
  1241. /// </summary>
  1242. /// <param name="dto"></param>
  1243. /// <param name="currUserId"></param>
  1244. /// <returns></returns>
  1245. public async Task<JsonView> GoodsReceiveOp(GoodsReceiveOpDto dto, int currUserId)
  1246. {
  1247. var info = _mapper.Map<Pm_GoodsReceive>(dto);
  1248. info.CreateUserId = currUserId;
  1249. var auditEnums = new List<GoodsAuditEnum>() {
  1250. GoodsAuditEnum.Approved, // 1
  1251. GoodsAuditEnum.UnApproved, // 2
  1252. GoodsAuditEnum.OutConfirmed,// 5
  1253. GoodsAuditEnum.OutRejected // 6
  1254. };
  1255. _sqlSugar.BeginTran();
  1256. //物品现有库存
  1257. var goodsInfo = _sqlSugar.Queryable<Pm_GoodsInfo>().First(x => x.Id == info.GoodsId);
  1258. var stockQuantity = goodsInfo?.StockQuantity ?? 0.00M;
  1259. //待审核 该物品数量
  1260. var waitAuditQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>().Where(x => x.IsDel == 0 &&
  1261. x.GoodsId == dto.GoodsId &&
  1262. !auditEnums.Contains(x.AuditStatus)
  1263. ).SumAsync(x => x.Quantity);
  1264. //验证默认审核 多条 or 单条
  1265. //出库确认默认审核状态
  1266. var isAuditPer = GoodsAuditType(goodsInfo?.Type ?? 0);
  1267. //验证默认审核 多条审核时添加
  1268. var goodsAuditInfo1 = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr_Reception, info.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  1269. var goodsAuditInfo2 = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr, info.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  1270. var goodsAuditInfos = new List<Pm_GoodsAudit>() { goodsAuditInfo1 };
  1271. //状态描述
  1272. StringBuilder stringBuilder = new StringBuilder();
  1273. stringBuilder.Append($"领用确认:状态:待确认 审核人:- 审核时间:-;<br/>");
  1274. if (isAuditPer)
  1275. {
  1276. goodsAuditInfos.Add(goodsAuditInfo2);
  1277. stringBuilder.Append($"人事部:状态:待确认 审核人:- 审核时间:-;<br/>");
  1278. }
  1279. info.StatusDesc = stringBuilder.ToString();
  1280. if (info.Id > 0) //修改
  1281. {
  1282. //审核验证
  1283. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == info.Id);
  1284. if (auditEnums.Contains(selectInfo.AuditStatus))
  1285. {
  1286. _sqlSugar.RollbackTran();
  1287. _jv.Msg = $"该条数据已执行操作({selectInfo.AuditStatus.GetEnumDescription()}),不可更改!";
  1288. return _jv;
  1289. }
  1290. //物品数量验证
  1291. var editAfterQuantity = waitAuditQuantity - selectInfo.Quantity + info.Quantity;
  1292. if (editAfterQuantity > stockQuantity)
  1293. {
  1294. _sqlSugar.RollbackTran();
  1295. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1296. return _jv;
  1297. }
  1298. var edit = await _sqlSugar.Updateable(info)
  1299. .UpdateColumns(x => new
  1300. {
  1301. x.GroupId,
  1302. x.Quantity,
  1303. x.Reason,
  1304. x.Remark,
  1305. x.StatusDesc,
  1306. })
  1307. .Where(x => x.Id == info.Id)
  1308. .ExecuteCommandAsync();
  1309. if (edit > 0)
  1310. {
  1311. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.DataId == info.Id).ToListAsync();
  1312. if (!auditInfos.Any()) await _sqlSugar.Insertable(goodsAuditInfos).ExecuteCommandAsync();
  1313. _sqlSugar.CommitTran();
  1314. _jv.Msg = $"操作成功!";
  1315. _jv.Code = StatusCodes.Status200OK;
  1316. return _jv;
  1317. }
  1318. }
  1319. else if (info.Id < 1) //添加
  1320. {
  1321. //物品数量验证
  1322. decimal addAgoQuantity = waitAuditQuantity + info.Quantity;
  1323. if (addAgoQuantity > stockQuantity)
  1324. {
  1325. _sqlSugar.RollbackTran();
  1326. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1327. return _jv;
  1328. }
  1329. var add = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  1330. if (add > 0)
  1331. {
  1332. goodsAuditInfos.ForEach(x => x.DataId = add);
  1333. await _sqlSugar.Insertable(goodsAuditInfos).ExecuteCommandAsync();
  1334. _sqlSugar.CommitTran();
  1335. _jv.Msg = $"操作成功!";
  1336. _jv.Code = StatusCodes.Status200OK;
  1337. return _jv;
  1338. }
  1339. }
  1340. _sqlSugar.RollbackTran();
  1341. return _jv;
  1342. }
  1343. /// <summary>
  1344. /// 物品领用 Audit
  1345. /// </summary>
  1346. /// <param name="idArray"></param>
  1347. /// <param name="userId"></param>
  1348. /// <param name="auditEnum"></param>
  1349. /// <returns></returns>
  1350. public async Task<JsonView> GoodsReceiveAudit(int[] idArray, int userId, GoodsAuditEnum auditEnum)
  1351. {
  1352. if (idArray.Length < 1) return _jv;
  1353. if (!Enum.IsDefined(typeof(GoodsAuditEnum),(int)auditEnum))
  1354. {
  1355. _jv.Msg = $"出库确认状态超出可用范围!";
  1356. return _jv;
  1357. }
  1358. int receiveId = idArray[0];
  1359. var currUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == userId)?.CnName ?? "-";
  1360. _sqlSugar.BeginTran();
  1361. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.IsDel == 0 && receiveId == x.Id);
  1362. if (receiveInfo == null)
  1363. {
  1364. _sqlSugar.RollbackTran();
  1365. _jv.Msg = $"当前领用信息不存在!";
  1366. return _jv;
  1367. }
  1368. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  1369. int goodTypeId = goodsInfo?.Type ?? 0;
  1370. //审核前状态
  1371. var preChangeStatus = receiveInfo.AuditStatus;
  1372. //状态验证
  1373. //if (preChangeStatus == auditEnum)
  1374. //{
  1375. // _sqlSugar.RollbackTran();
  1376. // _jv.Msg = $"该条数据状态已设置,不可重复设置!";
  1377. // return _jv;
  1378. //}
  1379. var isAuditPer = GoodsAuditType(goodTypeId);
  1380. if (isAuditPer) //多人审核
  1381. {
  1382. var auditDep = GoodsAuditDepEnum.Hr_Reception;
  1383. var auditInfo = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1384. .FirstAsync(x => x.Type == 2 && x.DataId == receiveInfo.Id && x.Dep == GoodsAuditDepEnum.Hr_Reception);
  1385. if (auditInfo != null && auditInfo.AuditStatus == GoodsConfirmEnum.Confirmed)
  1386. {
  1387. auditDep = GoodsAuditDepEnum.Hr;
  1388. //多人审核的时候验证审核权限
  1389. var auditList = GoodsStorageConfirmAuditDep(2);
  1390. var auditDepInfo = auditList.Find(x => x.AuditorIds.Contains(userId));
  1391. if (auditDepInfo == null)
  1392. {
  1393. _jv.Msg = string.Format("未分配出库确认权限!");
  1394. _sqlSugar.RollbackTran();
  1395. return _jv;
  1396. }
  1397. }
  1398. // 确认中 确认 拒绝
  1399. _jv = await GoodsReceiveOutConfirmingMultiple(receiveInfo, userId, auditDep, auditEnum);
  1400. //switch (auditEnum)
  1401. //{
  1402. // case GoodsAuditEnum.Pending: //领用待确认(取消确认、取消拒绝)
  1403. // _jv = await GoodsReceivePending(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1404. // break;
  1405. // case GoodsAuditEnum.Approved: //领用确认
  1406. // _jv = await GoodsReceiveApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1407. // break;
  1408. // case GoodsAuditEnum.UnApproved: //领用拒绝
  1409. // _jv = await GoodsReceiveUnApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1410. // break;
  1411. // case GoodsAuditEnum.OutPending: //出库待确认(取消出库确认、取消出库拒绝)
  1412. // _jv = await GoodsReceiveOutPending(preChangeStatus, receiveInfo, userId, auditDep, auditEnum);
  1413. // break;
  1414. // case GoodsAuditEnum.OutConfirmed: //出库确认
  1415. // _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1416. // break;
  1417. // case GoodsAuditEnum.OutRejected: //出库拒绝
  1418. // _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1419. // break;
  1420. //}
  1421. }
  1422. else //单人审核
  1423. {
  1424. //领用确认 --> 已确认 / 领用拒绝 --> 已拒绝
  1425. _jv = await GoodsReceiveOutConfirmingSingle(receiveInfo,userId, currUserName,auditEnum);
  1426. }
  1427. if (_jv.Code == StatusCodes.Status200OK)
  1428. {
  1429. _sqlSugar.CommitTran();
  1430. _jv.Msg = $"操作成功!";
  1431. _jv.Data = auditEnum;
  1432. return _jv;
  1433. }
  1434. _sqlSugar.RollbackTran();
  1435. return _jv;
  1436. }
  1437. #region 多人审核状态变更
  1438. /// <summary>
  1439. /// 物品领用状态 - 领用待确认(取消确认、取消拒绝)
  1440. /// </summary>
  1441. /// <param name="preChangeStatus">更改前状态</param>
  1442. /// <param name="receiveId"></param>
  1443. /// <param name="userId"></param>
  1444. /// <param name="userName"></param>
  1445. /// <param name="auditEnum"></param>
  1446. /// <returns></returns>
  1447. public async Task<JsonView> GoodsReceivePending(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1448. {
  1449. _jv.Code = StatusCodes.Status400BadRequest;
  1450. //领用待确认 操作范围
  1451. var audirEnumPers = new List<GoodsAuditEnum>() {
  1452. GoodsAuditEnum.Approved,
  1453. GoodsAuditEnum.UnApproved
  1454. };
  1455. if (!audirEnumPers.Contains(preChangeStatus))
  1456. {
  1457. _jv.Msg = $"{GoodsAuditEnum.Approved.GetEnumDescription()}、{GoodsAuditEnum.UnApproved.GetEnumDescription()},状态下可取消操作!";
  1458. return _jv;
  1459. }
  1460. var currUserOpDt = DateTime.Now;
  1461. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1462. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1463. auditEnum = GoodsAuditEnum.Pending;
  1464. var changeStatus = await _sqlSugar
  1465. .Updateable<Pm_GoodsReceive>()
  1466. .SetColumns(x => new Pm_GoodsReceive()
  1467. {
  1468. AuditStatus = auditEnum,
  1469. AuditUserId = userId,
  1470. AuditTime = currUserOpDt,
  1471. StatusDesc = statusDesc
  1472. })
  1473. .Where(x => x.Id == receiveId)
  1474. .ExecuteCommandAsync();
  1475. if (changeStatus > 0)
  1476. {
  1477. _jv.Code = StatusCodes.Status200OK;
  1478. }
  1479. return _jv;
  1480. }
  1481. /// <summary>
  1482. /// 物品领用状态 - 领用确认
  1483. /// </summary>
  1484. /// <param name="preChangeStatus">更改前状态</param>
  1485. /// <param name="receiveId"></param>
  1486. /// <param name="userId"></param>
  1487. /// <param name="userName"></param>
  1488. /// <param name="auditEnum"></param>
  1489. /// <returns></returns>
  1490. public async Task<JsonView> GoodsReceiveApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1491. {
  1492. _jv.Code = StatusCodes.Status400BadRequest;
  1493. if (preChangeStatus != GoodsAuditEnum.Pending)
  1494. {
  1495. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1496. return _jv;
  1497. }
  1498. var currUserOpDt = DateTime.Now;
  1499. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1500. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1501. auditEnum = GoodsAuditEnum.OutPending;
  1502. var changeStatus = await _sqlSugar
  1503. .Updateable<Pm_GoodsReceive>()
  1504. .SetColumns(x => new Pm_GoodsReceive()
  1505. {
  1506. AuditStatus = auditEnum,
  1507. AuditUserId = userId,
  1508. AuditTime = currUserOpDt,
  1509. StatusDesc = statusDesc
  1510. })
  1511. .Where(x => x.Id == receiveId)
  1512. .ExecuteCommandAsync();
  1513. if (changeStatus > 0)
  1514. {
  1515. _jv.Code = StatusCodes.Status200OK;
  1516. }
  1517. return _jv;
  1518. }
  1519. /// <summary>
  1520. /// 物品领用状态 - 领用拒绝
  1521. /// </summary>
  1522. /// <param name="preChangeStatus">更改前状态</param>
  1523. /// <param name="receiveId"></param>
  1524. /// <param name="userId"></param>
  1525. /// <param name="userName"></param>
  1526. /// <param name="auditEnum"></param>
  1527. /// <returns></returns>
  1528. public async Task<JsonView> GoodsReceiveUnApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1529. {
  1530. _jv.Code = StatusCodes.Status400BadRequest;
  1531. if (preChangeStatus != GoodsAuditEnum.Pending)
  1532. {
  1533. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1534. return _jv;
  1535. }
  1536. auditEnum = GoodsAuditEnum.UnApproved;
  1537. var currUserOpDt = DateTime.Now;
  1538. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1539. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部认:状态:待确认 审核人:- 审核时间:-;<br/>", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1540. var changeStatus = await _sqlSugar
  1541. .Updateable<Pm_GoodsReceive>()
  1542. .SetColumns(x => new Pm_GoodsReceive()
  1543. {
  1544. AuditStatus = auditEnum,
  1545. AuditUserId = userId,
  1546. AuditTime = currUserOpDt,
  1547. StatusDesc = statusDesc
  1548. })
  1549. .Where(x => x.Id == receiveId)
  1550. .ExecuteCommandAsync();
  1551. if (changeStatus > 0)
  1552. {
  1553. _jv.Code = StatusCodes.Status200OK;
  1554. }
  1555. return _jv;
  1556. }
  1557. /// <summary>
  1558. /// 物品领用状态 - 出库待确认(出库确认、出库拒绝)
  1559. /// </summary>
  1560. /// <param name="preChangeStatus">更改前状态</param>
  1561. /// <param name="receiveId"></param>
  1562. /// <param name="userId"></param>
  1563. /// <param name="userName"></param>
  1564. /// <param name="auditEnum"></param>
  1565. /// <returns></returns>
  1566. public async Task<JsonView> GoodsReceiveOutPending(GoodsAuditEnum preChangeStatus, Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1567. {
  1568. _jv.Code = StatusCodes.Status400BadRequest;
  1569. //出库待确认 操作范围
  1570. var audirEnumPers = new List<GoodsAuditEnum>() {
  1571. GoodsAuditEnum.OutConfirmed,
  1572. GoodsAuditEnum.OutRejected
  1573. };
  1574. if (!audirEnumPers.Contains(preChangeStatus))
  1575. {
  1576. _jv.Msg = $"{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可取消操作!";
  1577. return _jv;
  1578. }
  1579. var currUserOpDt = DateTime.Now;
  1580. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1581. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1582. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1583. .ToListAsync();
  1584. auditEnum = GoodsAuditEnum.OutPending;
  1585. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1586. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1587. if (auditInfo == null)
  1588. {
  1589. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1590. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1591. if (addStatus < 1)
  1592. {
  1593. _jv.Msg = $"出库确认取消操作失败!";
  1594. return _jv;
  1595. }
  1596. auditInfos.Add(auditInfo);
  1597. }
  1598. else
  1599. {
  1600. //移除旧数据
  1601. auditInfos.Remove(auditInfo);
  1602. auditInfo.AuditStatus = auditStatus;
  1603. auditInfo.AuditUserId = userId;
  1604. auditInfo.AuditTime = currUserOpDt;
  1605. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1606. .SetColumns(x => new Pm_GoodsAudit()
  1607. {
  1608. AuditStatus = auditStatus,
  1609. AuditUserId = userId,
  1610. AuditTime = currUserOpDt
  1611. })
  1612. .Where(x => x.Id == auditInfo.Id)
  1613. .ExecuteCommandAsync();
  1614. if (updStatus < 1)
  1615. {
  1616. _jv.Msg = $"出库确认操作失败!";
  1617. return _jv;
  1618. }
  1619. //添加更新后的数据
  1620. auditInfos.Add(auditInfo);
  1621. }
  1622. //处理状态描述
  1623. StringBuilder statusDesc = new StringBuilder();
  1624. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1625. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1626. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1627. statusDesc.Append(receiveStatusDesc);
  1628. foreach (var auditInf in auditInfos)
  1629. {
  1630. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1631. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1632. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1633. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1634. statusDesc.Append(auditInfStatusDesc);
  1635. }
  1636. //批次库存信息
  1637. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1638. //出库确认取消是单独处理
  1639. if (preChangeStatus == GoodsAuditEnum.OutConfirming)
  1640. {
  1641. if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1642. {
  1643. auditEnum = GoodsAuditEnum.OutConfirming;
  1644. }
  1645. //2.1 更改库存
  1646. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1647. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1648. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1649. goodsInfo.LastUpdateTime = DateTime.Now;
  1650. goodsInfo.LastUpdateUserId = userId;
  1651. var editGoods = await _sqlSugar
  1652. .Updateable(goodsInfo)
  1653. .UpdateColumns(x => new
  1654. {
  1655. x.StockQuantity,
  1656. x.OQ_Total,
  1657. x.LastUpdateUserId,
  1658. x.LastUpdateTime,
  1659. })
  1660. .Where(x => x.Id == receiveInfo.GoodsId)
  1661. .ExecuteCommandAsync();
  1662. if (editGoods < 1)
  1663. {
  1664. _jv.Msg = $"库存更新失败!";
  1665. return _jv;
  1666. }
  1667. //2.2 入库批次关联领用人 更改批次库存
  1668. var goodsStorages = await _sqlSugar
  1669. .Queryable<Pm_GoodsStorage>()
  1670. .Where(x => x.IsDel == 0 &&
  1671. x.GoodsId == receiveInfo.GoodsId &&
  1672. (x.Quantity - x.ReceiveQuantity) > 0
  1673. )
  1674. .OrderBy(x => x.CreateTime)
  1675. .ToListAsync();
  1676. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1677. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1678. var receiveQuantity = 0.00M; //领用总数量
  1679. foreach (var storage in goodsStorages)
  1680. {
  1681. if (receiveInfo.Quantity == receiveQuantity) break;
  1682. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1683. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1684. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1685. const decimal unit = 0.50M;
  1686. while (receiveQuantity < receiveInfo.Quantity)
  1687. {
  1688. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1689. thisBatchReceiveQuantity += unit;
  1690. receiveQuantity += unit;
  1691. }
  1692. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1693. {
  1694. StorageId = storage.Id,
  1695. Quantity = thisBatchReceiveQuantity
  1696. });
  1697. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1698. var storageUpd = storage;
  1699. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1700. batchStorageInfos.Add(storageUpd);
  1701. }
  1702. //2.2.1 更改批次库存
  1703. if (goodsReceiveInfos.Count > 0)
  1704. {
  1705. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1706. .UpdateColumns(x => x.ReceiveQuantity)
  1707. .WhereColumns(x => x.Id)
  1708. .ExecuteCommandAsync();
  1709. if (edit1 < 1)
  1710. {
  1711. _jv.Msg = $"批次库存更新失败!";
  1712. return _jv;
  1713. }
  1714. }
  1715. //2.2.2 添加入库批次关联领用人
  1716. if (goodsReceiveInfos.Count > 0)
  1717. {
  1718. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1719. }
  1720. }
  1721. var statusDesc1 = statusDesc.ToString();
  1722. var changeStatus = await _sqlSugar
  1723. .Updateable<Pm_GoodsReceive>()
  1724. .SetColumns(x => new Pm_GoodsReceive()
  1725. {
  1726. AuditStatus = auditEnum,
  1727. StatusDesc = statusDesc1,
  1728. GoodsStorageInfo = goodsStorageInfo
  1729. })
  1730. .Where(x => x.Id == receiveInfo.Id)
  1731. .ExecuteCommandAsync();
  1732. if (changeStatus > 0)
  1733. {
  1734. _jv.Code = StatusCodes.Status200OK;
  1735. }
  1736. return _jv;
  1737. }
  1738. /// <summary>
  1739. /// 物品领用状态 - 出库确认中、出库确认完成、出库确认拒绝
  1740. /// </summary>
  1741. /// <param name="receiveInfo"></param>
  1742. /// <param name="userId"></param>
  1743. /// <param name="depEnum"></param>
  1744. /// <param name="auditEnum"></param>
  1745. /// <returns></returns>
  1746. public async Task<JsonView> GoodsReceiveOutConfirming(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1747. {
  1748. _jv.Code = StatusCodes.Status400BadRequest;
  1749. //更改前状态
  1750. var preChangeStatus = receiveInfo.AuditStatus;
  1751. if (preChangeStatus < GoodsAuditEnum.OutPending)
  1752. {
  1753. _jv.Msg = $"领用确认执行成功!才可执行出库确认或出库拒绝操作!";
  1754. return _jv;
  1755. }
  1756. var currUserOpDt = DateTime.Now;
  1757. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1758. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1759. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1760. .ToListAsync();
  1761. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1762. if (auditEnum == GoodsAuditEnum.OutRejected) auditStatus = GoodsConfirmEnum.UnApproved;
  1763. else if (auditEnum == GoodsAuditEnum.OutConfirmed) auditStatus = GoodsConfirmEnum.Confirmed;
  1764. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1765. if (auditInfo == null)
  1766. {
  1767. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1768. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1769. if (addStatus < 1)
  1770. {
  1771. _jv.Msg = $"出库确认操作失败!";
  1772. return _jv;
  1773. }
  1774. auditInfos.Add(auditInfo);
  1775. }
  1776. else
  1777. {
  1778. //移除旧数据
  1779. auditInfos.Remove(auditInfo);
  1780. auditInfo.AuditStatus = auditStatus;
  1781. auditInfo.AuditUserId = userId;
  1782. auditInfo.AuditTime = currUserOpDt;
  1783. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1784. .SetColumns(x => new Pm_GoodsAudit()
  1785. {
  1786. AuditStatus = auditStatus,
  1787. AuditUserId = userId,
  1788. AuditTime = currUserOpDt
  1789. })
  1790. .Where(x => x.Id == auditInfo.Id)
  1791. .ExecuteCommandAsync();
  1792. if (updStatus < 1)
  1793. {
  1794. _jv.Msg = $"出库确认操作失败!";
  1795. return _jv;
  1796. }
  1797. //添加更新后的数据
  1798. auditInfos.Add(auditInfo);
  1799. }
  1800. //处理状态描述
  1801. StringBuilder statusDesc = new StringBuilder();
  1802. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1803. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1804. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1805. statusDesc.Append(receiveStatusDesc);
  1806. foreach (var auditInf in auditInfos)
  1807. {
  1808. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1809. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1810. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1811. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1812. statusDesc.Append(auditInfStatusDesc);
  1813. }
  1814. if (preChangeStatus == auditEnum)
  1815. {
  1816. _jv.Msg = $"此操作已执行,不可重复操作!";
  1817. return _jv;
  1818. }
  1819. //批次库存信息
  1820. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1821. //更改领用状态及库存
  1822. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  1823. {
  1824. //出库确认拒绝
  1825. auditEnum = GoodsAuditEnum.OutRejected;
  1826. }
  1827. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  1828. {
  1829. //1.出库确认完成
  1830. auditEnum = GoodsAuditEnum.OutConfirmed;
  1831. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  1832. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  1833. {
  1834. //2.1 更改库存
  1835. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1836. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1837. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1838. goodsInfo.LastUpdateTime = DateTime.Now;
  1839. goodsInfo.LastUpdateUserId = userId;
  1840. var editGoods = await _sqlSugar
  1841. .Updateable(goodsInfo)
  1842. .UpdateColumns(x => new
  1843. {
  1844. x.StockQuantity,
  1845. x.OQ_Total,
  1846. x.LastUpdateUserId,
  1847. x.LastUpdateTime,
  1848. })
  1849. .Where(x => x.Id == receiveInfo.GoodsId)
  1850. .ExecuteCommandAsync();
  1851. if (editGoods < 1)
  1852. {
  1853. _jv.Msg = $"库存更新失败!";
  1854. return _jv;
  1855. }
  1856. //2.2 入库批次关联领用人 更改批次库存
  1857. var goodsStorages = await _sqlSugar
  1858. .Queryable<Pm_GoodsStorage>()
  1859. .Where(x => x.IsDel == 0 &&
  1860. x.GoodsId == receiveInfo.GoodsId &&
  1861. (x.Quantity - x.ReceiveQuantity) > 0
  1862. )
  1863. .OrderBy(x => x.CreateTime)
  1864. .ToListAsync();
  1865. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1866. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1867. var receiveQuantity = 0.00M; //领用总数量
  1868. foreach (var storage in goodsStorages)
  1869. {
  1870. if (receiveInfo.Quantity == receiveQuantity) break;
  1871. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1872. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1873. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1874. const decimal unit = 0.50M;
  1875. while (receiveQuantity < receiveInfo.Quantity)
  1876. {
  1877. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1878. thisBatchReceiveQuantity += unit;
  1879. receiveQuantity += unit;
  1880. }
  1881. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1882. {
  1883. StorageId = storage.Id,
  1884. Quantity = thisBatchReceiveQuantity
  1885. });
  1886. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1887. var storageUpd = storage;
  1888. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1889. batchStorageInfos.Add(storageUpd);
  1890. }
  1891. //2.2.1 更改批次库存
  1892. if (goodsReceiveInfos.Count > 0)
  1893. {
  1894. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1895. .UpdateColumns(x => x.ReceiveQuantity)
  1896. .WhereColumns(x => x.Id)
  1897. .ExecuteCommandAsync();
  1898. if (edit1 < 1)
  1899. {
  1900. _jv.Msg = $"批次库存更新失败!";
  1901. return _jv;
  1902. }
  1903. }
  1904. //2.2.2 添加入库批次关联领用人
  1905. if (goodsReceiveInfos.Count > 0)
  1906. {
  1907. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1908. }
  1909. }
  1910. }
  1911. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1912. {
  1913. //出库确认中
  1914. auditEnum = GoodsAuditEnum.OutConfirming;
  1915. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  1916. {
  1917. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  1918. if (!string.IsNullOrEmpty(goodsStorageInfo))
  1919. {
  1920. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  1921. if (goodsStorageInfos.Any())
  1922. {
  1923. foreach (var item in goodsStorageInfos)
  1924. {
  1925. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  1926. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  1927. .FirstAsync();
  1928. if (newStorageInfo == null) continue;
  1929. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  1930. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  1931. .ExecuteCommandAsync();
  1932. if (newEdit < 1)
  1933. {
  1934. _jv.Msg = $"批次领用库存更新失败!";
  1935. return _jv;
  1936. }
  1937. }
  1938. }
  1939. }
  1940. }
  1941. }
  1942. else
  1943. {
  1944. auditEnum = GoodsAuditEnum.OutPending;
  1945. }
  1946. var statusDesc1 = statusDesc.ToString();
  1947. var changeStatus = await _sqlSugar
  1948. .Updateable<Pm_GoodsReceive>()
  1949. .SetColumns(x => new Pm_GoodsReceive()
  1950. {
  1951. AuditStatus = auditEnum,
  1952. StatusDesc = statusDesc1,
  1953. GoodsStorageInfo = goodsStorageInfo,
  1954. })
  1955. .Where(x => x.Id == receiveInfo.Id)
  1956. .ExecuteCommandAsync();
  1957. if (changeStatus > 0)
  1958. {
  1959. _jv.Code = StatusCodes.Status200OK;
  1960. }
  1961. return _jv;
  1962. }
  1963. /// <summary>
  1964. ///
  1965. /// </summary>
  1966. /// <param name="receiveInfo"></param>
  1967. /// <param name="userId"></param>
  1968. /// <param name="depEnum"></param>
  1969. /// <param name="auditEnum"></param>
  1970. /// <returns></returns>
  1971. public async Task<JsonView> GoodsReceiveOutConfirmingMultiple(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1972. {
  1973. _jv.Code = StatusCodes.Status400BadRequest;
  1974. //更改前状态
  1975. receiveInfo.CreateUserId = userId;
  1976. var preChangeStatus = receiveInfo.AuditStatus;
  1977. var currUserOpDt = DateTime.Now;
  1978. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1979. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1980. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1981. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1982. .ToListAsync();
  1983. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1984. if (auditEnum == GoodsAuditEnum.UnApproved) auditStatus = GoodsConfirmEnum.UnApproved;
  1985. else if (auditEnum == GoodsAuditEnum.Approved) auditStatus = GoodsConfirmEnum.Confirmed;
  1986. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1987. if (auditInfo == null)
  1988. {
  1989. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1990. auditInfo.AuditUserId = userId;
  1991. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1992. if (addStatus < 1)
  1993. {
  1994. _jv.Msg = $"出库确认操作失败!";
  1995. return _jv;
  1996. }
  1997. auditInfos.Add(auditInfo);
  1998. }
  1999. else
  2000. {
  2001. //移除旧数据
  2002. auditInfos.Remove(auditInfo);
  2003. auditInfo.AuditStatus = auditStatus;
  2004. auditInfo.AuditUserId = userId;
  2005. auditInfo.AuditTime = currUserOpDt;
  2006. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  2007. .SetColumns(x => new Pm_GoodsAudit()
  2008. {
  2009. AuditStatus = auditStatus,
  2010. AuditUserId = userId,
  2011. AuditTime = currUserOpDt
  2012. })
  2013. .Where(x => x.Id == auditInfo.Id)
  2014. .ExecuteCommandAsync();
  2015. if (updStatus < 1)
  2016. {
  2017. _jv.Msg = $"出库确认操作失败!";
  2018. return _jv;
  2019. }
  2020. //添加更新后的数据
  2021. auditInfos.Add(auditInfo);
  2022. }
  2023. auditInfos = auditInfos.OrderBy(x => x.Dep).ToList();
  2024. //处理状态描述
  2025. StringBuilder statusDesc = new StringBuilder();
  2026. foreach (var auditInf in auditInfos)
  2027. {
  2028. string auditType = auditInf.Dep == GoodsAuditDepEnum.Hr ? "人事部" : "领用确认";
  2029. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  2030. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  2031. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  2032. auditType, auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  2033. statusDesc.Append(auditInfStatusDesc);
  2034. }
  2035. if (preChangeStatus == auditEnum)
  2036. {
  2037. _jv.Msg = $"此操作已执行,不可重复操作!";
  2038. return _jv;
  2039. }
  2040. //批次库存信息
  2041. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2042. //更改领用状态及库存
  2043. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  2044. {
  2045. //出库确认拒绝
  2046. auditEnum = GoodsAuditEnum.OutRejected;
  2047. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2048. {
  2049. //回滚库存
  2050. _jv = await GoodsInventoryRollback(receiveInfo);
  2051. if (_jv.Code != StatusCodes.Status200OK) return _jv;
  2052. }
  2053. }
  2054. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  2055. {
  2056. //1.出库确认完成
  2057. auditEnum = GoodsAuditEnum.OutConfirmed;
  2058. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  2059. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  2060. {
  2061. //2.1 更改库存
  2062. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2063. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2064. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2065. goodsInfo.LastUpdateTime = DateTime.Now;
  2066. goodsInfo.LastUpdateUserId = userId;
  2067. var editGoods = await _sqlSugar
  2068. .Updateable(goodsInfo)
  2069. .UpdateColumns(x => new
  2070. {
  2071. x.StockQuantity,
  2072. x.OQ_Total,
  2073. x.LastUpdateUserId,
  2074. x.LastUpdateTime,
  2075. })
  2076. .Where(x => x.Id == receiveInfo.GoodsId)
  2077. .ExecuteCommandAsync();
  2078. if (editGoods < 1)
  2079. {
  2080. _jv.Msg = $"库存更新失败!";
  2081. return _jv;
  2082. }
  2083. //2.2 入库批次关联领用人 更改批次库存
  2084. var goodsStorages = await _sqlSugar
  2085. .Queryable<Pm_GoodsStorage>()
  2086. .Where(x => x.IsDel == 0 &&
  2087. x.GoodsId == receiveInfo.GoodsId &&
  2088. (x.Quantity - x.ReceiveQuantity) > 0
  2089. )
  2090. .OrderBy(x => x.CreateTime)
  2091. .ToListAsync();
  2092. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2093. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2094. var receiveQuantity = 0.00M; //领用总数量
  2095. foreach (var storage in goodsStorages)
  2096. {
  2097. if (receiveInfo.Quantity == receiveQuantity) break;
  2098. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2099. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2100. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2101. const decimal unit = 0.50M;
  2102. while (receiveQuantity < receiveInfo.Quantity)
  2103. {
  2104. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2105. thisBatchReceiveQuantity += unit;
  2106. receiveQuantity += unit;
  2107. }
  2108. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2109. {
  2110. StorageId = storage.Id,
  2111. Quantity = thisBatchReceiveQuantity
  2112. });
  2113. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2114. var storageUpd = storage;
  2115. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2116. batchStorageInfos.Add(storageUpd);
  2117. }
  2118. //2.2.1 更改批次库存
  2119. if (goodsReceiveInfos.Count > 0)
  2120. {
  2121. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2122. .UpdateColumns(x => x.ReceiveQuantity)
  2123. .WhereColumns(x => x.Id)
  2124. .ExecuteCommandAsync();
  2125. if (edit1 < 1)
  2126. {
  2127. _jv.Msg = $"批次库存更新失败!";
  2128. return _jv;
  2129. }
  2130. }
  2131. //2.2.2 添加入库批次关联领用人
  2132. if (goodsReceiveInfos.Count > 0)
  2133. {
  2134. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2135. }
  2136. }
  2137. }
  2138. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  2139. {
  2140. //出库确认中
  2141. auditEnum = GoodsAuditEnum.OutConfirming;
  2142. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2143. {
  2144. //回滚库存
  2145. _jv = await GoodsInventoryRollback(receiveInfo);
  2146. if (_jv.Code != StatusCodes.Status200OK) return _jv;
  2147. }
  2148. }
  2149. else auditEnum = GoodsAuditEnum.OutPending;
  2150. var statusDesc1 = statusDesc.ToString();
  2151. var changeStatus = await _sqlSugar
  2152. .Updateable<Pm_GoodsReceive>()
  2153. .SetColumns(x => new Pm_GoodsReceive()
  2154. {
  2155. AuditStatus = auditEnum,
  2156. StatusDesc = statusDesc1,
  2157. GoodsStorageInfo = goodsStorageInfo,
  2158. })
  2159. .Where(x => x.Id == receiveInfo.Id)
  2160. .ExecuteCommandAsync();
  2161. if (changeStatus > 0)
  2162. {
  2163. _jv.Code = StatusCodes.Status200OK;
  2164. }
  2165. return _jv;
  2166. }
  2167. /// <summary>
  2168. /// 物品库存回滚
  2169. /// </summary>
  2170. /// <param name="receiveInfo"></param>
  2171. /// <returns></returns>
  2172. public async Task<JsonView> GoodsInventoryRollback(Pm_GoodsReceive receiveInfo)
  2173. {
  2174. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  2175. //回滚库存
  2176. var changeGoods = await _sqlSugar
  2177. .Updateable(goodsInfo)
  2178. .ReSetValue(it => {
  2179. it.StockQuantity += receiveInfo.Quantity;
  2180. it.OQ_Total -= receiveInfo.Quantity;
  2181. it.LastUpdateTime = DateTime.Now;
  2182. it.LastUpdateUserId = receiveInfo.CreateUserId;
  2183. })
  2184. .ExecuteCommandAsync();
  2185. if (changeGoods < 1)
  2186. {
  2187. _jv.Msg = $"库存回滚失败!";
  2188. return _jv;
  2189. }
  2190. //批次领用记录
  2191. _jv.Code = StatusCodes.Status200OK;
  2192. var goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2193. if (string.IsNullOrEmpty(goodsStorageInfo)) return _jv;
  2194. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2195. if (!goodsStorageInfos.Any()) return _jv;
  2196. foreach (var item in goodsStorageInfos)
  2197. {
  2198. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2199. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2200. .FirstAsync();
  2201. if (newStorageInfo == null) continue;
  2202. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2203. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  2204. .ExecuteCommandAsync();
  2205. if (newEdit < 1)
  2206. {
  2207. _jv.Code = StatusCodes.Status400BadRequest;
  2208. _jv.Msg = $"批次领用库存更新失败!";
  2209. return _jv;
  2210. }
  2211. }
  2212. _jv.Code = StatusCodes.Status200OK;
  2213. return _jv;
  2214. }
  2215. #endregion
  2216. #region 单人审核状态变更
  2217. /// <summary>
  2218. /// 物品领用状态 - 出库确认完成、出库确认拒绝
  2219. /// </summary>
  2220. /// <param name="receiveInfo"></param>
  2221. /// <param name="userId"></param>
  2222. /// <param name="currUserName"></param>
  2223. /// <param name="auditEnum"></param>
  2224. /// <returns></returns>
  2225. public async Task<JsonView> GoodsReceiveOutConfirmingSingle(Pm_GoodsReceive receiveInfo, int userId,string currUserName,GoodsAuditEnum auditEnum)
  2226. {
  2227. _jv.Code = StatusCodes.Status400BadRequest;
  2228. //状态操作范围验证
  2229. var auditEnums = new List<GoodsAuditEnum>() {
  2230. GoodsAuditEnum.Pending,
  2231. GoodsAuditEnum.Approved,
  2232. GoodsAuditEnum.UnApproved,
  2233. };
  2234. if (!auditEnums.Contains(auditEnum))
  2235. {
  2236. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()}、{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可操作!";
  2237. return _jv;
  2238. }
  2239. var currUserOpDt = DateTime.Now;
  2240. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  2241. //更改前状态
  2242. var preChangeStatus = receiveInfo.AuditStatus;
  2243. //前台状态验证
  2244. var auditInfo = await _sqlSugar.Queryable<Pm_GoodsAudit>().FirstAsync(x => x.IsDel == 0 && x.Type == 2 && x.DataId == receiveInfo.Id);
  2245. if (auditInfo == null)
  2246. {
  2247. var addInfo = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr_Reception, receiveInfo.Id, GoodsConfirmEnum.WaitConfirm, userId);
  2248. auditInfo = await _sqlSugar.Insertable(addInfo).ExecuteReturnEntityAsync();
  2249. if (auditInfo == null)
  2250. {
  2251. _jv.Msg = $"确认操作失败!";
  2252. return _jv;
  2253. }
  2254. }
  2255. if (auditEnum == GoodsAuditEnum.Approved)
  2256. {
  2257. auditInfo.AuditStatus = GoodsConfirmEnum.Confirmed;
  2258. auditEnum = GoodsAuditEnum.OutConfirmed;
  2259. }
  2260. else if (auditEnum == GoodsAuditEnum.UnApproved)
  2261. {
  2262. auditInfo.AuditStatus = GoodsConfirmEnum.UnApproved;
  2263. auditEnum = GoodsAuditEnum.OutRejected;
  2264. }
  2265. if (preChangeStatus == auditEnum)
  2266. {
  2267. _jv.Msg = $"此操作已执行,不可重复操作!";
  2268. return _jv;
  2269. }
  2270. //更新子表审核状态
  2271. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  2272. .SetColumns(x => new Pm_GoodsAudit()
  2273. {
  2274. AuditStatus = auditInfo.AuditStatus,
  2275. AuditUserId = userId,
  2276. AuditTime = currUserOpDt
  2277. })
  2278. .Where(x => x.Id == auditInfo.Id)
  2279. .ExecuteCommandAsync();
  2280. if (updStatus < 1)
  2281. {
  2282. _jv.Msg = $"确认操作失败!";
  2283. return _jv;
  2284. }
  2285. //处理状态描述
  2286. StringBuilder statusDesc = new StringBuilder();
  2287. statusDesc.Append(string.Format("领用确认:状态:{0} 审核人:{1} 审核时间:{2};", auditInfo.AuditStatus.GetEnumDescription(), currUserName, currUserOpTime));
  2288. //批次库存信息
  2289. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2290. if (auditEnum == GoodsAuditEnum.OutConfirmed) //出库确认
  2291. {
  2292. //1 更改库存
  2293. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2294. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2295. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2296. goodsInfo.LastUpdateTime = DateTime.Now;
  2297. goodsInfo.LastUpdateUserId = userId;
  2298. var editGoods = await _sqlSugar.Updateable(goodsInfo)
  2299. .UpdateColumns(x => new
  2300. {
  2301. x.StockQuantity,
  2302. x.OQ_Total,
  2303. x.LastUpdateUserId,
  2304. x.LastUpdateTime,
  2305. })
  2306. .Where(x => x.Id == receiveInfo.GoodsId)
  2307. .ExecuteCommandAsync();
  2308. if (editGoods < 1)
  2309. {
  2310. _jv.Msg = $"库存更新失败!";
  2311. return _jv;
  2312. }
  2313. //2 入库批次关联领用人 更改批次库存
  2314. var goodsStorages = await _sqlSugar
  2315. .Queryable<Pm_GoodsStorage>()
  2316. .Where(x => x.IsDel == 0 &&
  2317. x.GoodsId == receiveInfo.GoodsId &&
  2318. (x.Quantity - x.ReceiveQuantity) > 0
  2319. )
  2320. .OrderBy(x => x.CreateTime)
  2321. .ToListAsync();
  2322. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2323. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2324. var receiveQuantity = 0.00M; //领用总数量
  2325. foreach (var storage in goodsStorages)
  2326. {
  2327. if (receiveInfo.Quantity == receiveQuantity) break;
  2328. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2329. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2330. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2331. const decimal unit = 0.50M;
  2332. while (receiveQuantity < receiveInfo.Quantity)
  2333. {
  2334. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2335. thisBatchReceiveQuantity += unit;
  2336. receiveQuantity += unit;
  2337. }
  2338. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2339. {
  2340. StorageId = storage.Id,
  2341. Quantity = thisBatchReceiveQuantity
  2342. });
  2343. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2344. var storageUpd = storage;
  2345. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2346. batchStorageInfos.Add(storageUpd);
  2347. }
  2348. //3 更改批次库存
  2349. if (goodsReceiveInfos.Count > 0)
  2350. {
  2351. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2352. .UpdateColumns(x => x.ReceiveQuantity)
  2353. .WhereColumns(x => x.Id)
  2354. .ExecuteCommandAsync();
  2355. if (edit1 < 1)
  2356. {
  2357. _jv.Msg = $"批次库存更新失败!";
  2358. return _jv;
  2359. }
  2360. }
  2361. //4 添加入库批次关联领用人
  2362. if (goodsReceiveInfos.Count > 0)
  2363. {
  2364. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2365. }
  2366. }
  2367. else if (auditEnum == GoodsAuditEnum.OutRejected) //出库拒绝
  2368. {
  2369. //拒绝之前的状态为已完成 回滚库存
  2370. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2371. {
  2372. //回滚库存
  2373. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  2374. var changeGoods = await _sqlSugar
  2375. .Updateable(goodsInfo)
  2376. .ReSetValue(it => {
  2377. it.StockQuantity += receiveInfo.Quantity;
  2378. it.OQ_Total -= receiveInfo.Quantity;
  2379. it.LastUpdateTime = DateTime.Now;
  2380. it.LastUpdateUserId = userId;
  2381. })
  2382. .ExecuteCommandAsync();
  2383. if (changeGoods < 1)
  2384. {
  2385. _jv.Msg = $"库存回滚失败!";
  2386. return _jv;
  2387. }
  2388. //批次号库存 信息更新
  2389. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  2390. if (!string.IsNullOrEmpty(goodsStorageInfo))
  2391. {
  2392. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2393. if (goodsStorageInfos.Any())
  2394. {
  2395. foreach (var item in goodsStorageInfos)
  2396. {
  2397. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2398. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2399. .FirstAsync();
  2400. if (newStorageInfo == null) continue;
  2401. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2402. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  2403. .ExecuteCommandAsync();
  2404. if (newEdit < 1)
  2405. {
  2406. _jv.Msg = $"批次领用库存更新失败!";
  2407. return _jv;
  2408. }
  2409. }
  2410. }
  2411. }
  2412. }
  2413. }
  2414. var statusDesc1 = statusDesc.ToString();
  2415. var changeStatus = await _sqlSugar
  2416. .Updateable<Pm_GoodsReceive>()
  2417. .SetColumns(x => new Pm_GoodsReceive()
  2418. {
  2419. AuditStatus = auditEnum,
  2420. StatusDesc = statusDesc1,
  2421. GoodsStorageInfo = goodsStorageInfo,
  2422. })
  2423. .Where(x => x.Id == receiveInfo.Id)
  2424. .ExecuteCommandAsync();
  2425. if (changeStatus > 0) _jv.Code = StatusCodes.Status200OK;
  2426. return _jv;
  2427. }
  2428. #endregion
  2429. /// <summary>
  2430. /// 物品领用 Del
  2431. /// </summary>
  2432. /// <param name="id"></param>
  2433. /// <param name="currUserId"></param>
  2434. /// <returns></returns>
  2435. public async Task<JsonView> GoodsReceiveDel(int id, int currUserId)
  2436. {
  2437. var receiveInfo = await _sqlSugar
  2438. .Queryable<Pm_GoodsReceive>()
  2439. .Where(x => x.IsDel == 0 && x.Id == id)
  2440. .FirstAsync();
  2441. if (receiveInfo.AuditStatus >= GoodsAuditEnum.Approved)
  2442. {
  2443. _jv.Msg = $"该条数据已进入审批流程,不可删除!";
  2444. return _jv;
  2445. }
  2446. var edit = await _sqlSugar
  2447. .Updateable<Pm_GoodsReceive>()
  2448. .SetColumns(x => new Pm_GoodsReceive()
  2449. {
  2450. IsDel = 1,
  2451. DeleteUserId = currUserId,
  2452. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  2453. })
  2454. .Where(x => x.Id == id)
  2455. .ExecuteCommandAsync();
  2456. if (edit > 0)
  2457. {
  2458. _jv.Msg = $"操作成功!";
  2459. _jv.Code = StatusCodes.Status200OK;
  2460. return _jv;
  2461. }
  2462. return _jv;
  2463. }
  2464. }
  2465. }