username@email.com
2025-05-21 a980cd04341d71216e0f59bd4b7327fe9fc50032
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CY.IDAL.Inquiry;
using CY.Model;
using System.Data.SqlClient;
using System.Data;
using System.Transactions;
using CY.Infrastructure.Common;
 
namespace CY.SQLDAL
{
    /// <summary>
    /// 品牌相关操作--SQL数据库操作
    /// </summary>
    public class BrandInfoDAL : IBrandInfoDAL
    {
        private Database _dataBase = null;
 
        public BrandInfoDAL()
        {
            _dataBase = new Database();
        }
 
        public BrandInfoDAL(Database dataBase)
        {
            _dataBase = dataBase;
        }
 
        /// <summary>
        /// 新增品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InserModel(Infrastructure.Domain.IAggregateRoot model)
        {
            Model.SysInquiry_BrandInfo trueModel = model as Model.SysInquiry_BrandInfo;
            if (trueModel == null)
            {
                return false;
            }
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@BrandName",trueModel.BrandName),
                    new SqlParameter("@Status",trueModel.Status),
                    new SqlParameter("@OrderNum",trueModel.OrderNum),
                    new SqlParameter(){ParameterName="@KeyId", SqlDbType=SqlDbType.Int,Direction=ParameterDirection.Output}
            };
            try
            {
                _dataBase.Query("SysInquiry_BrandInfo_ADD", 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.SysInquiry_BrandInfo trueModel = model as Model.SysInquiry_BrandInfo;
            if (trueModel == null)
            {
                return false;
            }
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                    new SqlParameter("@BrandName",trueModel.BrandName),
                    new SqlParameter("@Status",trueModel.Status),
                    new SqlParameter("@KeyId",trueModel.KeyId),
                    new SqlParameter("@OrderNum",trueModel.OrderNum)
            };
            try
            {
                _dataBase.Query("SysInquiry_BrandInfo_Update", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool DeleteModel(Infrastructure.Domain.IAggregateRoot model)
        {
            Model.SysInquiry_BrandInfo trueModel = model as Model.SysInquiry_BrandInfo;
            if (trueModel == null)
            {
                return false;
            }
            IList<SqlParameter> sqlParms = new List<SqlParameter>()
            {
                 new SqlParameter("@KeyId",trueModel.KeyId)
            };
            try
            {
                _dataBase.Query("SysInquiry_BrandInfo_Delete", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }
 
        /// <summary>
        /// 分页返回品牌列表
        /// </summary>
        /// <param name="brandName">品牌名称</param>
        /// <param name="pagination">分页参数</param>
        /// <returns></returns>
        public IEnumerable<Model.SysInquiry_BrandInfo> SelectModelPage(int paperTypeId, int paperId, string brandName, int status, Infrastructure.Query.Pagination pagination)
        {
            //string condition=string.Empty;
            //condition = " 1=1 ";
            //if(!string.IsNullOrEmpty(brandName))
            //{
            //    condition +=" and BrandName like '%"+brandName+"%' ";
            //}
            //if (status != -1)
            //{
            //    condition += " and Status="+status;
            //}
            //return _dataBase.SelectModelPage<SysInquiry_BrandInfo>(pagination, "*", "SysInquiry_BrandInfo", "OrderNum", "OrderNum", condition);
            string selectTarget = @" t.* ";
            string fromSouce = @" ( select  distinct a.*,c.PaperName,e.OrderNum as paperTypeOrderNum,c.OrderNum AS PaperOrderNum,
                                    CASE 
                                    WHEN d.KeyId IS NULL THEN 0
                                    ELSE 1
                                    END AS IsDefaultBrand
                                from 
                                dbo.SysInquiry_BrandInfo a LEFT JOIN dbo.SysInquiry_BrandListByPaper b ON a.KeyId=b.BrandId
                                LEFT JOIN dbo.SysInquiry_PaperInfo c ON b.PaperId=c.PapeId and c.status='true'
                                LEFT JOIN dbo.SysInquiry_PaperInfo d ON a.KeyId=d.DefaultBrandId 
                                left join SysInquiry_PaperType e on e.KeyId=c.PaperTypeId
                                where 1=1 ";
 
            if (!string.IsNullOrEmpty(brandName))
            {
                fromSouce += " and a.BrandName like '%" + brandName + "%' ";
            }
            if (status != -1)
            {
                fromSouce += " and a.Status=" + status;
            }
            if (paperTypeId != -1)
            {
                fromSouce += " and c.PaperTypeId=" + paperTypeId;
            }
 
            if (paperId != -1)
            {
                fromSouce += " and b.PaperId=" + paperId;
            }
            fromSouce += " ) as t";
            IEnumerable<Model.SysInquiry_BrandInfo> list = _dataBase.SelectModelPage<SysInquiry_BrandInfo>(pagination, selectTarget, fromSouce, "t.paperTypeOrderNum,t.PaperOrderNum,t.OrderNum", "paperTypeOrderNum,PaperOrderNum,OrderNum");
            return list;
        }
 
        /// <summary>
        /// 根据主键返回品牌实体
        /// </summary>
        /// <param name="keyid">主键ID</param>
        /// <returns>品牌实体</returns>
        public SysInquiry_BrandInfo SelectModelByKey(int keyid)
        {
            //string condition=string.Empty;
            //condition=" KeyId="+keyid;
            //IList<SysInquiry_BrandInfo> result = _dataBase.SelectModel<SysInquiry_BrandInfo>("*", "SysInquiry_BrandInfo", condition);
            //return null == result || result.Count == 0 ? null : result[0];
 
            string selectTarget = @"a.*,c.PaperName,c.PaperTypeId,c.PapeId,
                                    CASE 
                                    WHEN d.KeyId IS NULL THEN 0
                                    ELSE 1
                                    END AS IsDefaultBrand";
            string fromSouce = @" dbo.SysInquiry_BrandInfo a LEFT JOIN dbo.SysInquiry_BrandListByPaper b ON a.KeyId=b.BrandId
                                LEFT JOIN dbo.SysInquiry_PaperInfo c ON b.PaperId=c.PapeId 
                                LEFT JOIN dbo.SysInquiry_PaperInfo d ON a.KeyId=d.DefaultBrandId ";
            string condition = string.Empty;
            condition = " a.KeyId=" + keyid;
            IList<SysInquiry_BrandInfo> result = _dataBase.SelectModel<SysInquiry_BrandInfo>(selectTarget, fromSouce, condition);
            return null == result || result.Count == 0 ? null : result[0];
        }
 
        /// <summary>
        /// 判断是否有相同的品牌名称
        /// </summary>
        /// <param name="brandName"></param>
        /// <returns></returns>
        public bool IsExistsBrandName(string brandName, int PaperId)
        {
            string condition = string.Empty;
            condition = " a.BrandName='" + brandName + "' and b.PaperId='" + PaperId + "'";
            IList<SysInquiry_BrandInfo> result = _dataBase.SelectModel<SysInquiry_BrandInfo>(" a.* ", "SysInquiry_BrandInfo as a left join SysInquiry_BrandListByPaper as b on a.KeyId = b.BrandId", condition);
            if (result != null && result.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        /// 批量删除品牌
        /// </summary>
        /// <param name="keyIds"></param>
        /// <returns></returns>
        public bool DeleteBrandInfoList(List<int> keyIds)
        {
            bool isSuccess = true;
            using (TransactionScope scope = new TransactionScope())
            {
                foreach (int keyId in keyIds)
                {
                    SysInquiry_BrandInfo model = SelectModelByKey(keyId);
                    isSuccess = DeleteModel(model);
                    if (!isSuccess)
                        break;
                }
                if (isSuccess)
                    scope.Complete();
            }
            return isSuccess;
        }
 
        /// <summary>
        /// 返回所有有效的品牌列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SysInquiry_BrandInfo> GetBrandList()
        {
            string condition = string.Empty;
            condition = " Status=1 order by OrderNum ";
            IList<SysInquiry_BrandInfo> result = _dataBase.SelectModel<SysInquiry_BrandInfo>("*", "SysInquiry_BrandInfo", condition);
            return result;
        }
 
        public bool InsertBrandModel(SysInquiry_BrandInfo model, SysInquiry_BrandListByPaper brandPaper, SysInquiry_PaperInfo paperModel)
        {
            bool isSuccess = true;
            PaperInfoDAL _paperInfoDAL = new PaperInfoDAL(_dataBase);
            using (TransactionScope scope = new TransactionScope())
            {
                if (model == null)
                {
                    isSuccess = false;
                }
                else
                {
                    IList<SqlParameter> sqlParms = new List<SqlParameter>()
                    {
                            new SqlParameter("@BrandName",model.BrandName),
                            new SqlParameter("@Status",model.Status),
                            new SqlParameter("@OrderNum",model.OrderNum),
                            new SqlParameter(){ParameterName="@KeyId", SqlDbType=SqlDbType.Int,Direction=ParameterDirection.Output}
                    };
                    try
                    {
                        _dataBase.Query("SysInquiry_BrandInfo_ADD", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
                        model.KeyId = sqlParms[3].Value.ToInt32().Value;
                        brandPaper.BrandId = model.KeyId;
                        if (paperModel != null)
                        {
                            paperModel.DefaultBrandId = brandPaper.BrandId;
                        }
                        isSuccess = true;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                        isSuccess = false;
                    }
                    if (isSuccess)
                    {
                        isSuccess = _paperInfoDAL.InsertBrandByPaper(brandPaper, paperModel);
                    }
                }
                if (isSuccess)
                    scope.Complete();
            }
            return isSuccess;
 
        }
 
        public bool UpdateBrandModel(SysInquiry_BrandInfo model, SysInquiry_BrandListByPaper brandPaper, SysInquiry_PaperInfo paperModel, SysInquiry_PaperInfo orPaperModel)
        {
            bool isSuccess = true;
            PaperInfoDAL _paperInfoDAL = new PaperInfoDAL(_dataBase);
            using (TransactionScope scope = new TransactionScope())
            {
                if (model == null)
                {
                    isSuccess = false;
                }
                else
                {
                    IList<SqlParameter> sqlParms = new List<SqlParameter>()
                    {
                            new SqlParameter("@BrandName",model.BrandName),
                            new SqlParameter("@Status",model.Status),
                            new SqlParameter("@KeyId",model.KeyId),
                            new SqlParameter("@OrderNum",model.OrderNum)
                    };
                    try
                    {
                        _dataBase.Query("SysInquiry_BrandInfo_Update", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
                        isSuccess = true;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    if (isSuccess)
                    {
                        isSuccess = _paperInfoDAL.UpdateBrandByPaper(brandPaper, paperModel, orPaperModel);
                    }
                    if (isSuccess)
                        scope.Complete();
                }
            }
            return isSuccess;
        }
 
        /// <summary>
        /// 获取最新排序顺序
        /// </summary>
        /// <returns></returns>
        public int GetOrderNumByMax(int paperId)
        {
            int orderNum = 1;
            IList<SysInquiry_BrandInfo> result = _dataBase.SelectModel<SysInquiry_BrandInfo>(" MAX(OrderNum)+1 AS OrderNum ", "dbo.SysInquiry_BrandInfo a LEFT JOIN dbo.SysInquiry_BrandListByPaper b ON a.KeyId=b.BrandId", " b.PaperId=" + paperId);
            if (result != null && result.Count > 0)
            {
                if (result[0].OrderNum.HasValue)
                {
                    orderNum = result[0].OrderNum.Value;
                }
                else
                {
                    orderNum = 1;
                }
            }
            return orderNum;
        }
    }
}