username@email.com
2024-09-09 e8fd9aa8a76c638991e60544ccab53e2e5bd5b6a
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
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 2024/3/29 11:16:20
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.baifenbingfa.Promote;
using CoreCms.Net.Model.Entities.baifenbingfa.Promote.OffLineDistributor;
using CoreCms.Net.Model.ViewModels.Basics;
using CoreCms.Net.Model.ViewModels.UI;
using NPOI.POIFS.Macros;
using SqlSugar;
 
 
namespace CoreCms.Net.Services
{
    /// <summary>
    /// 线下经销商采集 接口实现
    /// </summary>
    public class OfflineDistributorServices : BaseServices<OfflineDistributor>, IOfflineDistributorServices
    {
        private readonly IOfflineDistributorRepository _dal;
        private readonly IUnitOfWork _unitOfWork;
  
        private readonly IHttpContextUser _contextUser;
        public OfflineDistributorServices(IUnitOfWork unitOfWork, IOfflineDistributorRepository dal,IHttpContextUser httpContextUser)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
             _contextUser = httpContextUser;
        }
 
        #region 实现重写增删改查操作==========================================================
 
        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> InsertAsync(OfflineDistributor entity)
        {
            entity.createTime = DateTime.Now;
            entity.createBy = $"{_contextUser.Name}";
            return await _dal.InsertAsync(entity);
        }
 
        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UserInsertAsync(OfflineDistributor entity)
        {
             var b= await _unitOfWork.GetDbClient().Queryable<OfflineDistributor>().Where(x=>x.CollectionTime==entity.CollectionTime&&x.DealerName==entity.DealerName&&x.Adder==entity.Adder).CountAsync();
            if(b>0)
                 return new AdminUiCallBack()
                {
                    code = 1,                 
                    msg = "请勿重复提交数据"
 
                };
 
            entity.createTime = DateTime.Now;
            entity.createBy = $"{_contextUser.Name}";
            var bol = await _unitOfWork.GetDbClient().InsertNav(entity).Include(x => x.meetings).ExecuteCommandAsync();
            if (bol)
            {
                return new AdminUiCallBack()
                {
                    code = 0,
                    data = bol,
                    msg = "操作成功"
 
                };
            }
            else
            {
 
                return new AdminUiCallBack()
                {
                    code = 1,
                    data = bol,
                    msg = "操作失败"
 
                };
 
            }
            return await _dal.InsertAsync(entity);
        }
        public async Task<AdminUiCallBack> GetInfo(int id)
        {
            var data = await _unitOfWork.GetDbClient().Queryable<OfflineDistributor>().Where(x => x.Id == id)
                   .Includes(x => x.meetings)
                   .Includes(x => x.Oders).FirstAsync();
            if (data == null)
                return new AdminUiCallBack()
                {
                    msg = "没有找到该数据"
                };
            else
                return new AdminUiCallBack()
                {
                    code = 0,
                    data = data
                };
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateAsync(OfflineDistributor entity)
        {
            var db = _unitOfWork.GetDbClient();
            var oldmod = await _dal.QueryByIdAsync(entity.Id);
 
            if (oldmod == null)
            {
                return new AdminUiCallBack()
                {
                    data = "没有找到相关数据"
                };
            }
            oldmod.upDataTime = DateTime.Now;
            oldmod.upDataBy = $"{_contextUser.Name}";
            oldmod.BusinessManager = entity.BusinessManager;
            oldmod.PhoneNumber = entity.PhoneNumber;
            oldmod.PUserID = entity.PUserID;
            oldmod.Region = entity.Region;
            oldmod.Remake = entity.Remake;
            oldmod.DealerName = entity.DealerName;
            oldmod.UserID = entity.UserID;
            oldmod.IsMeeting = entity.IsMeeting;
            oldmod.StoreLogoUrl = entity.StoreLogoUrl;
            oldmod.Adder = entity.Adder;
            oldmod.ContactPerson = entity.ContactPerson;   
            oldmod.Category = entity.Category;
            oldmod.CooperationIntent = entity.CooperationIntent;
            oldmod.School = entity.School;
            var oldMeetings = await db.Queryable<OfflineDistributorMeeting>().Where(x => x.OfflineDistributorID == oldmod.Id).OrderBy(x => x.id).ToListAsync();
            var oldIntentions = await db.Queryable<OfflineDistributorOder>().Where(x => x.OfflineDistributorID == oldmod.Id).OrderBy(x => x.Id).ToListAsync();
 
            foreach (var intention in entity.Oders)
            {
                if (intention.Id == 9999999)
                {
                    intention.Id = 0;
                    intention.createBy = $"{_contextUser.Name}";
                    intention.createTime = DateTime.Now;
                    intention.isdelete = false;
                    continue;
                }
                var old = oldIntentions.Where(x=>x.Id==intention.Id).SingleOrDefault();
                if (old != null)
                {
                    intention.isdelete = old.isdelete;
                    intention.createBy = old.createBy;
                    intention.createTime = old.createTime;
                    intention.upDataBy = $"{_contextUser.Name}";
                    intention.upDataTime = DateTime.Now;
                }
            }
            oldmod.Oders = entity.Oders;
 
            foreach (OfflineDistributorMeeting meetin in entity.meetings)
            {
                if (meetin.id == 9999999)
                {
                    meetin.id = 0;
                    meetin.createBy = $"{_contextUser.Name}";
                    meetin.createTime = DateTime.Now;
                    meetin.isdelete = false;
                    continue;
                }
                var old = oldMeetings.Where(x => x.id == meetin.id).SingleOrDefault();
                if (old != null)
                {
                    meetin.isdelete = old.isdelete;
                    meetin.createBy = old.createBy;
                    meetin.createTime = old.createTime;
                    meetin.upDataBy = $"{_contextUser.Name}";
                    meetin.upDataTime = DateTime.Now;
                }
            }
            oldmod.meetings = entity.meetings;
 
            var bol = await _unitOfWork.GetDbClient().UpdateNav(oldmod).Include(x => x.meetings).Include(x => x.Oders).ExecuteCommandAsync();
            if (bol)
            {
                return new AdminUiCallBack()
                {
                    code = 0,
                    data = bol,
                    msg = "操作成功"
 
                };
            }
            else
            {
 
                return new AdminUiCallBack()
                {
                    code = 1,
                    data = bol,
                    msg = "操作失败"
 
                };
 
            }
 
            //return await _dal.UpdateAsync(entity);
        }
 
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateAsync(List<OfflineDistributor> entity)
        {
            return await _dal.UpdateAsync(entity);
        }
 
        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> DeleteByIdAsync(object id)
        {
            return await _dal.DeleteByIdAsync(id);
        }
 
        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> DeleteByIdsAsync(int[] ids)
        {
            return await _dal.DeleteByIdsAsync(ids);
        }
 
        #endregion
 
        #region 获取缓存的所有数据==========================================================
 
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<OfflineDistributor>> GetCaChe()
        {
            return await _dal.GetCaChe();
        }
 
        #endregion
 
       #region 重写根据条件查询分页数据
        /// <summary>
        ///     重写根据条件查询分页数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <param name="orderByExpression"></param>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        public async Task<IPageList<OfflineDistributor>> QueryPageAsync(Expression<Func<OfflineDistributor, bool>> predicate,
            Expression<Func<OfflineDistributor, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
            int pageSize = 20, bool blUseNoLock = false)
        {
          return       await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock);
          
        }
 
        #endregion
 
    }
}