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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CY.IDAL;
using System.Data.SqlClient;
using System.Data;
using CY.Model;
using System.Transactions;
 
namespace CY.SQLDAL
{
    public class EC_SellerBusinessLimitsDAL : IEC_SellerBusinessLimitsDAL
    {
 
        private Database _dataBase = null;
 
        
 
        private SysInquiry_PrintingTypeDAL _sysInquiry_PrintingTypeDAL = null;
 
        public EC_SellerBusinessLimitsDAL()
        {
            _dataBase = new Database();
            _sysInquiry_PrintingTypeDAL = new SysInquiry_PrintingTypeDAL();
        }
 
        public EC_SellerBusinessLimitsDAL(Database dataBase)
        {
            _dataBase = dataBase;
            _sysInquiry_PrintingTypeDAL = new SysInquiry_PrintingTypeDAL();
        }
 
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InserModel(Infrastructure.Domain.IAggregateRoot model)
        {
            Model.EC_SellerBusinessLimits trueModel = model as Model.EC_SellerBusinessLimits;
            if (trueModel == null)
            {
                return false;
            }
            SysInquiry_PrintingTypeDAL _printingTypeDAL = new SysInquiry_PrintingTypeDAL();
            SysInquiry_PrintingType _printModel = _printingTypeDAL.GetModelById(trueModel.BusinessId.Value);
            trueModel.JoinFlag = _printModel.JoinFlag;
            trueModel.SpecialFlag = _printModel.SpecialFlag;
 
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@SellerId",trueModel.SellerId),
                    new SqlParameter("@BusinessId",trueModel.BusinessId),
                    new SqlParameter("@JoinFlag",trueModel.JoinFlag),
                    new SqlParameter("@SpecialFlag",trueModel.SpecialFlag),
                    new SqlParameter("@Status",trueModel.Status),
            };
            try
            {
                _dataBase.Query("sp_EC_SellerBusinessLimits_Insert", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
            //string sqlStr=string.Empty;
            //sqlStr=" INSERT INTO EC_SellerBusinessLimits ";
            //sqlStr += " SELECT '{0}',KeyId,joinFlag,specialFlag,0 FROM dbo.SysInquiry_PrintingType WHERE ParentId<>0 AND Status=1";
            //sqlStr = string.Format(sqlStr, trueModel.SellerId.ToString());
            //try
            //{
            //    _dataBase.Query(sqlStr,CommandType.Text,null);
            //}
            //catch(Exception ex)
            //{
            //    return false;
            //}
            //return true;
        }
 
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateModel(Infrastructure.Domain.IAggregateRoot model)
        {
            Model.EC_SellerBusinessLimits trueModel = model as Model.EC_SellerBusinessLimits;
            if (trueModel == null)
            {
                return false;
            }
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@BusinessId",trueModel.BusinessId),
                    new SqlParameter("@SellerId",trueModel.SellerId),
                    new SqlParameter("@Status",trueModel.Status)
            };
            try
            {
                _dataBase.Query("EC_SellerBusinessLimits_Update", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
        public bool UpdateModelDetail(EC_SellerBusinessLimits model)
        {
            if (model == null)
                return false;
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@BusinessId",model.BusinessId),
                    new SqlParameter("@SellerId",model.SellerId),
                    new SqlParameter("@JoinFlag",model.JoinFlag),
                    new SqlParameter("@SpecialFlag",model.SpecialFlag)
            };
            try
            {
                _dataBase.Query("EC_SellerBusinessLimitsDetail_Update", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateALL(Guid firmId)
        {
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@SellerId",firmId)
            };
            try
            {
                _dataBase.Query("EC_SellerBusinessLimits_UpdateAll", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
 
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteModel(Infrastructure.Domain.IAggregateRoot model)
        {
            Model.EC_SellerBusinessLimits trueModel = model as Model.EC_SellerBusinessLimits;
            if (trueModel == null)
            {
                return false;
            }
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@BusinessId",trueModel.BusinessId)
            };
            try
            {
                _dataBase.Query("sp_EC_SellerBusinessLimits_DeleteRow", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
        public bool DeleteModel(Guid sellerId)
        {
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@SellerId",sellerId)
            };
            try
            {
                _dataBase.Query("sp_EC_SellerBusinessLimits_DeleteBySellerId", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
        public IList<Model.SysInquiry_PrintingType> GetBusinessRangeByFirmId(Guid firmId)
        {
            string condition = string.Empty;
            condition = " SellerId='" + firmId.ToString() + "' and Status='true' ";
            IList<EC_SellerBusinessLimits> limitList = _dataBase.SelectModel<EC_SellerBusinessLimits>("*", "EC_SellerBusinessLimits", condition);
            IList<SysInquiry_PrintingType> list = new List<SysInquiry_PrintingType>();
            foreach (EC_SellerBusinessLimits limitModel in limitList)
            {
                    SysInquiry_PrintingType model = _sysInquiry_PrintingTypeDAL.GetModelById(limitModel.BusinessId.Value);
                    if (model != null)
                        list.Add(model);
            }
           return list;
        }
 
        public bool SaveBusinessRangeByFirmId(IList<Model.EC_SellerBusinessLimits> list)
        {
            bool isSuccess = true;
 
            if (list != null && list.Count > 0)
            {
                //EC_SellerBusinessLimits m = list[0];
                //if (!IsExist(m.SellerId))
                //{
                //    isSuccess = InserModel(m);
 
                //}
                ////isSuccess = UpdateALL(m.SellerId);
                //if (isSuccess)
                //{
                //    foreach (EC_SellerBusinessLimits model in list)
                //    {
                //        isSuccess = UpdateModel(model);
                //        if (!isSuccess)
                //            break;
                //    }
                //}
                EC_SellerBusinessLimits m = list[0];
                Guid sellerId=m.SellerId;
                isSuccess=DeleteModel(sellerId);
                if (isSuccess)
                {
                    foreach (EC_SellerBusinessLimits model in list)
                    {
                        isSuccess = InserModel(model);
                        if (!isSuccess)
                            break;
                    }
                    if (isSuccess)
                    {
                        if (IsExistsFastSetByFirm(sellerId))
                        {
                            isSuccess = DeleteFastSetByFirm(sellerId);
                            if (isSuccess)
                            {
                                isSuccess = InsertFastSetByFirm(sellerId);
                            }
                        }
 
                        if (isSuccess)
                        {
                            isSuccess = InsertFastSetByCustomer(sellerId);
                        }
                    }
                }
            }
            return isSuccess;
        }
 
        /// <summary>
        /// 判断厂商是否保存了快速设置
        /// </summary>
        /// <param name="firmId"></param>
        /// <returns></returns>
        private bool IsExistsFastSetByFirm(Guid firmId)
        {
             bool isSussess = false;
            string sqlStr=@"SELECT * FROM inquiry_FastSetByFirm WHERE FirmId=@FirmId";
            SqlParameter par = new SqlParameter("@FirmId",firmId.ToString());
            DataTable dt=_dataBase.QueryDataTable(sqlStr,CommandType.Text,par);
            if(dt!=null&&dt.Rows.Count>0)
            {
                isSussess=true;
            }
            return isSussess;
        }
 
        /// <summary>
        /// 判断厂商的客户是否保存了快速设置,有就返回客户ID列表
        /// </summary>
        /// <param name="firmId"></param>
        /// <returns></returns>
        private bool IsExistsFastSetByCustomer(Guid firmId,IList<string> CustomerIds)
        {
            bool isSussess = false;
            string sqlStr = @"SELECT DISTINCT CustomerId FROM inquiry_FastSetByFirm WHERE FirmId=@FirmId and CustomerId is not null";
            SqlParameter par = new SqlParameter("@FirmId", firmId.ToString());
            DataTable dt = _dataBase.QueryDataTable(sqlStr, CommandType.Text, par);
            if (dt != null && dt.Rows.Count > 0)
            {
                isSussess = true;
                foreach (DataRow dr in dt.Rows)
                {
                    CustomerIds.Add(dr["CustomerId"].ToString());
                }
               
            }
            return isSussess;
        }
 
        /// <summary>
        /// 移除没有权限的快速设置
        /// </summary>
        /// <param name="firmId"></param>
        /// <returns></returns>
        private bool DeleteFastSetByFirm(Guid firmId)
        {
            bool isSussess = true;
            string sqlStr = @"DELETE inquiry_FastSetByFirm WHERE FirmId=@FirmId AND PrintTypeId NOT IN (SELECT BusinessId FROM EC_SellerBusinessLimits WHERE SellerId=@FirmId)";
            SqlParameter par = new SqlParameter("@FirmId",firmId.ToString());
            try
            {
                int i= _dataBase.ExecuteSql(sqlStr,par);
                if (i > 0)
                {
                    isSussess = true;
                }
            }
            catch(Exception ex)
            {
                isSussess = false;
            }
            return isSussess;
        }
 
        /// <summary>
        /// 给厂商新增有权限的快速设置数据
        /// </summary>
        /// <param name="firmId"></param>
        /// <returns></returns>
        private bool InsertFastSetByFirm(Guid firmId)
        {
            bool isSussess = true;
            string sqlStr = @"INSERT INTO inquiry_FastSetByFirm
                                SELECT @FirmId,NULL,100,BusinessId
                                FROM EC_SellerBusinessLimits
                                WHERE SellerId=@FirmId AND BusinessId NOT IN (
                                    SELECT PrintTypeId FROM inquiry_FastSetByFirm WHERE FirmId=@FirmId AND CustomerId IS NULL
                                )";
            SqlParameter par = new SqlParameter("@FirmId", firmId.ToString());
            try
            {
                int i = _dataBase.ExecuteSql(sqlStr, par);
                if (i > 0)
                {
                    isSussess = true;
                }
            }
            catch(Exception ex)
            {
                isSussess = false;
            }
            return isSussess;
        }
 
        /// <summary>
        /// 给已保存快速设置的客户新增有权限的快速设置数据
        /// </summary>
        /// <param name="firmId"></param>
        /// <returns></returns>
        private bool InsertFastSetByCustomer(Guid firmId)
        {
            bool isSussess = true;
            IList<string> CustomerIds = new List<string>();
            if (IsExistsFastSetByCustomer(firmId, CustomerIds))
            {
                try
                {
                    foreach (string customerId in CustomerIds)
                    {
                        string sqlStr = @"INSERT INTO inquiry_FastSetByFirm
                                        SELECT @FirmId,@CutomerId,100,BusinessId
                                        FROM EC_SellerBusinessLimits
                                        WHERE SellerId=@FirmId AND BusinessId NOT IN (
                                            SELECT PrintTypeId FROM inquiry_FastSetByFirm WHERE FirmId=@FirmId
                                            AND CustomerId=@CutomerId
                                        )";
                        SqlParameter[] pars = new SqlParameter[]{
                        new SqlParameter("@FirmId",firmId.ToString()),
                        new SqlParameter("@CutomerId",customerId),
                    };
                        int i = _dataBase.ExecuteSql(sqlStr, pars);
                        if (i > 0)
                        {
                            isSussess = true;
                        }
                    }
                }
                catch(Exception ex)
                {
                    isSussess = false;
                }
            }
            else
            {
                isSussess = true;
            }
            return isSussess;
        }
 
        /// <summary>
        /// 判断厂商是否有数据
        /// </summary>
        /// <param name="firmId"></param>
        /// <returns></returns>
        public bool IsExist(Guid firmId)
        {
            string condition = string.Empty;
            condition = " SellerId='" + firmId.ToString() + "' ";
            IList<EC_SellerBusinessLimits> limitList = _dataBase.SelectModel<EC_SellerBusinessLimits>("*", "EC_SellerBusinessLimits", condition);
            if (limitList != null && limitList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
 
        /// <summary>
        /// 根据厂商Id获取其业务范围明细
        /// </summary>
        /// <param name="firmId"></param>
        /// <returns></returns>
        public IList<EC_SellerBusinessLimits> GetBusinessRangeDetaiByFirmId(Guid firmId)
        {
            string condition = string.Empty;
            condition = " SellerId='" + firmId.ToString() + "' and status='true'";
            IList<EC_SellerBusinessLimits> limitList = _dataBase.SelectModel<EC_SellerBusinessLimits>("*", "EC_SellerBusinessLimits", condition);
            return limitList;
        }
 
        /// <summary>
        /// 保存厂商设置的业务范围明细
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool SaveBusinessRangeDetailByFirmId(IList<EC_SellerBusinessLimits> list)
        {
            bool isSuccess = true;
            using (TransactionScope scope = new TransactionScope())
            {
                foreach (EC_SellerBusinessLimits model in list)
                {
                    isSuccess = UpdateModelDetail(model);
                    if (!isSuccess)
                        break;
                }
                if (isSuccess)
                    scope.Complete();
            }
            return isSuccess;
        }
    }
}