VSMAppService.cs 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. using Azure;
  2. using Business.Core.Enum;
  3. using Business.Core.Utilities;
  4. using Business.Domain;
  5. using Business.Dto;
  6. using Business.EntityFrameworkCore;
  7. using Business.EntityFrameworkCore.SqlRepositories;
  8. using Business.ResourceExamineManagement;
  9. using Business.ResourceExamineManagement.Dto;
  10. using Business.StructuredDB.MES;
  11. using Business.StructuredDB.MES.IC;
  12. using Business.StructuredDB.SaleFcst;
  13. using Business.StructuredDB.WMS;
  14. using Business.VSM;
  15. using EFCore.BulkExtensions;
  16. using MathNet.Numerics.LinearAlgebra.Factorization;
  17. using Microsoft.EntityFrameworkCore;
  18. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  19. using Microsoft.Extensions.Configuration;
  20. using MongoDB.Driver.Linq;
  21. using Newtonsoft.Json;
  22. using NLog;
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Data;
  26. using System.Diagnostics.Metrics;
  27. using System.Linq;
  28. using System.Text;
  29. using System.Threading;
  30. using System.Threading.Tasks;
  31. using System.Xml.Linq;
  32. using Volo.Abp.Application.Services;
  33. using Volo.Abp.Domain.Repositories;
  34. using Volo.Abp.MultiTenancy;
  35. using WkHtmlToPdfDotNet;
  36. using ZstdSharp.Unsafe;
  37. using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
  38. namespace Business.VSMManagement
  39. {
  40. public class VSMAppService : ApplicationService,IVSMAppService
  41. {
  42. private readonly ISqlRepository<ItemMaster> _itemMaster;
  43. private readonly ISqlRepository<InvTransHist> _invTransHist;
  44. private readonly IRepository<srm_purchase> _srm_purchase;
  45. private readonly IRepository<ic_item> _ic_item;
  46. private readonly ISqlRepository<LocationDetail> _LocationDetail;
  47. private readonly ISqlRepository<InvTransHistDay> _InvTransHistDay;
  48. private readonly IExtRASqlRepository<DMS_IN_PODETAIL> _DMS_IN_PODETAIL;
  49. private readonly IExtRASqlRepository<DMS_IN_LOCDETAIL> _DMS_IN_LOCDETAIL;
  50. private readonly IExtRASqlRepository<DMS_IN_ITEMMAPPING> _DMS_IN_ITEMMAPPING;
  51. private readonly IExtRASqlRepository<DMS_IN_SHIPPINGDETAIL> _DMS_IN_SHIPPINGDETAIL;
  52. private readonly ISqlRepository<SAPInv> _SAPInv;
  53. private readonly IRepository<MonthlyShipmentPlan> _monthlyShipmentPlan;
  54. private readonly ISqlRepository<ASNBOLShipperDetail> _aSNBOLShipperDetail;
  55. private readonly BusinessDbContext _businessDbContext;
  56. public VSMAppService(
  57. ISqlRepository<ItemMaster> itemMaster,
  58. ISqlRepository<InvTransHist> invTransHist,
  59. IRepository<srm_purchase> srm_purchase,
  60. IRepository<ic_item> ic_item,
  61. ISqlRepository<LocationDetail> locationDetail,
  62. ISqlRepository<InvTransHistDay> invTransHistDay,
  63. IExtRASqlRepository<DMS_IN_PODETAIL> dms_IN_PODETAIL,
  64. IExtRASqlRepository<DMS_IN_LOCDETAIL> dms_IN_LOCDETAIL,
  65. IExtRASqlRepository<DMS_IN_ITEMMAPPING> dms_IN_ITEMMAPPING,
  66. IExtRASqlRepository<DMS_IN_SHIPPINGDETAIL> dms_IN_SHIPPINGDETAIL,
  67. ISqlRepository<SAPInv> SAPInv,
  68. IRepository<MonthlyShipmentPlan> monthlyShipmentPlan,
  69. ISqlRepository<ASNBOLShipperDetail> aSNBOLShipperDetail,
  70. BusinessDbContext businessDbContext
  71. )
  72. {
  73. _itemMaster = itemMaster;
  74. _invTransHist = invTransHist;
  75. _srm_purchase = srm_purchase;
  76. _ic_item = ic_item;
  77. _LocationDetail=locationDetail;
  78. _InvTransHistDay = invTransHistDay;
  79. _DMS_IN_PODETAIL = dms_IN_PODETAIL;
  80. _DMS_IN_LOCDETAIL= dms_IN_LOCDETAIL;
  81. _DMS_IN_ITEMMAPPING= dms_IN_ITEMMAPPING;
  82. _DMS_IN_SHIPPINGDETAIL = dms_IN_SHIPPINGDETAIL;
  83. _SAPInv =SAPInv;
  84. _monthlyShipmentPlan=monthlyShipmentPlan;
  85. _aSNBOLShipperDetail = aSNBOLShipperDetail;
  86. _businessDbContext = businessDbContext;
  87. }
  88. public string ValueFrequency(string Loc, string BeginDate, string EndDate, string KanBan, string Frequency)
  89. {
  90. try
  91. {
  92. DateTime beginDate = Convert.ToDateTime(BeginDate);
  93. DateTime endDate = Convert.ToDateTime(EndDate);
  94. string sql = $"select distinct ItemNum,Min(CreateTime) AS HistMinTime from InvTransHist where Domain='8010' and createTime>='{beginDate}' and createTime<='{endDate}' group by ItemNum";
  95. var InvTransHistDto = _businessDbContext.InvTransHistDayDto.FromSqlRaw(sql).ToList();
  96. var itemList = InvTransHistDto.Select(a => a.ItemNum).Distinct().ToList();
  97. ValueFrequencyDto dto = new ValueFrequencyDto();
  98. List<ValueFrequencyKanBanDto> kanBanDtos = new List<ValueFrequencyKanBanDto>();
  99. var icitems = _ic_item.GetListAsync(a => itemList.Contains(a.number)).Result;
  100. var srm_purchases = _srm_purchase.GetListAsync(a => icitems.Select(b=>b.Id).ToList().Contains(a.icitem_id)).Result;
  101. string sqldzd = @"select MATNR AS ItemNum,MAx(NETPR)*10 AS NETPR from SupplierStatement group by MATNR";
  102. var dzdList = _businessDbContext.SupplierStatementDto.FromSqlRaw(sqldzd).ToList();
  103. var InvTransHistDayDto = _InvTransHistDay.Select(a => a.HistDayTime >= beginDate.Date && a.HistDayTime <= endDate);
  104. itemList.ForEach(item =>
  105. {
  106. if(icitems.Any(a=>a.number==item))
  107. {
  108. ValueFrequencyKanBanDto itemKanBanDto = new ValueFrequencyKanBanDto();
  109. itemKanBanDto.ItemNumber = item;
  110. itemKanBanDto.ItemId = icitems.Find(a => a.number == item).Id;
  111. itemKanBanDto.Model = icitems.Find(a => a.number == item).model;
  112. itemKanBanDto.ItemName = icitems.Find(a => a.number == item).name;
  113. itemKanBanDto.PLT = 14;
  114. //供应提前期取货源清单最大值,没有默认14天
  115. if (srm_purchases.Any(a => a.icitem_id == itemKanBanDto.ItemId && a.quota_rate.GetValueOrDefault() > 0))
  116. {
  117. itemKanBanDto.PLT = srm_purchases.Where(a => a.number == item && a.quota_rate.GetValueOrDefault() > 0).Max(a => a.lead_time).Value;
  118. }
  119. var entry = dzdList.FirstOrDefault(p => p.ItemNum == item);
  120. if (entry != null)
  121. {
  122. itemKanBanDto.NETPR = entry.NETPR;
  123. }
  124. else
  125. {
  126. itemKanBanDto.NETPR = 1 * 10;
  127. }
  128. //出库总数
  129. itemKanBanDto.OutStockQty = InvTransHistDayDto.Where(a => a.ItemNum == item).Sum(b => b.QtyChangeOut);
  130. //库存数据取进出存期末数据
  131. itemKanBanDto.Amount = InvTransHistDayDto.Where(a => a.ItemNum == item).OrderByDescending(a => a.HistDayTime).First().BalanceNum * itemKanBanDto.NETPR;
  132. itemKanBanDto.MoveCount = InvTransHistDayDto.Where(a => a.ItemNum == item && a.QtyChangeOut > 0).Count();
  133. int days = endDate.Subtract(beginDate).Days;
  134. //同一天算1天
  135. if (days == 0)
  136. days = 1;
  137. itemKanBanDto.ADU = itemKanBanDto.OutStockQty / days;
  138. itemKanBanDto.Kanban = itemKanBanDto.ADU * itemKanBanDto.PLT;
  139. kanBanDtos.Add(itemKanBanDto);
  140. }
  141. });
  142. decimal totalmoveCount = kanBanDtos.Sum(a=>a.MoveCount);
  143. decimal totalAmount = kanBanDtos.Sum(a => a.Amount);
  144. decimal avgmoveCount = kanBanDtos.Average(a => a.MoveCount);
  145. kanBanDtos=kanBanDtos.OrderByDescending(a => a.Amount).ToList();
  146. for (int i=0;i< kanBanDtos.Count;i++)
  147. {
  148. if (kanBanDtos[i].MoveCount > avgmoveCount)
  149. {
  150. kanBanDtos[i].FMR = "F";
  151. }
  152. else if(kanBanDtos[i].MoveCount < avgmoveCount / 2)
  153. {
  154. kanBanDtos[i].FMR = "R";
  155. }else
  156. {
  157. kanBanDtos[i].FMR = "M";
  158. }
  159. if(i<=kanBanDtos.Count*0.7)
  160. {
  161. kanBanDtos[i].ABC = "A";
  162. }else if(i>=kanBanDtos.Count*0.9)
  163. {
  164. kanBanDtos[i].ABC = "C";
  165. }else
  166. {
  167. kanBanDtos[i].ABC = "B";
  168. }
  169. kanBanDtos[i].AmountTotal = totalAmount;
  170. kanBanDtos[i].MovePencent = totalmoveCount == 0 ? 0 : kanBanDtos[i].MoveCount * 100 / totalmoveCount;
  171. }
  172. int totalCount = kanBanDtos.Count();
  173. dto.jzpc1 = $"{kanBanDtos.Where(a => a.ABC == "A" && a.FMR == "R").Count()}/{kanBanDtos.Count}";
  174. dto.jzpc2 = $"{kanBanDtos.Where(a => a.ABC == "A" && a.FMR == "M").Count()}/{kanBanDtos.Count}";
  175. dto.jzpc3 = $"{kanBanDtos.Where(a => a.ABC == "A" && a.FMR == "F").Count()}/{kanBanDtos.Count}";
  176. dto.jzpc4 = $"{kanBanDtos.Where(a => a.ABC == "B" && a.FMR == "R").Count()}/{kanBanDtos.Count}";
  177. dto.jzpc5 = $"{kanBanDtos.Where(a => a.ABC == "B" && a.FMR == "M").Count()}/{kanBanDtos.Count}";
  178. dto.jzpc6 = $"{kanBanDtos.Where(a => a.ABC == "B" && a.FMR == "F").Count()}/{kanBanDtos.Count}";
  179. dto.jzpc7 = $"{kanBanDtos.Where(a => a.ABC == "C" && a.FMR == "R").Count()}/{kanBanDtos.Count}";
  180. dto.jzpc8 = $"{kanBanDtos.Where(a => a.ABC == "C" && a.FMR == "M").Count()}/{kanBanDtos.Count}";
  181. dto.jzpc9 = $"{kanBanDtos.Where(a => a.ABC == "C" && a.FMR == "F").Count()}/{kanBanDtos.Count}";
  182. return JsonConvert.SerializeObject(dto);
  183. }
  184. catch (Exception ex)
  185. {
  186. return ex.Message;
  187. }
  188. }
  189. public string ValueFrequencyDetail(string Loc, string BeginDate, string EndDate, string KanBan, string Frequency, string JZ, string PC)
  190. {
  191. try
  192. {
  193. List<ValueFrequencyDetailDto> dtos = new List<ValueFrequencyDetailDto>();
  194. try
  195. {
  196. DateTime beginDate = Convert.ToDateTime(BeginDate);
  197. DateTime endDate = Convert.ToDateTime(EndDate);
  198. string sql = $"select distinct ItemNum,Min(CreateTime) AS HistMinTime from InvTransHist where Domain='8010' and createTime>='{beginDate}' and createTime<='{endDate}' group by ItemNum";
  199. var InvTransHistDto = _businessDbContext.InvTransHistDayDto.FromSqlRaw(sql).ToList();
  200. var itemList = InvTransHistDto.Select(a => a.ItemNum).Distinct().ToList();
  201. ValueFrequencyDto dto = new ValueFrequencyDto();
  202. List<ValueFrequencyKanBanDto> kanBanDtos = new List<ValueFrequencyKanBanDto>();
  203. var icitems = _ic_item.GetListAsync(a => itemList.Contains(a.number)).Result;
  204. var srm_purchases = _srm_purchase.GetListAsync(a => icitems.Select(b => b.Id).ToList().Contains(a.icitem_id)).Result;
  205. string sqldzd = @"select MATNR AS ItemNum,Max(NETPR)*10 AS NETPR from SupplierStatement group by MATNR";
  206. var dzdList = _businessDbContext.SupplierStatementDto.FromSqlRaw(sqldzd).ToList();
  207. var InvTransHistDayDto = _InvTransHistDay.Select(a=>a.HistDayTime>= beginDate.Date && a.HistDayTime<= endDate);
  208. itemList.ForEach(item =>
  209. {
  210. if (icitems.Any(a => a.number == item))
  211. {
  212. ValueFrequencyKanBanDto itemKanBanDto = new ValueFrequencyKanBanDto();
  213. itemKanBanDto.ItemNumber = item;
  214. itemKanBanDto.ItemId = icitems.Find(a => a.number == item).Id;
  215. itemKanBanDto.Model = icitems.Find(a => a.number == item).model;
  216. itemKanBanDto.ItemName = icitems.Find(a => a.number == item).name;
  217. itemKanBanDto.PLT = 14;
  218. //供应提前期取货源清单最大值,没有默认14天
  219. if (srm_purchases.Any(a => a.icitem_id == itemKanBanDto.ItemId && a.quota_rate.GetValueOrDefault() > 0))
  220. {
  221. itemKanBanDto.PLT = srm_purchases.Where(a => a.number == item && a.quota_rate.GetValueOrDefault() > 0).Max(a => a.lead_time).Value;
  222. }
  223. var entry = dzdList.FirstOrDefault(p => p.ItemNum == item);
  224. if (entry != null)
  225. {
  226. itemKanBanDto.NETPR = entry.NETPR;
  227. }
  228. else
  229. {
  230. itemKanBanDto.NETPR = 1 * 10;
  231. }
  232. //出库总数
  233. itemKanBanDto.OutStockQty = InvTransHistDayDto.Where(a=>a.ItemNum==item).Sum(b=>b.QtyChangeOut);
  234. //库存数据取进出存期末数据
  235. itemKanBanDto.Amount = InvTransHistDayDto.Where(a => a.ItemNum == item).OrderByDescending(a=>a.HistDayTime).First().BalanceNum * itemKanBanDto.NETPR;
  236. itemKanBanDto.MoveCount = InvTransHistDayDto.Where(a => a.ItemNum == item && a.QtyChangeOut>0).Count();
  237. int days = endDate.Subtract(beginDate).Days;
  238. //同一天算1天
  239. if (days == 0)
  240. days = 1;
  241. itemKanBanDto.ADU = itemKanBanDto.OutStockQty / days;
  242. itemKanBanDto.Kanban = itemKanBanDto.ADU * itemKanBanDto.PLT;
  243. kanBanDtos.Add(itemKanBanDto);
  244. }
  245. });
  246. decimal totalmoveCount = kanBanDtos.Sum(a => a.MoveCount);
  247. decimal totalAmount = kanBanDtos.Sum(a => a.Amount);
  248. decimal avgmoveCount = kanBanDtos.Average(a => a.MoveCount);
  249. kanBanDtos = kanBanDtos.OrderByDescending(a => a.Amount).ToList();
  250. for (int i = 0; i < kanBanDtos.Count; i++)
  251. {
  252. if (kanBanDtos[i].MoveCount > avgmoveCount)
  253. {
  254. kanBanDtos[i].FMR = "F";
  255. }
  256. else if (kanBanDtos[i].MoveCount < avgmoveCount / 2)
  257. {
  258. kanBanDtos[i].FMR = "R";
  259. }
  260. else
  261. {
  262. kanBanDtos[i].FMR = "M";
  263. }
  264. if (i <= kanBanDtos.Count * 0.7)
  265. {
  266. kanBanDtos[i].ABC = "A";
  267. }
  268. else if (i >= kanBanDtos.Count * 0.9)
  269. {
  270. kanBanDtos[i].ABC = "C";
  271. }
  272. else
  273. {
  274. kanBanDtos[i].ABC = "B";
  275. }
  276. kanBanDtos[i].AmountTotal = totalAmount;
  277. kanBanDtos[i].MovePencent = totalmoveCount == 0 ? 0 : kanBanDtos[i].MoveCount * 100 / totalmoveCount;
  278. }
  279. kanBanDtos=kanBanDtos.Where(a=>a.ABC==JZ&&a.FMR==PC && a.Kanban > Convert.ToDecimal(KanBan) && a.MovePencent > Convert.ToDecimal(Frequency)).OrderByDescending(a=>a.Amount).ToList();
  280. for(int i=0;i< kanBanDtos.Count(); i++)
  281. {
  282. ValueFrequencyDetailDto detailDto = new ValueFrequencyDetailDto();
  283. detailDto.RowSeq =i+1;
  284. detailDto.ItemNum = kanBanDtos[i].ItemNumber;
  285. detailDto.ItemName = kanBanDtos[i].ItemName;
  286. detailDto.Model = kanBanDtos[i].Model;
  287. detailDto.ItemABC = kanBanDtos[i].ABC;
  288. detailDto.ItemFMR = kanBanDtos[i].FMR;
  289. detailDto.Amount = kanBanDtos[i].Amount;
  290. detailDto.AmountFix = Convert.ToDecimal(KanBan) * kanBanDtos[i].NETPR;
  291. detailDto.AmountDiff = detailDto.Amount- detailDto.AmountFix;
  292. dtos.Add(detailDto);
  293. }
  294. return JsonConvert.SerializeObject(dtos);
  295. }
  296. catch (Exception ex)
  297. {
  298. return ex.Message;
  299. }
  300. }
  301. catch (Exception ex)
  302. {
  303. return ex.Message;
  304. }
  305. }
  306. public string ChartLineOption(string Loc, string BeginDate, string EndDate, string ItemNum,string TurnOver,string KanBan)
  307. {
  308. try
  309. {
  310. DateTime beginDate = Convert.ToDateTime(BeginDate);
  311. DateTime endDate = Convert.ToDateTime(EndDate);
  312. var srm_purchases = _srm_purchase.GetListAsync(a =>a.number==ItemNum).Result;
  313. decimal chartPLT = 14;
  314. decimal chartKanBan = Convert.ToDecimal(KanBan);
  315. //供应提前期取货源清单最大值,没有默认14天
  316. if (srm_purchases.Any(a => a.number == ItemNum && a.quota_rate.GetValueOrDefault() > 0))
  317. {
  318. chartPLT = srm_purchases.Where(a => a.number == ItemNum && a.quota_rate.GetValueOrDefault() > 0).Max(a => a.lead_time).Value;
  319. }
  320. var InvTransHistDayDto = _InvTransHistDay.Select(a => a.ItemNum== ItemNum && a.HistDayTime >= beginDate.Date && a.HistDayTime <= endDate);
  321. int tranDays = (endDate-beginDate).Days;
  322. List<ValueFrequencyChartDto> chartDtos = new List<ValueFrequencyChartDto>();
  323. for(int i = 0; i < tranDays; i++)
  324. {
  325. ValueFrequencyChartDto dtoChart = new ValueFrequencyChartDto();
  326. dtoChart.Date = beginDate.AddDays(i).ToString("yyyy-MM-dd");
  327. var itemDays = InvTransHistDayDto.Where(a => beginDate.AddDays(i).Date == a.HistDayTime.Date).ToList();
  328. if(itemDays.Count>0)
  329. {
  330. dtoChart.InStockQty = itemDays[0].QtyChangeAdvance;
  331. dtoChart.OutStockQty = itemDays[0].QtyChangeOut;
  332. dtoChart.StockQty = itemDays[0].BalanceNum;
  333. dtoChart.SuggestQty = chartKanBan * chartPLT;
  334. }else
  335. {
  336. dtoChart.InStockQty = 0;
  337. dtoChart.OutStockQty = 0;
  338. dtoChart.StockQty = 0;
  339. dtoChart.SuggestQty = 0;
  340. }
  341. chartDtos.Add(dtoChart);
  342. }
  343. chartDtos = chartDtos.Where(a => string.Compare(a.Date, BeginDate) >= 0 && string.Compare(a.Date, EndDate) <= 0).ToList();
  344. StringBuilder sb = new StringBuilder();
  345. StringBuilder sbDays = new StringBuilder();
  346. StringBuilder sbDaysStock = new StringBuilder();
  347. StringBuilder sbDaysInStock = new StringBuilder();
  348. StringBuilder sbDaysOutStock = new StringBuilder();
  349. StringBuilder sbDaysSuggest = new StringBuilder();
  350. sb.Append("{tooltip:{trigger:'axis'},legend:{data: ['日库存数量','日出库数量','日入库数量','推荐日库存数量']},grid:{left:'3%',right:'4%',bottom:'3%',containLabel:true},toolbox:{feature:{saveAsImage: {}}},xAxis: {type:'category',boundaryGap: false,data:[");
  351. chartDtos.ForEach(a =>
  352. {
  353. sbDays.Append(string.Format("'{0}',",a.Date));
  354. sbDaysStock.Append(string.Format("{0},", a.StockQty));
  355. sbDaysOutStock.Append(string.Format("{0},", a.OutStockQty));
  356. sbDaysInStock.Append(string.Format("{0},", a.InStockQty));
  357. sbDaysSuggest.Append(string.Format("{0},", a.SuggestQty));
  358. });
  359. sb.Append(sbDays.ToString().TrimEnd(',')).
  360. Append("]},yAxis:{type:'value',},series:[{name:'日库存数量',type:'line',stack: 'Total',data:[").
  361. Append(sbDaysStock.ToString().TrimEnd(',')).
  362. Append("]},{name:'日出库数量',type:'line',stack:'Total',data:[").
  363. Append(sbDaysOutStock.ToString().TrimEnd(',')).
  364. Append("]},{name:'日入库数量',type:'line',stack:'Total',data:[").
  365. Append(sbDaysInStock.ToString().TrimEnd(',')).
  366. Append("]},{name:'推荐日库存数量',type:'line',stack:'Total',data:[").
  367. Append(sbDaysSuggest.ToString().TrimEnd(',')).
  368. Append("]}]}");
  369. return sb.ToString();
  370. }
  371. catch (Exception ex)
  372. {
  373. return ex.Message;
  374. }
  375. }
  376. public string ChartLineSupplierPLTOption()
  377. {
  378. return "{title:{text: '供应商PLT占比(%)',left:'center',},tooltip:{trigger: 'axis'},toolbox: {show: true,feature:{saveAsImage: {}}},xAxis: {type: 'category',data: ['1', '2', '3', '4', '5', '6', '7']},yAxis: {type: 'value',axisLabel: {formatter: '{value} %'},axisPointer: {snap: true} },series: [{data: [2, 5, 18, 55, 12, 5, 3],type: 'line',smooth: true}]}";
  379. }
  380. public string ChartLinePLTOption(string PLT)
  381. {
  382. if(PLT=="25")
  383. {
  384. string sql = @"select t.[Date] AS [Date],t.DateStockQty AS StockQty,t.DateOutStockQty AS OutStockQty,t.DateInStockQty AS InStockQty,0.0 AS SuggestQty from [dbo].[25PLT] t";
  385. var chartDtosTest = _businessDbContext.ValueFrequencyChartDto.FromSqlRaw(sql).ToList();
  386. StringBuilder sb = new StringBuilder();
  387. StringBuilder sbDays = new StringBuilder();
  388. StringBuilder sbDaysStock = new StringBuilder();
  389. StringBuilder sbDaysInStock = new StringBuilder();
  390. StringBuilder sbDaysOutStock = new StringBuilder();
  391. sb.Append("{tooltip:{trigger:'axis'},legend:{data: ['日库存数量','日出库数量','日入库数量']},grid:{left:'3%',right:'4%',bottom:'3%',containLabel:true},toolbox:{feature:{saveAsImage: {}}},xAxis: {type:'category',boundaryGap: false,data:[");
  392. chartDtosTest.ForEach(a =>
  393. {
  394. sbDays.Append(string.Format("'{0}',", a.Date));
  395. sbDaysStock.Append(string.Format("{0},", a.StockQty));
  396. sbDaysOutStock.Append(string.Format("{0},", a.OutStockQty));
  397. sbDaysInStock.Append(string.Format("{0},", a.InStockQty));
  398. });
  399. sb.Append(sbDays.ToString().TrimEnd(',')).
  400. Append("]},yAxis:{type:'value',},series:[{name:'日库存数量',type:'line',data:[").
  401. Append(sbDaysStock.ToString().TrimEnd(',')).
  402. Append("]},{name:'日出库数量',type:'line',data:[").
  403. Append(sbDaysOutStock.ToString().TrimEnd(',')).
  404. Append("]},{name:'日入库数量',type:'line',data:[").
  405. Append(sbDaysInStock.ToString().TrimEnd(',')).
  406. Append("]}").Append("]}");
  407. return sb.ToString();
  408. }else
  409. {
  410. string sql = @"select t.[Date] AS [Date],t.DateStockQty AS StockQty,t.DateOutStockQty AS OutStockQty,t.DateInStockQty AS InStockQty,0.0 AS SuggestQty from [dbo].[15PLT] t";
  411. var chartDtosTest = _businessDbContext.ValueFrequencyChartDto.FromSqlRaw(sql).ToList();
  412. StringBuilder sb = new StringBuilder();
  413. StringBuilder sbDays = new StringBuilder();
  414. StringBuilder sbDaysStock = new StringBuilder();
  415. StringBuilder sbDaysInStock = new StringBuilder();
  416. StringBuilder sbDaysOutStock = new StringBuilder();
  417. sb.Append("{tooltip:{trigger:'axis'},legend:{data: ['日库存数量','日出库数量','日入库数量']},grid:{left:'3%',right:'4%',bottom:'3%',containLabel:true},toolbox:{feature:{saveAsImage: {}}},xAxis: {type:'category',boundaryGap: false,data:[");
  418. chartDtosTest.ForEach(a =>
  419. {
  420. sbDays.Append(string.Format("'{0}',", a.Date));
  421. sbDaysStock.Append(string.Format("{0},", a.StockQty));
  422. sbDaysOutStock.Append(string.Format("{0},", a.OutStockQty));
  423. sbDaysInStock.Append(string.Format("{0},", a.InStockQty));
  424. });
  425. sb.Append(sbDays.ToString().TrimEnd(',')).
  426. Append("]},yAxis:{type:'value',},series:[{name:'日库存数量',type:'line',data:[").
  427. Append(sbDaysStock.ToString().TrimEnd(',')).
  428. Append("]},{name:'日出库数量',type:'line',data:[").
  429. Append(sbDaysOutStock.ToString().TrimEnd(',')).
  430. Append("]},{name:'日入库数量',type:'line',data:[").
  431. Append(sbDaysInStock.ToString().TrimEnd(',')).
  432. Append("]}").Append("]}");
  433. return sb.ToString();
  434. }
  435. }
  436. public string ChartLineModelOption(string factory_id, string model,string avgHW, string avgGK, string avgT1, string avgTotal)
  437. {
  438. var loclist = _DMS_IN_LOCDETAIL.Select(a => a.UPN == model);
  439. var lotList=loclist.Select(a => a.LOT).Distinct().ToList();
  440. decimal? hw = 0;
  441. decimal? gk = 0;
  442. decimal? t1 = 0;
  443. decimal? total = 0;
  444. lotList.ForEach(a =>
  445. {
  446. var itemHW = loclist.Where(x => x.LOT == a && x.DealerLevel == "LP" && x.DealerCode == "RQ000002").ToList();
  447. if(itemHW.Count>0)
  448. {
  449. hw += itemHW.OrderByDescending(y => y.InventoryDate).First().Qty;
  450. }
  451. var itemGK = loclist.Where(x => x.LOT == a && x.DealerLevel == "LP" && x.DealerCode == "RQ000005").ToList();
  452. if (itemGK.Count > 0)
  453. {
  454. gk += itemGK.OrderByDescending(y => y.InventoryDate).First().Qty;
  455. }
  456. var itemT1 = loclist.Where(x => x.LOT == a && (x.DealerLevel == "T1" || x.DealerLevel == "LS")).ToList();
  457. if (itemT1.Count > 0)
  458. {
  459. t1 += itemT1.OrderByDescending(y => y.InventoryDate).First().Qty;
  460. }
  461. });
  462. total=hw+gk+t1;
  463. if(avgHW!="0")
  464. {
  465. string numDecimalStr = (hw.Value / decimal.Parse(avgHW)).ToString("#0.00");
  466. hw = decimal.Parse(numDecimalStr);
  467. }else
  468. {
  469. hw = 0;
  470. }
  471. if (avgGK != "0")
  472. {
  473. string numDecimalStr = (hw.Value / decimal.Parse(avgGK)).ToString("#0.00");
  474. gk = decimal.Parse(numDecimalStr);
  475. }
  476. else
  477. {
  478. gk = 0;
  479. }
  480. if (avgT1 != "0")
  481. {
  482. string numDecimalStr = (hw.Value / decimal.Parse(avgT1)).ToString("#0.00");
  483. t1 = decimal.Parse(numDecimalStr);
  484. }
  485. else
  486. {
  487. t1 = 0;
  488. }
  489. if (avgTotal != "0")
  490. {
  491. string numDecimalStr = (hw.Value / decimal.Parse(avgTotal)).ToString("#0.00");
  492. total = decimal.Parse(numDecimalStr);
  493. }
  494. else
  495. {
  496. total = 0;
  497. }
  498. StringBuilder sb = new StringBuilder();
  499. sb.Append("{title: {text:'库存可供应周期(月)',left: 'center'},xAxis:{type:'category',data:['海王库存','国科库存','T1','Total']},yAxis:{type:'value'},series:[{data:[" + hw+","+gk+","+t1+","+total+"],type:'bar',label:{show:true,position:'top'},itemStyle:{normal:{color:function(params){if(params.value<4){return 'red';}else{return '#5470c6';}}}}}]}");
  500. return sb.ToString();
  501. }
  502. public string ChartLineOutStockOption(string factory_id, string model)
  503. {
  504. DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Now.AddMonths(-14));
  505. var polist=_DMS_IN_PODETAIL.Select(a => a.UPN == model && a.OrderStatus != "Revoked" && a.OrderStatus != "Rejected" && a.SubmitDate>=dateOnly);
  506. StringBuilder sbDate = new StringBuilder();
  507. StringBuilder sbHWOutStock = new StringBuilder();
  508. StringBuilder sbGKOutStock = new StringBuilder();
  509. StringBuilder sbT1OutStock = new StringBuilder();
  510. StringBuilder sbTotalOutStock = new StringBuilder();
  511. for (int i=-14;i<1;i++)
  512. {
  513. sbDate.Append(string.Format("'{0}',",DateTime.Now.AddMonths(i).Date.ToString("yyyy-MM-dd")));
  514. var hw = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(i)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(i + 1)) && a.DealerCode== "RQ000002" && a.DealerLevel=="LP").Sum(a => a.RequiredQty);
  515. var gk = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(i)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(i + 1)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty);
  516. var t1 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(i)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(i + 1)) &&(a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty);
  517. var total = hw + gk + t1;
  518. sbHWOutStock.Append(string.Format("{0},", hw));
  519. sbGKOutStock.Append(string.Format("{0},", gk));
  520. sbT1OutStock.Append(string.Format("{0},", t1));
  521. sbTotalOutStock.Append(string.Format("{0},", total));
  522. }
  523. StringBuilder sb = new StringBuilder();
  524. sb.Append("{title: {text:'");
  525. sb.Append(model+"出货趋势',");
  526. sb.Append("left: 'center',},tooltip: {trigger: 'axis'},legend: {data:['海王', '国科', 'T1','Total'],bottom:-5,orient: 'horizontal'},grid: {left: '3%',right: '4%',bottom: '3%',containLabel: true},toolbox: {feature: {saveAsImage: {}}},xAxis:{type: 'category',boundaryGap: false,data:[");
  527. sb.Append(sbDate.ToString().TrimEnd(',')).
  528. Append("]},yAxis:{type:'value',},series:[{name:'海王',type:'line',stack: 'Total',data:[").
  529. Append(sbHWOutStock.ToString().TrimEnd(',')).
  530. Append("]},{name:'国科',type:'line',stack:'Total',data:[").
  531. Append(sbGKOutStock.ToString().TrimEnd(',')).
  532. Append("]},{name:'T1',type:'line',stack:'Total',data:[").
  533. Append(sbT1OutStock.ToString().TrimEnd(',')).
  534. Append("]},{name:'Total',type:'line',stack:'Total',data:[").
  535. Append(sbTotalOutStock.ToString().TrimEnd(',')).
  536. Append("]}]}");
  537. return sb.ToString();
  538. }
  539. public string ChartLineOutStock(string factory_id, string model)
  540. {
  541. DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Now.AddMonths(-14));
  542. var polist = _DMS_IN_PODETAIL.Select(a => a.UPN == model && a.OrderStatus != "Revoked" && a.OrderStatus != "Rejected" && a.SubmitDate >= dateOnly);
  543. List<ModelMonthOutkDto> modelMonthOuts=new List<ModelMonthOutkDto>();
  544. ModelMonthOutkDto stockDto1 = new ModelMonthOutkDto();
  545. stockDto1.Month0 = "海王";
  546. stockDto1.Month1 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-14)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-13)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  547. stockDto1.Month2 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-13)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-12)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  548. stockDto1.Month3 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-12)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-11)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  549. stockDto1.Month4 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-11)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-10)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  550. stockDto1.Month5 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-10)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-9)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  551. stockDto1.Month6 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-9)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-8)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  552. stockDto1.Month7 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-8)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-7)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  553. stockDto1.Month8 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-7)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-6)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  554. stockDto1.Month9 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-6)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-5)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  555. stockDto1.Month10 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-5)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-4)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  556. stockDto1.Month11 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-4)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-3)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  557. stockDto1.Month12 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-3)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-2)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  558. stockDto1.Month13 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-2)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-1)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  559. stockDto1.Month14 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-1)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(0)) && a.DealerCode == "RQ000002" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  560. modelMonthOuts.Add(stockDto1);
  561. ModelMonthOutkDto stockDto2 = new ModelMonthOutkDto();
  562. stockDto2.Month0 = "国科";
  563. stockDto2.Month1 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-14)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-13)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value);
  564. stockDto2.Month2 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-13)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-12)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  565. stockDto2.Month3 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-12)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-11)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  566. stockDto2.Month4 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-11)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-10)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  567. stockDto2.Month5 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-10)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-9)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  568. stockDto2.Month6 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-9)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-8)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  569. stockDto2.Month7 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-8)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-7)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  570. stockDto2.Month8 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-7)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-6)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  571. stockDto2.Month9 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-6)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-5)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  572. stockDto2.Month10 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-5)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-4)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  573. stockDto2.Month11 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-4)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-3)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  574. stockDto2.Month12 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-3)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-2)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  575. stockDto2.Month13 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-2)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-1)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  576. stockDto2.Month14 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-1)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(0)) && a.DealerCode == "RQ000005" && a.DealerLevel == "LP").Sum(a => a.RequiredQty.Value); ;
  577. modelMonthOuts.Add(stockDto2);
  578. ModelMonthOutkDto stockDto3 = new ModelMonthOutkDto();
  579. stockDto3.Month0 = "T1";
  580. stockDto3.Month1 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-14)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-13)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value);
  581. stockDto3.Month2 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-13)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-12)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  582. stockDto3.Month3 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-12)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-11)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  583. stockDto3.Month4 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-11)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-10)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  584. stockDto3.Month5 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-10)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-9)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  585. stockDto3.Month6 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-9)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-8)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  586. stockDto3.Month7 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-8)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-7)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  587. stockDto3.Month8 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-7)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-6)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  588. stockDto3.Month9 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-6)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-5)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  589. stockDto3.Month10 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-5)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-4)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  590. stockDto3.Month11 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-4)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-3)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  591. stockDto3.Month12 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-3)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-2)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  592. stockDto3.Month13 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-2)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(-1)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  593. stockDto3.Month14 = polist.Where(a => a.SubmitDate >= DateOnly.FromDateTime(DateTime.Now.AddMonths(-1)) && a.SubmitDate < DateOnly.FromDateTime(DateTime.Now.AddMonths(0)) && (a.DealerLevel == "LP" || a.DealerLevel == "LS")).Sum(a => a.RequiredQty.Value); ;
  594. modelMonthOuts.Add(stockDto3);
  595. ModelMonthOutkDto stockDto4 = new ModelMonthOutkDto();
  596. stockDto4.Month0 = "Total";
  597. stockDto4.Month1 =stockDto1.Month1+stockDto2.Month1+stockDto3.Month1;
  598. stockDto4.Month2 = stockDto1.Month2 + stockDto2.Month2 + stockDto3.Month2;
  599. stockDto4.Month3 = stockDto1.Month3 + stockDto2.Month3 + stockDto3.Month3;
  600. stockDto4.Month4 = stockDto1.Month4 + stockDto2.Month4 + stockDto3.Month4;
  601. stockDto4.Month5 = stockDto1.Month5 + stockDto2.Month5 + stockDto3.Month5;
  602. stockDto4.Month6 = stockDto1.Month6 + stockDto2.Month6 + stockDto3.Month6;
  603. stockDto4.Month7 = stockDto1.Month7 + stockDto2.Month7 + stockDto3.Month7;
  604. stockDto4.Month8 = stockDto1.Month8 + stockDto2.Month8 + stockDto3.Month8;
  605. stockDto4.Month9 = stockDto1.Month9 + stockDto2.Month9 + stockDto3.Month9;
  606. stockDto4.Month10 = stockDto1.Month10 + stockDto2.Month10 + stockDto3.Month10;
  607. stockDto4.Month11 = stockDto1.Month11 + stockDto2.Month11 + stockDto3.Month11;
  608. stockDto4.Month12 = stockDto1.Month12 + stockDto2.Month12 + stockDto3.Month12;
  609. stockDto4.Month13 = stockDto1.Month13 + stockDto2.Month13 + stockDto3.Month13;
  610. stockDto4.Month14 = stockDto1.Month14 + stockDto2.Month14 + stockDto3.Month14;
  611. modelMonthOuts.Add(stockDto4);
  612. ModelMonthOutkDto stockDto5 = new ModelMonthOutkDto();
  613. stockDto5.Month0 = "海王均值";
  614. stockDto5.Month1 = decimal.Ceiling((stockDto1.Month12+stockDto1.Month13+stockDto1.Month14)/3);
  615. stockDto5.Month2 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  616. stockDto5.Month3 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  617. stockDto5.Month4 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  618. stockDto5.Month5 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  619. stockDto5.Month6 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  620. stockDto5.Month7 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  621. stockDto5.Month8 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  622. stockDto5.Month9 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  623. stockDto5.Month10 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  624. stockDto5.Month11 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  625. stockDto5.Month12 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  626. stockDto5.Month13 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  627. stockDto5.Month14 = decimal.Ceiling((stockDto1.Month12 + stockDto1.Month13 + stockDto1.Month14) / 3);
  628. modelMonthOuts.Add(stockDto5);
  629. ModelMonthOutkDto stockDto6 = new ModelMonthOutkDto();
  630. stockDto6.Month0 = "国科均值";
  631. stockDto6.Month1 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  632. stockDto6.Month2 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  633. stockDto6.Month3 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  634. stockDto6.Month4 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  635. stockDto6.Month5 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  636. stockDto6.Month6 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  637. stockDto6.Month7 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  638. stockDto6.Month8 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  639. stockDto6.Month9 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  640. stockDto6.Month10 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  641. stockDto6.Month11 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  642. stockDto6.Month12 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  643. stockDto6.Month13 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  644. stockDto6.Month14 = decimal.Ceiling((stockDto2.Month12 + stockDto2.Month13 + stockDto2.Month14) / 3);
  645. modelMonthOuts.Add(stockDto6);
  646. ModelMonthOutkDto stockDto7 = new ModelMonthOutkDto();
  647. stockDto7.Month0 = "T1均值";
  648. stockDto7.Month1 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  649. stockDto7.Month2 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  650. stockDto7.Month3 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  651. stockDto7.Month4 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  652. stockDto7.Month5 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  653. stockDto7.Month6 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  654. stockDto7.Month7 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  655. stockDto7.Month8 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  656. stockDto7.Month9 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  657. stockDto7.Month10 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  658. stockDto7.Month11 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  659. stockDto7.Month12 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  660. stockDto7.Month13 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  661. stockDto7.Month14 = decimal.Ceiling((stockDto3.Month12 + stockDto3.Month13 + stockDto3.Month14) / 3);
  662. modelMonthOuts.Add(stockDto7);
  663. ModelMonthOutkDto stockDto8 = new ModelMonthOutkDto();
  664. stockDto8.Month0 = "Total均值";
  665. stockDto8.Month1 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  666. stockDto8.Month2 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  667. stockDto8.Month3 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  668. stockDto8.Month4 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  669. stockDto8.Month5 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  670. stockDto8.Month6 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  671. stockDto8.Month7 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  672. stockDto8.Month8 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  673. stockDto8.Month9 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  674. stockDto8.Month10 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  675. stockDto8.Month11 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  676. stockDto8.Month12 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  677. stockDto8.Month13 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  678. stockDto8.Month14 = decimal.Ceiling((stockDto4.Month12 + stockDto4.Month13 + stockDto4.Month14) / 3);
  679. modelMonthOuts.Add(stockDto8);
  680. return JsonConvert.SerializeObject(modelMonthOuts);
  681. }
  682. /// <summary>
  683. ///
  684. /// </summary>
  685. /// <param name="factory_id"></param>
  686. /// <param name="isItemNum">统计维度:M物料、S系列</param>
  687. /// <param name="itemNumRange">统计范围:F工厂、G国科、H海王、A工厂+平台</param>
  688. /// <param name="itemNum">物料编码</param>
  689. /// <param name="groupBy">系列</param>
  690. /// <returns></returns>
  691. public string ForecastActual(string factory_id, string isItemNum, string itemNumRange, string itemNum, string groupBy)
  692. {
  693. string sql = $"exec [dbo].[pr_DOP_InventoryMonitoring]";
  694. var InventoryDto = _businessDbContext.InventoryDto.FromSqlRaw(sql).ToList();
  695. DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Now.AddMonths(-2));
  696. var polist = _DMS_IN_PODETAIL.Select(a =>a.SubmitDate >= dateOnly && a.OrderStatus != "Revoked" && a.OrderStatus != "Rejected");
  697. var rqShip = _aSNBOLShipperDetail.Select(a => a.CreateTime >= DateTime.Now.AddMonths(-2) && a.Domain == factory_id);
  698. List<ForecastActualDto> modelMonthOuts = new List<ForecastActualDto>();
  699. //按照物料统计
  700. if (isItemNum == "M")
  701. {
  702. switch (itemNumRange)
  703. {
  704. case "F":
  705. if(itemNum!="")
  706. {
  707. var shipPlan=_monthlyShipmentPlan.GetListAsync(a => a.SAPItemNumber == itemNum
  708. && (a.PlanMonth==DateTime.Now.AddMonths(-1).ToString("yyyy-MM") || a.PlanMonth == DateTime.Now.ToString("yyyy-MM"))
  709. && a.DistributionChannel == "瑞奇").Result;
  710. var itemDto = InventoryDto.Find(a => a.ItemNumber == itemNum);
  711. if(itemDto!=null)
  712. {
  713. ForecastActualDto forecastActualDto = new ForecastActualDto();
  714. forecastActualDto.ItemNum = itemNum;
  715. forecastActualDto.GroupBy = itemDto.Series;
  716. forecastActualDto.StockTurnOver = itemDto.GKMonthlyDemand + itemDto.HWMonthlyDemand==0?0:decimal.Ceiling((itemDto.GKInv+ itemDto.HWInv)/(itemDto.GKMonthlyDemand+ itemDto.HWMonthlyDemand));
  717. //月度发货计划找不到可以去月度发货计划历史表按照版本号找,暂不处理
  718. forecastActualDto.M1Plan = 0;
  719. forecastActualDto.M2Plan = 0;
  720. if (shipPlan.Any(a=> a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM")))
  721. {
  722. forecastActualDto.M1Plan = shipPlan.Find(a=>a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM")).Qty;
  723. }
  724. if (shipPlan.Any(a => a.PlanMonth == DateTime.Now.ToString("yyyy-MM")))
  725. {
  726. forecastActualDto.M2Plan = shipPlan.Find(a => a.PlanMonth == DateTime.Now.ToString("yyyy-MM")).Qty;
  727. }
  728. forecastActualDto.M1Real = rqShip.Where(a=>a.ContainerItem==itemNum && a.CreateTime>=DateTime.Now.AddMonths(-2) && a.CreateTime < DateTime.Now.AddMonths(-1)).Sum(b=>b.QtyToShip.Value);
  729. forecastActualDto.M2Real = rqShip.Where(a => a.ContainerItem == itemNum && a.CreateTime >= DateTime.Now.AddMonths(-1) && a.CreateTime < DateTime.Now).Sum(b => b.QtyToShip.Value);
  730. forecastActualDto.M1Diff = (forecastActualDto.M1Plan == 0 || forecastActualDto.M1Real == 0) ? "100%" : $"{decimal.Ceiling(forecastActualDto.M1Real / forecastActualDto.M1Plan)}%";
  731. forecastActualDto.M2Diff = (forecastActualDto.M2Plan == 0 || forecastActualDto.M2Real == 0) ? "100%" : $"{decimal.Ceiling(forecastActualDto.M2Real / forecastActualDto.M2Plan)}%";
  732. modelMonthOuts.Add(forecastActualDto);
  733. }
  734. }else
  735. {
  736. var shipPlan = _monthlyShipmentPlan.GetListAsync(a => (a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM") || a.PlanMonth == DateTime.Now.ToString("yyyy-MM"))
  737. && a.DistributionChannel == "瑞奇").Result;
  738. var itemList= shipPlan.Select(a=>a.SAPItemNumber).Distinct().ToList();
  739. itemList.ForEach(x=>
  740. {
  741. var itemDto = InventoryDto.Find(a => a.ItemNumber == x);
  742. if (itemDto != null)
  743. {
  744. ForecastActualDto forecastActualDto = new ForecastActualDto();
  745. forecastActualDto.ItemNum = x;
  746. forecastActualDto.GroupBy = itemDto.Series;
  747. forecastActualDto.StockTurnOver = itemDto.GKMonthlyDemand + itemDto.HWMonthlyDemand == 0 ? 0 : decimal.Ceiling((itemDto.GKInv + itemDto.HWInv) / (itemDto.GKMonthlyDemand + itemDto.HWMonthlyDemand));
  748. //月度发货计划找不到可以去月度发货计划历史表按照版本号找,暂不处理
  749. forecastActualDto.M1Plan = 0;
  750. forecastActualDto.M2Plan = 0;
  751. if (shipPlan.Any(a => a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM")))
  752. {
  753. forecastActualDto.M1Plan = shipPlan.Find(a => a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM")).Qty;
  754. }
  755. if (shipPlan.Any(a => a.PlanMonth == DateTime.Now.ToString("yyyy-MM")))
  756. {
  757. forecastActualDto.M2Plan = shipPlan.Find(a => a.PlanMonth == DateTime.Now.ToString("yyyy-MM")).Qty;
  758. }
  759. forecastActualDto.M1Real = rqShip.Where(a => a.ContainerItem == itemNum && a.CreateTime >= DateTime.Now.AddMonths(-2) && a.CreateTime < DateTime.Now.AddMonths(-1)).Sum(b => b.QtyToShip.Value);
  760. forecastActualDto.M2Real = rqShip.Where(a => a.ContainerItem == itemNum && a.CreateTime >= DateTime.Now.AddMonths(-1) && a.CreateTime < DateTime.Now).Sum(b => b.QtyToShip.Value);
  761. forecastActualDto.M1Diff = (forecastActualDto.M1Plan == 0 || forecastActualDto.M1Real == 0) ? "100%" : $"{decimal.Ceiling(forecastActualDto.M1Real / forecastActualDto.M1Plan)}%";
  762. forecastActualDto.M2Diff = (forecastActualDto.M2Plan == 0 || forecastActualDto.M2Real == 0) ? "100%" : $"{decimal.Ceiling(forecastActualDto.M2Real / forecastActualDto.M2Plan)}%";
  763. modelMonthOuts.Add(forecastActualDto);
  764. }
  765. });
  766. }
  767. break;
  768. case "G":
  769. if (itemNum != "")
  770. {
  771. var shipPlan = _monthlyShipmentPlan.GetListAsync(a => a.SAPItemNumber == itemNum
  772. && (a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM") || a.PlanMonth == DateTime.Now.ToString("yyyy-MM"))
  773. && a.DistributionChannel == "国科").Result;
  774. var item=_DMS_IN_ITEMMAPPING.Select(a => a.CfnERPCode == itemNum);
  775. if(item!=null)
  776. {
  777. var itemDto = InventoryDto.Find(a => a.ItemNumber == itemNum);
  778. if (itemDto != null)
  779. {
  780. ForecastActualDto forecastActualDto = new ForecastActualDto();
  781. forecastActualDto.ItemNum = itemNum;
  782. forecastActualDto.GroupBy = itemDto.Series;
  783. forecastActualDto.StockTurnOver = itemDto.GKMonthlyDemand + itemDto.HWMonthlyDemand == 0 ? 0 : decimal.Ceiling((itemDto.GKInv + itemDto.HWInv) / (itemDto.GKMonthlyDemand + itemDto.HWMonthlyDemand));
  784. //月度发货计划找不到可以去月度发货计划历史表按照版本号找,暂不处理
  785. forecastActualDto.M1Plan = 0;
  786. forecastActualDto.M2Plan = 0;
  787. if (shipPlan.Any(a => a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM")))
  788. {
  789. forecastActualDto.M1Plan = shipPlan.Find(a => a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM")).Qty;
  790. }
  791. if (shipPlan.Any(a => a.PlanMonth == DateTime.Now.ToString("yyyy-MM")))
  792. {
  793. forecastActualDto.M2Plan = shipPlan.Find(a => a.PlanMonth == DateTime.Now.ToString("yyyy-MM")).Qty;
  794. }
  795. forecastActualDto.M1Real = rqShip.Where(a => a.ContainerItem == itemNum && a.CreateTime >= DateTime.Now.AddMonths(-2) && a.CreateTime < DateTime.Now.AddMonths(-1)).Sum(b => b.QtyToShip.Value);
  796. forecastActualDto.M2Real = rqShip.Where(a => a.ContainerItem == itemNum && a.CreateTime >= DateTime.Now.AddMonths(-1) && a.CreateTime < DateTime.Now).Sum(b => b.QtyToShip.Value);
  797. forecastActualDto.M1Diff = (forecastActualDto.M1Plan == 0 || forecastActualDto.M1Real == 0) ? "100%" : $"{decimal.Ceiling(forecastActualDto.M1Real / forecastActualDto.M1Plan)}%";
  798. forecastActualDto.M2Diff = (forecastActualDto.M2Plan == 0 || forecastActualDto.M2Real == 0) ? "100%" : $"{decimal.Ceiling(forecastActualDto.M2Real / forecastActualDto.M2Plan)}%";
  799. modelMonthOuts.Add(forecastActualDto);
  800. }
  801. }
  802. }
  803. else
  804. {
  805. var shipPlan = _monthlyShipmentPlan.GetListAsync(a => (a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM") || a.PlanMonth == DateTime.Now.ToString("yyyy-MM"))
  806. && a.DistributionChannel == "瑞奇").Result;
  807. var itemList = shipPlan.Select(a => a.SAPItemNumber).Distinct().ToList();
  808. itemList.ForEach(x =>
  809. {
  810. var itemDto = InventoryDto.Find(a => a.ItemNumber == x);
  811. if (itemDto != null)
  812. {
  813. ForecastActualDto forecastActualDto = new ForecastActualDto();
  814. forecastActualDto.ItemNum = x;
  815. forecastActualDto.GroupBy = itemDto.Series;
  816. forecastActualDto.StockTurnOver = itemDto.GKMonthlyDemand + itemDto.HWMonthlyDemand == 0 ? 0 : decimal.Ceiling((itemDto.GKInv + itemDto.HWInv) / (itemDto.GKMonthlyDemand + itemDto.HWMonthlyDemand));
  817. //月度发货计划找不到可以去月度发货计划历史表按照版本号找,暂不处理
  818. forecastActualDto.M1Plan = 0;
  819. forecastActualDto.M2Plan = 0;
  820. if (shipPlan.Any(a => a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM")))
  821. {
  822. forecastActualDto.M1Plan = shipPlan.Find(a => a.PlanMonth == DateTime.Now.AddMonths(-1).ToString("yyyy-MM")).Qty;
  823. }
  824. if (shipPlan.Any(a => a.PlanMonth == DateTime.Now.ToString("yyyy-MM")))
  825. {
  826. forecastActualDto.M2Plan = shipPlan.Find(a => a.PlanMonth == DateTime.Now.ToString("yyyy-MM")).Qty;
  827. }
  828. forecastActualDto.M1Real = rqShip.Where(a => a.ContainerItem == itemNum && a.CreateTime >= DateTime.Now.AddMonths(-2) && a.CreateTime < DateTime.Now.AddMonths(-1)).Sum(b => b.QtyToShip.Value);
  829. forecastActualDto.M2Real = rqShip.Where(a => a.ContainerItem == itemNum && a.CreateTime >= DateTime.Now.AddMonths(-1) && a.CreateTime < DateTime.Now).Sum(b => b.QtyToShip.Value);
  830. forecastActualDto.M1Diff = (forecastActualDto.M1Plan == 0 || forecastActualDto.M1Real == 0) ? "100%" : $"{decimal.Ceiling(forecastActualDto.M1Real / forecastActualDto.M1Plan)}%";
  831. forecastActualDto.M2Diff = (forecastActualDto.M2Plan == 0 || forecastActualDto.M2Real == 0) ? "100%" : $"{decimal.Ceiling(forecastActualDto.M2Real / forecastActualDto.M2Plan)}%";
  832. modelMonthOuts.Add(forecastActualDto);
  833. }
  834. });
  835. }
  836. break;
  837. case "H":
  838. break;
  839. case "A":
  840. break;
  841. default:
  842. break;
  843. }
  844. }else
  845. {
  846. }
  847. return JsonConvert.SerializeObject(modelMonthOuts);
  848. }
  849. public string CalcInvDayHist(string domain)
  850. {
  851. string sql = $"select distinct ItemNum,Min(CreateTime) AS HistMinTime from InvTransHist where Domain='{domain}' group by ItemNum";
  852. var InvTransHistDto = _businessDbContext.InvTransHistDayDto.FromSqlRaw(sql).ToList();
  853. string invHistTime = $"select A.ItemNum,Sum((case when a.QtyChange>0 then a.QtyChange else 0 end)) as QtyChangeAdvance,Sum((case when a.QtyChange<0 then abs(a.QtyChange) else 0 end)) as QtyChangeOut,CONVERT(varchar,a.createTime, 23) AS createTime from InvTransHist A where Domain='{domain}' group by ItemNum,CONVERT(varchar,a.createTime, 23)";
  854. var InvTransHistDetailDto = _businessDbContext.InvTransHistDetailDto.FromSqlRaw(invHistTime).ToList();
  855. string sqlMaxTime = $"select t1.* from InvTransHistDay t1 inner join (SELECT max(HistDayTime) as HistDayTime, ItemNum FROM InvTransHistDay where Domain='{domain}' group by ItemNum) t2 on t1.HistDayTime=t2.HistDayTime and t1.ItemNum=t2.ItemNum and t1.Domain='{domain}'";
  856. var InvTransHistMaxTimeDayDto = _businessDbContext.InvTransHistDay.FromSqlRaw(sqlMaxTime).ToList();
  857. string sqlQtyChange = $"select ItemNum,Sum(QtyChange) AS QtyChange from InvTransHist where Domain='{domain}' group by ItemNum";
  858. var QtyChangeDto = _businessDbContext.QtyChangeDto.FromSqlRaw(sqlQtyChange).ToList();
  859. string sqlBalanceNum = $"select a.ItemNum,Sum(BeginBalance)+Sum(QtyChange) AS BalanceNum from InvTransHist a inner join (select ItemNum,LotSerial,Max(CreateTime) AS CreateTime from InvTransHist group by ItemNum,LotSerial ) b on a.ItemNum=b.ItemNum and a.LotSerial=b.LotSerial and a.CreateTime=b.CreateTime and Domain='{domain}' group by a.ItemNum";
  860. var BalanceNumDto = _businessDbContext.BalanceNumDto.FromSqlRaw(sqlBalanceNum).ToList();
  861. List<InvTransHistDay> daysHist = new List<InvTransHistDay>();
  862. InvTransHistDto?.ForEach(x => {
  863. if(InvTransHistMaxTimeDayDto.Any(a=>a.ItemNum==x.ItemNum))
  864. {
  865. var xMaxDay = InvTransHistMaxTimeDayDto.Find(a => a.ItemNum == x.ItemNum).HistDayTime;
  866. int day = 1;
  867. while(true)
  868. {
  869. if(xMaxDay.AddDays(day).Date<=DateTime.Now.Date)
  870. {
  871. InvTransHistDay invTransHistDay = new InvTransHistDay();
  872. var daydetail = InvTransHistDetailDto.Find(a => a.ItemNum == x.ItemNum && a.CreateTime== xMaxDay.AddDays(day).Date.ToString("yyyy-MM-dd"));
  873. invTransHistDay.Domain = domain;
  874. invTransHistDay.ItemNum = x.ItemNum;
  875. if(day==1)
  876. {
  877. invTransHistDay.BeginBalance = InvTransHistMaxTimeDayDto.Find(a => a.ItemNum == x.ItemNum).BalanceNum;
  878. }else
  879. {
  880. invTransHistDay.BeginBalance = daysHist.Find(a => a.ItemNum == x.ItemNum && a.HistDayTime.Date == xMaxDay.AddDays(day - 1).Date).BalanceNum;
  881. }
  882. invTransHistDay.QtyChangeAdvance = daydetail == null ? 0 : daydetail.QtyChangeAdvance;
  883. invTransHistDay.QtyChangeOut = daydetail == null ? 0 : daydetail.QtyChangeOut;
  884. invTransHistDay.BalanceNum = invTransHistDay.BeginBalance + invTransHistDay.QtyChangeAdvance - invTransHistDay.QtyChangeOut;
  885. invTransHistDay.HistDayTime = xMaxDay.AddDays(day).Date;
  886. daysHist.Add(invTransHistDay);
  887. day++;
  888. }else
  889. {
  890. break;
  891. }
  892. }
  893. }else
  894. {
  895. var xMaxDay = InvTransHistDto.Find(a => a.ItemNum == x.ItemNum).HistMinTime.Value;
  896. int day = 0;
  897. while (true)
  898. {
  899. if (xMaxDay.AddDays(day).Date <= DateTime.Now.Date)
  900. {
  901. InvTransHistDay invTransHistDay = new InvTransHistDay();
  902. var daydetail = InvTransHistDetailDto.Find(a => a.ItemNum == x.ItemNum && a.CreateTime == xMaxDay.AddDays(day).Date.ToString("yyyy-MM-dd"));
  903. invTransHistDay.Domain = domain;
  904. invTransHistDay.ItemNum = x.ItemNum;
  905. if (day == 0)
  906. {
  907. var xBalanceNum = BalanceNumDto.Find(b => b.ItemNum == x.ItemNum);
  908. var xQtyChangeDto = QtyChangeDto.Find(b => b.ItemNum == x.ItemNum);
  909. if (xBalanceNum != null && xQtyChangeDto != null)
  910. {
  911. invTransHistDay.BeginBalance = xBalanceNum.BalanceNum + xQtyChangeDto.QtyChange;
  912. }
  913. else
  914. {
  915. invTransHistDay.BeginBalance = 0;
  916. }
  917. }
  918. else
  919. {
  920. var dayPredetail = daysHist.Find(a => a.ItemNum == x.ItemNum && a.HistDayTime.Date == xMaxDay.AddDays(day - 1).Date);
  921. invTransHistDay.BeginBalance = dayPredetail == null ? 0 : dayPredetail.BalanceNum;
  922. }
  923. invTransHistDay.QtyChangeAdvance = daydetail == null ? 0 : daydetail.QtyChangeAdvance;
  924. invTransHistDay.QtyChangeOut = daydetail == null ? 0 : daydetail.QtyChangeOut;
  925. invTransHistDay.BalanceNum = invTransHistDay.BeginBalance + invTransHistDay.QtyChangeAdvance - invTransHistDay.QtyChangeOut;
  926. invTransHistDay.HistDayTime = xMaxDay.AddDays(day).Date;
  927. daysHist.Add(invTransHistDay);
  928. day++;
  929. }
  930. else
  931. {
  932. break;
  933. }
  934. }
  935. }
  936. });
  937. _businessDbContext.BulkInsert(daysHist);
  938. return "ok";
  939. }
  940. }
  941. }