移动系统liao
2025-03-07 8c92db325f7c801d2df095763a17673fa6c92085
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
using cylsg.Model.ECTEModel;
using cylsg.Model.utilityViewModel;
using ECTESTOA;
using EzCoreNet.Redis;
using Furion.LinqBuilder;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
namespace cylsg.Application.CyOS
{
    /// <summary>
    /// 司机相关
    /// </summary>
    [Authorize]
    [ApiDescriptionSettings("CYOA")]
    public class CyOSSDriver: IDynamicApiController
    {
        private readonly ISqlSugarClient _client;
        private readonly IOAServices _oAServices;
        private readonly IEzCoreNetRedisService _eZCoreNetRedisService;
        private ECTESTOAPermissions Permissions;
        public CyOSSDriver(ISqlSugarClient client, IOAServices oAServices, IEzCoreNetRedisService netRedisService)
        {
            _client = client.AsTenant().GetConnection("ECTESTOADB");
            _oAServices = oAServices;
            _eZCoreNetRedisService = netRedisService;
        }
        /// <summary>
        /// 获取送货单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<DeliverOderPageRet> GetDeliverPlans(DeliverPlanSearchParam Param)
        {
            if (!await CheckRols())
            {
                throw Oops.Oh("没有权限");
            }
 
            Expression<Func<OA_DeliverPlan, bool>> SearchList = (x) => true;
            SearchList = SearchList.And(x => x.FirmId == _oAServices.firmId);
            SearchList = SearchList.And(x => x.DriverId == Permissions.KeyId);//只看自己
            SearchList = SearchList.And(x => x.ShifouDelivery != 2);//过滤有效
            if (Param.DeliverKeyID != null)
            {
                SearchList = SearchList.And(x => x.DriverId == Param.DeliverKeyID);
            }
            //客户名称
            if (!string.IsNullOrEmpty(Param.CorporateClient))
            {
                SearchList = SearchList.And(x => x.CompanyName.Contains(Param.CorporateClient));
            }
            if (Param.TimeStart != null)
            {
                SearchList = SearchList.And(x => x.CreateTime > Param.TimeStart);
            }
            if (Param.TimeEnd != null)
            {
                SearchList = SearchList.And(x => x.CreateTime < Param.TimeEnd.Value.AddDays(1));
            }
            if (Param.DeliveredState != null)
            {
                if (Param.DeliveredState != DeliveredType.Delivered)
                {
                    SearchList = SearchList.And(x => x.Deliveredstatus != DeliveredType.Delivered);
                }
                else
                {
                    SearchList = SearchList.And(x => x.Deliveredstatus == DeliveredType.Delivered);
                }
            }
 
            //强制增加员工过滤
            RefAsync<int> totle = 0;
            var data = await _client.Queryable<OA_DeliverPlan>().Includes(x=>x.SentStaff)
                .Where(SearchList).OrderByDescending(x => x.CreateTime.Value.Date).OrderBy(x => SqlFunc.Asc(x.Sort)).ToPageListAsync(Param.page.PageIndex, Param.page.PageSize, totle);
 
            Param.page.TotalCount = totle;
 
 
            return new DeliverOderPageRet
 
            {
                Data = data,
                page = Param.page,
 
            };
 
 
        }
        /// <summary>
        /// 获取DeliverPlan详情
        /// </summary>
        /// <param name="Param"></param>
        /// <returns></returns>
        public async Task<OA_DeliverPlan> GetDeliverPlanInfo(Guid KeyId)
        {
            
            if (!await CheckRols())
            {
                throw Oops.Oh("没有权限");
            }
            return await _client.Queryable<OA_DeliverPlan>().Includes(x=>x.Attachments.Where(y=>y.AttachmentType==3).ToList()).Includes(x=>x.Manager).Includes(x=>x.SentStaff)
                .Where(x => x.DriverId == Permissions.KeyId && x.Keyid == KeyId&&x.ShifouDelivery!=2).FirstAsync();
 
        }
        
 
        /// <summary>
        /// 开始配送
        /// </summary>
        /// <param name="KeyId"></param>
        /// <returns></returns>
        public async Task<int > StartDeliverPlan(Guid KeyId)
        {
 
            if (!await CheckRols())
            {
                throw Oops.Oh("没有权限");
            }
             var a= await _client.Queryable<OA_DeliverPlan>().Where(x => x.DriverId == Permissions.KeyId && x.Keyid == KeyId && x.ShifouDelivery != 2).FirstAsync();
            if(a==null)
                throw Oops.Oh("没有权限");
            if(a.Deliveredstatus!= DeliveredType.Undeliver)
            {
                throw Oops.Oh("已经开始或者结束了");
            }
            a.Deliveredstatus = DeliveredType.Deliver;
            a.Updater = Permissions.MemberId;
            a.LastUpdateTime = DateTime.Now;
            
           return await _client.Updateable(a).ExecuteCommandAsync();
 
 
        }
 
 
        /// <summary>
        /// 完成订单
        /// </summary>
        /// <param name="Param"></param>
        /// <returns></returns>
        public async Task<bool> FinishDeliverPlan(OA_DeliverPlan Param)
        {
            if(Param.Keyid==null)
                throw Oops.Oh("ID错误");
            if((Param.Deliveredstatus != DeliveredType.Delivered)&&(Param.Deliveredstatus != DeliveredType.Reject))
                throw Oops.Oh("接受状态错误");
 
            if (!await CheckRols())
            {
                throw Oops.Oh("没有权限");
            }
            var a = await _client.Queryable<OA_DeliverPlan>().Where(x => x.DriverId == Permissions.KeyId && x.Keyid == Param.Keyid && x.ShifouDelivery != 2).FirstAsync();
            if (a == null)
                throw Oops.Oh("没有权限");
            if (a.Deliveredstatus != DeliveredType.Deliver)
            {
                throw Oops.Oh("订单状态结束");
            }
            foreach (var item in Param.Attachments)
            {
                if(string.IsNullOrWhiteSpace(item.PlanAttachment))
                {
                    throw Oops.Oh("有附件地址为空");
                }
 
                // URL 正则表达式
                string urlPattern = @"^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$";
                if(! Regex.IsMatch(item.PlanAttachment, urlPattern))
                {
                    throw Oops.Oh("请输入正确的附件地址");
 
                }
                if (item.Keyid>0)
                {
                    item.OA_Id = a.Keyid;
                    item.AttachmentType = 3;
                    
                }
                else
                {
                    item.OA_Id=a.Keyid;
                    item.AttachmentType = 3;
                    item.CreateTime = DateTime.Now;
                    item.Operator = Permissions.Name;
                }
            }
            a.Deliveredstatus = DeliveredType.Deliver;
            a.Updater = Permissions.MemberId;
            a.LastUpdateTime = DateTime.Now;
            if(Param.Deliveredstatus== DeliveredType.Delivered)
            a.DeliveredTime = DateTime.Now;
            a.Wentifankui = Param.Wentifankui;
            a.Deliveredstatus= Param.Deliveredstatus;
            a.Attachments = Param.Attachments;
            return await _client.UpdateNav(a).Include(x=>x.Attachments,new SqlSugar.UpdateNavOptions()
                { 
                   OneToManyInsertOrUpdate = true,//配置启用 插入、更新或删除模式
                }).ExecuteCommandAsync();
 
 
        }
 
