王 垚
2021-02-04 3bdc41a3c5a0857b04c1ee4d6f7cc515d1983820
src/BLL/Demo.BillView/PRD/Pro_ScDayImport.cs
@@ -27,6 +27,7 @@
    [HotUpdate]
    public class Pro_ScDayImport : AbstractDynamicFormPlugIn
    {
        CloudClient cloudClient = new CloudClient("http://localhost/K3Cloud/");
        private string fileFullPath = string.Empty;
        //public override void BarItemClick(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.BarItemClickEventArgs e)
        //{
@@ -54,6 +55,7 @@
            base.AfterBarItemClick(e);
            if (e.BarItemKey.ToUpper().Equals("TBIMPORT"))
            {
                LogService.WriteAsync("导入功能:");
                ImportData();
            }
@@ -66,7 +68,8 @@
                //// 下载地址
                //string downloadUrl = "FileUpLoadServices/download.aspx";
                //updateControl.SetCustomPropertyValue("ExDownLoadUrl", downloadUrl);
                LogHelper.Info("222222");
                LogService.WriteAsync("上传功能:");
                ImportDataSC(fileFullPath);
            }
        }
@@ -104,14 +107,38 @@
                {
                    DataSet ds = helper.ReadFromFile(fileFullPath, 1);
                    DataTable dt = ds.Tables[0];
                    LogService.WriteAsync("导入功能:dt行数:" + dt.Rows.Count);
                    DateTime date = Convert.ToDateTime(this.Model.GetValue("F_Paez_Date"));
                    if (dt.Rows.Count > 0)
                    {
                        //Entity entity = this.View.BillBusinessInfo.GetEntity("FEntity");
                        //DynamicObjectCollection rows = this.Model.GetEntityDataObject(entity);
                        this.Model.DeleteEntryData("FEntity");
                        //验证导入模板
                        string ErrorResult = "";
                        string error = "";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            error = "";
                            if (dt.Rows[i][1].ToString() == "")
                                error += "生产订单号不能为空;";
                            if (dt.Rows[i][2].ToString() == "")
                                error += "生产组织不能为空;";
                            if (dt.Rows[i][3].ToString() == "")
                                error += "生产车间不能为空;";
                            if (dt.Rows[i][5].ToString() == "")
                                error += "产品代码不能为空;";
                            if (dt.Rows[i][13].ToString() == "")
                                error += "交货日期不能为空;";
                            if (dt.Rows[i][14].ToString() == "")
                                error += "订单等级不能为空;";
                            if (error.Length > 0)
                                ErrorResult += $"Excel模板存在错误,行数{i + 1}:{error}\r\n";
                        }
                        if (ErrorResult.Length > 0)
                        {
                            this.View.ShowErrMessage(ErrorResult);
                            return;
                        }
                        this.Model.DeleteEntryData("FEntity");
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            //根据生产订单号和物料查询是否存在明细
@@ -135,12 +162,12 @@
                            string fDayPlanBillNo = "";
                            sql = string.Format(@"select a.FID,FBILLNO,FSCOrderNo,FHMaterID,FHSourceID from Sc_WorkBillSortBillMain a JOIN T_BD_MATERIAL b ON a.FHMaterID = b.FMATERIALID 
left join T_ENG_WORKCENTER c on a.FHSourceID=c.fid 
 where FSCOrderNo ='{0}' and b.FNUMBER ='{1}' and isnull(c.FNUMBER,'') ='{2}' ", dt.Rows[i][1].ToString(), dt.Rows[i][5].ToString(), dt.Rows[i][4].ToString());
 where FSCOrderNo ='{0}' and b.FNUMBER ='{1}' and (isnull(c.FNUMBER,'') ='{2}' or (select count(*) from T_ENG_WORKCENTER where isnull(c.FNUMBER,'') ='{2}')=0)", dt.Rows[i][1].ToString(), dt.Rows[i][5].ToString(), dt.Rows[i][4].ToString());
                            DataTable dayPlanDt = DBServiceHelper.ExecuteDataSet(Context, sql).Tables[0];
                            if (dayPlanDt.Rows.Count > 0)
                            {
                                fDayPlanID = _dt.Rows[0]["FID"].ToString();
                                fDayPlanBillNo = _dt.Rows[0]["FBILLNO"].ToString();
                                fDayPlanID = dayPlanDt.Rows[0]["FID"].ToString();
                                fDayPlanBillNo = dayPlanDt.Rows[0]["FBILLNO"].ToString();
                            }
                            //DynamicObject row = new DynamicObject(entity.DynamicObjectType);
                            this.Model.CreateNewEntryRow("FEntity");
@@ -225,14 +252,26 @@
            try
            {
                LogService.WriteAsync("测试");
                string sql;
                DateTime date = Convert.ToDateTime(this.Model.GetValue("F_Paez_Date"));
                //获取单据体信息
                Entity entity = this.View.BillBusinessInfo.GetEntity("FEntity");
                //单据体信息转换为列表集合
                DynamicObjectCollection entityDataObjoct = this.View.Model.GetEntityDataObject(entity);
                List<string> sqlList = new List<string>();
                foreach (DynamicObject current in entityDataObjoct)
                {
                    if (Convert.ToString(current["FDayPlanID"]) == "0")
                        continue;
                    for (int i = 0; i <= 60; i++)
                    {
                        JObject jsonFPOOrderEntry = new JObject();
                        //重复导入的更新掉计划数量以及更新齐套状态为空
                        sql = string.Format($"update SC_WORKBILLSORTBILLSub set FHQTY ='{Convert.ToString(current["FT" + "" + i + ""])}',FCOMPLETE='' where FID ={Convert.ToString(current["FDayPlanID"])} and FHMASTERDATE ={date.AddDays(i).ToShortDateString()} and FHLOCKEDSUB = 0");
                        sqlList.Add(sql);
                    }
                }
                int res = DBServiceHelper.ExecuteBatch(Context, sqlList);
                string _result = "";
                foreach (DynamicObject current in entityDataObjoct)
                {
@@ -251,24 +290,20 @@
                    this.View.ShowErrMessage(_result);
                    return;
                }
                foreach (DynamicObject current in entityDataObjoct)
                {
                    if (Convert.ToInt32(current["FNoScheduled"]) < 0)
                        _result += "序号:" + Convert.ToString(current["SEQ"]) + ",日计划总数量超出生产订单数量";
                }
                if (_result != "")
                {
                    this.View.ShowErrMessage(_result);
                    return;
                }
                foreach (DynamicObject current in entityDataObjoct)
                {
                    if (string.IsNullOrEmpty(Convert.ToString(current["FSalOrderNo"])) || string.IsNullOrEmpty(Convert.ToString(current["FSCOrderNo"])))
                        continue;
                    //根据生产订单号和物料查询是否存在明细
                    sql = string.Format(@"/*dialect*/SELECT T1.FBILLNO,T2.* FROM T_PRD_MO T1
                                LEFT JOIN T_PRD_MOENTRY T2 ON T1.FID = T2.FID
@@ -277,13 +312,11 @@
                    dt = DBServiceHelper.ExecuteDataSet(Context, sql).Tables[0];
                    if (dt.Rows.Count == 0)
                        continue;
                    string FID = "";//FID 源单主内码
                    string FENTRYID = "";//FENTRYID 源单子内码
                    string FLOT = "";//FLOT  批次
                    string FQTY = "";//FQTY 生产订单数量
                    string FBILLNO = "";//FBILLNO 原单号 
                    if (dt.Rows.Count > 0)
                    {
                        FID = dt.Rows[0]["FID"].ToString();
@@ -296,10 +329,9 @@
                    jsonRoot.Add("Creator", "");
                    jsonRoot.Add("NeedUpDateFields", new JArray());
                    jsonRoot.Add("NeedReturnFields", new JArray());
                    jsonRoot.Add("IsDeleteEntry", "true");
                    jsonRoot.Add("IsDeleteEntry", "false");
                    jsonRoot.Add("SubSystemId", "");
                    jsonRoot.Add("IsVerifyBaseDataField", "");
                    JObject jsonModel = new JObject();
                    jsonModel.Add("FPreparatDate", date.ToString());//编制日期
                    jsonModel.Add("FSCOrderNo", current["FSCOrderNo"].ToString()); //生产订单号FSCOrderNo
@@ -313,18 +345,12 @@
                    jsonModel.Add("FPrdBillNo", FBILLNO); ;//源单编号
                    jsonModel.Add("FPRDMOMAINID", FID); ;//源单主内码
                    jsonModel.Add("FPRDMOENTYID", FENTRYID); ;//源单子内码
                    jsonModel.Add("FOrderQuantity", current["FOrderQuantity"].ToString()); ;//生产订单数量
                    jsonModel.Add("FDayPlanQuantity", current["FDayPlanQuantity"].ToString()); ;//日计划数量
                    jsonModel.Add("FNoScheduled", current["FNoScheduled"].ToString()); ;//未排数量
                    JObject jsonFHMaterID = new JObject();
                    jsonFHMaterID.Add("FNumber", (current["FMatrailId"] as DynamicObject)["Number"].ToString());
                    jsonModel.Add("FHMaterID", jsonFHMaterID);//FHMaterID 物料
                    if (Convert.ToString(current["FORGID_Id"]) != "0")
                    {
                        JObject jsonFHPRDORGID = new JObject();
@@ -338,7 +364,6 @@
                        jsonFProductWorkShopId.Add("FNumber", (current["FResourcesId"] as DynamicObject)["Number"].ToString());
                        jsonModel.Add("FHSourceID", jsonFProductWorkShopId);//FHSourceID  生产车间
                    }
                    if (Convert.ToString(current["FProductWorkShopId_Id"]) != "0")
                    {
                        JObject jsonFResourcesId = new JObject();
@@ -346,28 +371,23 @@
                        jsonModel.Add("FHWorkShopID", jsonFResourcesId);//FHWorkShopID  生产资源
                    }
                    JArray Entry = new JArray();
                    for (int i = 0; i <= 60; i++)
                    {
                        JObject jsonFPOOrderEntry = new JObject();
                        jsonFPOOrderEntry.Add("FHMasterDate", date.AddDays(i).ToShortDateString()); ;//主日期
                        jsonFPOOrderEntry.Add("FHQty ", Convert.ToString(current["FT" + "" + i + ""])); ;//日计划数量
                        jsonFPOOrderEntry.Add("FColumn", "FT" + "" + i + ""); ;//对应的列头
                                                                               //jsonFPOOrderEntry.Add("FHMainSourceBillType ", "生产订单"); ;//源单类型
                                                                               //jsonFPOOrderEntry.Add("FHMainSourceBillNo", FBILLNO); ;//源单编号
                                                                               //jsonFPOOrderEntry.Add("FHMainSourceInterID", FID); ;//源单主内码
                                                                               //jsonFPOOrderEntry.Add("FHMainSourceEntryID", FENTRYID); ;//源单子内码
                        Entry.Add(jsonFPOOrderEntry);
                    }
                    jsonModel.Add("FEntity", Entry);
                    //日计划工单不为空时update
                    if (Convert.ToString(current["FDayPlanID"]) != "0")
                    {
                        sql = $"select max(FHMASTERDATE)FHMASTERDATE from SC_WORKBILLSORTBILLSub where FID={Convert.ToString(current["FDayPlanID"])} ";
                        DateTime EndDate = DBServiceHelper.ExecuteScalar<DateTime>(Context, sql, DateTime.Now);
                        for (int i = 0; i <= 60; i++)
                        {
                            if (EndDate >= date.AddDays(i))
                                continue;
                            JObject jsonFPOOrderEntry = new JObject();
                            jsonFPOOrderEntry.Add("FHMasterDate", date.AddDays(i).ToShortDateString()); ;//主日期
                            jsonFPOOrderEntry.Add("FHQty ", Convert.ToString(current["FT" + "" + i + ""])); ;//日计划数量
                            jsonFPOOrderEntry.Add("FColumn", "FT" + "" + i + ""); ;//对应的列头
                            Entry.Add(jsonFPOOrderEntry);
                        }
                        jsonModel.Add("FEntity", Entry);
                        jsonModel.Add("FID", Convert.ToString(current["FDayPlanID"])); ;//源单编号
                        jsonRoot.Add("Model", jsonModel);
                        CloudClient cloudClient = new CloudClient("http://localhost/K3Cloud/");
                        var result = cloudClient.Save("Paez_Sc_WorkBillAutoSortBill", jsonRoot.ToString());
                        JObject saveObj = JObject.Parse(result);
                        string saveIsSuc = saveObj["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper();
@@ -376,13 +396,10 @@
                        {
                            string FDayPlanBillNo = saveObj["Result"]["Number"].ToString();
                            string FDayPlanID = saveObj["Result"]["Id"].ToString();
                            this.Model.SetValue("FDayPlanBillNo", FDayPlanBillNo, Convert.ToInt32(current["SEQ"]) - 1);//
                            this.Model.SetValue("FDayPlanID", FDayPlanID, Convert.ToInt32(current["SEQ"]) - 1);//
                            this.View.ShowMessage("导入成功");
                        }
                        else
                        {
                            //LogService.WriteAsync(saveIsSuc);
                            LogService.WriteAsync(jsonRoot);
                            LogHelper.Error(jsonRoot.ToString());
                            this.View.ShowErrMessage(jsonRoot.ToString());
@@ -390,8 +407,16 @@
                    }
                    else
                    {
                        for (int i = 0; i <= 60; i++)
                        {
                            JObject jsonFPOOrderEntry = new JObject();
                            jsonFPOOrderEntry.Add("FHMasterDate", date.AddDays(i).ToShortDateString()); ;//主日期
                            jsonFPOOrderEntry.Add("FHQty ", Convert.ToString(current["FT" + "" + i + ""])); ;//日计划数量
                            jsonFPOOrderEntry.Add("FColumn", "FT" + "" + i + ""); ;//对应的列头
                            Entry.Add(jsonFPOOrderEntry);
                        }
                        jsonModel.Add("FEntity", Entry);
                        jsonRoot.Add("Model", jsonModel);
                        CloudClient cloudClient = new CloudClient("http://localhost/K3Cloud/");
                        var result = cloudClient.Save("Paez_Sc_WorkBillAutoSortBill", jsonRoot.ToString());
                        JObject saveObj = JObject.Parse(result);
                        string saveIsSuc = saveObj["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper();
@@ -400,29 +425,18 @@
                        {
                            string FDayPlanBillNo = saveObj["Result"]["Number"].ToString();
                            string FDayPlanID = saveObj["Result"]["Id"].ToString();
                            this.Model.SetValue("FDayPlanBillNo", FDayPlanBillNo, Convert.ToInt32(current["SEQ"]) - 1);// 
                            this.Model.SetValue("FDayPlanID", FDayPlanID, Convert.ToInt32(current["SEQ"]) - 1);// 
                            this.View.ShowMessage("导入成功");
                        }
                        else
                        {
                            // LogService.WriteAsync(saveIsSuc);
                            LogService.WriteAsync(jsonRoot);
                            LogHelper.Error(jsonRoot.ToString());
                            this.View.ShowErrMessage(jsonRoot.ToString());
                        }
                    }
                    this.View.UpdateView();
                }
            }
            catch (Exception ex)