duhe
2025-03-17 d1b14d00d9ee4ff4a1b271e6b4fe094f5049a55f
WebAPI/Controllers/JHGL/Gy_MaterialTechParamBillController.cs
@@ -10,7 +10,9 @@
using System.Web.Http;
using System.Windows.Forms;
using WebAPI.Models;
using WebAPI.Controllers.SCGL.日计划管理;
using System.IO;
using SyntacticSugar.constant;
namespace WebAPI.Controllers.MJGL
{
    public class Gy_MaterialTechParamBillController : ApiController
@@ -643,5 +645,484 @@
            }
        }
        #endregion
        #region 产品工艺参数 文件导入保存
        #region 产品工艺参数 文件上传
        [Route("Gy_MaterialTechParamBill/Gy_MaterialTechParamBill_Excel")]
        [HttpPost]
        public object Gy_MaterialTechParamBill_Excel()
        {
            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("HOrgID", typeof(Int32));//组织ID
                tb2.Columns.Add("HDeptID", typeof(Int32));//部门ID
                tb2.Columns.Add("HEmpID", typeof(Int32));//负责人ID
                tb2.Columns.Add("HMaterID", typeof(Int32));//物料ID
                tb2.Columns.Add("HTechParamID", typeof(Int32));//工艺参数ID
                tb2.Columns.Add("HTechParamUnitID", typeof(Int32));//工艺参数单位ID
                //添加数据
                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();
                    }
                    tb2.Rows.Add(row);
                }
                var error = "";
                //查询没有的列
                if (!tb2.Columns.Contains("组织代码"))
                    error += "没有找到【组织代码】的标题,";
                if (!tb2.Columns.Contains("组织名称"))
                    error += "没有找到【组织名称】的标题,";
                if (!tb2.Columns.Contains("部门代码"))
                    error += "没有找到【部门代码】的标题,";
                if (!tb2.Columns.Contains("部门名称"))
                    error += "没有找到【部门名称】的标题,";
                if (!tb2.Columns.Contains("负责人代码"))
                    error += "没有找到【负责人代码】的标题,";
                if (!tb2.Columns.Contains("负责人名称"))
                    error += "没有找到【负责人名称】的标题,";
                if (!tb2.Columns.Contains("物料代码"))
                    error += "没有找到【物料代码】的标题,";
                if (!tb2.Columns.Contains("物料名称"))
                    error += "没有找到【物料名称】的标题,";
                if (!tb2.Columns.Contains("备注"))
                    error += "没有找到【备注】的标题,";
                if (!tb2.Columns.Contains("工艺参数代码"))
                    error += "没有找到【工艺参数代码】的标题,";
                if (!tb2.Columns.Contains("工艺参数名称"))
                    error += "没有找到【工艺参数名称】的标题,";
                if (!tb2.Columns.Contains("工艺参数单位代码"))
                    error += "没有找到【工艺参数单位代码】的标题,";
                if (!tb2.Columns.Contains("工艺参数单位名称"))
                    error += "没有找到【工艺参数单位名称】的标题,";
                if (!tb2.Columns.Contains("标准值"))
                    error += "没有找到【标准值】的标题,";
                if (!tb2.Columns.Contains("上限"))
                    error += "没有找到【上限】的标题,";
                if (!tb2.Columns.Contains("下限"))
                    error += "没有找到【下限】的标题,";
                if (!tb2.Columns.Contains("采集来源"))
                    error += "没有找到【采集来源】的标题,";
                if (!tb2.Columns.Contains("采集周期频率"))
                    error += "没有找到【采集周期频率】的标题,";
                if (!tb2.Columns.Contains("采集周期单位"))
                    error += "没有找到【采集周期单位】的标题,";
                if (!tb2.Columns.Contains("单据号"))
                    error += "没有找到【单据号】的标题,";
                if (error.Length > 0)
                {
                    objJsonResult.code = CodeConstant.FAIL;
                    objJsonResult.count = CountConstant.FAIL;
                    objJsonResult.Message = $"Excel模板存在错误,{error}\r\n";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                for (int i = 0; i <= tb2.Rows.Count - 1; i++)
                {
                    string HORGNumber = "";
                    string HORGName = "";
                    string HDeptName = "";
                    string HDeptNumber = "";
                    string HEmpName = "";
                    string HEmpNumber = "";
                    string HMaterNumber = "";
                    string HMaterName = "";
                    string HTechParamNumber = "";
                    string HTechParamName = "";
                    string HTechParamUnitNumber = "";
                    string HTechParamUnitName = "";
                    string HBillNo = "";
                    HORGNumber = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["组织代码"].ToString());
                    HORGName = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["组织名称"].ToString());
                    HDeptNumber = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["部门代码"].ToString());
                    HDeptName = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["部门名称"].ToString());
                    HEmpNumber = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["负责人代码"].ToString());
                    HEmpName = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["负责人名称"].ToString());
                    HMaterNumber = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["物料代码"].ToString());
                    HMaterName = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["物料名称"].ToString());
                    HTechParamNumber = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["工艺参数代码"].ToString());
                    HTechParamName = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["工艺参数名称"].ToString());
                    HTechParamUnitNumber = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["工艺参数单位代码"].ToString());
                    HTechParamUnitName = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["工艺参数单位名称"].ToString());
                    HBillNo = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["单据号"].ToString());
                    //检查
                    int index = i + 1;
                    //组织
                    if (HORGNumber != "")
                    {
                        //查询组织
                        ds = oCN.RunProcReturn("select * from Xt_ORGANIZATIONS where  HNumber='" + HORGNumber + "' and Hname='" + HORGName + "'", "Xt_ORGANIZATIONS");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = CodeConstant.FAIL;
                            objJsonResult.count = CountConstant.FAIL;
                            objJsonResult.Message = "第" + index + "行,组织不存在!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            tb2.Rows[i]["HOrgID"] = ds.Tables[0].Rows[0]["HItemID"].ToString();
                        }
                    }
                    else
                    {
                        objJsonResult.code = CodeConstant.FAIL;
                        objJsonResult.count = CountConstant.FAIL;
                        objJsonResult.Message = "第" + index + "行,组织代码为空";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    //部门代码
                    if (HDeptNumber != "")
                    {
                        ds = oCN.RunProcReturn("select * from Gy_Department where  HNumber='" + HDeptNumber + "' and Hname='" + HDeptName + "'", "Gy_Department");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = CodeConstant.FAIL;
                            objJsonResult.count = CountConstant.FAIL;
                            objJsonResult.Message = "第" + index + "行,部门不存在!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            tb2.Rows[i]["HDeptID"] = ds.Tables[0].Rows[0]["HItemID"].ToString();
                        }
                    }
                    else
                    {
                        tb2.Rows[i]["HDeptID"] = "0";
                    }
                    //负责人代码
                    if (HEmpNumber != "")
                    {
                        ds = oCN.RunProcReturn("select * from Gy_Employee where  HNumber='" + HEmpNumber + "' and Hname='" + HEmpName + "'", "Gy_Employee");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = CodeConstant.FAIL;
                            objJsonResult.count = CountConstant.FAIL;
                            objJsonResult.Message = "第" + index + "行,负责人不存在!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            tb2.Rows[i]["HEmpID"] = ds.Tables[0].Rows[0]["HItemID"].ToString();
                        }
                    }
                    //物料代码
                    if (HMaterNumber != "")
                    {
                        ds = oCN.RunProcReturn("select * from Gy_Material where  HNumber='" + HMaterNumber + "' and Hname='" + HMaterName + "'", "Gy_Material");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = CodeConstant.FAIL;
                            objJsonResult.count = CountConstant.FAIL;
                            objJsonResult.Message = "第" + index + "行,物料不存在!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            tb2.Rows[i]["HMaterID"] = ds.Tables[0].Rows[0]["HItemID"].ToString();
                        }
                    }
                    //工艺参数代码
                    if (HTechParamNumber != "")
                    {
                        ds = oCN.RunProcReturn("select * from Gy_TechnologyParameter where  HNumber='" + HTechParamNumber + "' and Hname='" + HTechParamName + "'", "Gy_TechnologyParameter");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = CodeConstant.FAIL;
                            objJsonResult.count = CountConstant.FAIL;
                            objJsonResult.Message = "第" + index + "行,工艺参数不存在!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            tb2.Rows[i]["HTechParamID"] = ds.Tables[0].Rows[0]["HItemID"].ToString();
                        }
                    }
                    //工艺参数单位代码
                    if (HTechParamUnitNumber != "")
                    {
                        ds = oCN.RunProcReturn("select * from Gy_TechnologyParameterUnit where  HNumber='" + HTechParamUnitNumber + "' and Hname='" + HTechParamUnitName + "'", "Gy_TechnologyParameterUnit");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = CodeConstant.FAIL;
                            objJsonResult.count = CountConstant.FAIL;
                            objJsonResult.Message = "第" + index + "行,工艺参数单位不存在!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            tb2.Rows[i]["HTechParamUnitID"] = ds.Tables[0].Rows[0]["HItemID"].ToString();
                        }
                    }
                    //单据号
                    if (HBillNo == "")
                    {
                        objJsonResult.code = CodeConstant.FAIL;
                        objJsonResult.count = CountConstant.FAIL;
                        objJsonResult.Message = "第" + index + "行,单据号不能为空!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                objJsonResult.code = CodeConstant.SUCCEED;
                objJsonResult.count = CountConstant.SUCCEED;
                objJsonResult.Message = error;
                objJsonResult.data = tb2;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = CodeConstant.FAIL;
                objJsonResult.count = CountConstant.FAIL;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        #region 产品工艺参数项目 导入(保存)
        [Route("Gy_MaterialTechParamBill/Gy_MaterialTechParamBill_btnSave")]
        [HttpPost]
        public object Gy_MaterialTechParamBill_btnSave([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { "&和" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString();
            try
            {
                if (!DBUtility.ClsPub.Security_Log("Gy_MaterialTechParamBillMain_Edit", 1, false, user))
                {
                    objJsonResult.code = CodeConstant.FAIL;
                    objJsonResult.count = CountConstant.FAIL;
                    objJsonResult.Message = "无保存权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                List<object> Excel = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(msg2);
                List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
                foreach (JObject item in Excel)
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    foreach (var itm in item.Properties())
                    {
                        dic.Add(itm.Name, itm.Value.ToString());
                    }
                    list.Add(dic);
                }
                oCN.BeginTran();
                int i = 1;
                foreach (Dictionary<string, string> item in list)
                {
                    string HBillType = "3334";
                    string HMaker = user;//制单人
                    DateTime HDate = DBUtility.ClsPub.isDate(DateTime.Now.ToString("yyyy-MM-dd"));//  --日期
                    long HYear = DBUtility.ClsPub.isLong(DateTime.Now.Year);
                    long HPeriod = DBUtility.ClsPub.isLong(DateTime.Now.Month);
                    string HBillNo = item["单据号"].ToString();//单据号
                    string HOrgID = item["HOrgID"].ToString();//组织ID
                    string HDeptID = item["HDeptID"].ToString() == "" ? "0" : item["HDeptID"].ToString();
                    string HEmpID = item["HEmpID"].ToString() == "" ? "0" : item["HEmpID"].ToString();
                    string HMaterID = item["HMaterID"].ToString()== "" ? "0" : item["HMaterID"].ToString();
                    string HRemark = item["备注"].ToString();
                    //子表
                    string HTechParamID = item["HTechParamID"].ToString() == "" ? "0" : item["HTechParamID"].ToString();//工艺参数
                    string HTechParamUnitID = item["HTechParamUnitID"].ToString() == "" ? "0" : item["HTechParamUnitID"].ToString();//工艺参数代码
                    string HStd = item["标准值"].ToString() == "" ? "0" : item["标准值"].ToString();
                    string HMax = item["上限"].ToString() == "" ? "0" : item["上限"].ToString();
                    string HMin = item["下限"].ToString() == "" ? "0" : item["下限"].ToString();
                    string HDataForm = item["采集来源"].ToString();
                    string HDataTimes = item["采集周期频率"].ToString() == "" ? "0" : item["采集周期频率"].ToString();
                    string HDataUnit = item["采集周期单位"].ToString();
                    ds = oCN.RunProcReturn("select * from Gy_MaterialTechParamBillMain where HBillNo='" + HBillNo + "'", "Gy_MaterialTechParamBillMain");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        long HInterID = DBUtility.ClsPub.CreateBillID(HBillType, ref DBUtility.ClsPub.sExeReturnInfo);
                        //插入主表
                        oCN.RunProc("insert into Gy_MaterialTechParamBillMain" +
                            "(HYear,HPeriod,HBillType,HBillSubType,HInterID,HDate,HBillNo,HRemark,HMaker,HMakeDate" +
                            ",HDeptID,HEmpID,HMaterID,HWorkCenterID,HEquipID,HOrgID,HProcID,HMouldID ) " +
                            "values(" +
                            "" + HYear.ToString() +
                            "," + HPeriod.ToString() +
                            ",'" + HBillType +
                            "','" + HBillType +
                            "'," + HInterID +
                            ",'" + HDate +
                            "','" + HBillNo +
                            "','" + HRemark +
                            "','" + HMaker +
                            "'," + "getdate()" +
                            "," + HDeptID +
                            "," + HEmpID +
                            ",'" + HMaterID +
                            "','" + 0 +
                            "','" + 0 +
                            "','" + HOrgID +
                            "','" + 0 +
                             "','" + 0 +
                            "')");
                        //插入子表
                        oCN.RunProc("insert into Gy_MaterialTechParamBillSub" +
                           "(HInterID,HEntryID,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType" +
                           ",HTechParamID,HTechParamUnitID,HStd,HMax,HMin,HDataForm,HDataUnit,HDataTimes,HTechParamClassID,HSNO,HStdNextTimes,HTechParamIDStd) " +
                           " values(" +
                           "" + HInterID.ToString() +
                           "," + 1 +
                           "," + 0 +
                           "," + 0 +
                           ",'" + "" +
                           "','" + "" +
                           "','" + HTechParamID +
                           "','" + HTechParamUnitID +
                           "','" + HStd +
                           "','" + HMax +
                           "','" + HMin +
                           "','" + HDataForm +
                           "','" + HDataUnit +
                           "','" + HDataTimes +
                           "','" + 0 +
                           "','" + 0 +
                           "','" + 0 +
                           "','" + "" +
                           "')");
                    }
                    else
                    {
                        long HInterID = long.Parse(ds.Tables[0].Rows[0]["HInterID"].ToString());
                        //获取最大子id
                        DataSet ds2 = oCN.RunProcReturn("select max(HEntryID) HEntryID from Gy_MaterialTechParamBillSub where HInterID='" + HInterID + "'", "Gy_MaterialTechParamBillSub");
                        long HEntryID = 1;
                        if (ds2.Tables[0].Rows.Count > 0)
                        {
                            HEntryID = long.Parse(ds2.Tables[0].Rows[0]["HEntryID"].ToString()) + 1;
                        }
                        //插入子表
                        oCN.RunProc("insert into Gy_MaterialTechParamBillSub" +
                           "(HInterID,HEntryID,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType" +
                           ",HTechParamID,HTechParamUnitID,HStd,HMax,HMin,HDataForm,HDataUnit,HDataTimes,HTechParamClassID,HSNO,HStdNextTimes,HTechParamIDStd) " +
                           " values(" +
                           "" + HInterID.ToString() +
                           "," + HEntryID +
                           "," + 0 +
                           "," + 0 +
                           ",'" + "" +
                           "','" + "" +
                           "','" + HTechParamID +
                           "','" + HTechParamUnitID +
                           "','" + HStd +
                           "','" + HMax +
                           "','" + HMin +
                           "','" + HDataForm +
                           "','" + HDataUnit +
                           "','" + HDataTimes +
                           "','" + 0 +
                           "','" + 0 +
                           "','" + 0 +
                           "','" + "" +
                           "')");
                    }
                    i++;
                }
                oCN.Commit();
                objJsonResult.code = CodeConstant.SUCCEED;
                objJsonResult.count = CountConstant.SUCCEED;
                objJsonResult.Message = "导入成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                LogService.Write(e);
                oCN.RollBack();
                objJsonResult.code = CodeConstant.FAIL;
                objJsonResult.count = CountConstant.FAIL;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        #endregion
    }
}