ImportMethod_ProcessRoute.cs 12.8 KB
using Hh.Mes.Common;
using Hh.Mes.Common.config;
using Hh.Mes.Common.DataTableTo;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity;
using Hh.Mes.POJO.WebEntity.bus;
using Hh.Mes.Service.Repository;
using MySqlX.XDevAPI.Common;
using MySqlX.XDevAPI.Relational;
using NPOI.OpenXmlFormats.Dml.Diagram;
using NPOI.POIFS.FileSystem;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using static Hh.Mes.Common.Exel.NpoiExcelImportHelper;

namespace Hh.Mes.Service.WebService.Planned
{
    /// <summary>
    /// 定义导入方法加上特性
    /// dicDtSource 添加需要保存到数据源的数据源 
    /// 注意:只改对应业务的dt 数据【公共方法里面的读取的数据源不要修改、其他地方可能需要用到】
    /// </summary>
    public partial class ImportMethod 
    {
        #region 工艺路线表 Action里面的 base_process_route_head  是数据库表名 ,方法名没有特殊要求 ,注意不要删除sysDicDtSource里面的key
        [Action("base_process_route_head")]
        public Response BaseProcessRouteHeadMethod(Dictionary<string, SysDtAllCls> sysDicDtSource, dynamic user, DateTime nowTime)
        {
            #region init
            var response = new Response();
            //【修改项1】
            var excelFileNameKeys = ExcelName.base_process_route_head.ToString();
            response = ImportOhter.ImportMethodBefore(sysDicDtSource, response, excelFileNameKeys);
            if (response.Code != 200)
            {
                response.Message = $"【{TableName.excelFileNameBaseProcessRouteHead}】" + response.Message;
                return response;
            }
            //公共方法数据源,需要判断的数据源单独建一个
            DataTable excelDtSourceDt = response.Result.data;

            DataTable titleVerifySourceDt = response.Result.title;
            #endregion

            #region 创建dt、添加保存到数据库的dicDtSource 【修改项2
            var tableName = TableName.tableNameBaseProcessRouteHead;
            var eqSaveDt = GetTableStructure(tableName);
            if (eqSaveDt.Columns.Contains("Id")) eqSaveDt.Columns.Remove("Id");
            var temp = new SysDtAllCls()
            {
                dtData = eqSaveDt
            };
            var dtSourceNameKey = tableName + TableName.tableSuffix;
            sysDicDtSource.Add(dtSourceNameKey, temp);
            #endregion

            #region 去重 【修改项3 根据实际业务】
            var tempExcelDtSourceDt = excelDtSourceDt.Distinct("processCode");
            #endregion
            ///////////////////////////////////////////////////////////////////
            #region 工艺路线明细
            #region init
            var response2 = new Response();
            //【修改项1】
            var excelFileNameKeys2 = ExcelName.base_process_route_detail.ToString();
            response2 = ImportOhter.ImportMethodBefore(sysDicDtSource, response2, excelFileNameKeys2);
            if (response2.Code != 200)
            {
                response2.Message = $"【{TableName.excelFileNameBaseProcessRouteDetail}】" + response2.Message;
                return response2;
            }
            //公共方法数据源,需要判断的数据源单独建一个
            DataTable excelDtSourceDt2 = response2.Result.data;

            DataTable titleVerifySourceDt2 = response2.Result.title;
            #endregion

            #region 创建dt、添加保存到数据库的dicDtSource 【修改项2
            var tableName2 = TableName.tableNameBaseProcessRouteDetail;
            var eqSaveDt2 = GetTableStructure(tableName2);
            if (eqSaveDt2.Columns.Contains("Id")) eqSaveDt2.Columns.Remove("Id");
            var temp2 = new SysDtAllCls()
            {
                dtData = eqSaveDt2
            };
            var dtSourceNameKey2 = tableName2 + TableName.tableSuffix;
            sysDicDtSource.Add(dtSourceNameKey2, temp2);
            #endregion

            #region 去重 【修改项3 根据实际业务】
            var tempExcelDtSourceDt_detail = excelDtSourceDt2.Distinct("oprSequenceCode");
            #endregion

            #endregion

            #region 赋值  理论上是通用的,修改excel文件名 【修改项4
            var columns = tempExcelDtSourceDt.Columns;
            var detail_columns = tempExcelDtSourceDt_detail.Columns;
            for (int i = 0; i < tempExcelDtSourceDt.Rows.Count; i++)
            {
                //主表赋值
                var dr = eqSaveDt.NewRow();
                for (int c = 0; c < columns.Count; c++)
                {
                    var colName = columns[c].ColumnName;
                    dynamic colNameVal = tempExcelDtSourceDt.Rows[i][colName].ToString();
                    response = ImportOhter.SetDataTableDataBefore(response, i, colName, colNameVal, titleVerifySourceDt, TableName.excelFileNameBaseProcessRouteHead);
                    if (response.Code != 200) return response;

                    colNameVal = ImportOhter.SetDataTableData(colNameVal, eqSaveDt.Columns[colName].DataType.Name);
                    dr[colName] = colNameVal;
                    if (colName == "productCode") productCodes = colNameVal;
                }
                dr["keys"] = Guid.NewGuid().ToString();
                dr["createBy"] = user.Account;
                dr["createTime"] = nowTime;
                dr["productCode"] = "";
                dr["productHeaderCode"] = "";
                eqSaveDt.Rows.Add(dr);

                //明细表赋值
                for (int j = 0; j < tempExcelDtSourceDt_detail.Rows.Count; j++)
                {
                    var dr2 = eqSaveDt2.NewRow();
                    for (int c = 0; c < detail_columns.Count; c++)
                    {
                        var colName = detail_columns[c].ColumnName;
                        dynamic colNameVal = tempExcelDtSourceDt_detail.Rows[j][colName].ToString();
                        response2 = ImportOhter.SetDataTableDataBefore(response2, j, colName, colNameVal, titleVerifySourceDt2, TableName.excelFileNameBaseProcessRouteDetail);
                        if (response2.Code != 200) return response2;

                        colNameVal = ImportOhter.SetDataTableData(colNameVal, eqSaveDt2.Columns[colName].DataType.Name);
                        dr2[colName] = colNameVal;
                    }
                    dr2["headkeys"] = dr["keys"];
                    dr2["bodyKeys"] = Guid.NewGuid().ToString();
                    dr2["createBy"] = user.Account;
                    dr2["createTime"] = nowTime;
                    eqSaveDt2.Rows.Add(dr2);
                }
            }
            #endregion

            #region 逻辑判断
            #endregion

            response = SaveBaseProcessRouteAfter(sysDicDtSource, user);//有关联的一系列表赋值
            return response;
        }
        #endregion

