using Appointment.Entity; using Appointment.IRepository; using System; using System.Linq; using System.Collections.Generic; using System.Text; using Newtonsoft.Json; namespace Appointment.Repository { public class ApptAuditorRepo : DbContext, IApptAuditorRepo { private List cache = null; IApptLogRepo _ApptLogRepo; public ApptAuditorRepo(IApptLogRepo apptLogRepo) { _ApptLogRepo = apptLogRepo; } public ApiResult Add(ApptAuditor req) { var apptType = Db.Queryable() .Where(t => t.Id == req.ApptTypeId) .Any(); if (!apptType) return ApiResult.Failed($"操作失败,预订类型无效!"); var groupAny = Db.Queryable() .Where(t => t.OpGroup == req.OpGroup.Trim() && t.Descript == req.OpDescript.Trim()) .Any(); if (!groupAny) return ApiResult.Failed($"操作失败,组别无效!"); cache = Db.Queryable().Where(t => t.ApptTypeId == req.ApptTypeId).ToList(); if (req.Pid > 0) { var rootEntity = cache.Where(t => t.Id == req.Pid).FirstOrDefault(); if (rootEntity == null) return ApiResult.Failed($"操作失败,上级审核组无效!"); } var auditorAny = cache.Where(t => t.Pid == req.Pid).FirstOrDefault(); if (auditorAny != null) return ApiResult.Failed($"操作失败,当前位置已经配置了审核组!"); var result = InsertReturnId(req); if (!result.IsSuccess) return ApiResult.Failed(result.Msg); req.Id = result.Info; return ApiResult.Success(string.Empty, req); } public ApiResult Delete(int id) { var childData = Db.Queryable().Where(t => t.Pid == id).Any(); if (childData) return ApiResult.Failed("操作失败,请先删除子级数据!"); var apptAuditor = GetById(id); if (apptAuditor == null) return ApiResult.Success(string.Empty); var ls = Db.Queryable().Where( t => t.ApptTypeId == apptAuditor.ApptTypeId && t.AuditorId == apptAuditor.Id && t.AuditorStatus == (int)EnumAuditorStatus.审核中) .Select(t => t.Id).ToList(); if (ls.Count > 0) return ApiResult.Failed($"操作失败,有待审核的预订[{string.Join(",", ls)}],请审核后再删除!"); _ApptLogRepo.SetLog("ppt_auditor", apptAuditor.Id.ToString(), JsonConvert.SerializeObject(apptAuditor), null, null, 1); DeleteById(id); return ApiResult.Success(); } public ApiResult GetModel(int id) { var entity = GetById(id); return ApiResult.Success(string.Empty, entity); } public ApiResult> GetList(ApptAuditor req) { var ls = Db.Queryable() .Where(t => req.Pid == 0) .WhereIF(req.Id > 0, t => t.Id == req.Id) .WhereIF(req.ApptTypeId > 0, t => t.ApptTypeId == req.ApptTypeId) .WhereIF(!string.IsNullOrWhiteSpace(req.OpGroup), t => t.OpGroup == req.OpGroup) .WhereIF(!string.IsNullOrWhiteSpace(req.OpDescript), t => t.OpDescript.Contains(req.OpDescript)) .WhereIF(req.Pid > 0, t => t.Pid == req.Pid) .ToList(); return ApiResult>.Success(string.Empty, ls); } public ApiResult> Page(CommPageReq req) { int total = 0; var ls = Db.Queryable() .WhereIF(!string.IsNullOrWhiteSpace(req.Keywords), t => t.OpDescript.Contains(req.Keywords)) .ToPageList(req.PageIndex, req.PageSize, ref total); PagerData result = new PagerData() { PageIndex = req.PageIndex, PageSize = req.PageSize, Total = total, Data = ls }; return ApiResult>.Success(string.Empty, result); } public ApiResult AuditorLine(int apptTypeId) { cache = Db.Queryable().Where(t => t.ApptTypeId == apptTypeId).ToList(); if (cache.Count == 0) return ApiResult.Success(string.Empty, null); ApptAuditor apptAuditor = cache.Where(t => t.ApptTypeId == apptTypeId && t.Pid == 0).FirstOrDefault(); if (apptAuditor == null) return ApiResult.Success(string.Empty, null); apptAuditor = GetRecursion(apptAuditor); return ApiResult.Success(string.Empty, apptAuditor); } private ApptAuditor GetRecursion(ApptAuditor req) { var entity = cache.Where(t => t.Pid == req.Id).FirstOrDefault(); if (entity == null) return req; req.Child = GetRecursion(entity); return req; } } }