using cylsg.Model.ECTEModel; using cylsg.Model.utilityViewModel; using ECTESTOA; using Furion.LinqBuilder; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace cylsg.Application.CyOS { /// /// 报销主管相关控制器 /// [Authorize] public class CyOSBaoXiaoZHuGuanController: IDynamicApiController { private readonly IOAServices _OAServices; private readonly ISqlSugarClient _SugarClient; private ECTESTOAPermissions Permissions; public CyOSBaoXiaoZHuGuanController(IOAServices oaservices, ISqlSugarClient sugarClient) { _OAServices = oaservices; _SugarClient = sugarClient.AsTenant().GetConnection("ECTESTOADB"); } /// /// 查询 /// [HttpPost] public async Task getplanelist(SearchOaWorekPlan param) { if (!await CheckRols()) { throw Oops.Oh("没有权限"); } Expression> SearchList = (x) => true; //川印工作单 SearchList = SearchList.And(x => x.MemberId == _OAServices.firmId); SearchList = SearchList.And(x => x.PlanType == 3); SearchList = SearchList.And(x => x.PlanDataType == 17); //SearchList = SearchList.And(x => x.ApprovalStatus == PlanStatusType.unApproval); //SearchList = SearchList.And(x => x.OaWorkPlanShenpis.Where(y => y.Buzhou == 1 && y.ApprovalStaffId == Permissions.MemberId).Count()<=0); SearchList = SearchList.And(x => x.DepartId == Permissions.DepartId); if (!string.IsNullOrEmpty(param.StaffName)) { SearchList = SearchList.And(x => x.SentStaff.Name.Contains(param.StaffName)); } if (param.StaffMemberIdId != null) { SearchList = SearchList.And(x => x.MemberId == param.StaffMemberIdId); } if (param.PlanDataType > 0) { SearchList = SearchList.And(x => x.PlanDataType == param.PlanDataType); } if (param.PlanStartTimeStart != null) { SearchList = SearchList.And(x => x.PlanStartTime > param.PlanStartTimeStart); } if (param.PlanStartTimeEnd != null) { SearchList = SearchList.And(x => x.PlanStartTime <= param.PlanStartTimeEnd.Value.AddDays(1)); } //审批状态 if (param.ApprovalStatus != null) { if (param.ApprovalStatus == PlanStatusType.Yes) SearchList = SearchList.And(x => x.ApprovalStatus == param.ApprovalStatus || ((x.OaWorkPlanShenpis.Count(y => y.ApprovalStatus == PlanStatusType.Yes&&y.ApprovalStaffId==Permissions.MemberId && y.Buzhou == 1) >0))); else if(param.ApprovalStatus == PlanStatusType.No) { SearchList = SearchList.And(x => x.ApprovalStatus == param.ApprovalStatus ); } else { SearchList= SearchList.And(x=>x.ApprovalStatus== PlanStatusType.unApproval&&(x.OaWorkPlanShenpis.Count(y => y.Buzhou == 1&& y.ApprovalStaffId == Permissions.MemberId) <=0)); } } //报销状态 if (param.EvaluationStatus != null) { SearchList = SearchList.And(x => x.EvaluationStatus == param.EvaluationStatus); } RefAsync totle = 0; //强制增加员工过滤 var data = await _SugarClient.Queryable().Includes(x=>x.Depart) .Includes(x=>x.OaWorkPlanShenpis.Where(y=>y.ApprovalStaffId==Permissions.MemberId&&y.Buzhou==1).ToList()) .Where(SearchList).OrderByDescending(x => x.PlanStartTime).Mapper( x => { if (x.ApprovalStatus == PlanStatusType.unApproval) { var oashenpi = x.OaWorkPlanShenpis?.FirstOrDefault(); if (oashenpi != null) x.ApprovalStatus = oashenpi.ApprovalStatus; } ; } ).ToPageListAsync(param.page.PageIndex, param.page.PageSize, totle); param.page.TotalCount = totle; var sum = await _SugarClient.Queryable() .Where(SearchList).SumAsync(x => x.PlanMoney); return new WorekPlandto { list = data, page = param.page, TotalMoney = sum ?? 0 }; } /// /// 获取报销单详情 /// /// /// [HttpGet] public async Task GetPlanInfo(int id) { if (!await CheckRols()) { throw Oops.Oh("没有权限"); } return await _SugarClient.Queryable().Includes(x => x.OaWorkPlanShenpis.Where(y => y.Buzhou == 1).ToList(), y => y.ApprovalStaff).Includes(x => x.SentStaff).Includes(x => x.Depart).Includes(x => x.WorkPlanAttachments).Where(x=>x.Keyid==id).FirstAsync(); } /// /// 审批权限 /// /// [HttpPost] public async Task ApprovalPlan(OaWorkPlanShenpi param) { if (param.ApprovalStatus == PlanStatusType.unApproval) throw Oops.Oh("不可设置为未审批状态"); if (!await CheckRols()) { throw Oops.Oh("没有权限"); } var data = await _SugarClient.Queryable().Includes(x=>x.OaWorkPlanShenpis).Where(x => x.Keyid == param.OA_WorkPlanId).FirstAsync(); if (data == null) throw Oops.Oh("没有找到报销单"); if (data.ApprovalStatus != null && data.ApprovalStatus != PlanStatusType.unApproval) throw Oops.Oh("报销单已经批复完毕"); OaWorkPlanShenpi shenpi = data.OaWorkPlanShenpis.Where(x => x.ApprovalStaffId == Permissions.MemberId&&x.Buzhou==1).FirstOrDefault(); if (shenpi!=null) { //自己已经批复,不可更改 throw Oops.Oh("报销单已经批复"); //shenpi.ApprovalStatus = PlanStatusType.Yes; //await _SugarClient.Updateable(shenpi).ExecuteCommandAsync(); } else { await _SugarClient.AsTenant().BeginTranAsync(); try { if (param.ApprovalStatus == PlanStatusType.No) { //当有人审批拒绝后直接将申请单设置为拒绝,不在进行其他审核 data.ApprovalStatus = PlanStatusType.No; data.ApprovalTime = DateTime.Now; data.LastUpdateTime = DateTime.Now; data.Operator = Permissions.Name; await _SugarClient.Updateable(data).ExecuteCommandAsync(); } shenpi = new OaWorkPlanShenpi() { ApprovalStaffId = Permissions.MemberId, ApprovalContent = param.ApprovalContent, ApprovalStatus = param.ApprovalStatus, ApprovalTime = DateTime.Now, Buzhou = 1, CreateTime = DateTime.Now, Operator = Permissions.Name, OA_WorkPlanId = param.OA_WorkPlanId, }; await _SugarClient.Insertable(shenpi).ExecuteCommandAsync(); await _SugarClient.AsTenant().CommitTranAsync(); } catch (Exception) { await _SugarClient.AsTenant().RollbackTranAsync(); throw; } } return true; } /// /// 判断是否具有权限 /// /// private async Task CheckRols() { var pr = await _OAServices.GetOAPermissions(); Permissions = pr; if (pr != null && pr.MemberId != null) { if ( pr.BF_IsBaoxiaozhuguan) return true; else return false; } else return false; } } }