using cylsg.Model.ECTEModel;
using cylsg.Model.utilityViewModel;
using ECTESTOA;
using EzCoreNet.Redis;
using Furion.LinqBuilder;
using Microsoft.AspNetCore.Http.HttpResults;
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.DeliverTime >= Param.TimeStart.Value.Date);
}
if (Param.TimeEnd != null)
{
SearchList = SearchList.And(x => x.DeliverTime < Param.TimeEnd.Value.Date.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.Manager)
.Where(SearchList).OrderByDescending(x => x.DeliverTime.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).Includes(x=>x.Manager).Includes(x=>x.DeliverPlans)
.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("没有权限");
}
DateTime specificDateTime = new DateTime(2025, 4, 9, 23, 59, 59);//2025年4月10 日之后的,必须一单一单点击完成后才能送货
var count = await _client.Queryable().Where(x => x.DriverId == Permissions.KeyId && x.Keyid != KeyId && x.ShifouDelivery != 2 && x.Deliveredstatus == DeliveredType.Deliver&&x.DeliverTime> specificDateTime).CountAsync();
if(count > 0)
{
throw Oops.Oh($"你还有{count}送货任务没有确认完成,不能开始新的任务");
}
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_DeliverPlanPaiche 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().Includes(x=>x.DeliverPlans).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 a.DeliverPlans)
{
//同步 送货单状态
item.Updater = Permissions.MemberId;
item.LastUpdateTime = DateTime.Now;
item.Deliveredstatus= Param.Deliveredstatus;
}
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).ExecuteCommandAsync();
//await _client.AsTenant().BeginTranAsync();
//try
//{
// var id = await _client.Insertable(pram).ExecuteReturnIdentityAsync();
// if (pram.WorkPlanAttachments?.Count > 0)
// {
// foreach (var item in pram.WorkPlanAttachments)
// {
// item.OA_WorkPlanId = id;
// }
// await _client.Insertable(pram.WorkPlanAttachments).ExecuteCommandAsync();
// }
// await _client.AsTenant().CommitTranAsync();
//}
//catch (Exception)
//{
// await _client.AsTenant().RollbackTranAsync();
// throw;
//}
//await _client.AsTenant().BeginTranAsync();
//try
//{
// await _client.UpdateNav(a).Include(x => x.Attachments, new SqlSugar.UpdateNavOptions()
// {
// OneToManyInsertOrUpdate = true,//配置启用 插入、更新或删除模式
// }).ExecuteCommandAsync();
// if (a.DeliverPlans?.Count > 0)
// await _client.Updateable(a.DeliverPlans).ExecuteCommandAsync();
// await _client.AsTenant().CommitTranAsync();
//}
//catch (Exception)
//{
// await _client.AsTenant().RollbackTranAsync();
// throw;
//}
//return true;
}
///
/// 获取打卡记录
///
///
///
[HttpPost]
public async Task GetDriverRecords(DeliverRecordSearchParam Param)
{
if (!await CheckRols())
{
throw Oops.Oh("没有权限");
}
Expression> SearchList = (x) => true;
SearchList = SearchList.And(x => x.CreaterID == Permissions.MemberId);//只看自己
SearchList = SearchList.And(x => x.ClockType != DriverClockType.RoughDraft);//不看草稿
if (Param.CarID != null)
{
SearchList = SearchList.And(x => x.CarID == Param.CarID);
}
if (Param.TimeStart != null)
{
SearchList = SearchList.And(x => x.CreatTime >= Param.TimeStart.Value.Date);
}
if (Param.TimeEnd != null)
{
SearchList = SearchList.And(x => x.CreatTime < Param.TimeEnd.Value.Date.AddDays(1));
}
if (Param.ClockType != null)
{
SearchList = SearchList.And(x => x.ClockType == Param.ClockType);
}
//强制增加员工过滤
RefAsync totle = 0;
var data = await _client.Queryable().Includes(x => x.Car)
.Where(SearchList).OrderByDescending(x => x.CreatTime.Date).Mapper((x)=>
{
if (x.CreatTime.AddMinutes(30) < DateTime.Now)
x.CanEdit = true;
else
x.CanEdit = false;
})
.ToPageListAsync(Param.page.PageIndex, Param.page.PageSize, totle);
Param.page.TotalCount = totle;
var count = await _client.Queryable().Where(x => x.CreatTime.Date == DateTime.Now.Date && x.ClockType != DriverClockType.RoughDraft&& x.CreaterID==Permissions.MemberId).CountAsync();
return new DeliverRecordPageRet
{
Data = data,
page = Param.page,
NextClockType= count switch
{
1=> DriverClockType.Clockout,
2=> DriverClockType.Unknown,
0=> DriverClockType.ClockIn,
_=> DriverClockType.Unknown
}
};
}
///
/// 获取草稿记录 没有代表没有
///
///
public async Task GetClockRoughDraft()
{
if (!await CheckRols())
{
throw Oops.Oh("没有权限");
}
return await _client.Queryable()
.Where(x => x.CreaterID == Permissions.MemberId
&& x.ClockType == DriverClockType.RoughDraft
&& x.CreatTime.Date == DateTime.Now.Date).Includes(x=>x.Attachments).Includes(x=>x.CarConditionAttachments).FirstAsync();
}
///
/// 上班打卡 上下班打卡都调用一个字段
///
///
[HttpPost]
public async Task Clock(OA_DriverRecord Param)
{
if (Param.ClockType == DriverClockType.Unknown)
throw Oops.Oh("类型不可为未知");
if (!await CheckRols())
{
throw Oops.Oh("没有权限");
}
if (Param.ClockType!= DriverClockType.RoughDraft)
{
var clockid= await _client.Queryable().Where(x => x.CreaterID == Permissions.MemberId
&& x.CreatTime.Date == DateTime.Now.Date
&& x.ClockType == Param.ClockType
).Select(x => x.Id).FirstAsync();
if(clockid!=null)
{
throw Oops.Oh("今天已经打过该类型的卡了");
}
}
var carId = await _client.Queryable().Where(x => x.StaffID == Permissions.KeyId && x.MemberID == _oAServices.firmId).Select(x => x.Keyid).FirstAsync();
if(carId==null||carId==0)
{
throw Oops.Oh("该司机未分配车辆,不需要打卡");
}
OA_DriverRecord data = null;
data= await _client.Queryable().Where(x =>
//x.Id == Param.Id&& 最多每天只能有一个草稿
x.CreatTime.Date == DateTime.Now.Date
&& x.CreaterID == Permissions.MemberId
&& x.ClockType == DriverClockType.RoughDraft).FirstAsync();
Param.CarID = carId;
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.EmptyCause = Param.EmptyCause ?? "";
Param.CarCondition = Param.CarCondition ?? "";
Param.Remark = Param.Remark ?? "";
Param.CreatTime = DateTime.Now;
Param.CreaterID = Permissions.MemberId;
Param.Creater = Permissions.Name;
if (data == null)
{
//没有单子
Param.Id = Guid.NewGuid();
if (!(carId > 0))
{
throw Oops.Oh("没找到匹配车辆");
}
return await _client.InsertNav(Param).Include(x => x.Attachments).Include(x => x.CarConditionAttachments).ExecuteCommandAsync();
}
else
{
data.CarID = Param.CarID;
data.CreatTime= Param.CreatTime;
data.Attachments = Param.Attachments;//导航更行分类条件不起作用,这里更新将 这两个合并操作
data.CarCondition=Param.CarCondition;
data.CarConditionAttachments = Param.CarConditionAttachments;
if (data.CarConditionAttachments == null)
data.CarConditionAttachments = new List();
if (data.Attachments != null)
data.CarConditionAttachments.AddRange(data.Attachments);
data.ClockType = Param.ClockType;
data.EmptyCause = Param.EmptyCause;
return await _client.UpdateNav(data)
//分开导航会产生冲突应该sqlsun的bug
// .Include(x => x.Attachments,new UpdateNavOptions
//{
// OneToManyInsertOrUpdate = true,
//})
.Include(x => x.CarConditionAttachments).ExecuteCommandAsync();
}
}
///
/// 修改打卡记录 支持半小时内修改记录
///
public async Task UpdataDriverRecord(OA_DriverRecord Param)
{
if (!await CheckRols())
{
throw Oops.Oh("没有权限");
}
var data = await _client.Queryable()
.Includes(x => x.Attachments)
.Includes(x => x.CarConditionAttachments)
.Where(x => x.CreaterID == Permissions.MemberId && x.Id == Param.Id).FirstAsync();
if (data != null)
throw Oops.Oh("没有权限");
if(data.CreatTime.AddMinutes(30) 0)
continue;
else
{
attachment.AttachmentType = 4;
attachment.CreateTime = DateTime.Now;
attachment.Operator = Permissions.Name;
}
}
}
if (Param.CarConditionAttachments != null)
{
foreach (var attachment in Param.CarConditionAttachments)
{
if (attachment.Keyid > 0)
continue;
else
{
attachment.AttachmentType = 5;
attachment.CreateTime = DateTime.Now;
attachment.Operator = Permissions.Name;
}
}
}
data.Attachments = Param.Attachments;
data.CarConditionAttachments = Param.CarConditionAttachments;
data.CarCondition = Param.CarCondition;
return await _client.UpdateNav(data).Include(x => x.Attachments, new UpdateNavOptions
{
OneToManyInsertOrUpdate = true,
}).Include(x => x.CarConditionAttachments, new UpdateNavOptions
{
OneToManyInsertOrUpdate = true,
}).ExecuteCommandAsync();
}
///
/// 打卡记录详情
///
public async Task GetDriverRecordInfo(Guid Id)
{
if (!await CheckRols())
{
throw Oops.Oh("没有权限");
}
var data= await _client.Queryable()
.Includes(x => x.Car)
.Includes(x=>x.Attachments)
.Includes(x=>x.CarConditionAttachments)
.Where(x => x.CreaterID == Permissions.MemberId&&x.Id== Id).FirstAsync();
if (data == null)
throw Oops.Oh("没有权限");
return data;
}
///
/// 判断是否具有权限
///
///
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;
}
}
///
/// 司机打卡记录搜索选项
///
public class DeliverRecordSearchParam
{
///
/// 起始时间
///
public DateTime? TimeStart { get; set; }
///
/// 结束时间
///
public DateTime? TimeEnd { get; set; }
///
/// 司机KeyID
///
public int? DeliverMemudID { get; set; }
///
/// 车辆ID
///
public int? CarID { get; set; }
///
/// 打卡 类型
///
public DriverClockType? ClockType { get; set; }
///
/// 页面参数
///
public PageModel page { get; set; }
}
///
/// 打卡记录输出
///
public class DeliverRecordPageRet
{
///
/// 数据列表
///
public List Data { get; set; }
///
/// 页面参数
///
public PageModel page { get; set; }
///
/// 下一次打卡状态 为空时不显示按钮
///
public DriverClockType? NextClockType { get; set; } = null;
}
}