using DocumentFormat.OpenXml.EMMA; using DocumentFormat.OpenXml.Office.CustomUI; using Infrastructure; using NPOI.SS.Util; using OpenAuth.App.BaseApp.Base; using OpenAuth.App.Interface; using OpenAuth.App.Request; using OpenAuth.App.Response; using OpenAuth.App.ServiceApp.MiManager.Request; using OpenAuth.App.ServiceApp.Request; using OpenAuth.App.ServiceApp.Response; using OpenAuth.Repository; using OpenAuth.Repository.Domain; using SqlSugar; namespace OpenAuth.App { public class MiViolationReportApp : SqlSugarBaseApp { public MiViolationReportApp( ISugarUnitOfWork unitWork, ISimpleClient repository, IAuth auth ) : base(unitWork, repository, auth) { } #region 查询 /// /// 分页 /// public async Task>>> LoadAllPage(MiviolationReq req) { RefAsync totalCount = 0; var user = _auth.GetCurrentUser(); var users = user.User; var level = user.Orgs?.Min(x => x.Level); //如果不是超级管理员 if (user != null && (users.Id == -1 || level == 0)) { var list = await base.Repository.AsQueryable() .Where(p => p.IsDelete == false) .WhereIF(req.begindate != null && req.enddate != null, p => p.ReportTime >= req.begindate && p.ReportTime < req.enddate) .WhereIF(req.status != null, p => p.Status == req.status) .WhereIF(!string.IsNullOrEmpty(req.key), p => p.Title.Contains(req.key)) .WhereIF(req.viotype != null, p => p.ViolationType == req.viotype) .LeftJoin((p, m) => p.MinePointId == m.Id) .LeftJoin((p, m, u) => p.Reporter == u.Id.ToString()) .LeftJoin((p, m, u, o) => p.ReportUnit == o.Id.ToString()) .WhereIF(!string.IsNullOrEmpty(req.pointname), (p, m, u, o) => m.Name.Contains(req.pointname)) .OrderBy((p, m, u, o) => p.Status) .OrderByDescending((p, m, u, o) => p.ReportTime) .Select((p, m, u, o) => new { Id = p.Id.SelectAll(), ReportUserName = u.Name, PointName = m.Name, OrgName = o.Name, StatusName = SqlFunc.Subqueryable().Where(a => a.ItemCode == "FMViolationStatus" && a.ItemValue == p.Status.ToString()).Select(a => a.ItemName), TypeName = SqlFunc.Subqueryable().Where(a => a.ItemCode == "JGViolationType" && a.ItemValue == p.ViolationType.ToString()).Select(a => a.ItemName) }) .ToPageListAsync(req.page, req.limit, totalCount); return new Response>> { Result = new PageInfo> { Items = list, Total = totalCount } }; } else { if (user.Orgs.Count == 0 || level == null) { throw new Exception("无权限查看数据"); } else { //查询所有子部门 List orgidlist = new List(); var orgs = user.Orgs?.Select(r => r.Id).ToList(); using (var uow = base.UnitWork.CreateContext()) { foreach (var item in orgs) { //orgidlist.Add(item.ToString()); var allchilds = uow.SysOrg.AsQueryable().ToChildList(r => r.ParentId, item); if (allchilds.Count > 0) { orgidlist = orgidlist.Concat(allchilds.Select(r => r.Id.ToString())).ToList(); } } var list = await uow.MiViolationReport.AsQueryable() .Where(p => p.IsDelete == false) .WhereIF(req.begindate != null && req.enddate != null, p => p.ReportTime >= req.begindate && p.ReportTime < req.enddate) .WhereIF(req.status != null, p => p.Status == req.status) .WhereIF(!string.IsNullOrEmpty(req.key), p => p.Title.Contains(req.key)) .WhereIF(req.viotype != null, p => p.ViolationType == req.viotype) .Where(p => orgidlist.Contains(p.ReportUnit)) .LeftJoin((p, m) => p.MinePointId == m.Id) .LeftJoin((p, m, u) => p.Reporter == u.Id.ToString()) .LeftJoin((p, m, u, o) => p.ReportUnit == o.Id.ToString()) .WhereIF(!string.IsNullOrEmpty(req.pointname), (p, m, u, o) => m.Name.Contains(req.pointname)) .OrderBy((p, m, u, o) => p.Status) .OrderByDescending((p, m, u, o) => p.ReportTime) .Select((p, m, u, o) => new { Id = p.Id.SelectAll(), ReportUserName = u.Name, PointName = m.Name, OrgName = o.Name, StatusName = SqlFunc.Subqueryable().Where(a => a.ItemCode == "FMViolationStatus" && a.ItemValue == p.Status.ToString()).Select(a => a.ItemName).First(), TypeName = SqlFunc.Subqueryable().Where(a => a.ItemCode == "JGViolationType" && a.ItemValue == p.ViolationType.ToString()).Select(a => a.ItemName).First() }) .ToPageListAsync(req.page, req.limit, totalCount); return new Response>> { Result = new PageInfo> { Items = list, Total = totalCount } }; } } } } #endregion public async Task Get(string id) { using (var uow = base.UnitWork.CreateContext()) { var result = await base.Repository.AsQueryable() .Where(p => p.Id == id) .LeftJoin((p, r) => p.ReportUnit == r.Id.ToString()) .LeftJoin((p, r, m) => p.MinePointId == m.Id) .LeftJoin((p, r, m, pk) => p.ParkingId == pk.Id) .LeftJoin((p, r, m, pk,u) => p.Reporter == u.Id.ToString()) .LeftJoin((p, r, m, pk, u, u2) => p.Handler == u2.Id.ToString()) .Select((p, r, m, pk, u, u2) => new { // 提车信息 ReporterName = u.Name, HandlerName = u2.Name, ReportUnitName=r.Name, // 违法上报信息 p.Id, p.Title, ReportStatus = p.Status, p.PartyName, p.PartyPhone, p.ViolationType, p.ProblemDescription, p.HandlingOpinion, p.HandlingUnit, p.Handler, p.HandlingTime, p.ReportTime, p.Reporter, p.ReportUnit, p.Lng, p.Lat, // 盗采点信息 MinePointId = m.Id, MinePointName = m.Name, m.CountyName, MinePointLng = m.Lng, MinePointLat = m.Lat, // 停车场信息 ParkingId = pk.Id, ParkingCode = pk.Num, ParkingName = pk.Name, pk.Phone, pk.Address }) .FirstAsync(); if (result == null) return null; // 查询车辆信息 var vehicles = await uow.MiVehicle.AsQueryable() .Where(v => v.ViolationReportId == result.Id) .ToListAsync(); // 查询车辆图片 List vehicleImages = new List(); if (vehicles.Any()) { var vehicleIds = vehicles.Select(v => v.Id).ToList(); vehicleImages = await uow.MiVehicleImage.AsQueryable() .Where(vi => vehicleIds.Contains(vi.VehicleId)) .ToListAsync(); } // 查询现场照片 var scenePhotos = await uow.MiScenePhoto.AsQueryable() .Where(sp => sp.ViolationReportId == result.Id) .ToListAsync(); return new { ViolationReport = new { result.Id, result.Title, result.ReportStatus, result.PartyName, result.PartyPhone, result.ViolationType, result.ProblemDescription, result.HandlingOpinion, result.HandlingUnit, result.Handler, result.HandlingTime, result.ReportTime, result.Reporter, result.ReportUnit, result.ReportUnitName, result.Lng, result.Lat }, MinePoint = string.IsNullOrEmpty(result.MinePointId) ? null : new { result.MinePointId, result.MinePointName, result.CountyName, result.MinePointLng, result.MinePointLat }, Parking = string.IsNullOrEmpty(result.ParkingId) ? null : new { result.ParkingId, result.ParkingCode, result.ParkingName, result.Phone, result.Address }, Vehicles = vehicles.Select(v => new { v.Id, v.LicensePlate, v.Type, v.Name, v.IdCard, v.Phone, Images = vehicleImages .Where(img => img.VehicleId == v.Id) .Select(img => new { img.Id, img.Image, img.Lng, img.Lat, img.Angle, img.CreateTime }) .ToList() }).ToList(), ScenePhotos = scenePhotos.Select(sp => new { sp.Id, sp.Image, sp.Lng, sp.Lat, sp.Angle, sp.CreateTime }).ToList() }; } } /// /// 添加 /// public async Task> Add(MiViolationReport model) { var flag = await Repository.InsertAsync(model); return new Response { Result = flag, Message = flag == true ? "success" : "error" }; } /// /// 删除 /// /// public async Task> Delete(List ids) { using (var uow = base.UnitWork.CreateContext()) { await uow.MiViolationReport.UpdateSetColumnsTrueAsync(a => new MiViolationReport { IsDelete = true }, a => ids.Contains(a.Id)); var flag=uow.Commit(); return new Response { Result = flag, Message = flag ? "success" : "error" }; } } /// /// 更新 /// /// public async Task> Update(MiViolationReport model) { bool flag = await base.Repository.UpdateAsync(model); return new Response { Result = flag, Message = flag == true ? "success" : "error" }; } /// /// 事务示例 /// /// /// public async Task> AssignModule() { using (var uwo = UnitWork.CreateContext()) { //await uwo.SysRoleElement.InsertRangeAsync(model.ElementIds.Select(a => new SysRoleElement { RoleId = model.RoleId, ElementId = a }).ToList()); var flag = uwo.Commit(); return new Response { Result = flag, Message = flag == true ? "success" : "error" }; } } /// /// 上报违法 /// /// /// public async Task> Report(MiViolationReportRequest request) { using (var uwo = UnitWork.CreateContext()) { var user=_auth.GetCurrentUser(); var org=user.Orgs.FirstOrDefault(); if (org == null) { throw new Exception("请先分配部门"); } //上报信息 var model = request.MapTo(); model.Id = Guid.NewGuid().ToString(); model.Status = 0; model.ReportTime = DateTime.Now; model.Reporter=user.User.Id.ToString(); model.IsDelete = false; model.ReportUnit=org.Id.ToString(); //现场照片 var photos = request.SencePhotos.MapToList(); photos.ForEach(a => { a.Id = Guid.NewGuid().ToString(); a.ViolationReportId = model.Id; a.CreateTime = DateTime.Now; }); var vehicleList = new List(); var vehicleImageList = new List(); foreach (var item in request.Vehicles) { //车辆信息 var vehicle = item.MapTo(); vehicle.Id = Guid.NewGuid().ToString(); vehicle.ViolationReportId = model.Id; vehicle.CreateTime=DateTime.Now; //车辆图片 var images = item.VehicleImages.MapToList(); images.ForEach(a => { a.Id = Guid.NewGuid().ToString(); a.ViolationReportId = model.Id; a.ParkingId = model.ParkingId; a.VehicleId = vehicle.Id; a.CreateTime = DateTime.Now; vehicleImageList.Add(a); }); vehicleList.Add(vehicle); } await uwo.MiViolationReport.InsertAsync(model); await uwo.MiScenePhoto.InsertRangeAsync(photos); await uwo.MiVehicle.InsertRangeAsync(vehicleList); await uwo.MiVehicleImage.InsertRangeAsync(vehicleImageList); var flag = uwo.Commit(); return new Response { Result = flag, Message = flag == true ? "success" : "error" }; } } public async Task> ReportHandle(HandleInfo info) { var user = _auth.GetCurrentUser().User; var level = _auth.GetCurrentUser().Orgs.Min(a => a.Level); //路径 var org = _auth.GetCurrentUser().Orgs.FirstOrDefault(); if (org == null) { throw new Exception("请先分配部门"); } int count = await base.Repository.AsUpdateable().SetColumns( a => new MiViolationReport { HandlingOpinion = info.HandlingOpinion, HandlingTime = DateTime.Now, HandlingUnit = org.Id.ToString(), Status=1 }).Where(a => a.Id == info.Id).ExecuteCommandAsync(); return new Response { Result = count > 0, Message = count > 0 ? "success" : "error" }; } } }