using AutoMapper; using FluentEmail.Core; using LinqKit; using Microsoft.EntityFrameworkCore; using Ropin.Inspection.Common.Accessor.Interface; using Ropin.Inspection.Model; using Ropin.Inspection.Model.Entities; using Ropin.Inspection.Model.ViewModel; using Ropin.Inspection.Model.ViewModel.LGS; using Ropin.Inspection.Model.ViewModel.MTN; using Ropin.Inspection.Repository; using Ropin.Inspection.Repository.Interface; using Ropin.Inspection.Repository.LGS; using Ropin.Inspection.Repository.LGS.Interface; using Ropin.Inspection.Repository.MTN; using Ropin.Inspection.Repository.MTN.Interface; using Ropin.Inspection.Service.MTN.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ZXing.Common; namespace Ropin.Inspection.Service.MTN { public class DevOpsPlanService : IDevOpsPlanService { private readonly IDevOpsPlanRepository _devOpsPlanRepository; private readonly IDevOpsPlanDeviceRepository _devOpsPlanDeviceRepository; private readonly IDevOpsPlanDeviceContentRepository _devOpsPlanDeviceContentRepository; private readonly IDevOpsPlanPersonRepository _devOpsPlanPersonRepository; private readonly IPushMsgService _pushMsgService; private readonly IClaimsAccessor _claims; private readonly IMapper _mapper; private readonly ITispSpotContentRepository _tispSpotContentRepository; public DevOpsPlanService(IDevOpsPlanRepository devOpsPlanRepository, IClaimsAccessor claims, IDevOpsPlanDeviceRepository devOpsPlanDeviceRepository, IDevOpsPlanDeviceContentRepository devOpsPlanDeviceContentRepository, IDevOpsPlanPersonRepository devOpsPlanPersonRepository, ITispSpotContentRepository tispSpotContentRepository, IPushMsgService pushMsgService, IMapper mapper) { _devOpsPlanRepository = devOpsPlanRepository; _claims = claims; _devOpsPlanDeviceRepository = devOpsPlanDeviceRepository; _devOpsPlanDeviceContentRepository = devOpsPlanDeviceContentRepository; _devOpsPlanPersonRepository = devOpsPlanPersonRepository; _pushMsgService = pushMsgService; _mapper = mapper; _tispSpotContentRepository = tispSpotContentRepository; } public async Task CreateOneAsync(DevOpsPlanModel viewModel) { TmtnDevOpsPlan entity = new TmtnDevOpsPlan() { CId = Ulid.NewUlid().ToString(), CName = viewModel.CName, CStoreCode = viewModel.CStoreCode, CNumber = viewModel.CNumber, IType = viewModel.IType, IGhzQty = viewModel.IGhzQty, IGhzQtyLat = viewModel.IGhzQtyLat, DStartDate = viewModel.DStartDate, DEndDate = viewModel.DEndDate, IStatus = viewModel.IStatus, CRemark = viewModel.CRemark, CCreateBy = _claims.ApiUserId.ToString(), D_CreateOn = DateTime.Now, CCreator = _claims.ApiUserName, IIsDel = false }; _devOpsPlanRepository.Create(entity); var result = await _devOpsPlanRepository.SaveAsync(); if (!result) { throw new Exception("创建失败"); } } public async Task> PageAsync(DevOpsPlanInput input) { var predicate = PredicateBuilder.New(x => x.CStoreCode == input.StoreCode && x.IIsDel == false);//查询条件,推荐后台使用这种方式灵活筛选 if (!string.IsNullOrEmpty(input.Name)) { predicate = predicate.And(i => i.CName == input.Name); } if (input.Type.HasValue) { predicate = predicate.And(i => i.IType == input.Type); } if (input.Status.HasValue) { predicate = predicate.And(i => i.IStatus == input.Status); } var pageData = await _devOpsPlanRepository.GetPageAsync(predicate, "-D_CreateOn", input.IsPagination, input.PageIndex, input.PageSize); input.TotalCount = pageData.Totals; var result = pageData.Rows.Select(x => new DevOpsPlanModel { CId = x.CId, CName = x.CName, CNumber = x.CNumber, CStoreCode = x.CStoreCode, IType = x.IType, IGhzQty = x.IGhzQty, IGhzQtyLat = x.IGhzQtyLat, DStartDate = x.DStartDate, DEndDate = x.DEndDate, IStatus = x.IStatus, CRemark = x.CRemark, IIsDel = x.IIsDel, }).ToList(); return result; } public async Task DeleteAsync(string id) { var entity = await _devOpsPlanRepository.GetByIdAsync(id); entity.IIsDel = true; _devOpsPlanRepository.Update(entity); var result = await _devOpsPlanRepository.SaveAsync(); } public async Task GetByIdAsync(string id) { var entity = await _devOpsPlanRepository.GetByIdAsync(id); var model = new DevOpsPlanModel { CId = entity.CId, CName = entity.CName, CNumber = entity.CNumber, IType = entity.IType, IGhzQty = entity.IGhzQty, IGhzQtyLat = entity.IGhzQtyLat, DStartDate = entity.DStartDate, DEndDate = entity.DEndDate, CRemark = entity.CRemark, IStatus = entity.IStatus, CStoreCode = entity.CStoreCode, IIsDel = entity.IIsDel, }; return model; } public Task IsExistAsync(string id) { throw new NotImplementedException(); } public async Task UpdateOneAsync(DevOpsPlanModel viewModel, params string[] fields) { var entity = await _devOpsPlanRepository.GetByIdAsync(viewModel.CId); entity.CName = viewModel.CName; entity.CNumber = viewModel.CNumber; entity.IType = viewModel.IType; entity.IGhzQty = viewModel.IGhzQty; entity.IGhzQtyLat = viewModel.IGhzQtyLat; entity.DStartDate = viewModel.DStartDate; entity.DEndDate = viewModel.DEndDate; entity.CRemark = viewModel.CRemark; entity.IStatus = viewModel.IStatus; entity.IIsDel = viewModel.IIsDel; _devOpsPlanRepository.Update(entity); var result = await _devOpsPlanRepository.SaveAsync(); return result ? 1 : 0; } public async Task AddDeviceAsync(DevOpsPlanDeviceModel devOpsPlanDeviceModel) { var delModels = await _devOpsPlanDeviceRepository.GetByConditionAsync(x => x.CDevOpsPlanCode == devOpsPlanDeviceModel.CDevOpsPlanCode && x.CDevStoreCode == devOpsPlanDeviceModel.CDevStoreCode); delModels.ForEach(x => { x.IIsDel = true; _devOpsPlanDeviceRepository.Update(x); }); List entities = new List(); devOpsPlanDeviceModel.cSpotCodes.ForEach(x => { var entity = new TmtnDevOpsPlanDevice() { CId = Ulid.NewUlid().ToString(), CDevOpsPlanCode = devOpsPlanDeviceModel.CDevOpsPlanCode, CSpotCode = x, CDevStoreCode = devOpsPlanDeviceModel.CDevStoreCode, CRemark = devOpsPlanDeviceModel.CRemark, CCreateBy = _claims.ApiUserId.ToString(), D_CreateOn = DateTime.Now, CCreator = _claims.ApiUserName, IIsDel = false, }; entities.Add(entity); }); var result = await _devOpsPlanDeviceRepository.CreateRangeAsync(entities); //await _devOpsPlanDeviceRepository.SaveAsync(); if (result == 0) { throw new Exception("创建失败"); } } public async Task> DeviceListAsync(string id) { var query = from a in _devOpsPlanDeviceRepository.DbContext.TmtnDevOpsPlanDevices join b in _devOpsPlanDeviceRepository.DbContext.TDEV_DevStore on a.CDevStoreCode equals b.C_ID into temp from x in temp.DefaultIfEmpty() join c in _devOpsPlanDeviceRepository.DbContext.TISP_Spot on a.CSpotCode equals c.C_Code.ToString() into temp1 from y in temp1.DefaultIfEmpty() //join d in _devOpsPlanDeviceRepository.DbContext.TmtnDevOpsPlanContents on a.CId equals d.CDevOpsPlanDeviceCode into temp2 //from z in temp2.DefaultIfEmpty() where a.CDevOpsPlanCode == id && a.IIsDel == false select new DevOpsPlanDeviceModel { CDevOpsPlanCode = a.CDevOpsPlanCode, CDevStoreCode = a.CDevStoreCode, CSpotCode = a.CSpotCode, CId = a.CId, CRemark = a.CRemark, D_CreateOn = a.D_CreateOn, IIsDel = a.IIsDel, DevStoreName = x.C_Name, Url = x.C_Url, CNumberCode = x.C_NumberCode, SpotName = y.C_Name, IsConfig = a.TmtnDevOpsPlanContents.Any(), }; var result = await query.ToListAsync(); return result; //var result = await _devOpsPlanDeviceRepository.GetDevOpsPlanDevices(id); //return result; } public async Task DeleteDeviceAsync(string id) { var entity = await _devOpsPlanDeviceRepository.GetByIdAsync(id); entity.IIsDel = true; _devOpsPlanDeviceRepository.Update(entity); var result = await _devOpsPlanDeviceRepository.SaveAsync(); } public async Task DeleteDeviceSpotAsync(string planId, string spotId) { var entities = await _devOpsPlanDeviceRepository.GetByConditionAsync(x => x.CDevOpsPlanCode == planId && x.CSpotCode == spotId); entities.ForEach(x => { x.IIsDel = true; _devOpsPlanDeviceRepository.Update(x); }); var result = await _devOpsPlanDeviceRepository.SaveAsync(); } public async Task AddDeviceContentAsync(DevOpsPlanContentModel devOpsPlanContentModel) { var delModels = await _devOpsPlanDeviceContentRepository.GetByConditionAsync(x => x.CDevOpsPlanDeviceCode == devOpsPlanContentModel.CDevOpsPlanDeviceCode && x.CDevOpsPlanDeviceCode == devOpsPlanContentModel.CDevOpsPlanDeviceCode); delModels.ForEach(x => { x.IIsDel = true; _devOpsPlanDeviceContentRepository.Update(x); }); List entities = new List(); devOpsPlanContentModel.CDevOpsContentCodes.ForEach(x => { var entity = new TmtnDevOpsPlanContent() { CId = Ulid.NewUlid().ToString(), CDevOpsPlanDeviceCode = devOpsPlanContentModel.CDevOpsPlanDeviceCode, CSpotCode = devOpsPlanContentModel.CSpotCode, CDevOpsContentCode = x, ISort = devOpsPlanContentModel.ISort, IIsDel = false, CRemark = devOpsPlanContentModel.CRemark, CCreateBy = _claims.ApiUserId.ToString(), D_CreateOn = DateTime.Now, CCreator = _claims.ApiUserName, }; entities.Add(entity); }); var result = await _devOpsPlanDeviceContentRepository.CreateRangeAsync(entities); //await _devOpsPlanDeviceRepository.SaveAsync(); if (result == 0) { throw new Exception("创建失败"); } } public async Task> DeviceContentListAsync(string deviceId, string id) { var result = await _devOpsPlanDeviceContentRepository.GetByConditionAsync(x => x.CSpotCode == id && x.CDevOpsPlanDeviceCode == deviceId && x.IIsDel == false); return result.Select(x => x.CDevOpsContentCode).ToList(); } public async Task AddPlanPersonAsync(DevOpsPlanPersonModel devOpsPlanContentModel) { var dels = await _devOpsPlanPersonRepository.GetByConditionAsync(x => x.CDevOpsPlanCode == devOpsPlanContentModel.CDevOpsPlanCode); await _devOpsPlanPersonRepository.RemoveRangeAsync(dels); if (devOpsPlanContentModel.CDevOpsBys.Count <= 0) { return; } List entities = new List(); devOpsPlanContentModel.CDevOpsBys.ForEach(x => { var entity = new TmtnDevOpsPlanPerson { CId = Ulid.NewUlid().ToString(), CDevOpsPlanCode = devOpsPlanContentModel.CDevOpsPlanCode, CDevOpsBy = x, CCreateBy = _claims.ApiUserId.ToString(), CCreator = _claims.ApiUserName, DCreateOn = DateTime.Now }; entities.Add(entity); }); var result = await _devOpsPlanPersonRepository.CreateRangeAsync(entities); if (result == 0) { throw new Exception("创建失败"); } } public async Task> PlanPersonListAsync(string id) { var result = await _devOpsPlanPersonRepository.GetByConditionAsync(x => x.CDevOpsPlanCode == id); return result.Select(x => x.CDevOpsBy).ToList(); } public async Task CancelPlan(string id) { var entity = await _devOpsPlanRepository.GetByIdAsync(id); //if (entity.IStatus == 3 || entity.IStatus == 5 || entity.IStatus == 6) //{ // throw new Exception("计划确认失败"); //} entity.IStatus = 4; _devOpsPlanRepository.Update(entity); var result = await _devOpsPlanRepository.SaveAsync(); return result; } public async Task ConfirmPlan(string id) { var entity = await _devOpsPlanRepository.GetByIdAsync(id); if (entity.IStatus == 3 || entity.IStatus == 5 || entity.IStatus == 6) { throw new Exception("计划确认失败"); } entity.IStatus = 3; _devOpsPlanRepository.Update(entity); var result = await _devOpsPlanRepository.SaveAsync(); return result; } public async Task GenerateWorkOrder(string id) { var entity = await _devOpsPlanRepository.GetByIdAsync(id); if (entity.IStatus != 3) { throw new Exception("计划未确认,工单生成失败"); } // 生成工单 if (entity.IType == 1) { List devOps = new List(); List devOpsRecordList = new List(); var devices = (await _devOpsPlanDeviceRepository.GetByConditionAsync(x => x.CDevOpsPlanCode == id && x.IIsDel == false)).ToList(); int number = 1; for (int i = 0; i < entity.IGhzQty; i++) { //生成工单 foreach (var item in devices) { var workOrder = new TMTN_DevOps() { C_ID = Guid.NewGuid().ToString(), C_SpotCode = item.CSpotCode, C_DevOpsPlanCode = entity.CId, C_DevStoreCode = item.CDevStoreCode, C_CreateBy = _claims.ApiUserId, D_CreateOn = DateTime.Now, C_Status = "1", C_Name = entity.CName, C_Remark = entity.CName, }; workOrder.C_Number = (entity.IType == 1 ? "WB" : "DJ") + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + number.ToString(); var allContents = (await _devOpsPlanDeviceContentRepository.GetByConditionAsync(c => c.CDevOpsPlanDeviceCode == item.CId && c.IIsDel == false)).ToList(); if (allContents == null || !allContents.Any()) { throw new Exception($"未配置维保内容"); } //生成维保记录 allContents.ForEach(c => { TMTN_DevOpsRecord record = new TMTN_DevOpsRecord(); record.C_SpotDevOpsContentCode = c.CDevOpsContentCode; record.C_DevOpsCode = workOrder.C_ID; record.C_Record = entity.CName; record.C_Remark = entity.CName; record.C_ID = Guid.NewGuid().ToString(); record.C_CreateBy = _claims.ApiUserId; record.D_CreateOn = DateTime.Now; record.C_Status = "1"; devOpsRecordList.Add(record); }); //生成维保记录 devOps.Add(workOrder); number++; } } await _devOpsPlanRepository.DbContext.TMTN_DevOpsRecord.AddRangeAsync(devOpsRecordList); await _devOpsPlanRepository.DbContext.TMTN_DevOps.AddRangeAsync(devOps); } else { List workOrders = new List(); var devices = await _devOpsPlanDeviceRepository.GetByConditionAsync(x => x.CDevOpsPlanCode == id && x.IIsDel == false); int number = 1; for (int i = 0; i < entity.IGhzQty; i++) { devices.ForEach(x => { var workOrder = new TispInspectionWorkOrder() { CId = Guid.NewGuid().ToString(), CName = "计划生成", CDevOpsPlanCode = entity.CId, CSpotCode = x.CSpotCode, CDevStoreCode = x.CDevStoreCode, CRemark = "计划生成", CCreateBy = _claims.ApiUserId.ToString(), DCreateOn = DateTime.Now, CStatus = "2", }; workOrder.CNumber = entity.IType == 1 ? "WB" : "DJ" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + number.ToString(); workOrders.Add(workOrder); number++; }); } await _devOpsPlanDeviceRepository.DbContext.TispInspectionWorkOrders.AddRangeAsync(workOrders); } entity.IStatus = 5; _devOpsPlanRepository.Update(entity); var result = await _devOpsPlanRepository.SaveAsync(); return result; } public async Task> GetDevOps(string id, BaseSearchModel searchModel) { var entity = await _devOpsPlanRepository.GetByIdAsync(id); if (entity.IType == 1) { var query = from plan in _devOpsPlanRepository.DbContext.TmtnDevOpsPlans join a in _devOpsPlanRepository.DbContext.TMTN_DevOps on plan.CId equals a.C_DevOpsPlanCode into temp from ops in temp.DefaultIfEmpty() join c in _devOpsPlanRepository.DbContext.TISP_Spot on ops.C_SpotCode equals c.C_Code.ToString() into temp1 from spot in temp1.DefaultIfEmpty() join d in _devOpsPlanRepository.DbContext.TDEV_DevStore on ops.C_DevStoreCode equals d.C_ID into temp2 from store in temp2.DefaultIfEmpty() join e in _devOpsPlanRepository.DbContext.TMTN_DevOpsRecord on ops.C_ID equals e.C_DevOpsCode into temp3 from record in temp3.DefaultIfEmpty() join f in _devOpsPlanRepository.DbContext.TSYS_User on record.C_CreateBy equals f.C_UserID into temp4 from user in temp4.DefaultIfEmpty() where plan.CId == id select new TmtnDevOpsDetailViewModel { OpsId = ops.C_ID, C_Name = spot.C_Name, DevStoreName = store.C_Name, DevOpsName = ops.C_Name, C_CreateByName = user.C_Name, D_CreateOn = record.D_CreateOn, C_Status = record.C_Status, PlanStartDate = ops.D_PlanStartDate, PlanEndDate = ops.D_PlanEndDate, CExamineBy = ops.C_ExamineBy.ToString(), DExamineOn = ops.D_ExamineOn, DevOpsStatus = ops.C_Status }; searchModel.TotalCount = await query.CountAsync(); var result = searchModel.IsPagination ? await query.Skip((searchModel.PageIndex - 1) * searchModel.PageSize).Take(searchModel.PageSize).ToListAsync() : await query.ToListAsync(); return result; } else { var query = from plan in _devOpsPlanRepository.DbContext.TmtnDevOpsPlans join a in _devOpsPlanRepository.DbContext.TispInspectionWorkOrders on plan.CId equals a.CDevOpsPlanCode into temp from ops in temp.DefaultIfEmpty() join c in _devOpsPlanRepository.DbContext.TISP_Spot on ops.CSpotCode equals c.C_Code.ToString() into temp1 from spot in temp1.DefaultIfEmpty() join d in _devOpsPlanRepository.DbContext.TDEV_DevStore on ops.CDevStoreCode equals d.C_ID into temp2 from store in temp2.DefaultIfEmpty() //join e in _devOpsPlanRepository.DbContext.TISP_Record on ops.CId equals e.C_InspectionWorkOrderCode into temp3 //from record in temp3.DefaultIfEmpty() //join f in _devOpsPlanRepository.DbContext.TSYS_User on record.C_CreateBy equals f.C_UserID into temp4 //from user in temp4.DefaultIfEmpty() join g in _devOpsPlanRepository.DbContext.TSYS_User on ops.CExamineBy equals g.C_UserID.ToString() into temp5 from user1 in temp5.DefaultIfEmpty() where plan.CId == id select new TmtnDevOpsDetailViewModel { OpsId = ops.CId, C_Name = spot.C_Name, DevStoreName = store.C_Name, DevOpsName = ops.CName, //C_CreateByName = user.C_Name, //D_CreateOn = record.D_CreateOn, //C_Status = record.C_Status, PlanStartDate = ops.DPlanStartDate, PlanEndDate = ops.DPlanEndDate, CExamineBy = ops.CExamineBy, DExamineOn = ops.DExamineOn, DevOpsStatus = ops.CStatus }; searchModel.TotalCount = await query.CountAsync(); var result = searchModel.IsPagination ? await query.Skip((searchModel.PageIndex - 1) * searchModel.PageSize).Take(searchModel.PageSize).ToListAsync() : await query.ToListAsync(); return result; } } public async Task SetOpsTime(SetOpsModel model) { var plan = await _devOpsPlanRepository.GetByIdAsync(model.PlanId); if (plan.IType == 1) { var entity = await _devOpsPlanRepository.DbContext.TMTN_DevOps.Where(x => x.C_ID == model.Id).FirstOrDefaultAsync(); entity.D_PlanStartDate = model.Start; entity.D_PlanEndDate = model.End; var result = await _devOpsPlanRepository.DbContext.SaveChangesAsync(); return result > 0; } else { var entity = await _devOpsPlanRepository.DbContext.TispInspectionWorkOrders.Where(x => x.CId == model.Id).FirstOrDefaultAsync(); entity.DPlanStartDate = model.Start; entity.DPlanEndDate = model.End; var result = await _devOpsPlanRepository.DbContext.SaveChangesAsync(); return result > 0; } } public async Task GetSpotContent(GetOpsSpotContentModel model) { var entity = await _devOpsPlanRepository.DbContext.TispInspectionWorkOrders.Where(x => x.CId == model.OpsId).FirstOrDefaultAsync(); if (entity is null) { throw new Exception("工单不存在"); } var id = new Guid(entity.CSpotCode); TispSpotViewModel spotDto = await _devOpsPlanRepository.DbContext.TISP_Spot.Where(x => x.C_Code == id && x.C_StoreCode == model.StoreCode) .Select(x => new TispSpotViewModel { C_Code = x.C_Code, C_Number = x.C_Number, C_Name = x.C_Name, C_QRCode = x.C_QRCode, C_GPS = x.C_GPS, I_Offset = x.I_Offset, F_Map_X = x.F_Map_X, F_Map_Y = x.F_Map_Y, }).FirstOrDefaultAsync(); if (spotDto.C_QRCode != model.QrCode) { throw new Exception("二维码不正确"); } //获取运维计划配置的内容 var queryAble1 = from a in _devOpsPlanDeviceContentRepository.DbContext.TmtnDevOpsPlans join b in _devOpsPlanDeviceContentRepository.DbContext.TmtnDevOpsPlanDevices on a.CId equals b.CDevOpsPlanCode into temp from c in temp.DefaultIfEmpty() join d in _devOpsPlanDeviceContentRepository.DbContext.TmtnDevOpsPlanContents on c.CId equals d.CDevOpsPlanDeviceCode into temp1 from e in temp1.DefaultIfEmpty() join f in _devOpsPlanDeviceContentRepository.DbContext.TISP_Content on e.CDevOpsContentCode equals f.C_ID.ToString() into temp2 from g in temp2.DefaultIfEmpty() where a.CId == entity.CDevOpsPlanCode && c.CSpotCode == entity.CSpotCode && c.CDevStoreCode == entity.CDevStoreCode select new TispContentViewModel { C_ID = g.C_ID, C_Name = g.C_Name, C_AlarmLevel = g.C_AlarmLevel, C_Number = g.C_Number, C_Remark = g.C_Remark, C_Status = g.C_Status, C_StoreCode = g.C_StoreCode, I_Sort = g.I_Sort, D_CreateOn = "", }; List spotContentDtoList = await queryAble1.ToListAsync(); spotDto.ContentList = spotContentDtoList; return spotDto; } public async Task SetWorkOrderStatus(GetOpsSpotContentModel model) { var entity = await _devOpsPlanRepository.DbContext.TispInspectionWorkOrders.Where(x => x.CId == model.OpsId).FirstOrDefaultAsync(); if (entity is null) { throw new Exception("工单不存在"); } entity.CStatus=model.Status; _devOpsPlanRepository.DbContext.TispInspectionWorkOrders.Update(entity); await _devOpsPlanRepository.SaveAsync(); } } }