package com.newfiber.api.pc.service.impl; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.newfiber.api.core.utils.DateUtil; import com.newfiber.api.core.utils.DateUtils; import com.newfiber.api.pc.dao.RiverManage.PatrolManageStatisticsMapper; import com.newfiber.api.pc.dao.RiverManage.PatrolManageUserRelationMapper; import com.newfiber.api.pc.model.River.*; import com.newfiber.api.pc.service.PatrolManageConfigService; import com.newfiber.api.pc.service.PatrolManageStatisticsService; import com.newfiber.api.pc.service.PatrolManageUserRelationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; import java.util.stream.Collectors; @Service("patrolManageStatisticsService") public class PatrolManageStatisticsServiceImpl extends ServiceImpl<PatrolManageStatisticsMapper, PatrolManageStatistics> implements PatrolManageStatisticsService { @Autowired private PatrolManageStatisticsMapper patrolManageStatisticsMapper; @Autowired private PatrolManageConfigService patrolManageConfigService; @Autowired private PatrolManageUserRelationService patrolManageUserRelationService; @Autowired private PatrolManageUserRelationMapper patrolManageUserRelationMapper; @Override public PageInfo<PatrolManageStatistics> listForPage(String searchStr, Integer pageNo, Integer pageSize) { PageHelper.startPage(pageNo, pageSize); EntityWrapper<PatrolManageStatistics> wapper = new EntityWrapper<>(); //name 模糊匹配的字段 wapper.like("name", searchStr); List<PatrolManageStatistics> list = patrolManageStatisticsMapper.selectList(wapper); PageInfo<PatrolManageStatistics> result = new PageInfo<>(); if (!list.isEmpty()) { result = new PageInfo<PatrolManageStatistics>(list); } return result; } @Override public void insertData(PatrolManageRecord record) { //判断是否是当前和河道的巡查人员 if(!checkUser(record)){ return; } //查询当前所在的年月日是否存在数据 EntityWrapper<PatrolManageStatistics> wrapper = new EntityWrapper<>(); wrapper.eq("user_no",record.getCreateUserNo()); wrapper.eq("bind_no",record.getBindNo()); wrapper.orderDesc(Arrays.asList("update_time")); PatrolManageStatistics data = this.selectOne(wrapper); if (data != null) { boolean flag = isInsertOrUpdate(data); //巡查时间 double day = addPatrolTime(data.getDayPatrolTime(), record.getPatrolTime()); double month = addPatrolTime(data.getMonthPatrolTime(), record.getPatrolTime()); double year = addPatrolTime(data.getYearPatrolTime(), record.getPatrolTime()); data.setDayPatrolTime(day); data.setMonthPatrolTime(month); data.setYearPatrolTime(year); //巡查次数 data.setDayPatrolCount(data.getDayPatrolCount() + 1); data.setMonthPatrolCount(data.getMonthPatrolCount() + 1); data.setYearPatrolCount(data.getYearPatrolCount() + 1); //将巡查状态 checkState(data); //判断是否是当天的数据 data.setTime(DateUtil.getDayBegin()); data.setUpdateTime(new Date()); if (flag) { this.updateById(data); }else{ data.setCreateTime(new Date()); this.insert(data); } } else { Date time = new Date(); Date now = DateUtil.getDayBegin(); int year = DateUtils.getYear(time); int month = DateUtils.getMonth(time); PatrolManageStatistics patrol = new PatrolManageStatistics(); patrol.setPatrolType(record.getPatrolType()); patrol.setBindNo(record.getBindNo()); patrol.setUserNo(record.getCreateUserNo()); patrol.setDayPatrolTime(record.getPatrolTime()); patrol.setMonthPatrolTime(record.getPatrolTime()); patrol.setYearPatrolTime(record.getPatrolTime()); patrol.setDayPatrolCount(1); patrol.setMonthPatrolCount(1); patrol.setYearPatrolCount(1); patrol.setYear(year); patrol.setMonth(month); patrol.setTime(now); checkState(patrol); this.insert(patrol); } } @Override public PageInfo<PatrolManageUser> getList(String userName,Integer patrolType, String startTime, String endTime, Integer pageNo, Integer pageSize) { PageHelper.startPage(pageNo,pageSize); List<PatrolManageUserRelation> list = patrolManageUserRelationMapper.selectByUsername(userName,patrolType); PageInfo<PatrolManageUserRelation> listresult = new PageInfo<>(); for (PatrolManageUserRelation relation : list) { listresult = new PageInfo<PatrolManageUserRelation>(list); } PageInfo<PatrolManageUser> result = new PageInfo<>(); if(!list.isEmpty()){ Set<Long> param = new HashSet<>(); for (PatrolManageUserRelation relation : list) { param.add(relation.getBindNo()); } List<PatrolManageStatistics> statistics = patrolManageStatisticsMapper.selectByUser(patrolType,param,startTime,endTime); Map<Long, List<PatrolManageStatistics>> map = new HashMap<>(); if(statistics!=null && statistics.size()>0){ map = statistics.stream().collect(Collectors.groupingBy(PatrolManageStatistics::getBindNo)); } List<PatrolManageUser> users = new ArrayList<>(); for (PatrolManageUserRelation relation : list) { PatrolManageUser user = new PatrolManageUser(); user.setBindNo(relation.getBindNo()); user.setPatrolType(relation.getPatrolType()); user.setProjectName(relation.getProjectName()); user.setUserNo(relation.getHandlerNo()); user.setUserName(relation.getHandlerName()); if(map!=null && map.size()>0 && map.get(relation.getBindNo())!=null && !map.get(relation.getBindNo()).isEmpty()){ List<PatrolManageStatistics> statisticsList = map.get(relation.getBindNo()); if(statisticsList!=null && !statisticsList.isEmpty()){ setParam(statisticsList,user); } }else{ user.setPatrolHour(0); user.setPatrolCount(0); user.setState(0); } users.add(user); } result = new PageInfo<PatrolManageUser>(users); } result.setTotal(listresult.getTotal()); return result; } @Override public List<PatrolManageUserData> getDetail(String userNo, Integer patrolType, Long bindNo) { Date firstDayOfWeek = DateUtil.getFirstDayOfWeek(new Date()); Date lastDayOfWeek = DateUtil.getLastDayOfWeek(new Date()); List<PatrolManageStatistics> statisticsList = patrolManageStatisticsMapper.getDetail(userNo,patrolType,bindNo,firstDayOfWeek,lastDayOfWeek); List<PatrolManageUserData> list = new ArrayList<>(); PatrolManageUserData week = new PatrolManageUserData(); PatrolManageUserData month = new PatrolManageUserData(); PatrolManageUserData year = new PatrolManageUserData(); if (statisticsList!=null && !statisticsList.isEmpty()){ double time = 0L; boolean flag = true; Integer count = 0; for (PatrolManageStatistics statistics : statisticsList) { if(statistics.getDayPatrolTime()!=null){ time+=statistics.getDayPatrolTime(); } if(statistics.getDayPatrolCount()!=null){ count+=statistics.getDayPatrolCount(); } if(flag){ if(statistics.getDayPatrolState()!=null && statistics.getDayPatrolState()==0){ flag = false; } } } BigDecimal weekHour = new BigDecimal(time).divide(new BigDecimal(60),1, RoundingMode.HALF_UP); week.setType("周巡检"); week.setPatrolHour(weekHour.doubleValue()); if(flag){ week.setState(1); }else{ week.setState(0); } week.setCount(count); PatrolManageStatistics data = statisticsList.get(0); month.setType("月巡查"); month.setCount(data.getMonthPatrolCount()); month.setState(data.getMonthPatrolState()); BigDecimal monthHour = new BigDecimal(data.getMonthPatrolTime()).divide(new BigDecimal(60),1, RoundingMode.HALF_UP); month.setPatrolHour(monthHour.doubleValue()); year.setType("年巡查"); year.setCount(data.getYearPatrolCount()); year.setState(data.getYearPatrolState()); BigDecimal yearHour = new BigDecimal(data.getYearPatrolTime()).divide(new BigDecimal(60),1, RoundingMode.HALF_UP); year.setPatrolHour(yearHour.doubleValue()); }else{ EntityWrapper<PatrolManageStatistics> wrapper = new EntityWrapper<>(); wrapper.eq("user_no",userNo); wrapper.eq("bind_no",bindNo); wrapper.orderDesc(Arrays.asList("update_time")); PatrolManageStatistics data = this.selectOne(wrapper); if (data != null) { boolean flag = isInsertOrUpdate(data); //将巡查状态 checkState(data); //判断是否是当天的数据 data.setTime(DateUtil.getDayBegin()); data.setUpdateTime(new Date()); if (!flag) { data.setCreateTime(new Date()); this.insert(data); } week.setType("周巡查"); week.setCount(0); week.setState(0); week.setPatrolHour(0); month.setType("月巡查"); month.setCount(data.getMonthPatrolCount()); month.setState(data.getMonthPatrolState()); month.setPatrolHour(data.getMonthPatrolTime()); year.setType("年巡查"); year.setCount(data.getYearPatrolCount()); year.setState(data.getYearPatrolState()); month.setPatrolHour(data.getYearPatrolTime()); }else{ week.setType("周巡查"); week.setCount(0); week.setState(0); week.setPatrolHour(0); month.setType("月巡查"); month.setCount(0); month.setState(0); month.setPatrolHour(0); year.setType("年巡查"); year.setCount(0); year.setState(0); month.setPatrolHour(0); } } list.add(week); list.add(month); list.add(year); return list; } /** * 保存值 * @param statisticsList * @param user */ private void setParam(List<PatrolManageStatistics> statisticsList,PatrolManageUser user) { double time = 0L; boolean flag = true; Integer count = 0; for (PatrolManageStatistics statistics : statisticsList) { if(statistics.getDayPatrolTime()!=null){ time+=statistics.getDayPatrolTime(); } if(statistics.getDayPatrolCount()!=null){ count+=statistics.getDayPatrolCount(); } if(flag){ if(statistics.getDayPatrolState()!=null && statistics.getDayPatrolState()==0){ flag = false; } } } BigDecimal divide = new BigDecimal(time).divide(new BigDecimal(60),1, RoundingMode.HALF_UP); user.setPatrolHour(divide.doubleValue()); if(flag){ user.setState(1); }else{ user.setState(0); } user.setPatrolCount(count); } /** * 判断是否是巡查人员的巡查 * @param data * @return */ private boolean checkUser(PatrolManageRecord data) { EntityWrapper<PatrolManageUserRelation> wrapper = new EntityWrapper(); wrapper.eq("bind_no", data.getBindNo()); wrapper.eq("handler_no", data.getCreateUserNo()); PatrolManageUserRelation relation = patrolManageUserRelationService.selectOne(wrapper); if(relation!=null){ return true; } return false; } /** * 判断日月年是否达标 * * @param data */ private void checkState(PatrolManageStatistics data) { Double dayPatrolTime = data.getDayPatrolTime(); Double monthPatrolTime = data.getMonthPatrolTime(); Double yearPatrolTime = data.getYearPatrolTime(); EntityWrapper<PatrolManageConfig> wrapper = new EntityWrapper(); wrapper.eq("bind_no", data.getBindNo()); PatrolManageConfig config = patrolManageConfigService.selectOne(wrapper); Integer dayTime = 0; if (config != null) { data.setProjectName(config.getProjectName()); if (config.getDayTime() != null) { dayTime = config.getDayTime(); } }else{ dayTime = 8; } if (dayPatrolTime.compareTo(Double.valueOf(dayTime * 60)) > 0) { data.setDayPatrolState(1); } else { data.setDayPatrolState(0); } //获取当前月的天数 Integer monthCount = DateUtil.getDaysOfMonth(new Date()); if (monthPatrolTime.compareTo(Double.valueOf(dayTime * 60 * monthCount)) > 0) { data.setMonthPatrolState(1); } else { data.setMonthPatrolState(0); } //获取当前年的天数 Integer yearCount = DateUtil.getDaysOfYear(new Date()); if (yearPatrolTime.compareTo(Double.valueOf(dayTime * 60 * yearCount)) > 0) { data.setYearPatrolState(1); } else { data.setYearPatrolState(0); } } /** * 判断是否是当前日 月 年时间 * * @param data * @return */ private boolean isInsertOrUpdate(PatrolManageStatistics data) { Date time = new Date(); Date now = DateUtil.getDayBegin(); int year = DateUtils.getYear(time); int month = DateUtils.getMonth(time); Long time1 = data.getTime().getTime(); Long time2 = now.getTime(); //1.当天已存在巡查 if (time1.equals(time2)) { return true; } data.setId(null); data.setDayPatrolTime(0.0); data.setDayPatrolCount(0); //判断当前巡查是否是当前月 if (month == data.getMonth()) { return false; } data.setMonthPatrolState(0); data.setMonthPatrolTime(0.0); data.setMonthPatrolCount(0); //判断当前巡查是否是当前年 if (year != data.getYear()) { data.setYearPatrolState(0); data.setYearPatrolTime(0.0); data.setYearPatrolCount(0); } return false; } /** * double相加 * * @param num1 * @param num2 * @return */ private double addPatrolTime(double num1, double num2) { BigDecimal add = new BigDecimal(num1).add(new BigDecimal(num2)); return add.doubleValue(); } /** * integer相加 * * @param num1 * @param num2 * @return */ private Integer addPatrolCount(Integer num1, Integer num2) { BigDecimal add = new BigDecimal(num1).add(new BigDecimal(num2)); return add.intValue(); } }