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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
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;
 
namespace CY.SQLDAL
{
    public class Pay_IncExpRecordDAL : IPay_IncExpRecordDAL
    {
 
        private Database _dataBase = null;
 
        public Pay_IncExpRecordDAL()
        {
            _dataBase = new Database();
        }
        public Pay_IncExpRecordDAL(Database database)
        {
            _dataBase = database;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InserModel(Infrastructure.Domain.IAggregateRoot model)
        {
            Model.Pay_IncExpRecord trueModel = model as Model.Pay_IncExpRecord;
            if (trueModel == null)
            {
                return false;
            }
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                new SqlParameter("@PayId",trueModel.PayId),
                    new SqlParameter("@TradingName",trueModel.TradingName),
                    new SqlParameter("@TradingType",trueModel.TradingType),
                    new SqlParameter("@TradingState",trueModel.TradingState),
                    new SqlParameter("@TradingMoney",trueModel.TradingMoney),
                    new SqlParameter("@ResidualMoney",trueModel.ResidualMoney),
                    new SqlParameter("@PayerName",trueModel.PayerName),
                    new SqlParameter("@PayeeId",trueModel.PayeeId),
                    new SqlParameter("@PayeeName",trueModel.PayeeName),
                    new SqlParameter("@CreateTime",trueModel.CreateTime),
                    new SqlParameter("@Remark",trueModel.Remark),
                    new SqlParameter("@BalanceAccount",trueModel.BalanceAccount)
            };
            try
            {
                _dataBase.Query("sp_Pay_IncExpRecord_Insert", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateModel(Infrastructure.Domain.IAggregateRoot model)
        {
            Model.Pay_IncExpRecord trueModel = model as Model.Pay_IncExpRecord;
            if (trueModel == null)
            {
                return false;
            }
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {    new SqlParameter("@Keyid",trueModel.Keyid),
                new SqlParameter("@PayId",trueModel.PayId),
                    new SqlParameter("@TradingName",trueModel.TradingName),
                    new SqlParameter("@TradingType",trueModel.TradingType),
                    new SqlParameter("@TradingState",trueModel.TradingState),
                    new SqlParameter("@TradingMoney",trueModel.TradingMoney),
                    new SqlParameter("@ResidualMoney",trueModel.ResidualMoney),
                    new SqlParameter("@PayerName",trueModel.PayerName),
                    new SqlParameter("@PayeeId",trueModel.PayeeId),
                    new SqlParameter("@PayeeName",trueModel.PayeeName),
                    new SqlParameter("@CreateTime",trueModel.CreateTime),
                    new SqlParameter("@Remark",trueModel.Remark) 
            };
            try
            {
                _dataBase.Query("sp_Pay_IncExpRecord_Update", 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.Pay_IncExpRecord trueModel = model as Model.Pay_IncExpRecord;
            if (trueModel == null)
            {
                return false;
            }
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@Keyid",trueModel.Keyid)
            };
            try
            {
                _dataBase.Query("sp_Pay_IncExpRecord_DeleteRow", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public IEnumerable<Model.Pay_IncExpRecord> SelectModelPage(Infrastructure.Query.Query query, Infrastructure.Query.Pagination pagination)
        {
            throw new NotImplementedException();
        }
 
        /// <summary>
        /// 根据账户编号分页查询(type:支出-1收入1)
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="PayId"></param>
        /// <returns></returns>
        public IEnumerable<Model.Pay_IncExpRecord> SelectModelPage(Infrastructure.Query.Pagination pagination, int? PayId,int? Type)
        {
            if (PayId == null || PayId <= 0)
                return null;//错误数据返会空 
            try
            {
                string condtion = "";
                switch (Type)
                {
                    case -1:
                        condtion = string.Format(" PayId = '{0}' and TradingType={1}", PayId, Type);
                        break;
                    case 1:
                        condtion = string.Format(" PayeeId ='{0}'  and TradingType={1}", PayId, Type);
                        break;
                    default:
                        break;
                }
 
                IList<Pay_IncExpRecord> result = _dataBase.SelectModelPage<Model.Pay_IncExpRecord>(pagination, "*", "Pay_IncExpRecord", "CreateTime DESC", "CreateTime DESC", condtion) as IList<Pay_IncExpRecord>;//执行查询
                return null == result ? null : result;//返回结果
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 单个查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public IEnumerable<Model.Pay_IncExpRecord> SelectAllModel(Infrastructure.Query.Query query)
        {
            throw new NotImplementedException();
        }
 
        /// <summary>
        /// 分页返回全部流水账
        /// </summary>
        /// <param name="query"></param>
        /// <param name="payId"></param>
        /// <returns></returns>
        public IEnumerable<Model.Pay_IncExpRecord> SelectAllModel(Infrastructure.Query.Pagination pagination, int? PayId)
        {
            if (PayId == null || PayId <= 0)
                return null;//错误数据返会空 
            try
            {
                string condtion = "";
                condtion = string.Format("  (TradingType=-1 AND PayId={0}) OR (TradingType=1 AND PayeeId={0}) ", PayId);
                IList<Pay_IncExpRecord> result = _dataBase.SelectModelPage<Model.Pay_IncExpRecord>(pagination, "*", "Pay_IncExpRecord", "CreateTime DESC", "CreateTime DESC,TradingType desc", condtion) as IList<Pay_IncExpRecord>;//执行查询
                return null == result ? null : result;//返回结果
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
        }
 
        /// <summary>
        /// 根据条件分页返回全部流水账
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="payId"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="tradingType"></param>
        /// <param name="balanceAccount"></param>
        /// <param name="memberType"></param>
        /// <param name="memberName"></param>
        /// <returns></returns>
        public IEnumerable<Model.Pay_IncExpRecord> SelectAllModelByPaging(Infrastructure.Query.Pagination pagination, int? payId, string startDate, string endDate, int tradingType, int balanceAccount, string memberType, string memberName, string province, string city, string county)
        {
            if (payId == null || payId <= 0)
                return null;//错误数据返会空 
            try
            {
                string selectTarget = "t.*";
                string fromSouce = @"(
                                SELECT a.*,c.MemberType AS MemberTypeName,c.Name AS MemberName,d.Name as BalanceAccountName,c.Province,c.City,c.County
                                FROM Pay_IncExpRecord a INNER JOIN dbo.Pay_PaymentAccount b
                                ON a.PayId=b.keyid INNER JOIN dbo.EC_MemberBasic c
                                ON b.MemberId=c.MemberId left join Sys_Dictionary d on a.BalanceAccount=d.meanvalue
                                and d.DicType='收支科目'
                                WHERE TradingType=1
                                UNION
                                SELECT a.*,c.MemberType AS MemberTypeName,c.Name AS MemberName,d.Name as BalanceAccountName,c.Province,c.City,c.County
                                FROM Pay_IncExpRecord a INNER JOIN dbo.Pay_PaymentAccount b
                                ON a.PayeeId=b.keyid INNER JOIN dbo.EC_MemberBasic c
                                ON b.MemberId=c.MemberId left join Sys_Dictionary d on a.BalanceAccount=d.meanvalue
                                and d.DicType='收支科目'
                                WHERE TradingType=-1
                                ) t ";
                fromSouce += string.Format(" where ((t.TradingType=-1 AND t.PayId={0}) OR (t.TradingType=1 AND t.PayeeId={0}) or (t.TradingType=-1 AND t.PayeeId={0})) ", payId);
                if (!string.IsNullOrEmpty(startDate))
                {
                    fromSouce += " and t.CreateTime>='" + startDate + "' ";
                }
                if (!string.IsNullOrEmpty(endDate))
                {
                    fromSouce += " and t.CreateTime<'" + endDate + "' ";
                }
                if (tradingType!=0)
                {
                    fromSouce += " and t.TradingType='" + tradingType + "' ";
                }
                if (balanceAccount != -1)
                {
                    fromSouce += " and t.balanceAccount='" + balanceAccount + "' ";
                }
                if (!string.IsNullOrEmpty(memberType))
                {
                    fromSouce += " and t.MemberTypeName='" + memberType + "' ";
                }
                if (!string.IsNullOrEmpty(memberName))
                {
                    fromSouce += " and t.MemberName like '%" + memberName + "%'";
                }
                if (!string.IsNullOrEmpty(province))
                {
                    fromSouce += " and t.Province='"+province+"'";
                }
                if (!string.IsNullOrEmpty(city))
                {
                    fromSouce += " and t.City='" + city + "'";
                }
                if (!string.IsNullOrEmpty(county))
                {
                    fromSouce += " and t.County='" + county + "'";
                }
                IList<Pay_IncExpRecord> result = _dataBase.SelectModelPage<Model.Pay_IncExpRecord>(pagination, selectTarget, fromSouce, "t.CreateTime DESC", "CreateTime DESC,Keyid desc", string.Empty) as IList<Pay_IncExpRecord>;//执行查询
                return null == result ? null : result;//返回结果
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 获取已收佣金明细
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="payId"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="county"></param>
        /// <returns></returns>
        public IEnumerable<Model.Pay_IncExpRecord> SelectAllModelByPaging(Infrastructure.Query.Pagination pagination, int? payId, string startDate, string endDate, string province, string city, string county)
        {
            if (payId == null || payId <= 0)
                return null;//错误数据返会空 
            try
            {
                string selectTarget = "t.*";
                string fromSouce = @"(
                                SELECT a.*,c.MemberType AS MemberTypeName,c.Name AS MemberName,d.Name as BalanceAccountName,c.Province,c.City,c.County
                                FROM Pay_IncExpRecord a INNER JOIN dbo.Pay_PaymentAccount b
                                ON a.PayId=b.keyid INNER JOIN dbo.EC_MemberBasic c
                                ON b.MemberId=c.MemberId left join Sys_Dictionary d on a.BalanceAccount=d.meanvalue
                                and d.DicType='收支科目'
                                WHERE TradingType=1
                                UNION
                                SELECT a.*,c.MemberType AS MemberTypeName,c.Name AS MemberName,d.Name as BalanceAccountName,c.Province,c.City,c.County
                                FROM Pay_IncExpRecord a INNER JOIN dbo.Pay_PaymentAccount b
                                ON a.PayeeId=b.keyid INNER JOIN dbo.EC_MemberBasic c
                                ON b.MemberId=c.MemberId left join Sys_Dictionary d on a.BalanceAccount=d.meanvalue
                                and d.DicType='收支科目'
                                WHERE TradingType=-1
                                ) t ";
                fromSouce += string.Format(" where ((t.TradingType=-1 AND t.PayId={0}) OR (t.TradingType=1 AND t.PayeeId={0}) or (t.TradingType=-1 AND t.PayeeId={0})) ", payId);
                if (!string.IsNullOrEmpty(startDate))
                {
                    fromSouce += " and t.CreateTime>='" + startDate + "' ";
                }
                if (!string.IsNullOrEmpty(endDate))
                {
                    fromSouce += " and t.CreateTime<'" + endDate + "' ";
                }
                
                fromSouce += " and t.TradingType=1 ";
 
                fromSouce += " and (t.balanceAccount=3 or t.balanceAccount=10) ";
 
 
                if (!string.IsNullOrEmpty(province))
                {
                    fromSouce += " and t.Province='" + province + "'";
                }
                if (!string.IsNullOrEmpty(city))
                {
                    fromSouce += " and t.City='" + city + "'";
                }
                if (!string.IsNullOrEmpty(county))
                {
                    fromSouce += " and t.County='" + county + "'";
                }
                IList<Pay_IncExpRecord> result = _dataBase.SelectModelPage<Model.Pay_IncExpRecord>(pagination, selectTarget, fromSouce, "t.CreateTime DESC", "CreateTime DESC,Keyid desc", string.Empty) as IList<Pay_IncExpRecord>;//执行查询
                return null == result ? null : result;//返回结果
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 查询欠款明细
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="county"></param>
        /// <returns></returns>
        public IEnumerable<Pay_OwedMoney> selectAllOwedMoneyList(Infrastructure.Query.Pagination pagination,string startDate, string endDate, string province, string city, string county)
        {
            string selectTarget = " c.Name as MemberName,a.OwedAmount,a.Subject,a.CreateTime ";
 
            string fromSouce = @" dbo.Pay_OwedMoney a INNER JOIN dbo.Pay_PaymentAccount b
                        ON a.Payid=b.Keyid INNER JOIN dbo.EC_MemberBasic c ON c.MemberId=b.MemberId
                        WHERE a.IsEffective=1";
            if (!string.IsNullOrEmpty(startDate))
            {
                fromSouce += " and  a.CreateTime>='" + startDate + "'";
            }
            if (!string.IsNullOrEmpty(endDate))
            {
                fromSouce += " and  a.CreateTime<'" + endDate + "'";
            }
            if (!string.IsNullOrEmpty(province))
            {
                fromSouce += " and c.Province='" + province + "'";
            }
            if (!string.IsNullOrEmpty(city))
            {
                fromSouce += " and c.City='" + city + "'";
            }
            if (!string.IsNullOrEmpty(county))
            {
                fromSouce += " and c.County='" + county + "'";
            }
 
            var result = _dataBase.SelectModelPage<Model.Pay_OwedMoney>(pagination, selectTarget, fromSouce, " a.CreateTime DESC ", " CreateTime DESC ", string.Empty);//执行查询
            return null == result ? null : result;//返回结果
        }
 
 
        /// <summary>
        /// 查询时间段内会员授信情况
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="currentPayId"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="selectProvince"></param>
        /// <param name="selectCity"></param>
        /// <param name="selectCounty"></param>
        /// <returns></returns>
        public IEnumerable<Model.Pay_IncExpRecord> SelectAllCreditByPaging(Infrastructure.Query.Pagination pagination, int currentPayId, string startDate, string endDate, string selectProvince, string selectCity, string selectCounty)
        {
            if (currentPayId <= 0)
                return null;//错误数据返会空 
            try
            {
                string WhereString = " 1=1 ";
                string WhereString1 = "";
                string WhereString2 = "";
                string selectTarget = "t.*";
                if (!string.IsNullOrEmpty(selectProvince))
                {
                    WhereString += " and a.Province='" + selectProvince + "' ";
                }
 
                if (!string.IsNullOrEmpty(selectCity))
                {
                    WhereString += " and a.City='" + selectCity + "' ";
                }
 
                if (!string.IsNullOrEmpty(selectCounty))
                {
                    WhereString += " and a.County='" + selectCounty + "' ";
                }
                if (!string.IsNullOrEmpty(startDate))
                {
                    WhereString2 += " and  d.CreateTime >= '" + startDate + "' ";
                }
                if (!string.IsNullOrEmpty(endDate))
                {
                    WhereString1 += " and  c.CreateTime< '" + endDate + "' ";
                    WhereString2 += " and  d.CreateTime< '" + endDate + "' ";
                }
 
                string fromSouce = @" ( select * from  ( SELECT a.Name as TradingName,SUM(ISNULL(c.TradingMoney,0)) AS TradingMoney,SUM(ISNULL(d.TradingMoney,0)) AS ResidualMoney FROM dbo.EC_MemberBasic a INNER JOIN dbo.Pay_PaymentAccount b ON a.MemberId = b.MemberId LEFT JOIN dbo.Pay_CreditRecard c ON b.Keyid = c.PayId AND c.PayId <> 23 " + WhereString1 + " LEFT JOIN dbo.Pay_IncExpRecord d ON b.Keyid = d.PayId AND d.TradingType=-1 AND d.PayId=23 AND d.BalanceAccount=9 " + WhereString2 + " where " + WhereString + " GROUP BY a.Name ) as m where m.TradingMoney > 0 or ( m.TradingMoney = 0 and  m.ResidualMoney <> 0) ) t ";
 
                IList<Pay_IncExpRecord> result = _dataBase.SelectModelPage<Model.Pay_IncExpRecord>(pagination, selectTarget, fromSouce, "t.TradingName DESC", "TradingName DESC", string.Empty) as IList<Pay_IncExpRecord>;//执行查询
                return null == result ? null : result;//返回结果
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 查询时间段内会员授信使用
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="currentPayId"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="selectProvince"></param>
        /// <param name="selectCity"></param>
        /// <param name="selectCounty"></param>
        /// <returns></returns>
        public IEnumerable<Model.Pay_IncExpRecord> SelectUseCreditByPaging(Infrastructure.Query.Pagination pagination, int currentPayId, string startDate, string endDate, string selectProvince, string selectCity, string selectCounty)
        {
            if (currentPayId <= 0)
                return null;//错误数据返会空 
            try
            {
                string WhereString = "";
                string selectTarget = "t.*";
                if (!string.IsNullOrEmpty(selectProvince))
                {
                    WhereString += " and a.Province='" + selectProvince + "' ";
                }
 
                if (!string.IsNullOrEmpty(selectCity))
                {
                    WhereString += " and a.City='" + selectCity + "' ";
                }
 
                if (!string.IsNullOrEmpty(selectCounty))
                {
                    WhereString += " and a.County='" + selectCounty + "' ";
                }
                if (!string.IsNullOrEmpty(startDate))
                {
                    WhereString += " and  d.CreateTime >= '" + startDate + "' ";
                }
                if (!string.IsNullOrEmpty(endDate))
                {
                    WhereString += " and  d.CreateTime< '" + endDate + "' ";
                }
 
                string fromSouce = @"( SELECT d.* FROM dbo.Pay_IncExpRecord d LEFT JOIN dbo.EC_MemberBasic a INNER JOIN dbo.Pay_PaymentAccount b ON a.MemberId = b.MemberId ON b.Keyid = d.PayId WHERE d.TradingType=-1 AND d.PayId=23 AND d.BalanceAccount=9 " + WhereString + " ) t ";
 
                IList<Pay_IncExpRecord> result = _dataBase.SelectModelPage<Model.Pay_IncExpRecord>(pagination, selectTarget, fromSouce, "t.TradingName DESC", "TradingName DESC", string.Empty) as IList<Pay_IncExpRecord>;//执行查询
                return null == result ? null : result;//返回结果
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}