        /// <summary>
        /// 上班打卡 上下班打卡都调用一个字段
        /// </summary>     
        /// <returns></returns>
        [HttpPost]
        public async Task<bool > Clock(OA_DriverRecord Param)
        {
            if (Param.ClockType == DriverClockType.Unknown)
                throw Oops.Oh("类型不可为未知");
            Param.Id =  Guid.NewGuid();
            if (Param.Attachments!=null)
            foreach (var item in Param.Attachments)
            {
                    if (string.IsNullOrWhiteSpace(item.PlanAttachment))
                    {
                        throw Oops.Oh("有附件地址为空");
                    }
 
                    // URL 正则表达式
                    string urlPattern = @"^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$";
                    if (!Regex.IsMatch(item.PlanAttachment, urlPattern))
                    {
                        throw Oops.Oh("请输入正确的附件地址");
 
                    }
                    item.AttachmentType =4;
                    item.CreateTime = DateTime.Now;
                    item.Operator = Permissions.Name;
                    
                    
                }
             if(Param.CarConditionAttachments!=null)
                foreach (var item in Param.CarConditionAttachments)
                {
                    if (string.IsNullOrWhiteSpace(item.PlanAttachment))
                    {
                        throw Oops.Oh("有附件地址为空");
                    }
 
                    // URL 正则表达式
                    string urlPattern = @"^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$";
                    if (!Regex.IsMatch(item.PlanAttachment, urlPattern))
                    {
                        throw Oops.Oh("请输入正确的附件地址");
 
                    }
                    item.AttachmentType = 5;
                    item.CreateTime = DateTime.Now;
                    item.Operator = Permissions.Name;
 
 
                }
            Param.CreatTime = DateTime.Now;
            Param.CreaterID = Permissions.MemberId;
            Param.Creater = Permissions.Name;
           return await  _client.InsertNav(Param).Include(x => x.Attachments).Include(x => x.Attachments).ExecuteCommandAsync();
           
 
        }
        /// <summary>
        /// 判断是否具有权限
        /// </summary>
        /// <returns></returns>
        private async Task<bool> CheckRols()
        {
            var pr = await _oAServices.GetOAPermissions();
            Permissions = pr;
            if (pr != null && pr.MemberId != null)
            {
                if (pr.BF_IsDriver == true)
 
 
                    return true;
                else
                    return false;
 
            }
            else
                return false;
        }
 
    }
}