wtt
2025-05-26 d6e06131e35528e02e34de459565c26693a643a7
WebAPI/Controllers/SCGL/Èռƻ®¹ÜÀí/JIT_DayPlanPlatFormImportController.cs
@@ -504,7 +504,7 @@
        }
        #endregion
        #region ç”Ÿäº§æ—¥è®¡åˆ’(三升导入) æ–‡ä»¶ä¸Šä¼ 
        #region ç”Ÿäº§æ—¥è®¡åˆ’交期(三升导入) æ–‡ä»¶ä¸Šä¼ 
        [Route("JIT_DayPlanPlatFormImport/JIT_DayPlanPlatFormImport_SS")]
        [HttpPost]
        public object JIT_DayPlanPlatFormImport_SS()
@@ -608,7 +608,7 @@
                        tb2.Rows[i]["ICMOBillHEntryID"] = ds.Tables[0].Rows[0]["HEntryID"].ToString();//生产订单子id
                    }
                    //查找是否存在对应日计划工单
                    var dt = oCN.RunProcReturn($"select * from  h_v_JIT_Sc_WorkBillSortBillList where æºå•主内码 in({ds.Tables[0].Rows[0]["hmainid"].ToString()}) and  æºå•子内码 in({ds.Tables[0].Rows[0]["HEntryID"].ToString()})", "h_v_JIT_Sc_WorkBillSortBillList");
                    var dt = oCN.RunProcReturn($"select * from  h_v_JIT_Sc_WorkBillSortBillList_Left where æºå•主内码 in({ds.Tables[0].Rows[0]["hmainid"].ToString()}) and  æºå•子内码 in({ds.Tables[0].Rows[0]["HEntryID"].ToString()})", "h_v_JIT_Sc_WorkBillSortBillList");
                    if (dt.Tables[0].Rows.Count > 0)
                    {
                        tb2.Rows[i]["hmainid"] = dt.Tables[0].Rows[0]["hmainid"].ToString();
@@ -633,7 +633,7 @@
        }
        #endregion
        #region ç”Ÿäº§æ—¥è®¡åˆ’三升 å¯¼å…¥(保存)
        #region ç”Ÿäº§æ—¥è®¡åˆ’三升交期 å¯¼å…¥(保存)
        [Route("JIT_DayPlanPlatFormImport/JIT_DayPlanPlatFormImport_btnSave_SS")]
        [HttpPost]
        public object JIT_DayPlanPlatFormImport_btnSave_SS([FromBody] JObject sMainSub)
@@ -759,8 +759,11 @@
                    {
                        var hmainid = list[i]["hmainid"].ToString();
                        var HPlanEndDate = list[i]["计划结束日期"].ToString();
                        //主表
                        oCN.RunProc($"update Sc_WorkBillSortBillMain set HPlanEndDate = '{HPlanEndDate}' where HInterID ={hmainid}" );
                        //更新交期
                        if (HPlanEndDate != "" )
                        {
                            oCN.RunProc($"update Sc_WorkBillSortBillMain set HPlanEndDate = '{HPlanEndDate}' where HInterID ={hmainid}");
                        }
                    }
                }
