| using SyntacticSugar.constant; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Data; | 
| using System.Linq; | 
| using System.Threading.Tasks; | 
| using System.Web; | 
| using WebAPI.Exceptions; | 
| using WebAPI.WebBLL.JCJM.GYZL; | 
|   | 
| namespace WebAPI.WebBLL.JCJM.IMPL | 
| { | 
|     public class Gy_CustomerImpl : Gy_CustomerService | 
|     { | 
|         // 线程计数器 | 
|         List<Task> taskList = new List<Task>(); | 
|         SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); | 
|         #region 导入EXCEL | 
|         public void ImportExcel() | 
|         { | 
|             throw new NotImplementedException(); | 
|         } | 
|         #endregion | 
|   | 
|   | 
|         /// <summary> | 
|         /// 判断列 | 
|         /// </summary> | 
|         /// <param name="provisional"></param> | 
|         /// <returns></returns> | 
|         private static string JudgmentColumns(DataTable provisional) | 
|         { | 
|             var error = ""; | 
|             //查询没有的列 | 
|             if (!provisional.Columns.Contains("客户代码")) | 
|                 error += "没有找到【客户代码】的标题,"; | 
|   | 
|             if (!provisional.Columns.Contains("客户名称")) | 
|                 error += "没有找到【客户名称】的标题,"; | 
|             return error; | 
|         } | 
|   | 
|         #region 上传EXCEL | 
|         public DataTable UploadExcel(DataSet ExcelDs) | 
|         { | 
|             //创建临时表 | 
|             DataTable provisional = new DataTable("dt2"); | 
|   | 
|             //添加列名 | 
|             for (int i = 0; i < ExcelDs.Tables[0].Columns.Count; i++) | 
|             { | 
|                 provisional.Columns.Add(ExcelDs.Tables[0].Rows[0][i].ToString()); | 
|             } | 
|   | 
|             //添加数据 | 
|             for (int i = 1; i < ExcelDs.Tables[0].Rows.Count; i++) | 
|             { | 
|                 DataRow row = provisional.NewRow(); | 
|                 for (int j = 0; j < ExcelDs.Tables[0].Columns.Count; j++) | 
|                 { | 
|                     row[j] = ExcelDs.Tables[0].Rows[i][j].ToString(); | 
|                 } | 
|                 provisional.Rows.Add(row); | 
|             } | 
|   | 
|             //判断列 | 
|             string error = JudgmentColumns(provisional); | 
|             if (error.Length > 0) | 
|             { | 
|                 throw new ExcelException($"Excel模板存在错误,{error}\r\n"); | 
|             } | 
|   | 
|             for (int i = 0; i <= provisional.Rows.Count - 1; i++) | 
|             { | 
|                 string HRegionNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["地区代码"].ToString()); | 
|                 string HRegionName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["地区名称"].ToString()); | 
|                 string HCurrencyNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["默认货币代码"].ToString()); | 
|                 string HCurrencyName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["默认货币名称"].ToString()); | 
|                 //string HBillingNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["结算方式代码"].ToString()); | 
|                 //string HBillingName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["结算方式名称"].ToString()); | 
|   | 
|                 string HCusTypeNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["客户分类代码"].ToString()); | 
|                 string HCusTypeName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["客户分类名称"].ToString()); | 
|   | 
|                 //记录任务 | 
|                 var tasks = new List<Task>(); | 
|   | 
|                 //如果当前地区代码和地区名称不为空则判断是否存在 | 
|                 if (!String.IsNullOrEmpty(HRegionName) || !String.IsNullOrEmpty(HRegionNumber)) | 
|                 { | 
|                     tasks.Add(JudgeRegion(HRegionNumber, HRegionName)); | 
|                 } | 
|   | 
|                 // 如果当前货币代码和货币名称不为空则判断是否存在 | 
|                 if (!String.IsNullOrEmpty(HCurrencyName) || !String.IsNullOrEmpty(HCurrencyNumber)) | 
|                     { | 
|                         tasks.Add(JudgeCurrency(HCurrencyNumber, HCurrencyName)); | 
|                     } | 
|   | 
|                 //如果当前客户分类代码和名称 | 
|                 if (!String.IsNullOrEmpty(HCusTypeName) || !String.IsNullOrEmpty(HCusTypeNumber)) | 
|                     { | 
|                         tasks.Add(JudgeCusType(HCusTypeNumber, HCusTypeName)); | 
|                     } | 
|                 try | 
|                 { | 
|                     //等待任务完成 | 
|                     Task.WaitAll(tasks.ToArray()); | 
|                 } | 
|                 catch (AggregateException ae) | 
|                 { | 
|                     string msg = ae.Message; | 
|                     ae.Handle(ex => { | 
|                         if (ex is EmptyException) | 
|                             msg = ex.Message; | 
|                             Console.WriteLine(ex.Message); | 
|                         return ex is EmptyException; | 
|                     }); | 
|                     throw new Exception("第" + i +1 +"有问题: " +msg); | 
|                 } | 
|                    | 
|      | 
|                 //获取真实行数 | 
|                 int line = i + 1; | 
|             } | 
|             return provisional; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 判断地区是否正确 | 
|         /// </summary> | 
|         private Task<int> JudgeRegion(string HNumber, string HName) | 
|         { | 
|             return Task<int>.Factory.StartNew(() => | 
|             { | 
|                 try | 
|                 { | 
|                     SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); | 
|                     DataSet ds = oCN.RunProcReturn("select * from Gy_AreaSet where HNumber = '" + HNumber + "' and HName = '" + HName + "'", "Gy_AreaSet"); | 
|                     if (ds.Tables[0].Rows.Count == EmptyConstant.EMPTY) | 
|                     { | 
|                         throw new EmptyException("当前地区不存在,请重新选择"); | 
|                     } | 
|                     return (int)ds.Tables[0].Rows[0][0]; | 
|                 } | 
|                 catch (Exception e) | 
|                 { | 
|                     throw new EmptyException(e.Message); | 
|                 } | 
|             }); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 判断货币是否正确 | 
|         /// </summary> | 
|         private Task<int> JudgeCurrency(string HNumber, string HName) | 
|         { | 
|             { | 
|                 return Task<int>.Factory.StartNew(() => | 
|                 { | 
|                     try | 
|                     { | 
|                         SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); | 
|                         DataSet ds = oCN.RunProcReturn("select * from Gy_Currency where HNumber = '" + HNumber + "' and HName = '" + HName + "'", "Gy_Currency"); | 
|                         if (ds.Tables[0].Rows.Count == EmptyConstant.EMPTY) | 
|                         { | 
|                             throw new EmptyException("当前货币不存在,请重新选择"); | 
|                         } | 
|                         return (int)ds.Tables[0].Rows[0][0]; | 
|                     } | 
|                     catch (Exception e) | 
|                     { | 
|                         throw new EmptyException(e.Message); | 
|                     } | 
|                 }); | 
|             } | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 判断客户分类 | 
|         /// </summary> | 
|         private Task<int> JudgeCusType(string HNumber, string HName) | 
|         { | 
|             { | 
|                 return Task<int>.Factory.StartNew(() => | 
|                 { | 
|                     try | 
|                     { | 
|                         SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); | 
|                         DataSet ds = oCN.RunProcReturn("select * from Gy_CusType where HNumber = '" + HNumber + "' and HName = '" + HName + "'", "Gy_CusType"); | 
|                         if (ds.Tables[0].Rows.Count == EmptyConstant.EMPTY) | 
|                         { | 
|                             throw new EmptyException("当前分类不存在,请重新选择"); | 
|                         } | 
|                         return (int)ds.Tables[0].Rows[0][0]; | 
|                     } | 
|                     catch (Exception e) | 
|                     { | 
|                         throw new EmptyException(e.Message); | 
|                     } | 
|                 }); | 
|             } | 
|         } | 
|         #endregion | 
|     } | 
|   | 
| } |