ImportMethod.cs 8.77 KB
using Hh.Mes.Common;
using Hh.Mes.Common.config;
using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using System;
using System.Collections.Generic;
using System.Data;
using static Hh.Mes.Common.Exel.NpoiExcelImportHelper;

namespace Hh.Mes.Service.WebService.Planned
{
    /// <summary>
    /// 定义导入方法加上特性
    /// dicDtSource 添加需要保存到数据源的数据源 
    /// 注意:只改对应业务的dt 数据【公共方法里面的读取的数据源不要修改、其他地方可能需要用到】
    /// </summary>
    public partial class ImportMethod : RepositorySqlSugar<base_material>
    {
        public string materialDtKey { get; set; }

        private string productCodes { get; set; }

        public ImportMethod()
        {
        }

        #region 用户表 Action里面的 sys_user  是数据库表名 ,方法名没有特殊要求 ,注意不要删除sysDicDtSource里面的key
        [Action("sys_user")]
        public Response SysUserMethod(Dictionary<string, SysDtAllCls> sysDicDtSource, dynamic user, DateTime nowTime)
        {
            #region init
            var response = new Response();
            //【修改项1】
            var excelFileNameKeys = ExcelName.sys_user.ToString();
            response = ImportOhter.ImportMethodBefore(sysDicDtSource, response, excelFileNameKeys);
            if (response.Code != 200)
            {
                response.Message = $"【{TableName.excelFileNameSysUser}】" + response.Message;
                return response;
            }
            //公共方法数据源,需要判断的数据源单独建一个
            DataTable excelDtSourceDt = response.Result.data;

            DataTable titleVerifySourceDt = response.Result.title;
            #endregion

            #region 创建dt、添加保存到数据库的dicDtSource 【修改项2
            var tableName = TableName.tableNameSysUser;
            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("account");
            #endregion

            #region 赋值  理论上是通用的,修改excel文件名 【修改项4
            var columns = tempExcelDtSourceDt.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.excelFileNameSysUser);
                    if (response.Code != 200) return response;

                    colNameVal = ImportOhter.SetDataTableData(colNameVal, eqSaveDt.Columns[colName].DataType.Name);
                    dr[colName] = colNameVal;
                }

                dr["password"] = Encryption.Encrypt(ConfigRead.GetInstance.GetAppsetConnection().ResetPwd);
                dr["status"] = 1;
                dr["createBy"] = user.Account;
                dr["createTime"] = nowTime;
                eqSaveDt.Rows.Add(dr);
            }
            #endregion

            #region 逻辑判断

            //response.ResponseError("");
            //return response;
            #endregion

            return response;
        }
        #endregion

        #region 物料表 Action里面的 base_material  是数据库表名 ,方法名没有特殊要求 ,注意不要删除sysDicDtSource里面的key
        [Action("base_material")]
        public Response BaseMaterialMethod(Dictionary<string, SysDtAllCls> sysDicDtSource, dynamic user, DateTime nowTime)
        {
            #region init
            var response = new Response();
            //【修改项1】
            var excelFileNameKeys = ExcelName.base_material.ToString();
            response = ImportOhter.ImportMethodBefore(sysDicDtSource, response, excelFileNameKeys);
            if (response.Code != 200)
            {
                response.Message = $"【{TableName.excelFileNameBaseMaterial}】" + response.Message;
                return response;
            }
            //公共方法数据源,需要判断的数据源单独建一个
            DataTable excelDtSourceDt = response.Result.data;

            DataTable titleVerifySourceDt = response.Result.title;
            #endregion

            #region 创建dt、添加保存到数据库的dicDtSource 【修改项2
            var tableName = TableName.tableNameBaseMaterial;
            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("materialCode");
            #endregion

            #region 赋值  理论上是通用的,修改excel文件名 【修改项4
            var columns = tempExcelDtSourceDt.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.excelFileNameBaseMaterial);
                    if (response.Code != 200) return response;

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

            #region 逻辑判断

            //response.ResponseError("");
            //return response;
            #endregion

            return response;
        }
        #endregion

        #region Save SysUser Success After 
        /// <summary>
        /// SysUser表保存成功之后
        /// </summary>
        /// <param name="dateTime"></param>
        public void SaveSuccessAfterSysUesr(DateTime dateTime, dynamic sysWebUser)
        {
            UpdateSysRelevance(sysWebUser.Account);
        }
        /// <summary>
        /// 更新部门关联的用户
        /// </summary>
        private void UpdateSysRelevance(string user)
        {
            // UserOrg:用户机构关联KEY 
            var relKey = "UserOrg";
            // 26:是“工艺管线预制车间”==>“巨驰”的部门ID
            var deptId = 26;
            var sql = $@"insert into sys_relevance (relKey,firstId,secondId,createBy)
                         select  UserOrg='{relKey}' , 
		                         t1.id ,
		                         secondId={deptId},
		                         createBy='{user}' 
                        from  sys_user t1 with (nolock)
                        left join  (select distinct firstId from sys_relevance) t2 on t1.id=t2.firstId
                        where   t2.firstId  is null";
            var result = base.Context.Ado.ExecuteCommand(sql);

            if (result <= 0)
            {
                Log4NetHelper.Instance.Error($"【ImportMethod-UpdateSysRelevance 执行更新部门关联的用户失败 sql】" + sql);
            }
        }
        #endregion

        #region 公共方法

        /// <summary>
        /// 获取表结构 没有数据源,并设置dt的表名 ,去掉id列
        /// </summary>
        public DataTable GetTableStructure(string tableName)
        {
            var sql = $"select * from {tableName} where 1!=1";
            var dt = base.Context.Ado.GetDataTable(sql);
            dt.TableName = tableName;
            //var columnsId = "id";
            // if (dt.Columns.Contains(columnsId)) dt.Columns.Remove(columnsId);
            return dt;
        }


        #endregion
    }
}