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
{
///
/// 司机相关
///
[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;
}
///
/// 获取送货单
///
///
[HttpPost]
public async Task GetDeliverPlans(DeliverPlanSearchParam Param)
{
if (!await CheckRols())
{
throw Oops.Oh("没有权限");
}
Expression> 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 totle = 0;
var data = await _client.Queryable().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,
};
}
///
/// 获取DeliverPlan详情
///
///
///
public async Task GetDeliverPlanInfo(Guid KeyId)
{
if (!await CheckRols())
{
throw Oops.Oh("没有权限");
}
return await _client.Queryable().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();
}
///
/// 开始配送
///
///
///
public async Task StartDeliverPlan(Guid KeyId)
{
if (!await CheckRols())
{
throw Oops.Oh("没有权限");
}
var a= await _client.Queryable().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();
}
///
/// 完成订单
///
///
///
public async Task 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().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();
}
///
/// 上班打卡 上下班打卡都调用一个字段
///
///
[HttpPost]
public async Task 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();
}
///
/// 判断是否具有权限
///
///
private async Task 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;
}
}
}