using AutoMapper; using LinqKit; using Newtonsoft.Json; using Ropin.Inspection.Common.Accessor.Interface; using Ropin.Inspection.Common.Helper; using Ropin.Inspection.Model; using Ropin.Inspection.Model.Entities; using Ropin.Inspection.Repository; using Ropin.Inspection.Repository.Interface; using Ropin.Inspection.Repository.SYS.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace Ropin.Inspection.Service { public class TsysMessageService : ITsysMessageService { private readonly ITsysMessageRepository _repository; private readonly ITsysMessageFileRepository _tsysMessageFileRepository; private readonly IMapper _mapper; private readonly IClaimsAccessor _claims; private readonly IUnitOfWork _unitOfWork; public TsysMessageService(IClaimsAccessor claims, ITsysMessageRepository repository, IMapper mapper, ITsysMessageFileRepository tsysMessageFileRepository, IUnitOfWork unitOfWork) { _repository = repository; _mapper = mapper; _claims = claims; _tsysMessageFileRepository = tsysMessageFileRepository; _unitOfWork = unitOfWork; } public async Task CreateOneAsync(TsysMessageViewModel viewModel) { bool result = false; try { _unitOfWork.BeginTransaction(); var content = _mapper.Map(viewModel); content.C_ID = Guid.NewGuid().ToString(); content.C_CreateBy = _claims.ApiUserId.ToString(); content.D_CreateOn = DateTime.Now; content.C_Status = "1"; result = await _unitOfWork.RegisterNew(content); if (!result) { throw new Exception("创建失败"); } if (viewModel.fileList != null && viewModel.fileList.Count > 0) { List fileList = new List(); foreach (var item in viewModel.fileList) { TSYS_MessageFile app = new TSYS_MessageFile { C_ID = Guid.NewGuid().ToString(), C_MessageCode= content.C_ID, C_Url = item.C_Url, C_Type = item.C_Type, C_CreateBy = _claims.ApiUserId.ToString(), D_CreateOn = DateTime.Now, C_Status = "1" }; fileList.Add(app); } if (fileList.Count > 0) { result = await _unitOfWork.RegisterRangeNew(fileList); } } } catch { _unitOfWork.Rollback(); throw; } finally { if (result) await _unitOfWork.CommitAsync(); else _unitOfWork.Rollback(); } } public async Task AddMessage(AlmRecordOutput almRecord) { var IsData = await _repository.GetByConditionAsync(t => t.C_GenerationCode == almRecord.Id.ToString()); var dataList = IsData.ToList(); if (dataList != null && dataList.Count() > 0) { return true; } bool result = false; try { _unitOfWork.BeginTransaction(); TSYS_Message content = new TSYS_Message(); content.C_ID = Guid.NewGuid().ToString(); content.C_LicenseCode = "SYSTEM"; content.C_Content = almRecord.Event_Name; content.C_DevStoreCode = "d705ceb5-7473-4b19-91dd-d3eff223f05b"; content.C_MsgTypeCode = "MSG_TYPE_024"; content.C_Subject = almRecord.Device_TypeName + "报警"; content.D_MsgCreateOn = almRecord.StartTime; content.C_GenerationCode= almRecord.Id.ToString(); content.I_GenerationType = 1; content.I_MsgStatus = 1; content.C_Remark = "来源:AI项目报警处理"; content.C_CreateBy = "6e864cbc-5252-11ec-8681-fa163e02b3e4"; content.C_Creator = "AI"; content.D_CreateOn = DateTime.Now; content.C_Status = "1"; result = await _unitOfWork.RegisterNew(content); if (!result) { throw new Exception("创建失败"); } else { var textJson = new { PanoramaId=almRecord.PanoramaId, PanoramaUrl=almRecord.PanoramaUrl, RoiJson=almRecord.RoiJson, NonRoiJson=almRecord.NonRoiJson, DetectionFrameJson=almRecord.DetectionFrameJson }; var datas = JsonConvert.SerializeObject(textJson); TSYS_MessageFile app = new TSYS_MessageFile { C_ID = Guid.NewGuid().ToString(), C_MessageCode = content.C_ID, C_Type = "FILE_TYP_006", C_Text = datas, C_CreateBy = "6e864cbc-5252-11ec-8681-fa163e02b3e4", D_CreateOn = DateTime.Now, C_Status = "1" }; result = await _unitOfWork.RegisterNew(app); } } catch { _unitOfWork.Rollback(); throw; } finally { if (result) await _unitOfWork.CommitAsync(); else _unitOfWork.Rollback(); } return result; } public async Task DeleteAsync(string id) { var content = await _repository.GetByIdAsync(id); if (content == null) { throw new Exception("数据库中没有此数据"); } //_repository.Delete(content); //var result = await _repository.SaveAsync(); content.C_LastUpdatedBy = _claims.ApiUserId.ToString(); content.D_LastUpdatedOn = DateTime.Now; content.C_Status = "0"; _repository.Update(content); var result = await _repository.SaveAsync(); if (!result) { throw new Exception("删除失败"); } } public async Task> GetAllAsync() { var pagedList = await _repository.GetAllAsync(); var contentDtoList = _mapper.Map>(pagedList.Where(i => i.C_Status == "1").ToList()); return contentDtoList.ToList(); } public async Task> GetConditionAsync(TsysMessageSearchModel searchModel) { return await _repository.GetList(searchModel); } public async Task> GetConditionScreenAsync(TsysMessageSearchModel searchModel) { IEnumerable data= await _repository.GetList(searchModel); IEnumerable result = _mapper.Map, IEnumerable>(data); return result; } public async Task GetByIdAsync(string id) { TsysMessageSearchModel searchModel = new TsysMessageSearchModel(); searchModel.C_ID = id; searchModel.IsPagination = false; var content = await _repository.GetList(searchModel); if (content == null) { return null; } else { var contentDto = content.FirstOrDefault(); if (contentDto != null) { TsysMessageFileSearchModel fileSearch = new TsysMessageFileSearchModel(); fileSearch.C_MessageCode = id; fileSearch.IsPagination = false; fileSearch.C_Status = "1"; contentDto.fileList = (List)await _tsysMessageFileRepository.GetList(fileSearch); } return contentDto; } } public async Task UpdateAsync(string id, TsysMessageUpdateModel updateModel) { var content = await _repository.GetByIdAsync(id); if (content == null) { throw new Exception("没有此数据"); } _mapper.Map(updateModel, content, typeof(TsysMessageUpdateModel), typeof(TSYS_Message)); content.C_LastUpdatedBy = _claims.ApiUserId.ToString(); content.D_LastUpdatedOn = DateTime.Now; _repository.Update(content); var result = await _repository.SaveAsync(); if (result) { if (updateModel.DelFile!=null&&updateModel.DelFile.Count>0) { var delFileResult= await _tsysMessageFileRepository.DeleteMultitByID(updateModel.DelFile,null); } if (updateModel.fileList != null && updateModel.fileList.Count > 0) { List fileList = new List(); foreach (var item in updateModel.fileList) { TSYS_MessageFile app = new TSYS_MessageFile { C_ID = Guid.NewGuid().ToString(), C_MessageCode = content.C_ID, C_Url = item.C_Url, C_Type = item.C_Type, C_CreateBy = _claims.ApiUserId.ToString(), D_CreateOn = DateTime.Now, C_Status = "1" }; fileList.Add(app); } if (fileList.Count > 0) { result = await _tsysMessageFileRepository.CreateRangeAsync(fileList)>0; } } } if (!result) { throw new Exception("更新失败"); } } public async Task UpdateMsgStatusAsync(string id, int msgStatus) { var result = await _repository.UpdateMsgStatus(id,msgStatus); if (!result) { throw new Exception("更新失败"); } } public Task UpdateOneAsync(TsysMessageViewModel viewModel, params string[] fields) { throw new NotImplementedException(); } public Task> GetByConditionAsync(Expression> expression) { throw new NotImplementedException(); } public Task IsExistAsync(string id) { throw new NotImplementedException(); } } }