@@ -784,5 +787,402 @@
        }
        #endregion
        #region ç”Ÿäº§æ—¥è®¡åˆ’(三升导入) æ–‡ä»¶ä¸Šä¼ 
        [Route("JIT_DayPlanPlatFormImport/JIT_DayPlanPlatFormImport_SS_Sec")]
        [HttpPost]
        public object JIT_DayPlanPlatFormImport_SS_Sec()
        {
            try
            {
                //获取文件名称
                var file = HttpContext.Current.Request.Files[0];
                //获取文件物理路径
                string ExcelPath = HttpContext.Current.Server.MapPath("~/" + file.FileName);
                //保存文件
                file.SaveAs(ExcelPath);
                NpoiHelper np = new NpoiHelper();
                DataSet ExcelDs = np.ReadExcel(ExcelPath, 1, 1, "0");
                //删除文件
                File.Delete(ExcelPath);
                //创建临时表
                DataTable tb2 = new DataTable("dt2");
                //添加列名
                for (int i = 0; i < ExcelDs.Tables[0].Columns.Count; i++)
                {
                    tb2.Columns.Add(ExcelDs.Tables[0].Rows[0][i].ToString());
                }
                //模板缺少列 ä½†éœ€è¦ä»Žæ•°æ®åº“中查询出来显示在页面的字段
                tb2.Columns.Add("hmainid", typeof(Int32));//计划单id
                tb2.Columns.Add("HMaterID", typeof(Int32));
                tb2.Columns.Add("HSourceID", typeof(Int32));//生产资源
                tb2.Columns.Add("ICMOBillHInterID", typeof(Int32));
                tb2.Columns.Add("ICMOBillHEntryID", typeof(Int32));
                //获取系统参数
                string Ret = "";
                oSystemParameter.ShowBill(ref Ret);
                LogService.Write("excel值:" + ExcelDs.Tables[0].Rows[1][5].ToString());//2024-11-26
                //添加数据
                for (int i = 1; i < ExcelDs.Tables[0].Rows.Count; i++)
                {
                    DataRow row = tb2.NewRow();
                    for (int j = 0; j < ExcelDs.Tables[0].Columns.Count; j++)
                    {
                        row[j] = ExcelDs.Tables[0].Rows[i][j].ToString();
                    }
                    //如果表格第i行的第一列为空,则判断为这一行的数据为空,跳出循环并且不把数据写入 tb2
                    if (ExcelDs.Tables[0].Rows[i][0].ToString() == "" && ExcelDs.Tables[0].Rows[i][1].ToString() == "")
                    {
                        continue;
                    }
                    else
                    {
                        tb2.Rows.Add(row);
                    }
                }
                var error = "";
                var ErrorResult = "";
                //查询生产订单表判断有没有生产订单信息
                for (int i = 0; i < tb2.Rows.Count; i++)
                {
                    //if (tb2.Rows[i]["计划结束日期"].ToString() == "")
                    //    error += "计划结束日期不能为空;";
                    if (tb2.Rows[i]["客户订单号"].ToString() == "")
                        error += "客户订单号不能为空;";
                    if (tb2.Rows[i]["物料编码"].ToString() == "")
                        error += "物料编码不能为空;";
                    if (error.Length > 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = ErrorResult += $"Excel模板存在错误,行数{i + 1}:{error}\r\n"; ;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    //设置对应生产资源
                    if (tb2.Rows[i]["生产资源编码"].ToString() != "")
                    {
                        //生产资源
                        ds = oCN.RunProcReturn($"select * from Gy_Source with(nolock) where HNumber='{tb2.Rows[i]["生产资源编码*"].ToString()}'", "Gy_Source");
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            tb2.Rows[i]["HSourceID"] = ds.Tables[0].Rows[0]["HItemID"].ToString();
                        }
                        else
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{i + 1}行,生产资源查无数据!";
                            objJsonResult.data = tb2;
                            return objJsonResult;
                        }
                    }
                    //查找是否存在对应生产订单
                    string sql1 = string.Format("select * from h_v_IF_ICMOBillList where å®¢æˆ·è®¢å•号='{0}' and äº§å“ä»£ç ='{1}' and çŠ¶æ€ in ('计划确认','开工')",
                        tb2.Rows[i]["客户订单号"].ToString(), tb2.Rows[i]["物料编码"].ToString());
                    ds = oCN.RunProcReturn(sql1, "h_v_IF_ICMOBillList");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        error += $"没有符合条件得生产订单信息,客户订单号:{ tb2.Rows[i]["客户订单号"].ToString()},产品代码:{tb2.Rows[i]["物料编码"].ToString()}";
                    }
                    if (error.Length > 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = ErrorResult += $"Excel模板存在错误,行数{i + 1}:{error}\r\n"; ;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    else
                    {
                        tb2.Rows[i]["HMaterID"] = ds.Tables[0].Rows[0]["HMaterID"].ToString();
                        tb2.Rows[i]["ICMOBillHInterID"] = ds.Tables[0].Rows[0]["hmainid"].ToString();//生产订单主id
                        tb2.Rows[i]["ICMOBillHEntryID"] = ds.Tables[0].Rows[0]["HEntryID"].ToString();//生产订单子id
                    }
                    //查找是否存在对应日计划工单
                    var dt = oCN.RunProcReturn($"select * from  h_v_JIT_Sc_WorkBillSortBillList_Left where æºå•主内码 in({ds.Tables[0].Rows[0]["hmainid"].ToString()}) and  æºå•子内码 in({ds.Tables[0].Rows[0]["HEntryID"].ToString()})", "h_v_JIT_Sc_WorkBillSortBillList");
                    if (dt.Tables[0].Rows.Count > 0)
                    {
                        tb2.Rows[i]["hmainid"] = dt.Tables[0].Rows[0]["hmainid"].ToString();
                    }
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = ErrorResult;
                objJsonResult.data = tb2;
                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_DayPlanPlatFormImport/JIT_DayPlanPlatFormImport_btnSave_SS_Sec")]
        [HttpPost]
        public object JIT_DayPlanPlatFormImport_btnSave_SS_Sec([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            oCN.BeginTran();
            //保存主表
            objJsonResult = AddBillMain_SS_Sec(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_SS_Sec(string msg1)
        {
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string msg3 = sArray[1].ToString();
            string user = sArray[2].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;
                }
                //表头字段
                omdelMian = Newtonsoft.Json.JsonConvert.DeserializeObject<Sc_WorkBillSortBillMain>(msg2);
                //JSON序列化转换字典集合
                List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
                List<object> jb = JsonConvert.DeserializeObject<List<object>>(msg3);
                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);
                }
                for (int i = 0; i < list.Count; i++)
                {
                    int TrueCount = 0, SumCount = 0;//计数器判断子表导入数量和实际数量是否有出入
                    long HInterID = 0;
                    var HBillNo = "";
                    if (list[i]["hmainid"].ToString() == "")
                    {
                        HBillNo = DBUtility.ClsPub.CreateBillCode_Prod(BillType, ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号
                        HInterID = DBUtility.ClsPub.CreateBillID_Prod(BillType, ref DBUtility.ClsPub.sExeReturnInfo);//获得一个新的id
                        var HMainSourceInterID = list[i]["ICMOBillHInterID"].ToString();
                        var HMainSourceEntryID = list[i]["ICMOBillHEntryID"].ToString();
                        //获取对应订单信息
                        ds = oCN.RunProcReturn($"exec h_p_ICMOBillList_PrimarySubTable '{HMainSourceInterID}','{HMainSourceEntryID}'", "h_p_ICMOBillList_PrimarySubTable");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"没找到对应符合条件得订单信息,行数{i + 1}\r\n"; ;
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        var HICMOBillNo = ds.Tables[0].Rows[0]["生产订单号"].ToString();
                        var HICMOEntrySEQ = ds.Tables[0].Rows[0]["生产订单明细行号"].ToString();
                        var HProdORGID = ds.Tables[0].Rows[0]["HProdORGID"].ToString();
                        var HWorkShopID = ds.Tables[0].Rows[0]["HWorkShopID"].ToString();
                        var HSourceID = (list[i]["HSourceID"].ToString() != "0" && list[i]["HSourceID"].ToString() != "") ? list[i]["HSourceID"].ToString() : ds.Tables[0].Rows[0]["HSourceID"].ToString();//生产资源
                        var HWorkQty = DBUtility.ClsPub.isDoule((list[i]["小时产能"].ToString() != "0" && list[i]["小时产能"].ToString() != "") ? list[i]["小时产能"].ToString(): ds.Tables[0].Rows[0]["小时产能"].ToString());//小时产能
                        var HYX = (list[i]["优先级"].ToString()!="0" && list[i]["优先级"].ToString() != "")? list[i]["优先级"].ToString():(i+1).ToString();//设置优先级
                        var HEmpID = ds.Tables[0].Rows[0]["操作工"].ToString();
                        var HMaterID = ds.Tables[0].Rows[0]["HMaterID"].ToString();
                        var HMaterName = ds.Tables[0].Rows[0]["物料名称"].ToString();
                        var HMaterModel = ds.Tables[0].Rows[0]["规格型号"].ToString();
                        var HUnitID = ds.Tables[0].Rows[0]["HUnitID"].ToString();
                        var HBatchNo = "";//批次号
                        var HSeOrderBillQty = ds.Tables[0].Rows[0]["销售订单数量"].ToString();
                        var HPlanQty = ds.Tables[0].Rows[0]["计划数量"].ToString();
                        var HCompleteQty = "";//总齐套数量
                        var HOrderType = omdelMian.HOrderType;
                        var HDate = omdelMian.HDate;
                        var HSeOrderBillNo = ds.Tables[0].Rows[0]["销售订单号"].ToString();
                        var HPlanEndDate = list[i]["计划结束日期"].ToString() != "" ? list[i]["计划结束日期"].ToString():ds.Tables[0].Rows[0]["计划结束日期"].ToString();
                        var HLockOrder = 1;//锁定工单
                        //主表
                        oCN.RunProc("insert into Sc_WorkBillSortBillMain(HInterID,HBillNo,HYear,HPeriod,HBillType," +
                            "HBillSubType,HDate,HBillStatus,HMaker,HMakeDate,HICMOBillNo,HOrderType," +
                            "HICMOEntrySEQ,HWorkShopID,HSourceID, HYX,HWorkQty, HEmpID," +
                            " HMaterID, HMaterName, HMaterModel, HUnitID, HBatchNo," +
                            " HSeOrderBillQty, HPlanQty, HCompleteQty,HPreparatDate," +
                            "HMainSourceInterID,HMainSourceEntryID,HICMOInterID,HICMOEntryID,HProdORGID,HICMOInterID_Sec,HICMOEntryID_Sec" +
                            ",HSeOrderBillNo,HPlanEndDate,HLockOrder)values" +
                            $"({HInterID},'{HBillNo}',{DateTime.Now.Year},{DateTime.Now.Month},'{BillType}'," +
                            $"'{BillType}',GETDATE(),1,'{user}','{HDate}','{HICMOBillNo}','{HOrderType}'," +
                            $"{(HICMOEntrySEQ == "" ? 0.ToString() : HICMOEntrySEQ)},{(HWorkShopID == "" ? 0.ToString() : HWorkShopID)},{(HSourceID == "" ? 0.ToString() : HSourceID)}, {(HYX == "" ? 0.ToString() : HYX)},{(HWorkQty)}, {(HEmpID == "" ? 0.ToString() : HEmpID)}," +
                            $" {(HMaterID == "" ? 0.ToString() : HMaterID)}, '{HMaterName}', '{HMaterModel}', {(HUnitID == "" ? 0.ToString() : HUnitID)}, '{HBatchNo}'," +
                            $" {(HSeOrderBillQty == "" ? 0.ToString() : HSeOrderBillQty)}, {(HPlanQty == "" ? 0.ToString() : HPlanQty)}, {(HCompleteQty == "" ? 0.ToString() : HCompleteQty)},getdate()," +
                            $" {HMainSourceInterID},{HMainSourceEntryID},{HMainSourceInterID},{HMainSourceEntryID},{HProdORGID}, {HMainSourceInterID},{HMainSourceEntryID}" +
                            $",'{HSeOrderBillNo}','{HPlanEndDate}',{HLockOrder})");
                    }//如果已存在日计划工单更新对应生产资源小时产能
                    else
                    {
                        var hmainid = list[i]["hmainid"].ToString();
                        var HWorkQty = list[i]["小时产能"].ToString();
                        var HSourceID = list[i]["HSourceID"].ToString();
                        var HYX = list[i]["优先级"].ToString();
                        var HPlanEndDate = list[i]["计划结束日期"].ToString();
                        //更新小时产能
                        if (HWorkQty != ""&& HWorkQty != "0")
                        {
                            oCN.RunProc($"update Sc_WorkBillSortBillMain set HWorkQty = '{HWorkQty}' where HInterID ={hmainid}");
                        }
                        //更新生产资源
                        if (HSourceID != "" && HSourceID != "0")
                        {
                            oCN.RunProc($"update Sc_WorkBillSortBillMain set HSourceID = '{HSourceID}' where HInterID ={hmainid}");
                        }
                        //更新优先级
                        if (HYX != "" && HYX != "0")
                        {
                            oCN.RunProc($"update Sc_WorkBillSortBillMain set HYX = '{HYX}' where HInterID ={hmainid}");
                        }
                        //更新计划结束日期
                        if (HPlanEndDate != "")
                        {
                            oCN.RunProc($"update Sc_WorkBillSortBillMain set HPlanEndDate = '{HPlanEndDate}' where HInterID ={hmainid}");
                        }
                    }
                    int SumDay = 31; //动态两月之差 DateTime.Now.AddMonths(1).AddDays(-1).Subtract(DateTime.Now).Days;
                    // ç»Ÿè®¡æ€»å¤©æ•°
                    HashSet<DateTime> uniqueDates = new HashSet<DateTime>();
                    foreach (var key in list[i].Keys)
                    {
                        if (DateTime.TryParseExact(key, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date))
                        {
                            uniqueDates.Add(date);
                        }
                    }
                    SumDay = uniqueDates.Count;
                    //添加子表
                    for (int j = 0; j < SumDay; j++)
                    {
                        LogService.Write("1:" + list[i][DateTime.Now.AddDays(j).ToString("yyyy-MM-dd")].ToString());
                        LogService.Write("2:" + DateTime.Now.AddDays(j).ToString("yyyy-MM-dd"));
                        //主表子表都有数据
                        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_SS(HInterID == 0 ? list[i]["hmainid"].ToString() : HInterID.ToString()
                                 , 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 == SumDay - 1 && SumCount == 0 && HInterID != 0 && HBillNo != "")
                        {
                            objJsonResult = AddBillSub_SS(HInterID.ToString(),DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd").ToString()), 0);
                        }
                    }
                }
                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_SS(string HInterID,DateTime date, int HQTY)
        {
            //获取表格数据
            ds = oCN.RunProcReturn($"select * from  h_v_JIT_Sc_WorkBillSortBillList where hmainid='{HInterID}'", "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
    }
}