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; using System.Threading; using System.Globalization; namespace WebAPI.Controllers.SCGL.日计划管理 { public class JIT_DayPlanPlatFormBill_TKController : ApiController { 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"; Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter(); #region 生产日计划平台 保存 [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBill_btnSave_TK")] [HttpPost] public object JIT_DayPlanPlatFormBill_btnSave_TK([FromBody] JObject sMainSub) { var _value = sMainSub["sMainSub"].ToString(); string msg1 = _value.ToString(); //保存主表 objJsonResult = AddBillMain(msg1); if (objJsonResult.code == "0") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = objJsonResult.Message; objJsonResult.data = null; return objJsonResult; } 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 { Stopwatch sw = new Stopwatch();//计时器 sw.Start();//开始计时 if (!DBUtility.ClsPub.Security_Log("Sc_WorkBillSortBill_Edit", 1, false, user)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "无保存权限!"; objJsonResult.data = null; return objJsonResult; } LogService.CustomWriteLog("1:" + msg2, DateTime.Now.ToString("yyyy-MM-dd")); //JSON序列化转换字典集合 List> list = new List>(); List jb = JsonConvert.DeserializeObject>(msg2); foreach (JObject item in jb) { Dictionary dic = new Dictionary(); foreach (var itm in item.Properties()) { dic.Add(itm.Name, itm.Value.ToString()); } list.Add(dic); } LogService.CustomWriteLog("2:", DateTime.Now.ToString("yyyy-MM-dd")); int TrueCount = 0, SumCount = 0; LogService.CustomWriteLog("list.Count:" + list.Count, DateTime.Now.ToString("yyyy-MM-dd")); string sReturn = ""; oSystemParameter.ShowBill(ref sReturn); LogService.Write($"========================000.本次长度:" + list.Count); 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() == "") { LogService.CustomWriteLog("2.01:" + i, DateTime.Now.ToString("yyyy-MM-dd")); 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().Replace("'", "''"); 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 HPlanEndDate = 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(); var HEmpID = list[i]["HEmpID"].ToString(); var HWorkQty = list[i]["小时产能"].ToString(); var HSourceName = list[i]["生产资源"].ToString(); var HWorkQty_ProdLine = list[i]["产线总产能"].ToString(); LogService.CustomWriteLog("2.05:" + i, DateTime.Now.ToString("yyyy-MM-dd")); ds = oCN.RunProcReturn($"select 1 from Sc_WorkBillSortBillMain with(nolock) where HMainSourceInterID={HMainSourceInterID}" + $" and HMainSourceEntryID={HMainSourceEntryID} and HICMOBillNo='{HICMOBillNo}' and HICMOEntrySEQ={HICMOEntrySEQ} and HSourceID={HSourceID} and ISNULL(HDeleteMan,'')=''", "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 string sql = $"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" + ",HICMOInterID,HICMOEntryID,HPlanEndDate,HEmpID,HWorkQty,HSourceName,HWorkQty_ProdLine)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}," + $"{HMainSourceInterID},{HMainSourceEntryID},'{HPlanEndDate}','{HEmpID}','{HWorkQty}','{HSourceName}','{HWorkQty_ProdLine}')"; //LogService.Write("sql:" + sql); //主表 oCN.RunProc(sql); } else { LogService.CustomWriteLog("2.0:" + i, DateTime.Now.ToString("yyyy-MM-dd")); HBillNo = list[i]["单据号"].ToString(); HInterID = int.Parse(list[i]["hmainid"].ToString()); 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(); var HWorkQty = list[i]["小时产能"].ToString(); var HProdTimes = list[i]["生产周期"].ToString(); var HReadyTimes = list[i]["开工余量"].ToString(); var HLastBeginDate = list[i]["最迟开工日期"].ToString(); var HLastEndDate = list[i]["最迟完工日期"].ToString(); var HOrderNeedQty = list[i]["订单需求数量"].ToString(); var HOrderCommitDate = list[i]["订单交货期"].ToString(); var HPlanEndDate = list[i]["计划结束日期"].ToString(); var HSplitQty = list[i]["拆单数量"].ToString(); var HSourceName = list[i]["生产资源"].ToString(); var HWorkQty_ProdLine = list[i]["产线总产能"].ToString(); var HOrderType = list[i]["订单类型"].ToString(); var HWorkShopID = list[i]["HWorkShopID"].ToString(); var HProdORGID = list[i]["HProdORGID"].ToString(); var HMaterID = list[i]["HMaterID"].ToString(); var HMaterName = list[i]["物料名称"].ToString(); var HMaterModel = list[i]["规格型号"].ToString().Replace("'", "''"); var HUnitID = list[i]["HUnitID"].ToString(); var HSeOrderBillQty = 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 HPlanQty = list[i]["计划数量"].ToString(); var HEmpID = list[i]["HEmpID"].ToString(); LogService.CustomWriteLog("2.1:" + i, DateTime.Now.ToString("yyyy-MM-dd")); 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 1 from Sc_WorkBillSortBillMain with(nolock) where HMainSourceInterID={HMainSourceInterID}" + $" and HMainSourceEntryID={HMainSourceEntryID} and HICMOBillNo='{HICMOBillNo}' and HICMOEntrySEQ={HICMOEntrySEQ} and HSourceID={HSourceID} and ISNULL(HDeleteMan,'') = ''", "Sc_WorkBillSortBillMain"); if (ds.Tables[0].Rows.Count > 1) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = $"第{i + 1}行生产资源有重复,请修改!"; objJsonResult.data = null; return objJsonResult; } LogService.CustomWriteLog("2.2:" + i, DateTime.Now.ToString("yyyy-MM-dd")); oCN.RunProc($"update wsm1 set HSourceID={(HSourceID == "" ? 0.ToString() : HSourceID)},HYX={HYX}" + $",HWorkQty={HWorkQty},HProdTimes={HProdTimes},HReadyTimes={HReadyTimes},HLastBeginDate='{HLastBeginDate}'" + $",HLastEndDate='{HLastEndDate}',HOrderNeedQty={HOrderNeedQty},HOrderCommitDate='{HOrderCommitDate}',HPlanEndDate='{HPlanEndDate}',HSplitQty={HSplitQty},HSourceName='{HSourceName}',HWorkQty_ProdLine={HWorkQty_ProdLine}" + $" from Sc_WorkBillSortBillMain wsm1 with(nolock) where HInterID={list[i]["hmainid"].ToString()} and HBillNo='{list[i]["单据号"].ToString()}'"); } oCN.RunProc($"update wsm2 set HDayPlanSumQty={list[i]["日计划数量总量"].ToString()} from Sc_WorkBillSortBillMain wsm2 with(nolock) where HInterID={(HInterID == 0 ? int.Parse(list[i]["hmainid"].ToString()) : HInterID)} and HBillNo='{(HBillNo == "" ? list[i]["单据号"].ToString() : HBillNo)}'"); LogService.Write($"========================1.排产主表处理用时:" + sw.Elapsed); int SumDay = 180; //动态六月之差 DateTime.Now.AddMonths(1).AddDays(-1).Subtract(DateTime.Now).Days; LogService.CustomWriteLog("2.3:" + i, DateTime.Now.ToString("yyyy-MM-dd")); for (int j = 0; j < SumDay; j++) { //主表子表都有数据 if (list[i][DateTime.Now.AddDays(j).ToString("yyyy-MM-dd")].ToString() != "" && Math.Round(double.Parse(list[i][DateTime.Now.AddDays(j).ToString("yyyy-MM-dd")].ToString()), 1) > 0) { 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()) , Math.Round(double.Parse(list[i][DateTime.Now.AddDays(j).ToString("yyyy-MM-dd")].ToString()), 1) , list[i]); if (objJsonResult.count == 1) { TrueCount += 1; } } //主表有数据 子表无数据 if (j == SumDay - 1 && SumCount == 0 && HInterID != 0 && HBillNo != "") { LogService.Write($"========================000.主表有数据 子表无数据:" + HBillNo + sw.Elapsed); //LogService.CustomWriteLog("3:"+ HInterID+","+ HBillNo, DateTime.Now.ToString("yyyy-MM-dd")); objJsonResult = AddBillSub(HInterID.ToString(), HBillNo, DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd").ToString()), 0, list[i]); } } LogService.Write($"========================2.排产子表处理用时:" + sw.Elapsed); LogService.CustomWriteLog("2.4:" + i, DateTime.Now.ToString("yyyy-MM-dd")); if (TrueCount != SumCount) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = objJsonResult.Message; objJsonResult.data = null; return objJsonResult; } } LogService.CustomWriteLog("2.5:", DateTime.Now.ToString("yyyy-MM-dd")); //同步日计划工单 oCN.RunProc("exec REALESE_SC_ICMOBILLSTATUS_TMP"); LogService.CustomWriteLog("2.6:", DateTime.Now.ToString("yyyy-MM-dd")); sw.Stop();//结束计时 LogService.Write($"========================3.排产订单总用时:" + sw.Elapsed); 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, double HQTY, Dictionary dic) { Stopwatch ss = new Stopwatch();//计时器 ss.Start();//开始计时 string sReturn = ""; oSystemParameter.ShowBill(ref sReturn); //获取表格数据 ds = oCN.RunProcReturn($"select * from h_v_JIT_Sc_WorkBillSortBill_PC with(nolock) where 单据号='{HBillNo}'", "h_v_JIT_Sc_WorkBillSortBill_PC"); LogService.Write($"========================2.1.子表数据查询用时:" + ss.Elapsed); int count = 0; if (ds.Tables[0].Rows.Count > 0) { LogService.Write($"========================2.2.子表更新前用时:" + ss.Elapsed); //判断子表是否有当日的日期以及日期所对应的数量 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (DateTime.Parse(ds.Tables[0].Rows[i]["日计划生产日期"].ToString()) == date) { oCN.RunProc($"update wss1 set HMasterDate='{date}',HQty={HQTY},HUseTimes={(dic["产线总产能"] == "0" ? 0 : HQTY / float.Parse(dic["产线总产能"].ToString()))}" + $" from Sc_WorkBillSortBillSub wss1 with(nolock) where HInterID={HInterID} and HEntryID={ds.Tables[0].Rows[i]["hsubid"].ToString()}"); count += 1; } } LogService.Write($"========================2.2.子表更新后用时:" + ss.Elapsed); } if (count != 1) { //插入子表 oCN.RunProc("insert into Sc_WorkBillSortBillSub(HInterID,HSEQ," + "HMasterDate,HQty,HUseTimes)" + $"values({HInterID}, {(ds.Tables[0].Rows.Count + 1)}," + $" '{date}', {HQTY},{ ((dic["产线总产能"] == "0.0000" ? 0 : dic["产线总产能"] == "0" ? 0 : HQTY / float.Parse(dic["产线总产能"].ToString())).ToString() == "非数字" ? 0 : dic["产线总产能"] == "0.0000" ? 0 : dic["产线总产能"] == "0" ? 0 : HQTY / float.Parse(dic["产线总产能"].ToString())) })"); LogService.Write($"========================2.2.子表插入前用时:" + ss.Elapsed); } //把排产订单完工日期反写到金蝶销售订单上 string sql = "exec h_p_JIT_UpdateEndDateToERP " + HInterID + ",'" + HBillNo + "'"; oCN.RunProc(sql); LogService.Write($"========================2.3.子表存储过程调用耗时:" + ss.Elapsed); ss.Stop();//结束计时 LogService.Write($"========================2.4.子表保存总用时:" + ss.Elapsed); objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = null; objJsonResult.data = null; return objJsonResult; } #endregion #region 获取车间排产锁定天数 [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBillHinterIDList_TK")] [HttpGet] public object JIT_DayPlanPlatFormBillHinterIDList_TK(string sWhere, int num) { try { ds = oCN.RunProcReturn($"select isnull(DATEDIFF(DAY,GETDATE(),CONVERT(varchar(10),max(HMasterDate),20))+1,0) DayNum from Sc_WorkBillSortBillMain a with(nolock) left join Sc_WorkBillSortBillSub b with(nolock) on a.HInterID = b.HInterID where a.HWorkShopID = {sWhere} and HMasterDate>=CONVERT(varchar(10),GETDATE(),20) and HLockedSub=1", "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/PublishDate")] [HttpGet] public object PublishDate(string HDeptID) { try { //更新生产订单的最早开工日期 oCN.RunProc($"exec h_p_JIT_PublishDate '{HDeptID}'"); objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "订单完工日期发布成功!"; 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 } }