ImportMethod_ProcessRoute.cs 7.51 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();
            
            return response;
        }
        #endregion
    }
}