package com.moral.api.service.impl; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.moral.api.entity.GovMonitorPoint; import com.moral.api.entity.SysArea; import com.moral.api.mapper.GovMonitorPointMapper; import com.moral.api.mapper.SysAreaMapper; import com.moral.api.service.GovMonitorPointService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.util.LogUtils; import com.moral.constant.Constants; import com.moral.constant.RedisConstants; import com.moral.constant.ResponseCodeEnum; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.*; /** *

* 服务实现类 *

* * @author moral * @since 2021-09-09 */ @Service public class GovMonitorPointServiceImpl extends ServiceImpl implements GovMonitorPointService { @Autowired(required = false) private GovMonitorPointMapper govMonitorPointMapper; @Autowired(required = false) private SysAreaMapper sysAreaMapper; @Autowired(required = false) private RedisTemplate redisTemplate; @Autowired private LogUtils logUtils; /* * 从redis获取设备信息 * */ private Map getGovMonitorPointInfoFromRedis(String id) { return (Map) redisTemplate.opsForHash().get(RedisConstants.GOV_MONITOR_POINT, id); } /* * 设备信息存入redis */ private void setGovMonitorPointInfoToRedis(String id, GovMonitorPoint govMonitorPointInfo) { redisTemplate.opsForHash().put(RedisConstants.GOV_MONITOR_POINT, id, govMonitorPointInfo); } /* * 从redis删除设备信息 */ private void delGovMonitorPointInfoFromRedis(String id) { redisTemplate.opsForHash().delete(RedisConstants.GOV_MONITOR_POINT, id); } @Override public Map getDataByCondition(Map map) { Map resultMap = new HashMap<>(); if (!map.containsKey("current")||!map.containsKey("size")){ resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); return resultMap; } int current = Integer.parseInt(map.get("current").toString()); int size = Integer.parseInt(map.get("size").toString()); Page page = new Page<>(current,size); QueryWrapper wrapper_Condition = new QueryWrapper<>(); wrapper_Condition.eq("is_delete",Constants.NOT_DELETE); if (!ObjectUtils.isEmpty(map.get("name"))){ wrapper_Condition.like("name",map.get("name").toString()); } if (!ObjectUtils.isEmpty(map.get("stationLevel"))){ wrapper_Condition.like("station_level",map.get("stationLevel").toString()); } wrapper_Condition.orderByDesc("create_time"); Page resultPage = govMonitorPointMapper.selectPage(page,wrapper_Condition); int totalNumber = govMonitorPointMapper.selectCount(wrapper_Condition); List govMonitorPoints = resultPage.getRecords(); SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List> govMonitorPointList = new ArrayList<>(); for (GovMonitorPoint govMonitorPoint:govMonitorPoints) { Map govMonitorPointMap = JSON.parseObject(JSON.toJSONString(govMonitorPoint),Map.class); String createTime = SDF.format(govMonitorPoint.getCreateTime()); String updateTime = SDF.format(govMonitorPoint.getUpdateTime()); govMonitorPointMap.put("createTime",createTime); govMonitorPointMap.put("updateTime",updateTime); String position = ""; if (!ObjectUtils.isEmpty(govMonitorPoint.getProvinceCode())){ QueryWrapper wrapper_province = new QueryWrapper<>(); wrapper_province.eq("area_code",govMonitorPoint.getProvinceCode()); SysArea sysArea_provice = sysAreaMapper.selectOne(wrapper_province); if (!ObjectUtils.isEmpty(sysArea_provice)){ position = position+sysArea_provice.getAreaName(); if (!ObjectUtils.isEmpty(govMonitorPoint.getCityCode())){ QueryWrapper wrapper_city = new QueryWrapper<>(); wrapper_city.eq("area_code",govMonitorPoint.getCityCode()); SysArea sysArea_city = sysAreaMapper.selectOne(wrapper_city); if (!ObjectUtils.isEmpty(sysArea_city)){ position = position+"/"+sysArea_city.getAreaName(); if (!ObjectUtils.isEmpty(govMonitorPoint.getAreaCode())){ QueryWrapper wrapper_area = new QueryWrapper<>(); wrapper_area.eq("area_code",govMonitorPoint.getAreaCode()); SysArea sysArea_area = sysAreaMapper.selectOne(wrapper_area); if (!ObjectUtils.isEmpty(sysArea_area)){ position = position+"/"+sysArea_area.getAreaName(); } } } } } } govMonitorPointMap.put("position",position); govMonitorPointList.add(govMonitorPointMap); } resultMap.put("govMonitorPointList",govMonitorPointList); resultMap.put("totalNumber",totalNumber); resultMap.put("current",current); int totalPageNumber = totalNumber/size; if(totalNumber%size != 0){ totalPageNumber += 1; } resultMap.put("totalPageNumber",totalPageNumber); return resultMap; } @Override public void insert(GovMonitorPoint govMonitorPoint) { int count = govMonitorPointMapper.insert(govMonitorPoint); //数据插入redis setGovMonitorPointInfoToRedis(govMonitorPoint.getId().toString(),selectGovMonitorPointInfoById(govMonitorPoint.getId())); if (count > 0){ //操作日志记录 HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); StringBuilder content = new StringBuilder(); content.append("添加了政府站点:").append("id:").append(govMonitorPoint.getId()+";").append(govMonitorPoint.getName()+";").append(":").append("guid:").append(govMonitorPoint.getGuid()+";"); logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE); } } @Override public GovMonitorPoint selectGovMonitorPointInfoById(int id) { GovMonitorPoint govMonitorPoint = govMonitorPointMapper.selectById(id); return govMonitorPoint; } }