package com.newfiber.api.pc.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.newfiber.api.core.commons.CustomException; import com.newfiber.api.core.commons.PageRequestObject; import com.newfiber.api.core.commons.PageResultObject; import com.newfiber.api.core.commons.ResultCode; import com.newfiber.api.pc.dao.MeetAlterMapper; import com.newfiber.api.pc.dao.ReturnGoodsMapper; import com.newfiber.api.pc.dto.AlertStatisticsDTO; import com.newfiber.api.pc.dto.DataOperationDTO; import com.newfiber.api.pc.dto.MeetAlertDTO; import com.newfiber.api.pc.dto.MeetAlertSerchDTO; import com.newfiber.api.pc.model.meet.*; import com.newfiber.api.pc.model.vo.*; import com.newfiber.api.pc.service.HouseSiteSuppliesService; import com.newfiber.api.pc.service.MeetAlertInfoService; import com.newfiber.api.pc.service.MeetAlertMediaService; import com.newfiber.api.pc.service.MeetAlterService; import org.apache.catalina.servlet4preview.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * @Author:zzh * @CreateDate:2020/11/27 14:17 * @Description: */ @Service @Transactional(rollbackFor = Exception.class) public class MeetAlterServiceImpl extends ServiceImpl<MeetAlterMapper,MeetAlter> implements MeetAlterService { @Autowired private MeetAlertMediaService meetAlertMediaService; @Autowired private MeetAlertInfoService meetAlertInfoService; @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private HouseSiteSuppliesService houseSiteSuppliesService; @Autowired private ReturnGoodsMapper returnGoodsMapper; public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void addAlert(MeetAlertDTO meetAlertDTO, String userNo) { if(StringUtils.isEmpty(meetAlertDTO)){ throw new CustomException(ResultCode.PARAM_NULL); } MeetAlter meetAlter = new MeetAlter(); //主要负责人 meetAlter.setPrimaryUserNo(meetAlertDTO.getPrimaryUserNo()); meetAlter.setPeopleId(meetAlertDTO.getPeopleId()); meetAlter.setPlanId(meetAlertDTO.getPlanId()); meetAlter.setAlertDescribe(meetAlertDTO.getAlertDescribe()); meetAlter.setAlertName(meetAlertDTO.getAlertName()); meetAlter.setAlertTypeId(meetAlertDTO.getAlertTypeId()); meetAlter.setAlertType(meetAlertDTO.getAlertType()); meetAlter.setAlertAddress(meetAlertDTO.getAlertAddress()); //发起人 meetAlter.setLaunchUserNo(userNo); meetAlter.setStartTime(new Date()); meetAlter.setIsEnd(0); //触发方式 meetAlter.setWay(meetAlertDTO.getWay()); boolean insert = this.insert(meetAlter); if(!insert){ throw new CustomException(ResultCode.SAVE_ERROR); } //添加媒体数据 addMedia(meetAlertDTO.getMeetAlertMedia(),meetAlter.getAId()); //添加人数与物资数据 addInfo(meetAlertDTO.getMeetAlertInfo(),meetAlter.getAId()); } /** * 添加警情的人员与物资 * @param meetAlertInfo * @param alertId */ private void addInfo(MeetAlertInfo meetAlertInfo,Integer alertId){ if(meetAlertInfo != null){ List<People> peopleList = meetAlertInfo.getPeopleList(); String people = JSONObject.toJSONString(peopleList); List<HouseSiteSupplies> subList = meetAlertInfo.getSubList(); String subString = JSONObject.toJSONString(subList); meetAlertInfo.setPersons(people); meetAlertInfo.setSubplies(subString); meetAlertInfo.setAId(alertId); //构建添加警情时需要扣除的物资对象 List<DataOperationDTO> dtos = new ArrayList<>(); boolean insert = meetAlertInfoService.insert(meetAlertInfo); //只有当需要物资的时候才进行物资扣减 if(subList.size() > 0){ //手动添加警情时,只能选择一个站点的物资,要是物资不够,只能通过调度去拿到 for(HouseSiteSupplies supplies :subList){ DataOperationDTO dto = new DataOperationDTO(); dto.setSupId(supplies.getSupId()); dto.setCount(supplies.getSupCount()); dtos.add(dto); } //执行扣除物资操作 houseSiteSuppliesService.deductionSupplies(alertId,subList.get(0).getHId(),dtos); } if(!insert){ throw new CustomException(ResultCode.SAVE_ERROR); } } } /** * 添加警情时添加的媒体数据 */ private void addMedia(MeetAlertMedia meetAlertMedia,Integer alertId){ if(meetAlertMedia != null){ String descrives = JSONObject.toJSONString(meetAlertMedia.getDescribeList()); String picPaths = JSONObject.toJSONString(meetAlertMedia.getPicPathVoList()); meetAlertMedia.setDescribes(descrives); meetAlertMedia.setPicFilePath(picPaths); meetAlertMedia.setAlertId(alertId); //警情状态对应的媒体数据- 0:开始 1结束 meetAlertMedia.setMediaState(0); meetAlertMediaService.insert(meetAlertMedia); } } @Override public void overAlert(MeetAlertMedia media, HttpServletRequest request) { String userNo = request.getHeader("userNo"); MeetAlter meetAlter = this.selectById(media.getAlertId()); meetAlter.setIsEnd(1); meetAlter.setEndTime(new Date()); meetAlter.setTerminateUserNo(userNo); boolean b = this.updateById(meetAlter); //更新媒体表 MeetAlertMedia meetAlertMedia = new MeetAlertMedia(); String descrives = JSONObject.toJSONString(media.getDescribeList()); String picPaths = JSONObject.toJSONString(media.getPicPathVoList()); meetAlertMedia.setDescribes(descrives); meetAlertMedia.setPicFilePath(picPaths); meetAlertMedia.setAlertId(media.getAlertId()); //警情状态对应的媒体数据- 0:开始 1结束 meetAlertMedia.setMediaState(1); boolean insert = meetAlertMediaService.insert(meetAlertMedia); if(!(b && insert)){ throw new CustomException(ResultCode.SAVE_ERROR); } } @Override public PageResultObject<MeetAlter> queryPage(PageRequestObject<MeetAlertSerchDTO> pafeObject, String useNo, String token) { boolean isHightAuth = false; UserVo userVo = null; if(!StringUtils.isEmpty(token)){ String userVoJson = stringRedisTemplate.opsForValue().get("session:"+token); userVo = JSON.parseObject(userVoJson, UserVo.class); if("yes".equalsIgnoreCase(userVo.getHighestAuthority())){ isHightAuth = true; } } MeetAlertSerchDTO serchDTO = pafeObject.getObject(); //创建分页条件 Page<MeetAlter> page = new Page<>(pafeObject.getCurrent(), pafeObject.getSize()); EntityWrapper<MeetAlter> wrapper = new EntityWrapper<>(); if(!StringUtils.isEmpty(serchDTO.getIsEnd())){ wrapper.eq("is_end",serchDTO.getIsEnd()); } if(!StringUtils.isEmpty(serchDTO.getAlertName())){ wrapper.like("alert_name",serchDTO.getAlertName()); } wrapper.orderDesc(Arrays.asList(new String[]{"a_id"})); //如果是最高权限者,就查询全部 if(isHightAuth){ Page<MeetAlter> meetAlterPage = this.selectPage(page, wrapper); int count = this.selectCount(wrapper); return new PageResultObject<>(pafeObject.getCurrent(),pafeObject.getSize(),(long)count,meetAlterPage.getRecords()); } //如果不是最高权限 //查出所有的警情数据 List<MeetAlter> meetAlters = this.selectList(wrapper); List<Integer> aId = new ArrayList<>(); if(meetAlters != null && meetAlters.size() > 0){ List<Integer> aIds = meetAlters.stream().map(MeetAlter::getAId).collect(Collectors.toList()); EntityWrapper<MeetAlertInfo> infoEntityWrapper = new EntityWrapper<>(); infoEntityWrapper.in("a_id",aIds); List<MeetAlertInfo> meetAlertInfos = meetAlertInfoService.selectList(infoEntityWrapper); for(MeetAlertInfo meetAlertInfo : meetAlertInfos){ List<People> peoples = JSON.parseObject(meetAlertInfo.getPersons(), List.class); for(People p : peoples){ if(p.getPeopleName().equalsIgnoreCase(useNo)){ aId.add(meetAlertInfo.getAId()); break; } } } } if(aId.size() > 0){ wrapper.in("a_id",aId); wrapper.orderBy("a_id",false); Page<MeetAlter> meetAlterPage = this.selectPage(page, wrapper); int count = this.selectCount(wrapper); return new PageResultObject<>(pafeObject.getCurrent(),pafeObject.getSize(),(long)count,meetAlterPage.getRecords()); } //该用户没有警情数据 return new PageResultObject<>(pafeObject.getCurrent(),pafeObject.getSize(),(long)0,null); } @Override public MeetAlter viewDetail(Integer aId) { if(StringUtils.isEmpty(aId)){ throw new CustomException(ResultCode.PARAM_NULL); } EntityWrapper<MeetAlertInfo> entityWrapper = new EntityWrapper<>(); entityWrapper.eq("a_id",aId); MeetAlertInfo meetAlertInfo = meetAlertInfoService.selectOne(entityWrapper); List<People> peopleList = JSON.parseObject(meetAlertInfo.getPersons(), List.class); meetAlertInfo.setPeopleList(peopleList); List<HouseSiteSupplies> subLists = JSON.parseObject(meetAlertInfo.getSubplies(), List.class); meetAlertInfo.setSubList(subLists); meetAlertInfo.setPersons(null); meetAlertInfo.setSubplies(null); EntityWrapper<MeetAlertMedia> wrapper = new EntityWrapper<>(); wrapper.eq("alert_id",aId); List<MeetAlertMedia> meetAlertMedia = meetAlertMediaService.selectList(wrapper); meetAlertMedia.stream().forEach(s ->{ s.setPicPathVoList(JSON.parseArray(s.getPicFilePath(), MediaPicPathVo.class)); s.setDescribeList(JSON.parseArray(s.getDescribes(), MediaDescribeVo.class)); s.setPicFilePath(null); s.setDescribes(null); }); MeetAlter meetAlter = this.selectById(aId); meetAlter.setMedias(meetAlertMedia); //只会存在一个 meetAlter.setInfo(meetAlertInfo); return meetAlter; } @Override public PageResultObject<AlertStatisticsVo> alertStatistics(PageRequestObject<AlertStatisticsDTO> pageRequestObject) { int current = (pageRequestObject.getCurrent() - 1) * pageRequestObject.getSize(); AlertStatisticsDTO statisticsDTO = pageRequestObject.getObject(); String serchTime = " AND start_time "; if(!StringUtils.isEmpty(statisticsDTO.getStartTime()) && !StringUtils.isEmpty(statisticsDTO.getEndTime())){ if (statisticsDTO.getStartTime().after(statisticsDTO.getEndTime())) { throw new CustomException(500, "开始时间不能大于结束时间"); } serchTime += " BETWEEN '" +format.format(statisticsDTO.getStartTime())+ "' and '" + format.format(statisticsDTO.getEndTime()) + "'"; } //如果传入的开始时间不为null,且结束时间为null,就找大于开始时间的数据 else if(StringUtils.isEmpty(statisticsDTO.getEndTime()) && !StringUtils.isEmpty(statisticsDTO.getStartTime())){ serchTime += " > '" + format.format(statisticsDTO.getStartTime()) + "'"; } //如果传入的开始时间为null,且结束时间不为null,就找小于结束时间的数据 else if(!StringUtils.isEmpty(statisticsDTO.getEndTime()) && StringUtils.isEmpty(statisticsDTO.getStartTime())){ serchTime += " < '" + format.format(statisticsDTO.getEndTime()) + "'"; }else{ serchTime = null; } List<AlertStatisticsVo> vos = returnGoodsMapper.alertStatistics(statisticsDTO.getAlertTypeId(),serchTime,current,pageRequestObject.getSize()); long count = returnGoodsMapper.alertStatisticsCount(statisticsDTO.getAlertTypeId(),serchTime); return new PageResultObject(current + 1,pageRequestObject.getSize(),count,vos); } @Override public PageResultObject<MeetAlter> queryAlertByTypeId( PageRequestObject<Integer> pageRequestObject) { Page<MeetAlter> objectPage = new Page<>(pageRequestObject.getCurrent(), pageRequestObject.getSize()); if(StringUtils.isEmpty(pageRequestObject.getObject())){ throw new CustomException(ResultCode.PARAM_NULL); } EntityWrapper<MeetAlter> wrapper = new EntityWrapper<>(); wrapper.eq("alert_type_id",pageRequestObject.getObject()); Page<MeetAlter> meetAlterPage = this.selectPage(objectPage, wrapper); int count = this.selectCount(wrapper); return new PageResultObject(pageRequestObject.getCurrent(),pageRequestObject.getSize(),(long)count,meetAlterPage.getRecords()); } @Override public List<AlertTypeVo> selectAlertType() { List<MeetAlter> meetAlters = this.selectList(null); List<AlertTypeVo> list = new ArrayList<>(); for(int i = 0; i < meetAlters.size(); i++){ MeetAlter meetAlter = meetAlters.get(i); AlertTypeVo vo = new AlertTypeVo(); vo.setAlertTypeId(meetAlter.getAlertTypeId()); vo.setAlertTypeName(meetAlter.getAlertType()); list.add(vo); } return list.stream().distinct().collect(Collectors.toList()); } }