wtt
7 天以前 53e66c1f3884ac21f1034d0d996d6e3b630112f9
WebAPI/Controllers/SCGL/Èռƻ®¹ÜÀí/JIT_DayPlanPlatFormImportController.cs
@@ -1240,6 +1240,337 @@
        #endregion
        #region å®å·¥ï¼šæ—¥ç”Ÿäº§è®¡åˆ’ æ–‡ä»¶ä¸Šä¼ 
        [Route("JIT_DayPlanPlatForm_DayImport/JIT_DayPlanPlatForm_DayImport_DR")]
        [HttpPost]
        public object JIT_DayPlanPlatForm_DayImport_DR()
        {
            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");
                //将导入文件中的列添加到tb2中
                for (int i = 0; i < ExcelDs.Tables[0].Columns.Count; i++)
                {
                    tb2.Columns.Add(ExcelDs.Tables[0].Rows[0][i].ToString());
                }
                //模板缺少列 ä½†éœ€è¦ä»Žæ•°æ®åº“中查询出来显示在页面的字段
                tb2.Columns.Add("HWorkShopID", typeof(Int32));                                  //生产车间
                tb2.Columns.Add("生产车间编码*", typeof(string));                               //日计划单据主内码
                tb2.Columns.Add("hmainid", typeof(Int32));                                      //日计划单据主内码
                tb2.Columns.Add("单据号", typeof(string));                                      //日计划单号
                tb2.Columns.Add("HSourceID", typeof(Int32));                                    //生产资源id
                tb2.Columns.Add("ICMOBillHInterID", typeof(Int32));                             //生产订单主id
                tb2.Columns.Add("ICMOBillHEntryID", typeof(Int32));                             //生产订单子id
                tb2.Columns.Add("HMaterID", typeof(Int32));                                     //物料ID
                //获取系统参数
                string Ret = "";
                oSystemParameter.ShowBill(ref Ret);
                //将导入文件中的数据添加到tb2中
                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 += "生产订单号不能为空;\n";
                    if (tb2.Rows[i]["生产订单明细行号*"].ToString() == "")
                        error += "生产订单明细行号不能为空;";
                    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;
                    }
                    //判断导入文件中的生产订单是否存在
                    string sql1 = "";
                    if (oSystemParameter.omodel.WMS_CampanyName == "宝工")
                    {
                        sql1 = "select * from h_v_JIT_ICMOBillList_DR where å•据号='" + tb2.Rows[i]["生产订单号*"].ToString() + "' and äº§å“ä»£ç ='" + tb2.Rows[i]["物料编码*"].ToString() + "' and æ˜Žç»†è¡Œå·=" + tb2.Rows[i]["生产订单明细行号*"].ToString();
                    }
                    else
                    {
                        sql1 = string.Format("select * from h_v_JIT_ICMOBillList_DR where å•据号='{0}' and äº§å“ä»£ç ='{1}' and æ˜Žç»†è¡Œå·={2} ",
                        tb2.Rows[i]["生产订单号*"].ToString(), tb2.Rows[i]["物料编码*"].ToString(), tb2.Rows[i]["生产订单明细行号*"].ToString());
                    }
                    ds = oCN.RunProcReturn(sql1, "h_v_JIT_ICMOBillList_DR");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        error += $"没有生产订单信息,生产订单号:{ tb2.Rows[i]["生产订单号*"].ToString()}" +
                               $",生产订单明细行号:{ 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]["HWorkShopID"] = ds.Tables[0].Rows[0]["HDeptID"].ToString();        //生产车间内码
                        tb2.Rows[i]["生产车间编码*"] = ds.Tables[0].Rows[0]["生产车间代码"].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
                        tb2.Rows[i]["HWorkShopID"] = ds.Tables[0].Rows[0]["HDeptID"].ToString();
                        tb2.Rows[i]["HMaterID"] = ds.Tables[0].Rows[0]["HMaterID"].ToString();
                        tb2.Rows[i]["物料名称"] = ds.Tables[0].Rows[0]["产品名称"].ToString();
                        tb2.Rows[i]["规格型号"] = ds.Tables[0].Rows[0]["规格型号"].ToString();
                    }
                    //获取工单排产信息
                    ds = oCN.RunProcReturn("select * from h_v_JIT_WorkBillSortBillList_DR " +
                        $"where  ç”Ÿäº§è®¢å•号='{tb2.Rows[i]["生产订单号*"].ToString()}'  and ç”Ÿäº§è®¢å•明细行号='{tb2.Rows[i]["生产订单明细行号*"].ToString()}' " +
                        $"and ç‰©æ–™ä»£ç ='{tb2.Rows[i]["物料编码*"].ToString()}' " +
                        $"and ç”Ÿäº§èµ„源编码='{tb2.Rows[i]["生产资源编码*"].ToString()}' ", "h_v_JIT_WorkBillSortBillList_DR");
                    //组织编码='{tb2.Rows[i]["组织编码*"].ToString()}' and
                    //and ç”Ÿäº§èµ„源编码='{tb2.Rows[i]["生产资源编码*"].ToString()}'
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        tb2.Rows[i]["hmainid"] = ds.Tables[0].Rows[0]["hmainid"].ToString();
                        tb2.Rows[i]["单据号"] = ds.Tables[0].Rows[0]["单据号"].ToString();
                        tb2.Rows[i]["HSourceID"] = ds.Tables[0].Rows[0]["HSourceID"].ToString();
                    }
                    else
                    {
                        error += $"没有排产信息,生产订单号:{ tb2.Rows[i]["生产订单号*"].ToString()},生产车间编码:{tb2.Rows[i]["生产车间编码*"].ToString()}" +
                               $",生产订单明细行号:{ tb2.Rows[i]["生产订单明细行号*"].ToString()},物料编码:{ tb2.Rows[i]["物料编码*"].ToString()}" +
                               $",生产资源编码:{tb2.Rows[i]["生产资源编码*"].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_DayPlanPlatForm_DayImport/JIT_DayPlanPlatForm_DayImport_btnSave")]
        [HttpPost]
        public object JIT_DayPlanPlatForm_DayImport_btnSave([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            oCN.BeginTran();
            //保存主表
            objJsonResult = AddBillMain_DayImport(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_DayImport(string msg1)
        {
            string err = "";
            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;
                }
                //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++)
                {
                    var HInterID = list[i]["hmainid"].ToString();
                    var HBillNo = list[i]["单据号"].ToString();
                    var HSourceID = list[i]["HSourceID"].ToString();
                    var HSourceNumber = list[i]["生产资源编码*"].ToString();
                    var HMainSourceInterID = list[i]["ICMOBillHInterID"].ToString();
                    var HMainSourceEntryID = list[i]["ICMOBillHEntryID"].ToString();
                    var HICMOBillNo = list[i]["生产订单号*"].ToString();
                    var HICMOEntrySEQ = list[i]["生产订单明细行号*"].ToString();
                    var HMaterID = list[i]["HMaterID"].ToString();
                    var HMaterNumber = list[i]["物料编码*"].ToString();
                    var HMaterName = list[i]["物料名称"].ToString();
                    var HMaterModel = list[i]["规格型号"].ToString();
                    // ç»Ÿè®¡æ€»å¤©æ•°
                    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);
                        }
                    }
                    int SumDay = uniqueDates.Count; //动态两月之差 DateTime.Now.AddMonths(1).AddDays(-1).Subtract(DateTime.Now).Days;
                    foreach(var date in uniqueDates)
                    {
                        //主表子表都有数据
                        if (list[i][date.ToString("yyyy-MM-dd")].ToString() != "")
                        {
                            int HQty = int.Parse(list[i][date.ToString("yyyy-MM-dd")].ToString());
                            //保存子表
                            objJsonResult = AddBillSub_DayImport(HInterID, HBillNo, date, HQty);
                            if (objJsonResult.code == "0")
                            {
                                err += "【生产订单:" + HICMOBillNo + ";行号:" + HICMOEntrySEQ + ";物料编码:" + HMaterNumber + ";物料名称:" + HMaterName + ";生产资源编码:" + HSourceNumber + ";日期:" + date.ToString("yyyy-MM-dd") + ";数量:" + HQty + "】:" + objJsonResult.Message + ";";
                            }
                        }
                    }
                }
                if (err.Length > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = err;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                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_DayImport(string HInterID, string HBillNo, DateTime date, int HQTY)
        {
            //将日计划工单中该日期的记录作废
            string sql_delete = "update Sc_WorkBillSortBillSub set HDeleteFlag = 1 where HInterID = " + HInterID + " and convert(varchar(100),HMasterDate,23) = '" + date.ToString("yyyy-MM-dd") + "'";
            oCN.RunProc(sql_delete);
            //将导入的数据生成该日计划工单指定日期的记录
            string sql_insert = "insert into Sc_WorkBillSortBillSub(HInterID,HSEQ,HMasterDate,HQty,HDayImportFlag) " +
                "values(" +
                "" + HInterID + "" +
                "," + 0 + "" +
                ",'" + date.ToString("yyyy-MM-dd") + "'" +
                "," + HQTY + "" +
                "," + 1 + "" +
                ")";
            oCN.RunProc(sql_insert);
            objJsonResult.code = "1";
            objJsonResult.count = 1;
            objJsonResult.Message = null;
            objJsonResult.data = null;
            return objJsonResult;
        }
        #endregion
        #region ç”Ÿäº§æ—¥è®¡åˆ’ æ–‡ä»¶ä¸Šä¼ (凯贝)
        [Route("JIT_DayPlanPlatFormImport/JIT_DayPlanPlatFormImport_KB")]
        [HttpPost]