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
|
{
|
/// <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_DeliverPlanPaiche, 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.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<int> totle = 0;
|
var data = await _client.Queryable<OA_DeliverPlanPaiche>().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,
|
|
};
|
|
|
}
|
/// <summary>
|
/// 获取DeliverPlan详情
|
/// </summary>
|
/// <param name="Param"></param>
|
/// <returns></returns>
|
public async Task<OA_DeliverPlanPaiche> GetDeliverPlanInfo(Guid KeyId)
|
{
|
|
if (!await CheckRols())
|
{
|
throw Oops.Oh("没有权限");
|
}
|
return await _client.Queryable<OA_DeliverPlanPaiche>().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();
|
|
}
|
|
|
/// <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_DeliverPlanPaiche>().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_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<OA_DeliverPlanPaiche>().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,new SqlSugar.UpdateNavOptions()
|
{
|
OneToManyInsertOrUpdate = true,//配置启用 插入、更新或删除模式
|
}).Include(x=>x.DeliverPlans,new UpdateNavOptions()
|
{
|
OneToManyInsertOrUpdate=true,
|
}
|
).ExecuteCommandAsync();
|
|
//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;
|
}
|
/// <summary>
|
/// 获取打卡记录
|
/// </summary>
|
/// <param name="Param"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<DeliverRecordPageRet> GetDriverRecords(DeliverRecordSearchParam Param)
|
{
|
if (!await CheckRols())
|
{
|
throw Oops.Oh("没有权限");
|
}
|
|
Expression<Func<OA_DriverRecord, bool>> 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<int> totle = 0;
|
var data = await _client.Queryable<OA_DriverRecord>().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<OA_DriverRecord>().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
|
}
|
|
};
|
|
|
}
|
|
/// <summary>
|
/// 获取草稿记录 没有代表没有
|
/// </summary>
|
/// <returns></returns>
|
|
public async Task<OA_DriverRecord> GetClockRoughDraft()
|
{
|
if (!await CheckRols())
|
{
|
throw Oops.Oh("没有权限");
|
}
|
return await _client.Queryable<OA_DriverRecord>()
|
.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();
|
|
|
}
|
/// <summary>
|
/// 上班打卡 上下班打卡都调用一个字段
|
/// </summary>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<bool > 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<OA_DriverRecord>().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<OA_CarManage>().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<OA_DriverRecord>().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<OA_attachment>();
|
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,new UpdateNavOptions
|
{
|
OneToManyInsertOrUpdate = true,
|
}).ExecuteCommandAsync();
|
}
|
|
|
}
|
|
|
/// <summary>
|
/// 修改打卡记录 支持半小时内修改记录
|
/// </summary>
|
public async Task<bool > UpdataDriverRecord(OA_DriverRecord Param)
|
{
|
if (!await CheckRols())
|
{
|
throw Oops.Oh("没有权限");
|
}
|
|
var data = await _client.Queryable<OA_DriverRecord>()
|
.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)<DateTime.Now)
|
{
|
throw Oops.Oh("只能在半小时之内修改");
|
}
|
|
if(Param.Attachments!=null)
|
{
|
foreach (var attachment in Param.Attachments)
|
{
|
if (attachment.Keyid > 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();
|
|
}
|
|
/// <summary>
|
/// 打卡记录详情
|
/// </summary>
|
public async Task<OA_DriverRecord> GetDriverRecordInfo(Guid Id)
|
{
|
|
if (!await CheckRols())
|
{
|
throw Oops.Oh("没有权限");
|
}
|
|
var data= await _client.Queryable<OA_DriverRecord>()
|
.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;
|
|
}
|
/// <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;
|
}
|
|
}
|
/// <summary>
|
/// 司机打卡记录搜索选项
|
/// </summary>
|
public class DeliverRecordSearchParam
|
{
|
/// <summary>
|
/// 起始时间
|
/// </summary>
|
public DateTime? TimeStart { get; set; }
|
/// <summary>
|
/// 结束时间
|
/// </summary>
|
public DateTime? TimeEnd { get; set; }
|
|
/// <summary>
|
/// 司机KeyID
|
/// </summary>
|
public int? DeliverMemudID { get; set; }
|
|
|
/// <summary>
|
/// 车辆ID
|
/// </summary>
|
public int? CarID { get; set; }
|
|
/// <summary>
|
/// 打卡 类型
|
/// </summary>
|
public DriverClockType? ClockType { get; set; }
|
/// <summary>
|
/// 页面参数
|
/// </summary>
|
public PageModel page { get; set; }
|
}
|
|
/// <summary>
|
/// 打卡记录输出
|
/// </summary>
|
public class DeliverRecordPageRet
|
{
|
/// <summary>
|
/// 数据列表
|
/// </summary>
|
public List<OA_DriverRecord> Data { get; set; }
|
|
/// <summary>
|
/// 页面参数
|
/// </summary>
|
public PageModel page { get; set; }
|
|
/// <summary>
|
/// 下一次打卡状态 为空时不显示按钮
|
/// </summary>
|
public DriverClockType? NextClockType { get; set; } = null;
|
}
|
}
|