移动系统liao
2024-09-23 78028cee453a5878835a27f884ae36c0900fe8f7
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
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Helper;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinUserInfoBatchGetResponse.Types;
 
 
namespace CoreCms.Net.Services
{
    /// <summary>
    /// 提货单表 接口实现
    /// </summary>
    public class CoreCmsBillLadingServices : BaseServices<CoreCmsBillLading>, ICoreCmsBillLadingServices
    {
        private readonly ICoreCmsBillLadingRepository _dal;
        private readonly ICoreCmsClerkRepository _clerkRepository;
        private readonly ICoreCmsStoreRepository _storeRepository;
        private readonly ICoreCmsOrderItemRepository _orderItemRepository;
        private readonly ICoreCmsUserRepository _userRepository;
        private readonly IUnitOfWork _unitOfWork;
        private readonly ICoreCmsSettingServices _settingServices;
 
        private readonly IServiceProvider _serviceProvider;
 
 
        public CoreCmsBillLadingServices(IUnitOfWork unitOfWork, ICoreCmsBillLadingRepository dal, ICoreCmsClerkRepository clerkRepository, ICoreCmsStoreRepository storeRepository, ICoreCmsOrderItemRepository orderItemRepository, ICoreCmsUserRepository userRepository, IServiceProvider serviceProvider, ICoreCmsSettingServices settingServices)
        {
            this._dal = dal;
            _clerkRepository = clerkRepository;
            _storeRepository = storeRepository;
            _orderItemRepository = orderItemRepository;
            _userRepository = userRepository;
            _serviceProvider = serviceProvider;
            _settingServices = settingServices;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
        }
 
 
        /// <summary>
        /// 添加提货单
        /// </summary>
        /// <returns></returns>
        public async Task<WebApiCallBack> AddData(string orderId, int storeId, string name, string mobile)
        {
            return await _dal.AddData(orderId, storeId, name, mobile);
        }
 
 
        /// <summary>
        /// 核销提货单
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> LadingOperating(string[] ids, int userId = 0)
        {
            var jm = new AdminUiCallBack();
 
            var list = await _dal.QueryListByClauseAsync(p => ids.Contains(p.id) && p.status == false);
            if (list.Any())
            {
                foreach (var item in list)
                {
                    item.clerkId = userId;
                    item.pickUpTime = DateTime.Now;
                    item.status = true;
                }
 
                var outChanges = await _dal.UpdateAsync(list);
                jm.code = outChanges ? 0 : 1;
                jm.msg = outChanges ? "操作成功" : "操作失败";
            }
            else
            {
                jm.msg = "没有可提货的订单";
            }
 
            return jm;
        }
 
 
        /// <summary>
        /// 获取店铺提货单列表
        /// </summary>
        /// <returns></returns>
        public async Task<WebApiCallBack> GetStoreLadingList(int userId, int storeId, int page, int limit)
        {
            var jm = new WebApiCallBack();
 
            var where = PredicateBuilder.True<CoreCmsBillLading>();
 
            using var container = _serviceProvider.CreateScope();
            var _userServices = container.ServiceProvider.GetService<ICoreCmsUserServices>();
 
            var allConfigs = await _settingServices.GetConfigDictionaries();
            var shopManagerMobile = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopManagerMobile);
 
            var user = await _userServices.QueryByClauseAsync(p => p.id == userId);
            if (user == null)
            {
                jm.status = false;
                jm.msg = "用户获取失败";
                return jm;
            }
            var isManager = !string.IsNullOrEmpty(shopManagerMobile) && shopManagerMobile.Contains(user.mobile);
            if (isManager)
            {
                where = storeId == 0 ? where.And(p => p.isDel == false) : where.And(p => p.isDel == false && p.storeId == storeId);
            }
            else
            {
                where = where.And(p => p.isDel == false && p.storeId == storeId);
                //var clerks = await _clerkRepository.QueryListByClauseAsync(p => p.userId == userId);
                //var storeIds = clerks.Select(p => p.storeId).ToList();
                //where = where.And(p => storeIds.Contains(p.storeId) && p.isDel == false);
            }
 
            var ladingList = await _dal.QueryPageAsync(where, p => p.createTime, OrderByType.Desc, page, limit);
 
 
            jm.status = true;
            jm.msg = "获取成功";
            jm.data = ladingList;
            jm.otherData = new
            {
                ladingList.TotalCount,
                ladingList.TotalPages
            };
 
