using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.WeldingManage
{
    public partial class WeldReportDataIn : PageBase
    {
        #region 定义变量
        /// 
        /// 上传预设的虚拟路径
        /// 
        private string initPath = Const.ExcelUrl;
        /// 
        /// 导入模版文件原始的虚拟路径
        /// 
        private string initTemplatePath = Const.WeldReportDataInTemplateUrl;
        /// 
        /// 错误集合
        /// 
        public static List errorInfos = new List();
            #endregion
        #region 加载
            /// 
            /// 加载页面
            /// 
            /// 
            /// 
            protected void Page_Load(object sender, EventArgs e)
            {
                  if (!IsPostBack)
                  {
                        ///施工区域下拉
                        var unit = BLL.UnitService.GetUnitByUnitId(this.CurrUser.UnitId);
                        if (unit != null&& unit.UnitId!=Const.UnitId_TCC)
                        {
                              BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                              var getpu = ProjectUnitService.GetProjectUnitByUnitIdProjectId(this.CurrUser.LoginProjectId, unit.UnitId);
                              if (getpu != null && getpu.UnitType == Const.ProjectUnitType_2)
                              {
                                    BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                              }
                              //var getynitype = BLL.UnitTypeService.GetUnitTypeById(unit.UnitTypeId);
                              //if (getynitype != null && getynitype.UnitTypeName == "施工单位")
                              //{
                              //    BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                              //}
                              else if (getpu != null && getpu.UnitType == Const.ProjectUnitType_1)
                              {
                                    BLL.WorkAreaService.InitWorkAreaByProjectId(this.drpWorkArea, this.CurrUser.LoginProjectId,   true);
                              }
                              else
                              {
                                    Alert.ShowInParent("请先在项目单位中添加施工单位!", MessageBoxIcon.Warning);
                              }
                        }
                        else
                        {
                              BLL.WorkAreaService.InitWorkAreaByProjectId(this.drpWorkArea, this.CurrUser.LoginProjectId, true);
                        }
                        if (errorInfos != null)
                        {
                              errorInfos.Clear();
                        }
                  }
            }
        #endregion
        #region 区域焊口数
        /// 
        /// 区域焊口数
        /// 
        /// 
        /// 
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null)
            {
                int count = (from x in Funs.DB.PW_IsoInfo
                             join y in Funs.DB.PW_JointInfo on x.ISO_ID equals y.ISO_ID
                             where x.WorkAreaId == this.drpWorkArea.SelectedValue
                             select y).Count();
                Alert.ShowInTop("该区域共有" + count + "个焊口!", MessageBoxIcon.Information);
            }
            else
            {
                Alert.ShowInTop("请先选择区域", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 模板下载
        /// 
        /// 模板下载
        /// 
        /// 
        /// 
        protected void imgbtnUpload_Click(object sender, EventArgs e)
        {
            string uploadfilepath = Server.MapPath("~/") + initTemplatePath;
            string fileName = Path.GetFileName(initTemplatePath);
            FileInfo info = new FileInfo(uploadfilepath);
            if (info.Exists)
            {
                long fileSize = info.Length;
                Response.Clear();
                Response.ContentType = "application/x-zip-compressed";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                Response.AddHeader("Content-Length", fileSize.ToString());
                Response.TransmitFile(uploadfilepath, 0, fileSize);
                Response.Flush();
                Response.Close();
            }
            else
            {
                ShowNotify("文件不存在!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 审核
        /// 
        /// 审核
        /// 
        /// 
        /// 
        protected void btnAudit_Click(object sender, EventArgs e)
        {
            //try
            //{
                if (this.FileExcel.HasFile == false)
                {
                    Alert.ShowInTop("请您选择Excel文件!", MessageBoxIcon.Warning);
                    return;
                }
                string IsXls = Path.GetExtension(FileExcel.FileName).ToString().Trim().ToLower();
                if (IsXls != ".xls")
                {
                    Alert.ShowInTop("只可以选择Excel文件!", MessageBoxIcon.Warning);
                    return;
                }
                if (errorInfos != null)
                {
                    errorInfos.Clear();
                }
                string rootPath = Server.MapPath("~/");
                string initFullPath = rootPath + initPath;
                if (!Directory.Exists(initFullPath))
                {
                    Directory.CreateDirectory(initFullPath);
                }
                this.hdfileName.Text = BLL.Funs.GetNewFileName() + IsXls;
                string filePath = initFullPath + this.hdfileName.Text;
                FileExcel.PostedFile.SaveAs(filePath);
                ImportXlsToData(filePath);
            //}
            //catch (Exception ex)
            //{
            //    Alert.ShowInTop(ex.Message);
            //}
        }
        #region 读Excel提取数据
        /// 
        /// 从Excel提取数据--》Dataset
        /// 
        /// Excel文件路径名
        private void ImportXlsToData(string fileName)
        {
            //try
            //{
                //string oleDBConnString = String.Empty;
                //oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
                //oleDBConnString += "Data Source=";
                //oleDBConnString += fileName;
                //oleDBConnString += ";Extended Properties=Excel 8.0;";
                //OleDbConnection oleDBConn = null;
                //OleDbDataAdapter oleAdMaster = null;
                //DataTable m_tableName = new DataTable();
                //DataSet ds = new DataSet();
                //oleDBConn = new OleDbConnection(oleDBConnString);
                //oleDBConn.Open();
                //m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                //if (m_tableName != null && m_tableName.Rows.Count > 0)
                //{
                //    m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
                //}
                //string sqlMaster;
                //sqlMaster = " SELECT *  FROM [" + m_tableName.TableName + "]";
                //oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
                //oleAdMaster.SelectCommand.CommandTimeout = 1200;
                //oleAdMaster.Fill(ds, "m_tableName");
                //oleAdMaster.Dispose();
                //oleDBConn.Close();
                //oleDBConn.Dispose();
                //AddDatasetToSQL(ds.Tables[0], 27);
            var dataTable = BLL.Common.NPOIHelper.ExcelToDataTable1(fileName);
            AddDatasetToSQL(dataTable, 28);
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }
        #endregion
        #endregion
        /// 
        /// 将Dataset的数据导入数据库
        /// 
        /// 数据集
        /// 数据集列数
        /// 
        private bool AddDatasetToSQL(DataTable pds, int Cols)
        {
            string result = string.Empty;
            int ic, ir;
            ic = pds.Columns.Count;
            if (ic < Cols)
            {
                throw new Exception("导入Excel格式错误!Excel只有" + ic.ToString().Trim() + "列");
            }
            ir = pds.Rows.Count;
            if (pds != null && ir > 0)
            {
                var oldViewInfos = from x in Funs.DB.View_JointInfoAndIsoInfo
                                   where x.ProjectId == this.CurrUser.LoginProjectId
                                   select x;
                var units = from x in Funs.DB.Base_Unit
                            join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId
                            where y.ProjectId == this.CurrUser.LoginProjectId
                            select x;
                var installations = from x in Funs.DB.Project_Installation where x.ProjectId == this.CurrUser.LoginProjectId select x;
                var workAreas = from x in Funs.DB.ProjectData_WorkArea where x.ProjectId == this.CurrUser.LoginProjectId select x;
                var steels = from x in Funs.DB.Base_Material select x;
                var types = from x in Funs.DB.Base_WeldType select x;
                var methods = from x in Funs.DB.Base_WeldingMethod select x;
                var weldMethodItem = from x in Funs.DB.BS_WeldMethodItem select x;
                var teamGroups = from x in Funs.DB.ProjectData_TeamGroup where x.ProjectId == this.CurrUser.LoginProjectId select x;
                var welders = from x in Funs.DB.BS_Welder select x;
                var weldMaterials = from x in Funs.DB.Base_Consumables select x;//焊接耗材
                for (int i = 0; i < ir; i++)
                {
                    Model.View_JointInfoAndIsoInfo oldViewInfo = new Model.View_JointInfoAndIsoInfo();
                    oldViewInfo = oldViewInfos.Where(x => x.ProjectId == CurrUser.LoginProjectId && x.WorkAreaId == this.drpWorkArea.SelectedValue
                                                       && x.ISO_IsoNo == pds.Rows[i][2].ToString().Trim()
                                                       && x.JOT_JointNo == pds.Rows[i][3].ToString().Trim()
                                                       && x.JOT_JointStatus != "104").FirstOrDefault();
                    if (oldViewInfo != null)
                    {
                        Model.PW_JointInfo jointInfo = BLL.PW_JointInfoService.GetJointInfoByJotID(oldViewInfo.JOT_ID);
                        if (string.IsNullOrEmpty(jointInfo.DReportID))
                        {
                            string row0 = pds.Rows[i][0].ToString();
                            //var unit = units.Where(x => x.UnitCode == row0.Trim() && x.UnitId == this.CurrUser.UnitId).FirstOrDefault();
                            var unit = units.Where(x => x.UnitCode == row0.Trim()).FirstOrDefault();
                            if (!string.IsNullOrEmpty(row0))
                            {
                                if (units.Where(x => x.UnitCode == row0.Trim()).FirstOrDefault() == null)
                                {
                                    result += (i + 2).ToString() + "," + "单位代号" + "," + "[" + row0 + "]不存在!" + "|";
                                }
                            }
                            else
                            {
                                result += (i + 2).ToString() + "," + "单位代号" + "," + "此项为必填项!" + "|";
                            }
                            string row1 = pds.Rows[i][1].ToString();
                            if (!string.IsNullOrEmpty(row1))
                            {
                                var installation = installations.Where(x => x.InstallationCode == row1.Trim()).FirstOrDefault();
                                if (installation == null)
                                {
                                    result += (i + 2).ToString() + "," + "装置代号" + "," + "[" + row1 + "]不存在!" + "|";
                                }
                            }
                            else
                            {
                                result += (i + 2).ToString() + "," + "装置代号" + "," + "此项为必填项!" + "|";
                            }
                            if (!string.IsNullOrEmpty(row0))
                            {
                                if (unit != null)
                                {
                                    var area = workAreas.Where(x => x.UnitId == unit.UnitId && x.WorkAreaId == this.drpWorkArea.SelectedValue).FirstOrDefault();
                                    if (area == null)
                                    {
                                        result += (i + 2).ToString() + "," + "区域" + "," + "该单位没有此区域号!" + "|";
                                    }
                                }
                            }
                            if (string.IsNullOrEmpty(pds.Rows[i][2].ToString()))
                            {
                                result += (i + 2).ToString() + "," + "管线代号" + "," + "此项为必填项!" + "|";
                            }
                            if (string.IsNullOrEmpty(pds.Rows[i][3].ToString()))
                            {
                                result += (i + 2).ToString() + "," + "焊口代号" + "," + "此项为必填项!" + "|";
                            }
                            string row4 = pds.Rows[i][4].ToString();
                            if (!string.IsNullOrEmpty(row4))
                            {
                                if (steels.Where(x => x.MaterialCode == row4.Trim()).FirstOrDefault() == null)
                                {
                                    result += (i + 2).ToString() + "," + "材质1代号" + "," + "[" + row4 + "]错误!" + "|";
                                }
                            }
                            string row5 = pds.Rows[i][5].ToString();
                            if (!string.IsNullOrEmpty(row5))
                            {
                                if (steels.Where(x => x.MaterialCode == row5.Trim()).FirstOrDefault() == null)
                                {
                                    result += (i + 2).ToString() + "," + "材质2代号" + "," + "[" + row5 + "]错误!" + "|";
                                }
                            }
                            string row6 = pds.Rows[i][6].ToString();
                            if (!string.IsNullOrEmpty(row6))
                            {
                                if (types.Where(x => x.WeldTypeCode == row6.Trim()).FirstOrDefault() == null)
                                {
                                    result += (i + 2).ToString() + "," + "焊缝类型" + "," + "[" + row6 + "]错误!" + "|";
                                }
                            }
                            string row7 = pds.Rows[i][7].ToString();
                            if (!string.IsNullOrEmpty(row7))
                            {
                                if (row7.Trim() != "安装" && row7.Trim() != "预制")
                                {
                                    result += (i + 2).ToString() + "," + "焊接区域" + "," + "[" + row7 + "]错误!" + "|";
                                }
                            }
                            else
                            {
                                result += (i + 2).ToString() + "," + "焊接区域" + "," + "此项为必填项!" + "|";
                            }
                            string row8 = pds.Rows[i][8].ToString();
                            if (!string.IsNullOrEmpty(row8))
                            {
                                if (row8.Trim() != "固定" && row8.Trim() != "活动")
                                {
                                    result += (i + 2).ToString() + "," + "焊口属性" + "," + "[" + row8 + "]错误!" + "|";
                                }
                            }
                            else
                            {
                                result += (i + 2).ToString() + "," + "焊口属性" + "," + "此项为必填项!" + "|";
                            }
                            string row9 = pds.Rows[i][9].ToString();
                            if (!string.IsNullOrEmpty(row9))
                            {
                                try
                                {
                                    decimal doneDin = Convert.ToDecimal(row9.Trim());
                                }
                                catch (Exception)
                                {
                                    result += (i + 2).ToString() + "," + "达因数" + "," + "[" + row9 + "]错误!" + "|";
                                }
                            }
                            string row12 = pds.Rows[i][12].ToString();
                            if (!string.IsNullOrEmpty(row12))
                            {
                                if (methods.Where(x => x.WeldingMethodCode == row12.Trim()).FirstOrDefault() == null)
                                {
                                    result += (i + 2).ToString() + "," + "焊接方法" + "," + "[" + row12 + "]错误!" + "|";
                                }
                            }
                            string row13 = pds.Rows[i][13].ToString();
                            if (!string.IsNullOrEmpty(row13))
                            {
                                if (weldMaterials.Where(x => x.ConsumablesCode == row13.Trim()).FirstOrDefault() == null)
                                {
                                    result += (i + 2).ToString() + "," + "焊材代号" + "," + "[" + row13 + "]错误!" + "|";
                                }
                            }
                            string row14 = pds.Rows[i][14].ToString();
                            if (!string.IsNullOrEmpty(row14) && unit != null)
                            {
                                var teamGroup = teamGroups.Where(x => x.UnitId == unit.UnitId && x.TeamGroupCode == row14.Trim()).FirstOrDefault();
                                if (teamGroup == null)
                                {
                                    result += (i + 2).ToString() + "," + "班组代号" + "," + "该单位没有此班组代号!" + "|";
                                }
                            }
                            string row15 = pds.Rows[i][15].ToString();
                            if (!string.IsNullOrEmpty(row15))
                            {
                                if (unit != null)
                                {
                                    if (!string.IsNullOrEmpty(row14))
                                    {
                                        var teamGroup = teamGroups.Where(x => x.UnitId == unit.UnitId && x.TeamGroupCode == row14.Trim()).FirstOrDefault();
                                        if (teamGroup != null)
                                        {
                                            var welder = welders.Where(x => x.WED_Unit == unit.UnitId && x.TeamGroupId == teamGroup.TeamGroupId && x.WED_Code == row15.Trim()).FirstOrDefault();
                                            if (welder != null)
                                            {
                                                if(welder.OutTime.HasValue && welder.OutTime.Value < DateTime.Now)
                                                {
                                                    result += (i + 2).ToString() + "," + "盖面焊工" + "," + "已经出场!" + "|";
                                                }
                                                if (!welder.WED_IfOnGuard.HasValue || welder.WED_IfOnGuard.HasValue && !welder.WED_IfOnGuard.Value)
                                                {
                                                    result += (i + 2).ToString() + "," + "盖面焊工" + "," + "不在岗!" + "|";
                                                }
                                                var steel = steels.Where(x => x.MaterialCode == row4.Trim()).FirstOrDefault();
                                                if (steel != null && !string.IsNullOrEmpty(steel.SteelType) && !string.IsNullOrEmpty(welder.Steels) && !welder.Steels.Contains(steel.SteelType))
                                                {
                                                    result += (i + 2).ToString() + "," + "盖面焊工" + "," + "没有该材质资质!" + "|";
                                                }
                                                var method = methods.Where(x => x.WeldingMethodCode == row12.Trim()).FirstOrDefault();
                                                var weldMethodItems = weldMethodItem.Where(x => x.WED_ID == welder.WED_ID).ToList();
                                                var methodItem = weldMethodItem.Where(x => x.WED_ID == welder.WED_ID && x.WME_ID == method.WeldingMethodId).ToList();
                                                if (weldMethodItems.Count != 0 && methodItem.Count == 0)
                                                {
                                                    result += (i + 2).ToString() + "," + "盖面焊工" + "," + "没有焊接方法质资质!" + "|";
                                                }
                                                if (!string.IsNullOrEmpty(pds.Rows[i][12].ToString()) && !string.IsNullOrEmpty(welder.JOT_Sch) && !welder.JOT_Sch.Contains(pds.Rows[i][12].ToString()))
                                                {
                                                    result += (i + 2).ToString() + "," + "盖面焊工" + "," + "没有该壁厚资质!" + "|";
                                                }
                                            }
                                            else
                                            {
                                                result += (i + 2).ToString() + "," + "盖面焊工代号" + "," + "该单位没有此焊工代号!" + "|";
                                            }
                                        }
                                        else
                                        {
                                            var welder = welders.Where(x => x.WED_Unit == unit.UnitId && x.WED_Code == row15.Trim()).FirstOrDefault();
                                            if (welder == null)
                                            {
                                                result += (i + 2).ToString() + "," + "盖面焊工代号" + "," + "该单位没有此焊工代号!" + "|";
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                result += (i + 2).ToString() + "," + "盖面焊工代号" + "," + "此项为必填项!" + "|";
                            }
                            string row16 = pds.Rows[i][16].ToString();
                            if (!string.IsNullOrEmpty(row16))
                            {
                                if (unit != null)
                                {
                                    if (!string.IsNullOrEmpty(row14))
                                    {
                                        var teamGroup = teamGroups.Where(x => x.UnitId == unit.UnitId && x.TeamGroupCode == row14.Trim()).FirstOrDefault();
                                        if (teamGroup != null)
                                        {
                                            var welder = welders.Where(x => x.WED_Unit == unit.UnitId && x.TeamGroupId == teamGroup.TeamGroupId && x.WED_Code == row16.Trim()).FirstOrDefault();
                                            if (welder == null)
                                            {
                                                result += (i + 2).ToString() + "," + "打底焊工代号" + "," + "该单位没有此焊工代号!" + "|";
                                            }
                                            else
                                            {
                                                if (welder.OutTime.HasValue && welder.OutTime.Value < DateTime.Now)
                                                {
                                                    result += (i + 2).ToString() + "," + "打底焊工" + "," + "已经出场!" + "|";
                                                }
                                                if (!welder.WED_IfOnGuard.HasValue || (welder.WED_IfOnGuard.HasValue && !welder.WED_IfOnGuard.Value))
                                                {
                                                    result += (i + 2).ToString() + "," + "打底焊工" + "," + "不在岗!" + "|";
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        var welder = welders.Where(x => x.WED_Unit == unit.UnitId && x.WED_Code == row16.Trim()).FirstOrDefault();
                                        if (welder == null)
                                        {
                                            result += (i + 2).ToString() + "," + "打底焊工代号" + "," + "该单位没有此焊工代号!" + "|";
                                        }
                                        else
                                        {
                                            if (welder.OutTime.HasValue && welder.OutTime.Value < DateTime.Now)
                                            {
                                                result += (i + 2).ToString() + "," + "打底焊工" + "," + "已经出场!" + "|";
                                            }
                                            if (!welder.WED_IfOnGuard.HasValue || (welder.WED_IfOnGuard.HasValue && !welder.WED_IfOnGuard.Value))
                                            {
                                                result += (i + 2).ToString() + "," + "打底焊工" + "," + "不在岗!" + "|";
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                result += (i + 2).ToString() + "," + "打底焊工代号" + "," + "此项为必填项!" + "|";
                            }
                            string row17 = pds.Rows[i][17].ToString().Trim();
                            if (!string.IsNullOrEmpty(row17))
                            {
                                if (row17 != "1G" && row17 != "2G" && row17 != "3G" && row17 != "4G" && row17 != "5G" && row17 != "6G" && row17 != "1F" && row17 != "2F" && row17 != "2FR" && row17 != "4F" && row17 != "5F" && row17 != "5FG" && row17 != "6FG" && row17 != "2FG" && row17 != "4FG")
                                {
                                    result += (i + 2).ToString() + "," + "焊接位置" + "," + "[" + row17 + "]错误!" + "|";
                                }
                            }
                            else
                            {
                                result += (i + 2).ToString() + "," + "焊接位置" + "," + "此项为必填项!" + "|";
                            }
                            string row18 = pds.Rows[i][18].ToString();
                            if (!string.IsNullOrEmpty(row18))
                            {
                                try
                                {
                                    DateTime date = Convert.ToDateTime(row18.Trim());
                                }
                                catch (Exception)
                                {
                                    result += (i + 2).ToString() + "," + "焊接日期" + "," + "[" + row18 + "]错误!" + "|";
                                }
                            }
                            else
                            {
                                result += (i + 2).ToString() + "," + "焊接日期" + "," + "此项为必填项!" + "|";
                            }
                            string row22 = pds.Rows[i][22].ToString();
                            if (!string.IsNullOrEmpty(row22))
                            {
                                try
                                {
                                    decimal d = Convert.ToDecimal(row22.Trim());
                                }
                                catch (Exception)
                                {
                                    result += (i + 2).ToString() + "," + "预热温度" + "," + "[" + row22 + "]错误!" + "|";
                                }
                            }
                            string row23 = pds.Rows[i][23].ToString();
                            if (!string.IsNullOrEmpty(row23))
                            {
                                try
                                {
                                    decimal d = Convert.ToDecimal(row23.Trim());
                                }
                                catch (Exception)
                                {
                                    result += (i + 2).ToString() + "," + "实际预热温度" + "," + "[" + row23 + "]错误!" + "|";
                                }
                            }
                            string row24 = pds.Rows[i][24].ToString();
                            if (!string.IsNullOrEmpty(row24))
                            {
                                try
                                {
                                    decimal d = Convert.ToDecimal(row24.Trim());
                                }
                                catch (Exception)
                                {
                                    result += (i + 2).ToString() + "," + "层间温度" + "," + "[" + row24 + "]错误!" + "|";
                                }
                            }
                            string row25 = pds.Rows[i][25].ToString();
                            if (!string.IsNullOrEmpty(row25))
                            {
                                try
                                {
                                    decimal d = Convert.ToDecimal(row25.Trim());
                                }
                                catch (Exception)
                                {
                                    result += (i + 2).ToString() + "," + "后热温度" + "," + "[" + row25 + "]错误!" + "|";
                                }
                            }
                        }
                        else
                        {
                            result += (i + 2).ToString() + "," + "焊口代号" + "," + "该焊口日报已存在!" + "|";
                        }
                    }
                    else
                    {
                        result += (i + 2).ToString() + "," + "焊口代号" + "," + "该焊口不存在!" + "|";
                    }
                    //a++;
                }
                if (!string.IsNullOrEmpty(result))
                {
                    result = result.Substring(0, result.LastIndexOf("|"));
                }
                errorInfos.Clear();
                if (!string.IsNullOrEmpty(result))
                {
                    string results = result;
                    List errorInfoList = results.Split('|').ToList();
                    foreach (var item in errorInfoList)
                    {
                        string[] errors = item.Split(',');
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = errors[0];
                        errorInfo.Column = errors[1];
                        errorInfo.Reason = errors[2];
                        errorInfos.Add(errorInfo);
                    }
                    if (errorInfos.Count > 0)
                    {
                        Grid1.DataSource = errorInfos;
                        Grid1.DataBind();
                    }
                }
                else
                {
                    ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
                }
            }
            else
            {
                throw new Exception("导入数据为空!");
            }
            return true;
        }
        #region 导入
        /// 
        /// 导入
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (errorInfos.Count <= 0)
            {
                string rootPath = Server.MapPath("~/");
                string initFullPath = rootPath + initPath;
                if (!Directory.Exists(initFullPath))
                {
                    Directory.CreateDirectory(initFullPath);
                }
                string filePath = initFullPath + this.hdfileName.Text;
                ImportXlsToData2(filePath);
            }
            else
            {
                Alert.ShowInTop("请先将错误数据修正,再重新导入保存!", MessageBoxIcon.Warning);
            }
        }
        #region 读Excel提取数据
        /// 
        /// 从Excel提取数据--》Dataset
        /// 
        /// Excel文件路径名
        private void ImportXlsToData2(string fileName)
        {
            //try
            //{
            //string oleDBConnString = String.Empty;
            //oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
            //oleDBConnString += "Data Source=";
            //oleDBConnString += fileName;
            //oleDBConnString += ";Extended Properties=Excel 8.0;";
            //OleDbConnection oleDBConn = null;
            //OleDbDataAdapter oleAdMaster = null;
            //DataTable m_tableName = new DataTable();
            //DataSet ds = new DataSet();
            //oleDBConn = new OleDbConnection(oleDBConnString);
            //oleDBConn.Open();
            //m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            //if (m_tableName != null && m_tableName.Rows.Count > 0)
            //{
            //    m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
            //}
            //string sqlMaster;
            //sqlMaster = " SELECT *  FROM [" + m_tableName.TableName + "]";
            //oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
            //oleAdMaster.SelectCommand.CommandTimeout = 1200;
            //oleAdMaster.Fill(ds, "m_tableName");
            //oleAdMaster.Dispose();
            //oleDBConn.Close();
            //oleDBConn.Dispose();
            //AddDatasetToSQL2(ds.Tables[0], 27);
            var dataTable = BLL.Common.NPOIHelper.ExcelToDataTable1(fileName);
            AddDatasetToSQL2(dataTable, 28);
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }
        #endregion
        /// 
        /// 将Dataset的数据导入数据库
        /// 
        /// 数据集
        /// 数据集列数
        /// 
        private bool AddDatasetToSQL2(DataTable pds, int Cols)
        {
            string result = string.Empty;
            string dReportID = string.Empty;
            string dateStr = string.Empty;
            int ic, ir;
            ic = pds.Columns.Count;
            if (ic < Cols)
            {
                throw new Exception("导入Excel格式错误!Excel只有" + ic.ToString().Trim() + "列");
            }
            ir = pds.Rows.Count;
            if (pds != null && ir > 0)
            {
                var oldViewInfos = from x in Funs.DB.View_JointInfoAndIsoInfo
                                   where x.ProjectId == this.CurrUser.LoginProjectId
                                   select x;
                var units = from x in Funs.DB.Base_Unit
                            join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId
                            where y.ProjectId == this.CurrUser.LoginProjectId
                            select x;
                var workAreas = from x in Funs.DB.ProjectData_WorkArea where x.ProjectId == this.CurrUser.LoginProjectId select x;
                var steels = from x in Funs.DB.Base_Material select x;
                var types = from x in Funs.DB.Base_WeldType select x;
                var methods = from x in Funs.DB.Base_WeldingMethod select x;
                var teamGroups = from x in Funs.DB.ProjectData_TeamGroup where x.ProjectId == this.CurrUser.LoginProjectId select x;
                var welders = from x in Funs.DB.BS_Welder select x;
                var weldMaterials = from x in Funs.DB.Base_Consumables select x;//焊接耗材
                Model.View_JointInfoAndIsoInfo oldViewInfo1 = new Model.View_JointInfoAndIsoInfo();
                oldViewInfo1 = oldViewInfos.Where(x => x.WorkAreaId == this.drpWorkArea.SelectedValue
                                          && x.ISO_IsoNo == pds.Rows[0][2].ToString().Trim()
                                          && x.JOT_JointNo == pds.Rows[0][3].ToString().Trim()).FirstOrDefault();
                for (int i = 0; i < ir; i++)
                {
                    Model.View_JointInfoAndIsoInfo oldViewInfo = new Model.View_JointInfoAndIsoInfo();
                    oldViewInfo = oldViewInfos.Where(x => x.WorkAreaId == this.drpWorkArea.SelectedValue
                                                       && x.ISO_IsoNo == pds.Rows[i][2].ToString().Trim()
                                                       && x.JOT_JointNo == pds.Rows[i][3].ToString().Trim()).FirstOrDefault();
                    if (oldViewInfo != null)
                    {
                        if (dateStr != pds.Rows[i][18].ToString().Trim())
                        {
                            Model.BO_WeldReportMain report = new Model.BO_WeldReportMain();
                            string isoId = oldViewInfo1.ISO_ID;
                            string bawId = BLL.PW_IsoInfoService.GetIsoInfoByIsoInfoId(isoId).WorkAreaId;
                            string bawCode = BLL.WorkAreaService.getWorkAreaByWorkAreaId(bawId).WorkAreaCode;
                            string welder = pds.Rows[i][15].ToString().Trim();
                            string perfix = bawCode + "-" + welder + "-";
                            string reportNo = BLL.SQLHelper.RunProcNewId("SpGetNewCode5ByProjectId", "dbo.BO_WeldReportMain", "JOT_DailyReportNo", this.CurrUser.LoginProjectId, perfix);
                            report.DReportID = SQLHelper.GetNewID(typeof(Model.BO_WeldReportMain));
                            report.ProjectId = this.CurrUser.LoginProjectId;
                            var installation = Funs.DB.Project_Installation.FirstOrDefault(e => e.ProjectId == this.CurrUser.LoginProjectId && e.InstallationCode == pds.Rows[i][1].ToString().Trim());
                            if (installation != null)
                            {
                                report.InstallationId = installation.InstallationId;
                            }
                            report.UnitId = units.FirstOrDefault(e => e.UnitCode == pds.Rows[i][0].ToString().Trim()).UnitId;
                            report.JOT_WeldDate = Convert.ToDateTime(pds.Rows[i][18].ToString().Trim());
                            report.JOT_DailyReportNo = reportNo;
                            report.CHT_Tabler = this.CurrUser.UserId;
                            report.CHT_TableDate = DateTime.Now;
                            BLL.WeldReportService.AddWeldReport(report);
                            dReportID = report.DReportID;
                            dateStr = pds.Rows[i][18].ToString().Trim();
                        }
                        Model.PW_JointInfo jointInfo = BLL.PW_JointInfoService.GetJointInfoByJotID(oldViewInfo.JOT_ID);
                        jointInfo.WLO_Code = pds.Rows[i][7].ToString().Trim() == "安装" ? "F" : "S";
                        jointInfo.JOT_JointAttribute = pds.Rows[i][8].ToString().Trim();
                        if (!string.IsNullOrEmpty(pds.Rows[i][9].ToString().Trim()))
                        {
                            jointInfo.JOT_DoneDin = Convert.ToDecimal(pds.Rows[i][9].ToString().Trim());
                        }
                        if (!string.IsNullOrEmpty(pds.Rows[i][10].ToString().Trim()))
                        {
                            jointInfo.JOT_JointDesc = pds.Rows[i][10].ToString().Trim();
                        }
                        if (!string.IsNullOrEmpty(pds.Rows[i][11].ToString().Trim()))
                        {
                            jointInfo.JOT_Sch = pds.Rows[i][11].ToString().Trim();
                        }
                        if (!string.IsNullOrEmpty(pds.Rows[i][12].ToString().Trim()))
                        {
                            var md = methods.FirstOrDefault(e => e.WeldingMethodCode == pds.Rows[i][12].ToString().Trim());
                            if (md != null)
                            {
                                jointInfo.WME_ID = md.WeldingMethodId;
                            }
                        }
                        jointInfo.DReportID = dReportID;
                        var unit = units.Where(x => x.UnitCode == pds.Rows[i][0].ToString().Trim()).FirstOrDefault();
                        if (string.IsNullOrEmpty(pds.Rows[i][14].ToString().Trim()))
                        {
                            var welder1 = welders.FirstOrDefault(e => e.WED_Unit == unit.UnitId && e.WED_Code == pds.Rows[i][15].ToString().Trim());
                            if (welder1 != null)
                            {
                                jointInfo.JOT_CellWelder = welder1.WED_ID;
                            }
                            var welder2 = welders.FirstOrDefault(e => e.WED_Unit == unit.UnitId && e.WED_Code == pds.Rows[i][16].ToString().Trim());
                            if (welder2 != null)
                            {
                                jointInfo.JOT_FloorWelder = welder2.WED_ID;
                            }
                        }
                        else
                        {
                            var teamGroup = teamGroups.Where(x => x.UnitId == unit.UnitId && x.TeamGroupCode == pds.Rows[i][14].ToString().Trim()).FirstOrDefault();
                            if (teamGroup != null)
                            {
                                var welder1 = welders.FirstOrDefault(e => e.WED_Unit == unit.UnitId && e.TeamGroupId == teamGroup.TeamGroupId && e.WED_Code == pds.Rows[i][15].ToString().Trim());
                                if (welder1 != null)
                                {
                                    jointInfo.JOT_CellWelder = welder1.WED_ID;
                                }
                                var welder2 = welders.FirstOrDefault(e => e.WED_Unit == unit.UnitId && e.TeamGroupId == teamGroup.TeamGroupId && e.WED_Code == pds.Rows[i][16].ToString().Trim());
                                if (welder2 != null)
                                {
                                    jointInfo.JOT_FloorWelder = welder2.WED_ID;
                                }
                            }
                        }
                        jointInfo.JOT_Location = pds.Rows[i][17].ToString().Trim();
                        jointInfo.JOT_Electricity = pds.Rows[i][19].ToString().Trim();   //电流                  
                        jointInfo.JOT_Voltage = pds.Rows[i][20].ToString().Trim();//电压 
                        jointInfo.WeldingSpeed = pds.Rows[i][21].ToString().Trim();//焊接速度
                        jointInfo.JOT_PrepareTemp = Funs.GetNewDecimal(pds.Rows[i][22].ToString().Trim());//预热温度
                        jointInfo.ActualPrepareTemp = Funs.GetNewDecimal(pds.Rows[i][23].ToString().Trim());//实际预热温度
                        jointInfo.JOT_CellTemp = Funs.GetNewDecimal(pds.Rows[i][24].ToString().Trim());//层间温度
                        jointInfo.JOT_LastTemp = Funs.GetNewDecimal(pds.Rows[i][25].ToString().Trim());//后热温度
                        jointInfo.JOT_HeartNo1 = pds.Rows[i][26].ToString().Trim();//炉批号1
                        jointInfo.JOT_HeartNo2 = pds.Rows[i][27].ToString().Trim();//炉批号2
                        jointInfo.JOT_JointStatus = "100";
                        //if (string.IsNullOrEmpty(jointInfo.JOT_Remark))
                        //{
                        //    jointInfo.JOT_Remark = pds.Rows[i][28].ToString().Trim();
                        //}
                        //else
                        //{
                        //    jointInfo.JOT_Remark = jointInfo.JOT_Remark+" "+ pds.Rows[i][28].ToString().Trim();
                        //}
                        //若是固定口,在焊口号后+G,若是活动口,去掉后面的G
                        if (jointInfo.JOT_JointAttribute == "固定")
                        {
                            jointInfo.JOT_JointNo = jointInfo.JOT_JointNo + "G";
                        }
                        else
                        {
                            if (jointInfo.JOT_JointNo.Last() == 'G')
                            {
                                jointInfo.JOT_JointNo = jointInfo.JOT_JointNo.Substring(0, jointInfo.JOT_JointNo.Length - 1);
                            }
                        }
                        BLL.PW_JointInfoService.UpdateJointInfo(jointInfo);
                    }
                }
                ShowNotify("导入成功!", MessageBoxIcon.Success);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                throw new Exception("导入数据为空!");
            }
            return true;
        }
        #endregion
    }
}