GoodsRepository.cs 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785
  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. //财务人事审核权限处理
  1054. var auditList = GoodsStorageConfirmAuditDep(2);
  1055. bool hrAuditPer = false;
  1056. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  1057. if (hrAuditInfo.AuditorIds.Any(x => x == currUserId))
  1058. {
  1059. hrAuditPer = true;
  1060. }
  1061. var beginBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.BeginDt) ? $"{dto.BeginDt} 00:00:00" : string.Empty, out var begin);
  1062. var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
  1063. RefAsync<int> total = 0;
  1064. var data = _sqlSugar.Queryable<Pm_GoodsReceive>()
  1065. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1066. .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
  1067. .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
  1068. .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  1069. .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  1070. .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
  1071. .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
  1072. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
  1073. .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
  1074. .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
  1075. .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
  1076. .WhereIF(groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
  1077. .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
  1078. .WhereIF(hrAuditPer, (gr, gi, sd, u1, u2, di) => _goodsTypeIds.Contains(gi.Type))
  1079. .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  1080. {
  1081. Id = gr.Id,
  1082. GroupId = gr.GroupId,
  1083. GroupName = di.TeamName,
  1084. GoodsId = gr.GoodsId,
  1085. GoodsName = gi.Name,
  1086. GoodsTypeId = gi.Type,
  1087. GoodsType = sd.Name,
  1088. Quantity = gr.Quantity,
  1089. Unit = gi.Unit,
  1090. Reason = gr.Reason,
  1091. Remark = gr.Remark,
  1092. AuditStatus = gr.AuditStatus,
  1093. StatusDesc = gr.StatusDesc,
  1094. AuditUserId = gr.AuditUserId,
  1095. AuditUserName = u1.CnName,
  1096. AuditTime = gr.AuditTime,
  1097. CreateUserName = u2.CnName,
  1098. CreateTime = gr.CreateTime
  1099. })
  1100. .OrderByDescending(gr => gr.CreateTime);
  1101. //excel导出
  1102. if (dto.IsExcelDownload)
  1103. {
  1104. var fileName = $"物资领用{Guid.NewGuid()}.xlsx";
  1105. var excelTempPath = $"{_excelPath}Template/物资领用Temp.xlsx";
  1106. if (!File.Exists(excelTempPath))
  1107. {
  1108. _jv.Code = StatusCodes.Status204NoContent;
  1109. _jv.Msg = $"该模板文件不存在!";
  1110. return _jv;
  1111. }
  1112. _url = $"{_url}Office/Excel/GoodsFiles/";
  1113. _excelPath = $"{_excelPath}GoodsFiles";
  1114. if (!Directory.Exists(_excelPath))
  1115. {
  1116. Directory.CreateDirectory(_excelPath);
  1117. }
  1118. //载入模板
  1119. WorkbookDesigner designer = new WorkbookDesigner();
  1120. designer.Workbook = new Workbook(excelTempPath);
  1121. var tableData = await data.ToListAsync();
  1122. designer.SetDataSource("Export", tableData);
  1123. designer.Process();
  1124. string serverPath = $"{_url}{fileName}";
  1125. designer.Workbook.Save($"{_excelPath}/{fileName}");
  1126. _jv.Code = StatusCodes.Status200OK;
  1127. _jv.Data = new { url = serverPath };
  1128. _jv.Msg = $"操作成功";
  1129. return _jv;
  1130. }
  1131. //返回分页数据
  1132. //领用审核List只显示多条审核数据 陈湘
  1133. var view = await data.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. }