            if (ladingList.Any())
            {
                var storeModel = await _storeRepository.QueryAsync();
 
                foreach (var item in ladingList)
                {
                    item.orderItems = await _orderItemRepository.QueryListByClauseAsync(p => p.orderId == item.orderId);
                    item.storeName = storeModel.FirstOrDefault(p => p.id == item.storeId)?.storeName;
                    var statusInt = item.status
                        ? (int)GlobalEnumVars.BillLadingStatus.Recharge
                        : (int)GlobalEnumVars.BillLadingStatus.Order;
                    item.statusName = EnumHelper.GetEnumDescriptionByValue<GlobalEnumVars.BillLadingStatus>(statusInt);
                }
            }
 
            return jm;
        }
 
 
        /// <summary>
        /// 删除提货单(软删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<WebApiCallBack> LadingDelete(string id, int userId = 0)
        {
            var jm = new WebApiCallBack();
 
            var model = await _dal.QueryByClauseAsync(p => p.id == id);
            if (model != null)
            {
                if (model.status == false)
                {
                    jm.msg = "未提货的提货单不能删除";
                    return jm;
                }
                if (userId > 0)
                {
                    var clerks = await _clerkRepository.ExistsAsync(p => p.userId == userId && p.storeId == model.storeId);
                    if (!clerks)
                    {
                        jm.msg = "你无权删除该提货单";
                        return jm;
                    }
                }
                model.isDel = true;
                model.updateTime = DateTime.Now;
                var bl = await _dal.UpdateAsync(model);
 
                jm.status = bl;
                jm.msg = bl ? "删除成功" : "删除失败";
            }
            else
            {
                jm.msg = "未找到提货单";
            }
 
            return jm;
        }
 
        /// <summary>
        /// 获取提货单详情
        /// </summary>
        /// <returns></returns>
        public async Task<WebApiCallBack> GetInfo(string id, int userId = 0)
        {
            var jm = new WebApiCallBack();
 
            var list = await _dal.QueryListByClauseAsync(p => p.id.Contains(id) || p.orderId.Contains(id) || p.mobile.Contains(id), p => p.createTime, OrderByType.Desc);
            var data = new List<CoreCmsBillLading>();
            if (list != null)
            {
                var userInfo = await _userRepository.QueryByClauseAsync(p => p.id == userId);
                if (userInfo != null)
                {
                    var allConfigs = await _settingServices.GetConfigDictionaries();
                    var shopManagerMobile = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopManagerMobile);
 
                    var isManager = !string.IsNullOrEmpty(shopManagerMobile) && shopManagerMobile.Contains(userInfo.mobile);
                    if (isManager)
                    {
                        data = list;
                    }
                    else
                    {
                        var clerks = await _clerkRepository.QueryListByClauseAsync(p => p.userId == userId);
                        if (clerks != null && clerks.Any())
                        {
                            var storeIds = clerks.Select(p => p.storeId).ToList();
                            foreach (var item in list)
                            {
                                if (storeIds.Contains(item.storeId))
                                {
                                    data.Add(item);
                                }
                            }
                        }
                    }
                }
                foreach (var item in data)
                {
                    var statusInt = item.status
                        ? (int)GlobalEnumVars.BillLadingStatus.Recharge
                        : (int)GlobalEnumVars.BillLadingStatus.Order;
                    item.statusName = EnumHelper.GetEnumDescriptionByValue<GlobalEnumVars.BillLadingStatus>(statusInt);
                    if (item.clerkId > 0)
                    {
 
                        if (userInfo != null)
                        {
                            item.clerkIdName = !string.IsNullOrEmpty(userInfo.nickName)
                                ? userInfo.nickName + "(" + userInfo.mobile + ")"
                                : UserHelper.FormatMobile(userInfo.mobile) + "(" + userInfo.mobile + ")";
                        }
                    }
                    else
                    {
                        item.clerkIdName = item.status ? "(后台管理员)" : "";
                    }
                    item.orderItems = await _orderItemRepository.QueryListByClauseAsync(p => p.orderId == item.orderId);
                }
 
                jm.status = true;
                jm.msg = "获取成功";
                jm.data = data;
            }
            else
            {
                jm.msg = "提货单不存在";
            }
 
            return jm;
        }
 
 
 
    }
}