        #region Save BaseProcessRoute Success After
        /// <summary>
        /// 工艺路线表保存之后
        /// </summary>
        /// <param name="baseProcessRoute"></param>
        public Response SaveBaseProcessRouteAfter(Dictionary<string, SysDtAllCls> sysDicDtSource, dynamic sysWebUser)
        {
            Response response = new Response();
            response = ImportOhter.ImportMethodBefore(sysDicDtSource, response, ExcelName.base_process_route_head.ToString());
            string user = sysWebUser?.Account;

            try
            {
                #region AddBaseProcessProductRel => productCode 还是需要这个字段逗号隔开,用于生成 base_process_product_rel;base_product_header不存在新增,存在跳过;
                DataTable dt = sysDicDtSource["base_process_route_head_source"].dtData;
                //产品保存到base_process_product_rel表
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string[] products = productCodes?.ToString().Split(',').Distinct().ToArray();
                    var dr = dt.Rows[i];
                    if (products != null)
                    {
                        var bpprList = new List<base_process_product_rel> { };
                        foreach (var p in products)
                        {
                            var baseProcessProductRel = new base_process_product_rel()
                            {
                                keys = Guid.NewGuid(),
                                processCode = dr["processCode"].ToString(),
                                processName = dr["processName"].ToString(),
                                productCode = p,
                                createBy = user,
                                createTime = DateTime.Now
                            };
                            var isHave = Context.Queryable<base_process_product_rel>().Where(x => x.processCode == dr["processCode"].ToString() && x.productCode == p).ToList();
                            if (isHave.Count <= 0)//同时满足(相同工艺相同产品)条件;不存在新增,存在跳过
                            {
                                bpprList.Add(baseProcessProductRel);
                            }
                        }
                        sysDicDtSource.Add("base_process_product_rel" + TableName.tableSuffix, new SysDtAllCls() { dtData = DataTableHelp.ListToDt(bpprList) });

                        var bphList = new List<base_product_header> { };
                        foreach (var p in products)
                        {
                            //保存 base_product_header 不存在新增、存在跳过
                            //var ProcessHeadKeys = Context.Queryable<base_process_route_head>().Where(x => x.processCode == dr["processCode"].ToString())?.First();
                            var baseProductHeader = new base_product_header()
                            {
                                keys = Guid.NewGuid(),
                                productCode = p,
                                productName = "",
                                processCode = dr["processCode"].ToString(),
                                createTime = DateTime.Now,
                                createBy = user,
                                processHeadKeys = Guid.Parse(dt.Rows[i]["keys"].ToString()),
                            };
                            var isSave = Context.Queryable<base_product_header>().Where(x => x.processCode == dr["processCode"].ToString() && x.productCode == p).ToList();
                            if (isSave.Count <= 0)
                            {
                                bphList.Add(baseProductHeader);
                            }
                        }
                        var dtData = DataTableHelp.ListToDt(bphList);
                        if (dtData.Columns.Contains("productCodeOrProductName")) dtData.Columns.Remove("productCodeOrProductName");
                        if (dtData.Columns.Contains("Exel")) dtData.Columns.Remove("Exel");
                        sysDicDtSource.Add("base_product_header" + TableName.tableSuffix, new SysDtAllCls() { dtData = dtData });
                    }
                }
                #endregion

                #region AddBaseWorkCenter => workCenterCodeworkCenterName:保存到 base_work_center 表,不存在新增、存在跳过

                DataTable dt_detail = sysDicDtSource["base_process_route_detail_source"].dtData;
                var bwcList = new List<base_work_center> { };
                for (int i = 0; i < dt_detail.Rows.Count; i++)
                {
                    var dr = dt_detail.Rows[i];
                    var baseWorkCenter = new base_work_center()
                    {
                        key = Guid.NewGuid(),
                        workCenterCode = dr["workCenterCode"].ToString(),
                        workCenterName = dr["workCenterName"].ToString(),
                        createBy = user,
                        createTime = DateTime.Now,
                    };
                    var isHave = Context.Queryable<base_work_center>().Where(x => x.workCenterCode == dr["workCenterCode"].ToString()).ToList();
                    if (isHave.Count <= 0)//不存在新增,存在跳过
                    {
                        bwcList.Add(baseWorkCenter);
                    }
                }
                sysDicDtSource.Add("base_work_center" + TableName.tableSuffix, new SysDtAllCls() { dtData = DataTableHelp.ListToDt(bwcList) });
            }
            catch (Exception ex)
            {
                response.ResponseErr(ex.Message);
                return response;
            }
            #endregion
            return response;
        }
        #endregion
    }
}