1
wtt
5 天以前 b371b604a464e7249634bbdb45cda85dd5bd1f3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
    }
 
}