| using System; | 
| using System.Collections.Generic; | 
| using System.Data; | 
| using System.Linq; | 
| using System.Web; | 
| using System.Web.Http; | 
| using WebAPI.Models; | 
| using Newtonsoft.Json; | 
| using Newtonsoft.Json.Linq; | 
| using System.Diagnostics; | 
|   | 
| namespace WebAPI.Controllers.SCGL.日计划管理 | 
| { | 
|     public class JIT_DayPlanPlatFormBillController : ApiController | 
|     { | 
|         List<PRD_Inventory> ListInventory;//库存明细 | 
|         private json objJsonResult = new json(); | 
|         public DataSet ds = new DataSet(); | 
|         public WebServer webserver = new WebServer(); | 
|         SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); | 
|         Sc_WorkBillSortBillMain omdelMian = new Sc_WorkBillSortBillMain(); | 
|         public static string BillType = "4610"; | 
|   | 
|         #region 生产日计划平台 查询 | 
|         [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBillList")] | 
|         [HttpGet] | 
|         public object JIT_DayPlanPlatFormBillList(string sWhere, string user) | 
|         { | 
|             try | 
|             { | 
|                 //查看权限 | 
|                 if (!DBUtility.ClsPub.Security_Log("Sc_WorkBillSortBill_Query", 1, false, user)) | 
|                 { | 
|                     objJsonResult.code = "0"; | 
|                     objJsonResult.count = 0; | 
|                     objJsonResult.Message = "无查看权限!"; | 
|                     objJsonResult.data = null; | 
|                     return objJsonResult; | 
|                 } | 
|   | 
|                 omdelMian = JsonConvert.DeserializeObject<Sc_WorkBillSortBillMain>(sWhere); | 
|   | 
|                 ds = oCN.RunProcReturn($"exec h_p_JIT_Sc_WorkBillSortBill_Query '{omdelMian.HPlanBeginDate}',{(omdelMian.HProdORGID == null ? 0 : omdelMian.HProdORGID)},'{omdelMian.HSeOrderBillNo}'" + | 
|                     $",'{omdelMian.HPlanEndDate.AddDays(1)}',{omdelMian.HMaterID},'{omdelMian.HICMOBillNo}',{omdelMian.HWorkShopID}", "h_p_JIT_Sc_WorkBillSortBill_Query"); | 
|   | 
|                 objJsonResult.code = "1"; | 
|                 objJsonResult.count = 1; | 
|                 objJsonResult.Message = "Sucess!"; | 
|                 objJsonResult.data = ds.Tables[0]; | 
|                 return objJsonResult; | 
|             } | 
|             catch (Exception e) | 
|             { | 
|                 objJsonResult.code = "0"; | 
|                 objJsonResult.count = 0; | 
|                 objJsonResult.Message = "Exception!" + e.ToString(); | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 根据ID查询 齐套状态 和 锁定状态 | 
|         /// </summary> | 
|         /// <param name="sWhere"></param> | 
|         /// <param name="user"></param> | 
|         /// <returns></returns> | 
|         [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBillHinterIDList")] | 
|         [HttpGet] | 
|         public object JIT_DayPlanPlatFormBillHinterIDList(string sWhere,int num) | 
|         { | 
|             try | 
|             { | 
|                 if (num == 1) | 
|                 { | 
|                     ds = oCN.RunProcReturn($"select isnull(DATEDIFF(DAY,GETDATE(),CONVERT(varchar(10),max(HMasterDate),20))+1,0) DayNum from  Sc_WorkBillSortBillSub where HInterID in ({sWhere}) and  HMasterDate>=CONVERT(varchar(10),GETDATE(),20) and HLockedSub=1", "Sc_WorkBillSortBillSub"); | 
|                 } | 
|                 else if (num == 2) | 
|                 { | 
|                     ds = oCN.RunProcReturn($"select CONVERT(varchar(10),HMasterDate,20) HMasterDate,HComplete,HInterID from  Sc_WorkBillSortBillSub where HInterID in ({sWhere}) and  HMasterDate>=CONVERT(varchar(10),GETDATE(),20) and (HComplete='齐套' or  HComplete='未齐套') ", "Sc_WorkBillSortBillSub"); | 
|                 } | 
|   | 
|                 objJsonResult.code = "1"; | 
|                 objJsonResult.count = 1; | 
|                 objJsonResult.Message = "Sucess!"; | 
|                 objJsonResult.data = ds.Tables[0]; | 
|                 return objJsonResult; | 
|             } | 
|             catch (Exception e) | 
|             { | 
|                 objJsonResult.code = "0"; | 
|                 objJsonResult.count = 0; | 
|                 objJsonResult.Message = "Exception!" + e.ToString(); | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|         } | 
|         #endregion | 
|   | 
|         #region 生产日计划平台 保存 | 
|         [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBill_btnSave")] | 
|         [HttpPost] | 
|         public object JIT_DayPlanPlatFormBill_btnSave([FromBody] JObject sMainSub) | 
|         { | 
|             var _value = sMainSub["sMainSub"].ToString(); | 
|             string msg1 = _value.ToString(); | 
|             oCN.BeginTran(); | 
|             //保存主表 | 
|             objJsonResult = AddBillMain(msg1); | 
|             if (objJsonResult.code == "0") | 
|             { | 
|                 oCN.RollBack(); | 
|                 objJsonResult.code = "0"; | 
|                 objJsonResult.count = 0; | 
|                 objJsonResult.Message = objJsonResult.Message; | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|             oCN.Commit(); | 
|             objJsonResult.code = "1"; | 
|             objJsonResult.count = 1; | 
|             objJsonResult.Message = "单据保存成功!"; | 
|             objJsonResult.data = null; | 
|             return objJsonResult; | 
|         } | 
|   | 
|         public json AddBillMain(string msg1) | 
|         { | 
|             string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); | 
|             string msg2 = sArray[0].ToString(); | 
|             string user = sArray[1].ToString(); | 
|   | 
|             try | 
|             { | 
|                 if (!DBUtility.ClsPub.Security_Log("Sc_WorkBillSortBill_Edit", 1, false, user)) | 
|                 { | 
|                     objJsonResult.code = "0"; | 
|                     objJsonResult.count = 0; | 
|                     objJsonResult.Message = "无保存权限!"; | 
|                     objJsonResult.data = null; | 
|                     return objJsonResult; | 
|                 } | 
|   | 
|                 //JSON序列化转换字典集合 | 
|                 List<Dictionary<string, string>> list = new List<Dictionary<string, string>>(); | 
|                 List<object> jb = JsonConvert.DeserializeObject<List<object>>(msg2); | 
|                 foreach (JObject item in jb) | 
|                 { | 
|                     Dictionary<string, string> dic = new Dictionary<string, string>(); | 
|                     foreach (var itm in item.Properties()) | 
|                     { | 
|                         dic.Add(itm.Name, itm.Value.ToString()); | 
|                     } | 
|                     list.Add(dic); | 
|                 } | 
|   | 
|                 int TrueCount = 0, SumCount = 0; | 
|   | 
|                 for (int i = 0; i < list.Count; i++) | 
|                 { | 
|                     TrueCount = 0; | 
|                     SumCount = 0; | 
|                     long HInterID = 0; | 
|                     var HBillNo = ""; | 
|                     if (list[i]["单据号"].ToString() == "" && list[i]["hmainid"].ToString() == "") | 
|                     { | 
|                         var HICMOBillNo = list[i]["生产订单号"].ToString(); | 
|                         var HOrderType = list[i]["订单类型"].ToString(); | 
|                         var HICMOEntrySEQ = list[i]["生产订单明细行号"].ToString(); | 
|                         var HWorkShopID = list[i]["HWorkShopID"].ToString(); | 
|                         var HSourceID = list[i]["HSourceID"].ToString(); | 
|                         var HYX = list[i]["优先级"].ToString(); | 
|                         var HProdORGID = list[i]["HProdORGID"].ToString(); | 
|                         var HMaterID = list[i]["HMaterID"].ToString(); | 
|                         var HMaterName = list[i]["物料名称"].ToString(); | 
|                         var HMaterModel = list[i]["规格型号"].ToString(); | 
|                         var HUnitID = list[i]["HUnitID"].ToString(); | 
|                         var HSeOrderBillQty = list[i]["销售订单数量"].ToString(); | 
|                         //var HOrderNeedQty = list[i]["订单需求数量"].ToString(); | 
|                         var HSplitQty = list[i]["拆单数量"].ToString(); | 
|                         var HDayPlanSumQty = list[i]["日计划数量总量"].ToString(); | 
|                         var HPlanBeginDate = list[i]["计划开始日期"].ToString(); | 
|                         var HSeOrderBillNo = list[i]["销售订单号"].ToString(); | 
|                         var HICMOBillType = list[i]["生产订单单据类型"].ToString(); | 
|                         var HSourceStockInQty = list[i]["产线入库数量"].ToString(); | 
|                         var HLeftPlanQty = list[i]["未排数量"].ToString(); | 
|                         var HOrderLev = list[i]["订单等级"].ToString(); | 
|                         var HMainSourceInterID = list[i]["源单主内码"].ToString(); | 
|                         var HMainSourceEntryID = list[i]["源单子内码"].ToString(); | 
|                         var HPlanQty = list[i]["计划数量"].ToString(); | 
|   | 
|                         if (HSourceID == ""|| HSourceID == "0") | 
|                         { | 
|                             objJsonResult.code = "0"; | 
|                             objJsonResult.count = 0; | 
|                             objJsonResult.Message = $"第{i + 1}行生产资源不能为空!"; | 
|                             objJsonResult.data = null; | 
|                             return objJsonResult; | 
|                         } | 
|   | 
|                         ds = oCN.RunProcReturn($"select * from  Sc_WorkBillSortBillMain where HMainSourceInterID={HMainSourceInterID}" + | 
|                             $" and HMainSourceEntryID={HMainSourceEntryID} and HICMOBillNo='{HICMOBillNo}' and HICMOEntrySEQ={HICMOEntrySEQ} and HSourceID={HSourceID}", "Sc_WorkBillSortBillMain"); | 
|                         if (ds.Tables[0].Rows.Count > 0) | 
|                         { | 
|                             objJsonResult.code = "0"; | 
|                             objJsonResult.count = 0; | 
|                             objJsonResult.Message = $"第{i + 1}行生产资源有重复,请修改!"; | 
|                             objJsonResult.data = null; | 
|                             return objJsonResult; | 
|                         } | 
|   | 
|                         //新增单据号 id | 
|                         HBillNo = DBUtility.ClsPub.CreateBillCode_Prod(BillType, ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号 | 
|                         HInterID = DBUtility.ClsPub.CreateBillID_Prod(BillType, ref DBUtility.ClsPub.sExeReturnInfo);//获得一个新的id | 
|   | 
|                         //主表 | 
|                         oCN.RunProc("insert into Sc_WorkBillSortBillMain(HInterID,HBillNo,HYear,HPeriod,HBillType," + | 
|                             "HBillSubType,HDate,HBillStatus,HMaker,HMakeDate,HICMOBillNo,HOrderType," + | 
|                             "HICMOEntrySEQ,HWorkShopID,HSourceID, HYX, HProdORGID," + | 
|                             " HMaterID, HMaterName, HMaterModel, HUnitID, HSeOrderBillQty," + | 
|                             " HOrderNeedQty, HSplitQty, HDayPlanSumQty,HPlanBeginDate," + | 
|                             "HSeOrderBillNo,HICMOBillType,HSourceStockInQty,HLeftPlanQty,HOrderLev,HPreparatDate," + | 
|                             "HMainSourceInterID,HMainSourceEntryID,HICMOInterID_Sec,HICMOEntryID_Sec,HPlanQty)values" + | 
|                             $"({HInterID},'{HBillNo}',{DateTime.Now.Year},{DateTime.Now.Month},'{BillType}'," + | 
|                             $"'{BillType}',GETDATE(),1,'{user}',getdate(),'{HICMOBillNo}','{HOrderType}'," + | 
|                             $"{(HICMOEntrySEQ == "" ? 0.ToString() : HICMOEntrySEQ)},{(HWorkShopID == "" ? 0.ToString() : HWorkShopID)},{(HSourceID == "" ? 0.ToString() : HSourceID)}, {(HYX == "" ? 0.ToString() : HYX)}, {(HProdORGID == "" ? 0.ToString() : HProdORGID)}," + | 
|                             $" {(HMaterID == "" ? 0.ToString() : HMaterID)}, '{HMaterName}', '{HMaterModel}', {(HUnitID == "" ? 0.ToString() : HUnitID)}, {(HSeOrderBillQty == "" ? 0.ToString() : HSeOrderBillQty)}," + | 
|                             $" 0, {(HSplitQty == "" ? 0.ToString() : HSplitQty)}, {(HDayPlanSumQty == "" ? 0.ToString() : HDayPlanSumQty)},'{HPlanBeginDate}'," + | 
|                             $"'{HSeOrderBillNo}','{HICMOBillType}',{(HSourceStockInQty == "" ? 0.ToString() : HSourceStockInQty)},{(HLeftPlanQty == "" ? 0.ToString() : HLeftPlanQty)},'{HOrderLev}',getdate()," + | 
|                             $"{HMainSourceInterID},{HMainSourceEntryID},{HMainSourceInterID},{HMainSourceEntryID},{HPlanQty})"); | 
|                     } | 
|                     else | 
|                     { | 
|                         var HMainSourceInterID = list[i]["源单主内码"].ToString(); | 
|                         var HMainSourceEntryID = list[i]["源单子内码"].ToString(); | 
|                         var HICMOBillNo = list[i]["生产订单号"].ToString(); | 
|                         var HICMOEntrySEQ = list[i]["生产订单明细行号"].ToString(); | 
|                         var HSourceID = list[i]["HSourceID"].ToString(); | 
|                         var HYX = list[i]["优先级"].ToString(); | 
|   | 
|                         if (HSourceID == "" || HSourceID == "0") | 
|                         { | 
|                             objJsonResult.code = "0"; | 
|                             objJsonResult.count = 0; | 
|                             objJsonResult.Message = $"第{i + 1}行生产资源不能为空!"; | 
|                             objJsonResult.data = null; | 
|                             return objJsonResult; | 
|                         } | 
|                         var str = 0; | 
|                         if (!int.TryParse(HYX, out str)) | 
|                         { | 
|                             objJsonResult.code = "0"; | 
|                             objJsonResult.count = 0; | 
|                             objJsonResult.Message = $"第{i + 1}行优先级:{HYX},请输入数字!"; | 
|                             objJsonResult.data = null; | 
|                             return objJsonResult; | 
|                         } | 
|   | 
|                         ds = oCN.RunProcReturn($"select * from  Sc_WorkBillSortBillMain where HMainSourceInterID={HMainSourceInterID}" + | 
|                               $" and HMainSourceEntryID={HMainSourceEntryID} and HICMOBillNo='{HICMOBillNo}' and HICMOEntrySEQ={HICMOEntrySEQ} and HSourceID={HSourceID}", "Sc_WorkBillSortBillMain"); | 
|   | 
|                         if (ds.Tables[0].Rows.Count > 1) | 
|                         { | 
|                             objJsonResult.code = "0"; | 
|                             objJsonResult.count = 0; | 
|                             objJsonResult.Message = $"第{i + 1}行生产资源有重复,请修改!"; | 
|                             objJsonResult.data = null; | 
|                             return objJsonResult; | 
|                         } | 
|   | 
|                         oCN.RunProc($"update Sc_WorkBillSortBillMain set HSourceID={(HSourceID == "" ? 0.ToString() : HSourceID)},HYX={HYX} where HInterID={list[i]["hmainid"].ToString()} and HBillNo='{list[i]["单据号"].ToString()}'"); | 
|                     } | 
|   | 
|                     oCN.RunProc($"update Sc_WorkBillSortBillMain set HDayPlanSumQty={list[i]["日计划数量总量"].ToString()} where HInterID={(HInterID==0?int.Parse(list[i]["hmainid"].ToString()):HInterID)} and HBillNo='{(HBillNo==""?list[i]["单据号"].ToString(): HBillNo)}'"); | 
|   | 
|                     int SumDay = 31; //动态两月之差 DateTime.Now.AddMonths(1).AddDays(-1).Subtract(DateTime.Now).Days; | 
|   | 
|                     for (int j = 0; j < SumDay; j++) | 
|                     { | 
|                         //主表子表都有数据 | 
|                         if (list[i][DateTime.Now.AddDays(j).ToString("yyyy-MM-dd")].ToString() != "") | 
|                         { | 
|                             SumCount += 1; | 
|                             ////保存子表 | 
|                             objJsonResult = AddBillSub(HInterID == 0 ? list[i]["hmainid"].ToString() : HInterID.ToString() | 
|                                 , HBillNo == "" ? list[i]["单据号"].ToString() : HBillNo | 
|                                  , DateTime.Parse(DateTime.Now.AddDays(j).ToString("yyyy-MM-dd").ToString()) | 
|                                  , int.Parse(list[i][DateTime.Now.AddDays(j).ToString("yyyy-MM-dd")].ToString())); | 
|   | 
|                             if (objJsonResult.count == 1) | 
|                             { | 
|                                 TrueCount += 1; | 
|                             } | 
|                         } | 
|                         //主表有数据 子表无数据 | 
|                         if (j == 30 && SumCount == 0 && HInterID != 0 && HBillNo != "") | 
|                         { | 
|                             objJsonResult = AddBillSub(HInterID.ToString(), HBillNo, DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd").ToString()), 0); | 
|                         } | 
|                     } | 
|                 } | 
|   | 
|                 if (TrueCount != SumCount) | 
|                 { | 
|                     objJsonResult.code = "0"; | 
|                     objJsonResult.count = 0; | 
|                     objJsonResult.Message = objJsonResult.Message; | 
|                     objJsonResult.data = null; | 
|                     return objJsonResult; | 
|                 } | 
|   | 
|                 //同步日计划工单 | 
|                 oCN.RunProc("exec REALESE_SC_ICMOBILLSTATUS_TMP"); | 
|   | 
|                 objJsonResult.code = "1"; | 
|                 objJsonResult.count = 1; | 
|                 objJsonResult.Message = null; | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|             catch (Exception e) | 
|             { | 
|                 objJsonResult.code = "0"; | 
|                 objJsonResult.count = 0; | 
|                 objJsonResult.Message = "Exception!" + e.ToString(); | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|         } | 
|   | 
|         public json AddBillSub(string HInterID, string HBillNo, DateTime date, int HQTY) | 
|         { | 
|             //获取表格数据 | 
|             ds = oCN.RunProcReturn($"select * from  h_v_JIT_Sc_WorkBillSortBillList where 单据号='{HBillNo}'", "h_v_JIT_Sc_WorkBillSortBillList"); | 
|   | 
|             int count = 0; | 
|             if (ds.Tables[0].Rows.Count > 0) | 
|             { | 
|                 //判断子表是否有当日的日期以及日期所对应的数量 | 
|                 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) | 
|                 { | 
|                     if (DateTime.Parse(ds.Tables[0].Rows[i]["日计划生产日期"].ToString()) == date) | 
|                     { | 
|                         oCN.RunProc($"update Sc_WorkBillSortBillSub set HMasterDate='{date}',HQty={HQTY}" + | 
|                             $"  where HInterID={HInterID} and HEntryID={ds.Tables[0].Rows[i]["hsubid"].ToString()}"); | 
|                         count += 1; | 
|                     } | 
|                 } | 
|             } | 
|   | 
|             if (count != 1) | 
|             { | 
|                 //插入子表 | 
|                 oCN.RunProc("insert into Sc_WorkBillSortBillSub(HInterID,HSEQ," + | 
|                     "HMasterDate,HQty)" + | 
|                             $"values({HInterID}, {(ds.Tables[0].Rows.Count + 1)}," + | 
|                             $" '{date}', {HQTY})"); | 
|             } | 
|   | 
|             objJsonResult.code = "1"; | 
|             objJsonResult.count = 1; | 
|             objJsonResult.Message = null; | 
|             objJsonResult.data = null; | 
|             return objJsonResult; | 
|         } | 
|         #endregion | 
|   | 
|         #region 生产日计划平台 齐套分析 | 
|   | 
|         #region 清单配料方式 | 
|         public class PRD_PPBOM | 
|         { | 
|             /// <summary> | 
|             /// 日计划工单号 | 
|             /// </summary> | 
|             public string HMainICMOBillNo { get; set; } | 
|             /// <summary> | 
|             /// 日计划工单FID | 
|             /// </summary> | 
|             public long HInterID { get; set; } | 
|             /// <summary> | 
|             /// 日计划明细FEntryID | 
|             /// </summary> | 
|             public long HEntryID { get; set; } | 
|             /// <summary> | 
|             /// 生产用料清单单号 | 
|             /// </summary> | 
|             public string HBillNo { get; set; } | 
|             /// <summary> | 
|             /// 生产订单单号 | 
|             /// </summary> | 
|             public string HICMOBillNo { get; set; } | 
|             /// <summary> | 
|             /// 生产订单内码 | 
|             /// </summary> | 
|             public string HICMOInterID { get; set; } | 
|             /// <summary> | 
|             /// 父级物料ID | 
|             /// </summary> | 
|             public string FMaterialID { get; set; } | 
|             /// <summary> | 
|             /// 父级物料数量 | 
|             /// </summary> | 
|             public decimal FQty { get; set; } | 
|             /// <summary> | 
|             /// 子项物料ID | 
|             /// </summary> | 
|             public string FMATERIALID2 { get; set; } | 
|             /// <summary> | 
|             /// 子项物料名称 | 
|             /// </summary> | 
|             public string FNumber { get; set; } | 
|             /// <summary> | 
|             /// 需求数量 | 
|             /// </summary> | 
|             public decimal FNeedQty { get; set; } | 
|             /// <summary> | 
|             /// 即时库存 | 
|             /// </summary> | 
|             public decimal FBASEQTY { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 生产订单主码 | 
|             /// </summary> | 
|             public string HICMOInterID_Sec { get; set; } | 
|             /// <summary> | 
|             /// 生产订单子码 | 
|             /// </summary> | 
|             public string HICMOEntryID_Sec { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 日计划数量 | 
|             /// </summary> | 
|             public string HQty { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 日计划日期 | 
|             /// </summary> | 
|             public DateTime HMasterDate { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 订单等级 | 
|             /// </summary> | 
|             public string HOrderLev { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 库存组织 | 
|             /// </summary> | 
|             public string FStockOrgId { get; set; } | 
|             /// <summary> | 
|             /// 组织名称 | 
|             /// </summary> | 
|             public string OrgFnumber { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 单位用量 | 
|             /// </summary> | 
|             public decimal dwyl { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 预计消耗用量 | 
|             /// </summary> | 
|             public decimal PlanCount { get; set; } | 
|             /// <summary> | 
|             /// 物料属性 | 
|             /// </summary> | 
|             public int FErpClsID { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 车间 | 
|             /// </summary> | 
|             public string HWorkShopID { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 车间编码 | 
|             /// </summary> | 
|             public string FHWorkShopNumber { get; set; } | 
|   | 
|   | 
|         } | 
|         #endregion | 
|   | 
|         #region 即时库存 | 
|         public class PRD_Inventory | 
|         { | 
|             /// <summary> | 
|             /// 物料ID | 
|             /// </summary> | 
|             public string FMATERIALID { get; set; } | 
|             /// <summary> | 
|             /// 物料编码 | 
|             /// </summary> | 
|             public string FNumber { get; set; } | 
|             /// <summary> | 
|             /// 即时库存 | 
|             /// </summary> | 
|             public decimal FBASEQTY { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 库存组织 | 
|             /// </summary> | 
|             public string FStockOrgId { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 库存货主类型 | 
|             /// </summary> | 
|             public string FOwnerTypeId { get; set; } | 
|             /// <summary> | 
|             /// 库存货主 | 
|             /// </summary> | 
|             public string FOwnerId { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 货主编码 | 
|             /// </summary> | 
|             public string FOwnerNumber { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 仓库 | 
|             /// </summary> | 
|             public string FSTOCKID { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 仓库编码 | 
|             /// </summary> | 
|             public string FStockNumber { get; set; } | 
|             /// <summary> | 
|             /// 批号 | 
|             /// </summary> | 
|             public string HLOT { get; set; } | 
|             /// <summary> | 
|             /// id | 
|             /// </summary> | 
|             public string HInventoryFID { get; set; } | 
|         } | 
|         #endregion | 
|   | 
|         #region 车间仓库对照列表 | 
|         public class PRD_WorkHouseCompar | 
|         { | 
|             /// <summary> | 
|             /// 组织 | 
|             /// </summary> | 
|             public string FORGID { get; set; } | 
|             /// <summary> | 
|             /// 车间 | 
|             /// </summary> | 
|             public string FWORKSHOPID { get; set; } | 
|             /// <summary> | 
|             /// 仓库 | 
|             /// </summary> | 
|             public string FSTOCKID { get; set; } | 
|         } | 
|         #endregion | 
|   | 
|         #region 日计划+生产订单 | 
|         public class DayPlanPickedModel | 
|         { | 
|             /// <summary> | 
|             /// 订单入库数量 按照生产订单明细行 | 
|             /// </summary> | 
|             public decimal FCXStockInQty { get; set; } | 
|             /// <summary> | 
|             ///  主生产订单子内码   | 
|             /// </summary> | 
|             public string FPRDMOENTYID { get; set; } | 
|             /// <summary> | 
|             ///  物料单位用量 | 
|             /// </summary> | 
|             public decimal dwyl { get; set; } | 
|             /// <summary> | 
|             ///  已领数量 | 
|             /// </summary> | 
|             public decimal FPickedQty { get; set; } | 
|             /// <summary> | 
|             /// 剩余可用数量 | 
|             /// </summary> | 
|             public decimal FRemainPickedQty { get; set; } | 
|             /// <summary> | 
|             ///  物料   | 
|             /// </summary> | 
|             public string FMATERIALID { get; set; } | 
|   | 
|         } | 
|         #endregion | 
|   | 
|         #region 存储临时需要插入数据 | 
|         public class CompleteAnalysisTempModel | 
|         { | 
|             /// <summary> | 
|             /// 主生产订单主内码:FHMainICMOInterIDr  | 
|             /// </summary> | 
|             public string FHMainICMOInterIDr { get; set; } | 
|             /// <summary> | 
|             ///  主生产订单子内码:FHMainICMOEntryID   | 
|             /// </summary> | 
|             public string FHMainICMOEntryID { get; set; } | 
|             /// <summary> | 
|             ///  日计划订单单号:HMainICMOBillNo  | 
|             /// </summary> | 
|             public string HMainICMOBillNo { get; set; } | 
|             /// <summary> | 
|             ///  日计划订单主内码:FHICMOInterID  | 
|             /// </summary> | 
|             public string FHICMOInterID { get; set; } | 
|             /// <summary> | 
|             ///  日计划订单子内码:FHICMOEntryID   | 
|             /// </summary> | 
|             public string FHICMOEntryID { get; set; } | 
|             /// <summary> | 
|             ///  子件物料:FHMaterID   | 
|             /// </summary> | 
|             public string FHMaterID { get; set; } | 
|   | 
|             /// <summary> | 
|             ///  库存组织:FHStockOrgID | 
|             /// </summary> | 
|             public string FHStockOrgID { get; set; } | 
|   | 
|             /// <summary> | 
|             ///生产组织:FHPRDORGID  | 
|             /// </summary> | 
|             public string FHPRDORGID { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 生产订单号:FPRDBillNo  | 
|             /// </summary> | 
|             public string FPRDBillNo { get; set; } | 
|             /// <summary> | 
|             /// 标识 | 
|             /// </summary> | 
|             public string FIdentificat { get; set; } | 
|             /// <summary> | 
|             ///  货主名称:FOwnerName  | 
|             /// </summary> | 
|             public string FOwnerName { get; set; } | 
|             /// <summary> | 
|             ///  货主编码:FOwnerId | 
|             /// </summary> | 
|             public string FOwnerId { get; set; } | 
|             /// <summary> | 
|             /// 货主类型:FOwnerTypeId         | 
|             /// </summary> | 
|             public string FOwnerTypeId { get; set; } | 
|             /// <summary> | 
|             /// 齐套状态 | 
|             /// </summary> | 
|             public string FComPlete { get; set; } | 
|             /// <summary> | 
|             ///  数量 | 
|             /// </summary> | 
|             public string FHUseQty { get; set; } | 
|             /// <summary> | 
|             ///  及时库存数量:FHStockQty  | 
|             /// </summary> | 
|             public decimal FHStockQty { get; set; } | 
|             /// <summary> | 
|             ///可用数量:FHLeftQty  | 
|             /// </summary> | 
|             public decimal FHLeftQty { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 缺料数量 | 
|             /// </summary> | 
|             public decimal FLackCount { get; set; } | 
|             /// <summary> | 
|             /// 占用数量 | 
|             /// </summary> | 
|             public decimal FOccupyCount { get; set; } | 
|             /// <summary> | 
|             /// 占用领料数量 | 
|             /// </summary> | 
|             public decimal FOccupyPickedCount { get; set; } | 
|   | 
|             /// <summary> | 
|             /// 单位用量:FUnitDosage   | 
|             /// </summary> | 
|             public decimal FUnitDosage { get; set; } | 
|             /// <summary> | 
|             /// 计划总数量:FSumPlanCount  | 
|             /// </summary> | 
|             public decimal FSumPlanCount { get; set; } | 
|             /// <summary> | 
|             /// 齐套数量:FCompleteCount q | 
|             /// </summary> | 
|             public decimal FCompleteCount { get; set; } | 
|             public decimal FCompleteCount1 { get; set; } | 
|             /// <summary> | 
|             /// 日计划日期 | 
|             /// </summary> | 
|             public DateTime FPlanDate { get; set; } | 
|             //物料类型 | 
|             public int FErpClsID { get; set; } | 
|             //仓库 | 
|             public string FStock { get; set; } | 
|             //车间 | 
|             public string FWorkShop { get; set; } | 
|             //批号 | 
|             public string HLOT { get; set; } | 
|             public string HInventoryFID { get; set; } | 
|   | 
|         } | 
|         #endregion | 
|   | 
|         [Route("JIT_DayPlanPlatFormBill/JIT_CompleteSetAnalysis")] | 
|         [HttpGet] | 
|         public object JIT_CompleteSetAnalysis(string sWhere, string user) | 
|         { | 
|             try | 
|             { | 
|                 Stopwatch sw = new Stopwatch(); | 
|                 omdelMian = JsonConvert.DeserializeObject<Sc_WorkBillSortBillMain>(sWhere); | 
|                 string sql = $"exec JIT_qtfx {omdelMian.HProdORGID},{omdelMian.HWorkShopID},'{user}' "; | 
|                 ds = oCN.RunProcReturn(sql, "JIT_qtfx"); | 
|                 LogService.Write("齐套分析sql:" + sql); | 
|                 List<PRD_PPBOM> BomList = new List<PRD_PPBOM>(); | 
|                 DataTable resulTable = ds.Tables[0]; | 
|                 if (resulTable.Rows[0][0].ToString().Contains("失败")) | 
|                 { | 
|                     //释放齐套操作 避免多人同时进行齐套分析 | 
|                     oCN.RunProc($"update T_PRD_QTFX set FSTATUS = 0 where FPRDORGID ={omdelMian.HProdORGID} and FSTATUS = 1 "); | 
|   | 
|                     objJsonResult.code = "1"; | 
|                     objJsonResult.count = 1; | 
|                     objJsonResult.Message = resulTable.Rows[0][0].ToString(); | 
|                     objJsonResult.data = null; | 
|                     return objJsonResult; | 
|                 } | 
|                 else | 
|                 { | 
|                     BomList = ds.ToModelList<PRD_PPBOM>(); | 
|                 } | 
|   | 
|                 string fDayPlanFids = string.Empty; | 
|                 string fDayPlanFEntryids = string.Empty; | 
|                 if (BomList.Count == 0) | 
|                 { | 
|                     //释放齐套操作 避免多人同时进行齐套分析 | 
|                     oCN.RunProc($"update T_PRD_QTFX set FSTATUS = 0 where FPRDORGID ={omdelMian.HProdORGID} and FSTATUS = 1 "); | 
|   | 
|                     objJsonResult.code = "1"; | 
|                     objJsonResult.count = 1; | 
|                     objJsonResult.Message = "没有需要进行齐套分析的数据"; | 
|                     objJsonResult.data = null; | 
|                     return objJsonResult; | 
|                 } | 
|                 else | 
|                 { | 
|                     List<string> fidList = BomList.Select(x => x.HInterID.ToString()).Distinct().ToList(); | 
|                     List<string> fentryidList = BomList.Select(x => x.HEntryID.ToString()).Distinct().ToList(); | 
|                     fDayPlanFids = string.Join(",", fidList); | 
|                     fDayPlanFEntryids = string.Join(",", fentryidList); | 
|                 } | 
|                 LogService.Write("日计划工单ids" + fDayPlanFids); | 
|   | 
|                 sw.Start();//开始计时 | 
|   | 
|                 //获取当前即时库存 库存数量+库存组织+物料+货主+仓库    | 
|                 ds = oCN.RunProcReturn(@"  | 
| select distinct isnull(a.FBASEQTY,0) FBASEQTY,b.FNumber,a.FOwnerTypeId,a.FOwnerId,c.FNUMBER as FOwnerNumber,a.FSTOCKID,d.FNUMBER as FStockNumber ,a.FLOT HLOT,a.FID HInventoryFID  | 
| from  | 
| ( | 
| select distinct a.HProdORGID FStockOrgId,T2.FMATERIALID,FNumber from Sc_WorkBillSortBillMain a | 
| join Sc_WorkBillSortBillSub b on a.HInterID = b.HInterID | 
| join AIS20210811135644..T_PRD_PPBOM T1 on a.HICMOInterID_Sec = t1.FMoId and a.HICMOEntryID_Sec = t1.FMOENTRYID | 
| LEFT JOIN  AIS20210811135644..T_PRD_PPBOMENTRY T2 on T1.FID = t2.FID | 
| join  AIS20210811135644..T_BD_MATERIAL T3 on T2.FMATERIALID = T3.FMATERIALID | 
| ) T1  | 
| JOIN  AIS20210811135644..T_BD_MATERIAL b on  t1.FNUMBER = b.FNUMBER | 
| LEFT JOIN  AIS20210811135644..T_STK_Inventory a on b.FMATERIALID = a.FMATERIALID | 
| LEFT join (select distinct fitemid,fformid,FNUMBER from  AIS20210811135644..V_ITEMCLASS_OWNER) c on a.FOwnerTypeId = c.fformid and a.FOWNERID = c.fitemid | 
| join  AIS20210811135644..t_BD_Stock d on a.FSTOCKID =d.FSTOCKID | 
| where a.FBASEQTY>0  | 
| ", "SC_WORKBILLSORTBILLMAIN"); | 
|                 ListInventory = ds.ToModelList<PRD_Inventory>(); | 
|                 //车间 仓库对照列表  | 
|                 ds = oCN.RunProcReturn(@"  | 
| SELECT DISTINCT T1.HOrgId FORGID,HWorkShopID FWORKSHOPID,HWHID FSTOCKID FROM Gy_WorkWarehouseBillMain T1  | 
| JOIN Gy_WorkWarehouseBillMainSub T2 ON T1.HInterID=T2.HInterID  | 
| ", "Gy_WorkWarehouseBillMain"); | 
|                 List<PRD_WorkHouseCompar> pRD_WorkHouseCompar = ds.ToModelList<PRD_WorkHouseCompar>(); | 
|                 int i = 1;//用于进度条的加载 | 
|                 //日计划+生产订单子表编码ID | 
|                 var DatePlanList = BomList.GroupBy(p => new { p.HMasterDate, p.HICMOEntryID_Sec }).Select(x => new PRD_PPBOM { HMasterDate = x.Key.HMasterDate, HICMOEntryID_Sec = x.Key.HICMOEntryID_Sec }).ToList(); | 
|                 //取应发数量 FMustQty 不要取值需求数量FNeedQty 领料数据 | 
|                 string LLsql = string.Format(@" | 
| select max(t1.FCXStockInQty)FCXStockInQty,FPRDMOENTYID,max(dwyl)dwyl,sum(FPickedQty)FPickedQty,sum(FRemainPickedQty)FRemainPickedQty,FMATERIALID from  | 
| ( | 
| select a.FCXStockInQty, FPRDMOENTYID,convert( decimal(18,2),FMustQty/FQty ) as dwyl,FPickedQty,T2.FMATERIALID, | 
| case when convert( decimal(18,2),(FPickedQty - a.FCXStockInQty * FMustQty/FQty))< 0 then 0  | 
| else convert( decimal(18,2),(FPickedQty - a.FCXStockInQty * FMustQty/FQty)) end as FRemainPickedQty  | 
|  from  | 
| ( | 
| select sum(HSourceStockInQty)FCXStockInQty,HICMOEntryID_Sec FPRDMOENTYID from SC_WORKBILLSORTBILLMAIN a | 
| where a.HInterID IN ({0}) | 
| group by HICMOEntryID_Sec  | 
| ) a | 
| join  AIS20210811135644..T_PRD_PPBOM T1  on a.FPRDMOENTYID = t1.FMOENTRYID | 
| LEFT JOIN AIS20210811135644..T_PRD_PPBOMENTRY t2 on T1.FID = T2.FID  | 
| LEFT JOIN AIS20210811135644..T_PRD_PPBOMENTRY_Q t3 on t2.FENTRYID = T3.FENTRYID  | 
| LEFT JOIN AIS20210811135644..T_BD_MATERIAL T4 ON T2.FMATERIALID = T4.FMATERIALID  | 
| ) t1 | 
| group by FPRDMOENTYID,FMATERIALID | 
| ", fDayPlanFids); | 
|                 List<DayPlanPickedModel> DayPlanPickedModel = new List<DayPlanPickedModel>(); | 
|                 ds = oCN.RunProcReturn(LLsql, "SC_WORKBILLSORTBILLMAIN"); | 
|                 DayPlanPickedModel = ds.ToModelList<DayPlanPickedModel>(); | 
|                 //CompleteAnalysisTempModel 用于存储临时需要插入数据 | 
|                 List<CompleteAnalysisTempModel> completeAnalysisTempModel = new List<CompleteAnalysisTempModel>(); | 
|                 //创建字典 用于储存物料 + 占用数量 | 
|                 Dictionary<string, decimal> occupyDic = new Dictionary<string, decimal>(); | 
|                 LogService.Write($"齐套分析,数据准备阶段,用时" + sw.Elapsed + "总行数:" + BomList.Count); | 
|                 LogService.Write($"齐套分析,数据准备阶段,用时" + sw.Elapsed + "总需要循环的bom数量:" + DatePlanList.Count); | 
|   | 
|                 //第一个循环 日计划工单明细 一天+一个工单 一个循环 | 
|                 foreach (var item in DatePlanList) | 
|                 { | 
|                     //日计划用料清单明细 每一天的实际用料情况 | 
|                     var _BomList = BomList.Where(x => x.HMasterDate == item.HMasterDate && x.HICMOEntryID_Sec == item.HICMOEntryID_Sec).ToList(); | 
|                     //每一个物料进行库存计算 | 
|                     foreach (var _item in _BomList) | 
|                     { | 
|                         //车间仓库 对照表 每个日计划同属一个车间 A,B | 
|                         var _pRD_WorkHouseCompar = pRD_WorkHouseCompar.Where(c => c.FWORKSHOPID == _item.HWorkShopID).ToList(); | 
|                         decimal jskcQty = 0;//用于计算 | 
|                         decimal jskcQty2 = 0;//用于计算当时那个计算节点的即时库存 | 
|                         //计算当前这几个仓库总的库存量  | 
|                         if (_pRD_WorkHouseCompar.Count > 0) | 
|                         { | 
|                             foreach (var _WorkHouse in _pRD_WorkHouseCompar) | 
|                             { | 
|                                 //库存数据匹配 | 
|                                 List<PRD_Inventory> _ListInventory = ListInventory.Where(c => c.FNumber == _item.FNumber && c.FBASEQTY > 0 && c.FSTOCKID == _WorkHouse.FSTOCKID).ToList(); | 
|                                 if (_ListInventory.Count > 0) | 
|                                 { | 
|                                     foreach (var Inventory in _ListInventory) | 
|                                     { | 
|                                         jskcQty += Inventory.FBASEQTY; | 
|                                     } | 
|                                 } | 
|                             } | 
|                         } | 
|                         //找出物料被其它订单占用数量 | 
|                         decimal occupyMaterial = 0; | 
|                         if (occupyDic.ContainsKey(_item.HWorkShopID + _item.FMATERIALID2)) | 
|                         { | 
|                             occupyMaterial = occupyDic[_item.HWorkShopID + _item.FMATERIALID2]; | 
|                         } | 
|                         //虚拟日计划用料清单 需求数量  | 
|                         decimal planNeed = _item.PlanCount; //不参与计算 用于记录 | 
|                         decimal need = _item.PlanCount;//参与计算 | 
|                         decimal FOccupyPickedCount = 0;//占用领料单数量 | 
|                         //找到领用数量 | 
|                         var _DayPlanPickedModel = DayPlanPickedModel.Where(c => c.FPRDMOENTYID == _item.HICMOEntryID_Sec && c.FMATERIALID == _item.FMATERIALID2).FirstOrDefault(); | 
|                         if (_DayPlanPickedModel.FRemainPickedQty >= need) | 
|                         { | 
|                             //领料数量满足了当前这个需求量 直接齐套 不需要去占用仓库 | 
|                             completeAnalysisTempModel.Add(new CompleteAnalysisTempModel | 
|                             { | 
|                                 FHMainICMOInterIDr = _item.HICMOInterID_Sec, | 
|                                 FHMainICMOEntryID = _item.HICMOEntryID_Sec, | 
|                                 FHICMOInterID = _item.HInterID.ToString(), | 
|                                 FHICMOEntryID = _item.HEntryID.ToString(), | 
|                                 FHMaterID = _item.FMATERIALID2, | 
|                                 FErpClsID = _item.FErpClsID, | 
|                                 FHStockOrgID = _item.FStockOrgId, | 
|                                 FHPRDORGID = _item.FStockOrgId, | 
|                                 FPRDBillNo = _item.HICMOBillNo, | 
|                                 HMainICMOBillNo = _item.HMainICMOBillNo, | 
|                                 FOwnerTypeId = "", | 
|                                 FUnitDosage = _item.dwyl,//单位用量 | 
|                                 FSumPlanCount = planNeed,//计划总数量 | 
|                                 FCompleteCount1 = planNeed, //齐套数量 | 
|                                 FLackCount = 0,//缺料数量 | 
|                                 FHStockQty = jskcQty2,//即时库存 | 
|                                 FHLeftQty = jskcQty,//可用数量 | 
|                                 FOccupyCount = occupyMaterial,//占用数量 | 
|                                 FOccupyPickedCount = planNeed,//占用领料数量 | 
|                                 FComPlete = "齐套", | 
|                                 FPlanDate = item.HMasterDate, // 日计划日期 | 
|                                 FWorkShop = _item.HWorkShopID //车间 | 
|                             }); | 
|                             //扣除剩余可用领料数量 | 
|                             _DayPlanPickedModel.FRemainPickedQty -= need; | 
|                             //循环下一个物料 | 
|                             continue; | 
|                         } | 
|                         else | 
|                         { | 
|                             need -= _DayPlanPickedModel.FRemainPickedQty; //剩余需求数量 | 
|                             FOccupyPickedCount = _DayPlanPickedModel.FRemainPickedQty; | 
|                             _DayPlanPickedModel.FRemainPickedQty = 0; | 
|                         } | 
|                         if (_pRD_WorkHouseCompar.Count > 0) | 
|                         { | 
|                             //缺料数量 需求数量 - 实际库存  >0 ? 一个物料不管占用多少条 库存+货主 数据 缺料数量是同一个数量 | 
|                             decimal _fLackCount = need > jskcQty ? (need - jskcQty) : 0; | 
|                             //LogHelper.Info("组织:" + _item.FStockOrgId + " ,及时库存" + jskcQty); | 
|                             //总库存为0了 直接给出 缺料数量 | 
|                             if (jskcQty == 0) | 
|                             { | 
|                                 completeAnalysisTempModel.Add(new CompleteAnalysisTempModel | 
|                                 { | 
|                                     FHMainICMOInterIDr = _item.HICMOInterID_Sec, | 
|                                     FHMainICMOEntryID = _item.HICMOEntryID_Sec, | 
|                                     FHICMOInterID = _item.HInterID.ToString(), | 
|                                     FHICMOEntryID = _item.HEntryID.ToString(), | 
|                                     FHMaterID = _item.FMATERIALID2, | 
|                                     FErpClsID = _item.FErpClsID, | 
|                                     FHStockOrgID = _item.FStockOrgId, | 
|                                     FHPRDORGID = _item.FStockOrgId, | 
|                                     FPRDBillNo = _item.HICMOBillNo, | 
|                                     HMainICMOBillNo = _item.HMainICMOBillNo, | 
|                                     FOwnerTypeId = "", | 
|                                     FUnitDosage = _item.dwyl,//单位用量 | 
|                                     FSumPlanCount = planNeed,//计划总数量 | 
|                                     FCompleteCount1 = FOccupyPickedCount, //齐套数量 | 
|                                     FLackCount = need,//缺料数量 | 
|                                     FHStockQty = jskcQty2,//即时库存 | 
|                                     FHLeftQty = jskcQty,//可用数量 | 
|                                     FOccupyCount = occupyMaterial,//占用数量 | 
|                                     FOccupyPickedCount = FOccupyPickedCount,//占用领料数量 | 
|                                     FComPlete = "未齐套", | 
|                                     FPlanDate = item.HMasterDate, // 日计划日期 | 
|                                     FWorkShop = _item.HWorkShopID //车间 | 
|                                 }); | 
|                                 //终止该物料计算 进入下一个 | 
|                                 continue; | 
|                             } | 
|                             foreach (var WorkHouse in _pRD_WorkHouseCompar) | 
|                             { | 
|                                 //库存数据匹配 | 
|                                 List<PRD_Inventory> _ListInventory = ListInventory.Where(c => c.FNumber == _item.FNumber && c.FBASEQTY > 0 && c.FSTOCKID == WorkHouse.FSTOCKID).ToList(); | 
|                                 //需求数量 占用完后可以跳出车间仓库循环 | 
|                                 if (need == 0) | 
|                                     break; | 
|                                 if (_ListInventory.Count > 0) | 
|                                 { | 
|                                     foreach (var Inventory in _ListInventory) | 
|                                     { | 
|                                         if (need == 0) | 
|                                             break; | 
|                                         //齐套数量  | 
|                                         decimal fCompleteCount = need >= Inventory.FBASEQTY ? Inventory.FBASEQTY : need; | 
|                                         if (!occupyDic.ContainsKey(_item.HWorkShopID + _item.FMATERIALID2)) | 
|                                         { | 
|                                             occupyDic.Add(_item.HWorkShopID + _item.FMATERIALID2, fCompleteCount); | 
|                                         } | 
|                                         else | 
|                                         { | 
|                                             occupyMaterial = occupyDic[_item.HWorkShopID + _item.FMATERIALID2]; | 
|                                             occupyDic[_item.HWorkShopID + _item.FMATERIALID2] = occupyMaterial + fCompleteCount; | 
|                                         } | 
|                                         completeAnalysisTempModel.Add(new CompleteAnalysisTempModel | 
|                                         { | 
|                                             FHMainICMOInterIDr = _item.HICMOInterID_Sec, | 
|                                             FHMainICMOEntryID = _item.HICMOEntryID_Sec, | 
|                                             FHICMOInterID = _item.HInterID.ToString(), | 
|                                             FHICMOEntryID = _item.HEntryID.ToString(), | 
|                                             FHMaterID = _item.FMATERIALID2, | 
|                                             FHStockOrgID = _item.FStockOrgId, | 
|                                             FHPRDORGID = _item.FStockOrgId, | 
|                                             FErpClsID = _item.FErpClsID, | 
|                                             FPRDBillNo = _item.HICMOBillNo, | 
|                                             HMainICMOBillNo = _item.HMainICMOBillNo, | 
|                                             FOwnerTypeId = Inventory.FOwnerTypeId, | 
|                                             FOwnerId = Inventory.FOwnerId, | 
|                                             FUnitDosage = _item.dwyl,//单位用量 | 
|                                             FSumPlanCount = planNeed,//计划总数量 | 
|                                             FCompleteCount1 = fCompleteCount + FOccupyPickedCount, //齐套数量 | 
|                                             FLackCount = _fLackCount,//缺料数量 | 
|                                             FHStockQty = jskcQty2,//即时库存 | 
|                                             FHLeftQty = jskcQty,//可用数量 | 
|                                             FOccupyCount = occupyMaterial,//占用数量 | 
|                                             FOccupyPickedCount = FOccupyPickedCount,//领料占用数量 | 
|                                             FComPlete = jskcQty >= need ? "齐套" : "未齐套", | 
|                                             FPlanDate = item.HMasterDate, // 日计划日期 | 
|                                             FStock = Inventory.FSTOCKID,//仓库 | 
|                                             FWorkShop = _item.HWorkShopID, //车间 | 
|                                             HLOT = Inventory.HLOT, //批号 | 
|                                             HInventoryFID = Inventory.HInventoryFID //id | 
|                                         }); | 
|                                         //扣减总库存数量 | 
|                                         jskcQty = jskcQty >= fCompleteCount ? jskcQty - fCompleteCount : 0; | 
|                                         //剩余日计划需求数量 | 
|                                         need = need >= Inventory.FBASEQTY ? need - Inventory.FBASEQTY : 0; | 
|                                         //扣减消耗库存 | 
|                                         Inventory.FBASEQTY = Inventory.FBASEQTY - fCompleteCount; | 
|                                     } | 
|                                 } | 
|                             } | 
|                         } | 
|                         //没有车间仓库数据 既没有可用库存 为未齐套 | 
|                         else | 
|                         { | 
|                             if (occupyDic.ContainsKey(_item.HWorkShopID + _item.FMATERIALID2)) | 
|                             { | 
|                                 occupyMaterial = occupyDic[_item.HWorkShopID + _item.FMATERIALID2]; | 
|                             } | 
|                             completeAnalysisTempModel.Add(new CompleteAnalysisTempModel | 
|                             { | 
|                                 FHMainICMOInterIDr = _item.HICMOInterID_Sec, | 
|                                 FHMainICMOEntryID = _item.HICMOEntryID_Sec, | 
|                                 FHICMOInterID = _item.HInterID.ToString(), | 
|                                 FHICMOEntryID = _item.HEntryID.ToString(), | 
|                                 FHMaterID = _item.FMATERIALID2, | 
|                                 FErpClsID = _item.FErpClsID, | 
|                                 FHStockOrgID = _item.FStockOrgId, | 
|                                 FHPRDORGID = _item.FStockOrgId, | 
|                                 FPRDBillNo = _item.HICMOBillNo, | 
|                                 HMainICMOBillNo = _item.HMainICMOBillNo, | 
|                                 FOwnerTypeId = "", | 
|                                 FUnitDosage = _item.dwyl,//单位用量 | 
|                                 FSumPlanCount = planNeed,//计划总数量 | 
|                                 FCompleteCount1 = planNeed - need, //齐套数量 | 
|                                 FLackCount = need,//缺料数量 | 
|                                 FHStockQty = jskcQty2,//即时库存 | 
|                                 FHLeftQty = jskcQty,//可用数量 | 
|                                 FOccupyCount = occupyMaterial,//占用数量 | 
|                                 FOccupyPickedCount = FOccupyPickedCount,//占用领料数量 | 
|                                 FComPlete = "未齐套", | 
|                                 FPlanDate = item.HMasterDate, // 日计划日期 | 
|                                 FWorkShop = _item.HWorkShopID //车间 | 
|                             }); | 
|                         } | 
|                     } | 
|                     //Thread.Sleep(6000); | 
|                     i++; | 
|                 } | 
|                 LogService.Write($"齐套分析,数据准备完成,保存到Model实体,总行数:{completeAnalysisTempModel.Count},耗时:" + sw.Elapsed); | 
|                 List<string> sqlList = new List<string>(); | 
|                 DataTable insertDT = new DataTable(); | 
|                 insertDT.TableName = "JIT_MOMaterReadysBill"; | 
|                 insertDT.Columns.Add("FID", typeof(long)); | 
|                 insertDT.Columns.Add("FDOCUMENTSTATUS", typeof(string)); | 
|                 insertDT.Columns.Add("FHMAINICMOINTERIDR", typeof(long)); | 
|                 insertDT.Columns.Add("FHMAINICMOENTRYID", typeof(long)); | 
|                 insertDT.Columns.Add("FHICMOINTERID", typeof(long)); | 
|                 insertDT.Columns.Add("FHICMOENTRYID", typeof(long)); | 
|                 insertDT.Columns.Add("FHMATERID", typeof(long)); | 
|                 insertDT.Columns.Add("FHSTOCKORGID", typeof(long)); | 
|                 insertDT.Columns.Add("FHSTOCKQTY", typeof(decimal)); | 
|                 insertDT.Columns.Add("FHLEFTQTY", typeof(decimal)); | 
|                 insertDT.Columns.Add("FHPRDORGID", typeof(long)); | 
|                 insertDT.Columns.Add("FUNITDOSAGE", typeof(decimal)); | 
|                 insertDT.Columns.Add("FSUMPLANCOUNT", typeof(decimal)); | 
|                 insertDT.Columns.Add("FPRDBILLNO", typeof(string)); | 
|                 insertDT.Columns.Add("HMainICMOBillNo", typeof(string)); | 
|                 insertDT.Columns.Add("FOWNERID", typeof(long)); | 
|                 insertDT.Columns.Add("FOWNERTYPEID", typeof(string)); | 
|                 insertDT.Columns.Add("FPLANDATE", typeof(DateTime)); | 
|                 insertDT.Columns.Add("FCOMPLETE", typeof(string)); | 
|                 insertDT.Columns.Add("FLACKCOUNT", typeof(decimal)); | 
|                 insertDT.Columns.Add("FOCCUPYCOUNT", typeof(decimal)); | 
|                 insertDT.Columns.Add("FCOMPLETECOUNT1", typeof(decimal)); | 
|                 insertDT.Columns.Add("FERPCLSID", typeof(string)); | 
|                 insertDT.Columns.Add("FWORKSHOPID", typeof(long)); | 
|                 insertDT.Columns.Add("FSTOCKID", typeof(long)); | 
|                 insertDT.Columns.Add("FOCCUPYPICKEDCOUNT", typeof(decimal)); | 
|                 insertDT.Columns.Add("HLOT", typeof(long)); | 
|                 insertDT.Columns.Add("HInventoryFID", typeof(string)); | 
|                 int jdtmmm = 1; | 
|                 ds = oCN.RunProcReturn("select max(HInterID)FID FROM  JIT_MOMaterReadysBill", "JIT_MOMaterReadysBill"); | 
|                 int maxFid = int.Parse(ds.Tables[0].Rows[0][0].ToString() == "" ? "0" : ds.Tables[0].Rows[0][0].ToString()); | 
|                 foreach (var item in completeAnalysisTempModel) | 
|                 { | 
|                     DataRow dr = insertDT.NewRow(); | 
|                     dr["FID"] = maxFid + jdtmmm; | 
|                     dr["FDOCUMENTSTATUS"] = "A"; | 
|                     dr["FHMAINICMOINTERIDR"] = item.FHMainICMOInterIDr; | 
|                     dr["FHMAINICMOENTRYID"] = item.FHMainICMOEntryID; | 
|                     dr["FHICMOINTERID"] = item.FHICMOInterID; | 
|                     dr["FHICMOENTRYID"] = item.FHICMOEntryID; | 
|                     dr["FHMATERID"] = item.FHMaterID; | 
|                     dr["FHSTOCKORGID"] = item.FHStockOrgID; | 
|                     dr["FHSTOCKQTY"] = item.FHStockQty; | 
|                     dr["FHLEFTQTY"] = item.FHLeftQty; | 
|                     dr["FHPRDORGID"] = item.FHPRDORGID; | 
|                     dr["FUNITDOSAGE"] = item.FUnitDosage; | 
|                     dr["FSUMPLANCOUNT"] = item.FSumPlanCount; | 
|                     dr["FPRDBILLNO"] = item.FPRDBillNo; | 
|                     dr["HMainICMOBillNo"] = item.HMainICMOBillNo; | 
|                     dr["FOWNERID"] = item.FOwnerId == null ? "0" : item.FOwnerId; | 
|                     dr["FOWNERTYPEID"] = item.FOwnerTypeId; | 
|                     dr["FPLANDATE"] = item.FPlanDate.ToString("yyyy-MM-dd"); | 
|                     dr["FCOMPLETE"] = item.FComPlete; | 
|                     dr["FLACKCOUNT"] = item.FLackCount; | 
|                     dr["FOCCUPYCOUNT"] = item.FOccupyCount; | 
|                     dr["FCOMPLETECOUNT1"] = item.FCompleteCount1; | 
|                     dr["FERPCLSID"] = item.FErpClsID; | 
|                     dr["FWORKSHOPID"] = item.FWorkShop == null ? "0" : item.FWorkShop; | 
|                     dr["FSTOCKID"] = item.FStock == null ? "0" : item.FStock; | 
|                     dr["FOCCUPYPICKEDCOUNT"] = item.FOccupyPickedCount; | 
|                     dr["HLOT"] = item.HLOT == null? "0" : item.HLOT; | 
|                     dr["HInventoryFID"] = item.HInventoryFID == null ? "0" : item.HInventoryFID; | 
|                     insertDT.Rows.Add(dr); | 
|                     jdtmmm++; | 
|                 } | 
|   | 
|                 insertDT.EndLoadData(); // 灌入数据结束 | 
|                 //插入之前执行下清空语句(测试是否能够防止重复生成) | 
|                 string deletesql = $"delete from JIT_MOMaterReadysBill  where HICMOInterID  in ({fDayPlanFids}) "; | 
|                 oCN.RunProc(deletesql); | 
|                 // 批量插入到数据库 | 
|                 foreach (DataRow item in insertDT.Rows) | 
|                 { | 
|                     oCN.RunProc("insert into JIT_MOMaterReadysBill(HInterID,HDocumentStatus,HMainICMOInterID,HMainICMOEntryID,HICMOInterID" + | 
|                             ", HICMOEntryID, HMaterID, HStockORGID, HStockQty, HLeftQty" + | 
|                             ", HProdORGID, HUnitDosage, HSumPlanQty, HICMOBillNo, HOwnerID" + | 
|                             ", HOwnerTypeID, HPlanDate, HComplete, HLackQty, HOccupyQty" + | 
|                             ", HCompleteQty1, HErpClsID, HWorkShopID, HWHID, HOccupyPickedQTY,HMainICMOBillNo" + | 
|                             ",HLOT,HInventoryFID)values" + | 
|                              $"({item["FID"].ToString()}, 1, {item["FHMAINICMOINTERIDR"].ToString()}, {item["FHMAINICMOENTRYID"].ToString()}, {item["FHICMOINTERID"].ToString()}" + | 
|                              $", {item["FHICMOENTRYID"].ToString()}, {item["FHMATERID"].ToString()}, {item["FHSTOCKORGID"].ToString()}, {item["FHSTOCKQTY"].ToString()}, {item["FHLEFTQTY"].ToString()}" + | 
|                              $" , {item["FHPRDORGID"].ToString()}, {item["FUNITDOSAGE"].ToString()}, {item["FSUMPLANCOUNT"].ToString()}, '{item["FPRDBILLNO"].ToString()}', {item["FOWNERID"].ToString()}" + | 
|                              $", '{item["FOWNERTYPEID"].ToString()}', '{item["FPLANDATE"].ToString()}','{item["FCOMPLETE"].ToString()}', {item["FLACKCOUNT"].ToString()}, {item["FOCCUPYCOUNT"].ToString()}" + | 
|                              $", {item["FCOMPLETECOUNT1"].ToString()}, {item["FERPCLSID"].ToString()}, {item["FWORKSHOPID"].ToString()}, {item["FSTOCKID"].ToString()}, {item["FOCCUPYPICKEDCOUNT"].ToString()}, '{item["HMainICMOBillNo"].ToString()}'" + | 
|                              $",{item["HLOT"].ToString()},'{item["HInventoryFID"].ToString()}')"); | 
|                 } | 
|                 LogService.Write("批量插入到数据库用时:" + sw.Elapsed); | 
|                 //执行完成后 更新日计划工单状态 | 
|                 string updatesql = string.Format(@" | 
| update Sc_WorkBillSortBillSub set HComplete = '未齐套' where HEntryID IN  | 
| (select distinct HICMOEntryID from JIT_MOMaterReadysBill | 
|  where  isnull(HComplete,'未齐套') ='未齐套' | 
|  and HICMOEntryID in ({0}) | 
|  )", fDayPlanFEntryids); | 
|                 //LogService.Write(Context, updatesql); | 
|                 oCN.RunProc(updatesql); | 
|                 updatesql = string.Format(@" | 
| update Sc_WorkBillSortBillSub set HComplete  = '齐套'  | 
| where HEntryID IN ({0}) and HComplete  <>'未齐套' | 
| ", fDayPlanFEntryids); | 
|                 //LogService.Write(updatesql); | 
|                 oCN.RunProc(updatesql); | 
|                 //释放齐套操作 避免多人同时进行齐套分析 | 
|                 oCN.RunProc($"update T_PRD_QTFX set FSTATUS = 0 where FPRDORGID ={omdelMian.HProdORGID} and FSTATUS = 1 "); | 
|                 sw.Stop();//结束计时 | 
|                 LogService.Write("齐套分析运行总时长:" + sw.Elapsed); | 
|   | 
|                 objJsonResult.code = "1"; | 
|                 objJsonResult.count = 1; | 
|                 objJsonResult.Message = "操作成功"; | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|             catch (Exception e) | 
|             { | 
|                 LogService.Write("操作失败:" + e.Message.ToString()); | 
|                 objJsonResult.code = "0"; | 
|                 objJsonResult.count = 0; | 
|                 objJsonResult.Message = "Exception!" + e.Message.ToString(); | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|         } | 
|         #endregion | 
|   | 
|         #region 生产日计划平台 锁定 | 
|         [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBillHLockedDay")] | 
|         [HttpGet] | 
|         public object JIT_DayPlanPlatFormBillHLockedDay(int HLockedDay,int HWorkShopID) | 
|         { | 
|             try | 
|             { | 
|                 oCN.RunProc("update b set HLockedSub=0 from Sc_WorkBillSortBillMain a join Sc_WorkBillSortBillSub b on a.HInterID = b.HInterID " + | 
|               $" where a.HWorkShopID={HWorkShopID}"); | 
|   | 
|                 oCN.RunProc("update b set HLockedSub=1 from Sc_WorkBillSortBillMain a join Sc_WorkBillSortBillSub b on a.HInterID = b.HInterID " + | 
|                 $" where  HMasterDate between  DATEADD(day, -1, getdate()) " + | 
|                 $"and DATEADD(day,{(HLockedDay - 1)},getdate())  and a.HWorkShopID={HWorkShopID}"); | 
|   | 
|                 objJsonResult.code = "1"; | 
|                 objJsonResult.count = 1; | 
|                 objJsonResult.Message = "Sucess!"; | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|             catch (Exception e) | 
|             { | 
|                 objJsonResult.code = "0"; | 
|                 objJsonResult.count = 0; | 
|                 objJsonResult.Message = "Exception!" + e.ToString(); | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|         } | 
|         #endregion | 
|   | 
|         #region 生产日计划平台 解锁 | 
|         [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBillHUnlockDay")] | 
|         [HttpGet] | 
|         public object JIT_DayPlanPlatFormBillHUnlockDay(int HWorkShopID) | 
|         { | 
|             try | 
|             { | 
|                 oCN.RunProc("update b set HLockedSub=0 from Sc_WorkBillSortBillMain a join Sc_WorkBillSortBillSub b on a.HInterID = b.HInterID " + | 
|               $" where a.HWorkShopID={HWorkShopID}"); | 
|   | 
|                 objJsonResult.code = "1"; | 
|                 objJsonResult.count = 1; | 
|                 objJsonResult.Message = "Sucess!"; | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|             catch (Exception e) | 
|             { | 
|                 objJsonResult.code = "0"; | 
|                 objJsonResult.count = 0; | 
|                 objJsonResult.Message = "Exception!" + e.ToString(); | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|         } | 
|         #endregion | 
|   | 
|         #region 生产日计划平台 生产订单查询 | 
|         [Route("JIT_DayPlanPlatFormBill/JIT_ICMOBillList")] | 
|         [HttpGet] | 
|         public object JIT_ICMOBillList(string hmainid) | 
|         { | 
|             try | 
|             { | 
|                 var list = hmainid.Split(','); | 
|                 List<DataTable> listdt = new List<DataTable>(); | 
|                 DataTable dtable = new DataTable("dt"); | 
|                 for (int i = 0; i < list.Length; i++) | 
|                 { | 
|                     var listEvery = list[i].Split('_'); | 
|                     | 
|                     ds = oCN.RunProcReturn($"exec h_p_ICMOBillList_PrimarySubTable '{listEvery[0]}','{listEvery[1]}'", "h_p_ICMOBillList_PrimarySubTable"); | 
|   | 
|                     var dt = oCN.RunProcReturn($"select * from  h_v_JIT_Sc_WorkBillSortBillList where 源单主内码 in({ds.Tables[0].Rows[0]["源单主内码"].ToString()}) and  源单子内码 in({ds.Tables[0].Rows[0]["源单子内码"].ToString()})", "h_v_JIT_Sc_WorkBillSortBillList"); | 
|                     if (dt.Tables[0].Rows.Count > 0) | 
|                     { | 
|                         objJsonResult.code = "0"; | 
|                         objJsonResult.count = 0; | 
|                         objJsonResult.Message = "生产明细行重复,重复的生产订单号:" + ds.Tables[0].Rows[i]["生产订单号"].ToString() + ",明细行号:" + ds.Tables[0].Rows[i]["生产订单明细行号"].ToString(); | 
|                         objJsonResult.data = null; | 
|                         return objJsonResult; | 
|                     } | 
|                     if (i == 0) | 
|                     { | 
|                         //添加列名 | 
|                         for (int j = 0; j < ds.Tables[0].Columns.Count; j++) | 
|                         { | 
|                             dtable.Columns.Add(ds.Tables[0].Columns[j].ToString()); | 
|                         } | 
|                     } | 
|                     listdt.Add(ds.Tables[0]); | 
|                 } | 
|   | 
|                 //获取集合数据 | 
|                 for (int i = 0; i < listdt.Count; i++) | 
|                 { | 
|                     DataRow row = dtable.NewRow(); | 
|                     for (int j = 0; j < listdt[i].Columns.Count; j++) | 
|                     { | 
|                         if (listdt[i].Columns[j].ColumnName == "计划数量") | 
|                         { | 
|                             row[j] = double.Parse(listdt[i].Rows[0][j].ToString()).ToString(); | 
|                         } | 
|                         else | 
|                         { | 
|                             row[j] = listdt[i].Rows[0][j].ToString(); | 
|                         } | 
|                     } | 
|                     dtable.Rows.Add(row); | 
|                 } | 
|   | 
|                 //添加动态日期 | 
|                 for (int j = 1; j <= 31; j++) | 
|                 { | 
|                     dtable.Columns.Add(DateTime.Now.AddDays(j - 1).ToString("yyyy-MM-dd"), typeof(Int32)); | 
|                 } | 
|   | 
|                 objJsonResult.code = "1"; | 
|                 objJsonResult.count = 1; | 
|                 objJsonResult.Message = "Sucess!"; | 
|                 objJsonResult.data = dtable; | 
|                 return objJsonResult; | 
|             } | 
|             catch (Exception e) | 
|             { | 
|                 objJsonResult.code = "0"; | 
|                 objJsonResult.count = 0; | 
|                 objJsonResult.Message = "Exception!" + e.ToString(); | 
|                 objJsonResult.data = null; | 
|                 return objJsonResult; | 
|             } | 
|         } | 
|         #endregion | 
|   | 
|     } | 
| } |