ImportOhter.cs 7.59 KB
using Hh.Mes.POJO.Response;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using static Hh.Mes.Common.Exel.NpoiExcelImportHelper;

namespace Hh.Mes.Service.WebService.Planned
{
    /// <summary>
    /// 第一步 Excel页签的名称 对应数据库表
    /// </summary>
    public enum ExcelName
    {
        equipment,

        sys_user,

        base_material,

        base_process_route_head,
        base_process_route_detail,

        bus_workOrder_head,
        bus_workOrder_detail,
    }

    /// <summary>
    /// 第二步  定义表名  tableNamexxxxx :对应数据库的表名
    /// </summary>
    public class TableName 
    {
        /// <summary>
        /// 数据源 后缀(_source) 禁止修改
        /// </summary>
        public static readonly string tableSuffix = "_source";

        /// <summary>
        /// 设备管理 数据库表名
        /// </summary>
        public static readonly string tableNameEquipment = ExcelName.equipment.ToString();

        /// <summary>
        /// 设备管理 excel文件名 用于错误信息提示
        /// </summary>
        public static readonly string excelFileNameEquipment = "设备管理";


        /// <summary>
        /// 用户表 数据库表名
        /// </summary>
        public static readonly string tableNameSysUser = ExcelName.sys_user.ToString();

        /// <summary>
        /// 人员信息 excel文件名 用于错误信息提示
        /// </summary>
        public static readonly string excelFileNameSysUser = "人员信息";

        /// <summary>
        /// 物料表 数据库表名
        /// </summary>
        public static readonly string tableNameBaseMaterial = ExcelName.base_material.ToString();

        /// <summary>
        /// 物料信息 excel文件名 用于错误信息提示
        /// </summary>
        public static readonly string excelFileNameBaseMaterial = "物料信息";

        public static readonly string tableNameBaseProcessRouteHead=ExcelName.base_process_route_head.ToString();
        public static readonly string excelFileNameBaseProcessRouteHead = "工艺路线";


        public static readonly string tableNameBaseProcessRouteDetail = ExcelName.base_process_route_detail.ToString();
        public static readonly string excelFileNameBaseProcessRouteDetail = "工艺路线明细";

        public static readonly string tableNameBusWorkOrderHead=ExcelName.bus_workOrder_head.ToString();
        public static readonly string excelFileNameBusWorkOrderHead = "生产订单";

        public static readonly string tableNameBusWorkOrderDetail = ExcelName.bus_workOrder_detail.ToString();
        public static readonly string excelFileNameBusWorkOrderDetail = "生产订单明细";
    }

    #region 公共类 不需要理会

    /// <summary>
    /// ImportService 帮助类
    /// </summary>
    public static class ImportOhter
    {

        public static Response ImportMethodBefore(Dictionary<string, SysDtAllCls> pairs, Response response, string keys)
        {
            DataTable dt;
            if (pairs.ContainsKey(keys))
            {
                dt = pairs[keys].dtData;
            }
            else
            {
                response.Code = 500;
                response.Status = false;
                response.Message = $"导入Excel不存在[{keys}]文件]";
                return response;
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                response.Code = 500;
                response.Message = $"[{keys}]导入Excel文件没有数据!";
                return response;
            }
            response.Result = new
            {
                data = dt,
                title = pairs[keys].dtTitle
            };
            return response;
        }

        /// <summary>
        /// 过滤列名
        /// </summary>
        public static DataTable DtModifyColumnName(this DataTable dt, ref Response response, string ExcelName)
        {
            //查询更改列名
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                var columnName = Regex.Replace(dt.Columns[i].ColumnName, @"[^a-zA-Z1-9]", "").Trim();
                if (string.IsNullOrEmpty(columnName))
                {
                    response.Code = 500;
                    response.Message = $"{ExcelName} 列【{dt.Columns[i].ColumnName}】不存在英文 ";
                    return dt;
                }
                dt.Columns[i].ColumnName = columnName;
            }
            return dt;
        }

        /// <summary>
        /// 去重方法
        /// </summary>
        public static DataTable Distinct(this DataTable ds, string columns)
        {
            DataView dv = new DataView(ds);
            DataTable where = dv.ToTable(true, columns);
            DataTable data = ds.Clone();
            foreach (DataRow item in where.Rows)
            {
                DataRow[] dataRow = ds.Select($"{columns}='{item[columns]}'");
                data.ImportRow(dataRow[0]);
            }
            return data;
        }


        /// <summary>
        /// 获取dt所有列
        /// </summary>
        public static List<string> GetColumnsByDataTable(this DataTable dt)
        {
            var temp = new List<string>();
            foreach (DataColumn c in dt.Columns)
            {
                temp.Add(c.ColumnName);
            }
            return temp;
        }


        /// <summary>
        ///  设置DataTable 值 下拉框格式【name_code】 
        /// </summary>
        public static dynamic SetDataTableData(dynamic colNameVal, string dataTypeName)
        {
            var selectKey = "_";

            if (!string.IsNullOrEmpty(colNameVal) && colNameVal.IndexOf(selectKey) > -1)
            {
                var tempArr = colNameVal.Split(selectKey);
                switch (dataTypeName)
                {
                    case "Int32":
                        colNameVal = int.Parse(tempArr[1]);
                        break;
                    default:
                        colNameVal = tempArr[1];
                        break;
                }

            }
            else if (string.IsNullOrEmpty(colNameVal))
            {
                switch (dataTypeName)
                {
                    case "String":
                        colNameVal = "";
                        break;
                    case "Int32":
                    case "DateTime":
                        colNameVal = DBNull.Value;
                        break;
                    default:
                        colNameVal = DBNull.Value;
                        break;
                }
            }
            return colNameVal;
        }


        /// <summary>
        ///  设置DataTable  Before 判断是否必填
        /// </summary>
        public static Response SetDataTableDataBefore(Response response, int index, string colName, string colNameVal, DataTable titleVerifySourceDt, string excelName)
        {
            var keys = "*";
            if (index == 0)
            {
                string tempVerifyVal = titleVerifySourceDt.Rows[index][colName].ToString();
                if (tempVerifyVal.IndexOf(keys) > -1 && string.IsNullOrEmpty(colNameVal))
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = $"【{excelName}】导入 Excel 列名【{colName}、{tempVerifyVal}】不能为空";
                    return response;
                }
            }
            response.Message = "";
            response.Code = 200;
            return response;
        }
    } 
    #endregion
}