package com.moral.service.impl; import java.util.Arrays; import java.util.List; import java.util.Map; import javax.annotation.Resource; import com.github.pagehelper.PageHelper; import com.moral.common.bean.Constants; import com.moral.common.bean.PageBean; import com.moral.common.util.*; import org.springframework.stereotype.Service; import com.moral.entity.MonitorPoint; import com.moral.mapper.MonitorPointMapper; import com.moral.service.MonitorPointService; import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.entity.Example.Criteria; @Service public class MonitorPointServiceImpl implements MonitorPointService { @Resource private MonitorPointMapper monitorPointMapper; @Resource RedisUtils redisUtils; private static Class ENTITY_CLASS = MonitorPoint.class; @Override public List getMonitorPointsByAreaName(Map parameters) { ValidateUtil.notNull(parameters.get("areaName"), "param.is.null"); return monitorPointMapper.getMonitorPointsByAreaName(parameters); } @Override public List queryWithStateByMap(Map params){ List monitorPointList = monitorPointMapper.selectByMap(params); for(MonitorPoint monitorPoint:monitorPointList){ loadStateFromRedis(monitorPoint); } return monitorPointList; } private void loadStateFromRedis(MonitorPoint monitorPoint){ StringBuilder key = new StringBuilder(); key.append("*_").append(monitorPoint.getId()).append("_*"); List stateList = redisUtils.getList(key.toString(),Map.class); int state = -1; if(stateList!=null){ for (Map deviceState:stateList){ int s = Integer.parseInt(deviceState.get("state").toString()); state = s>state&&s<4?s:state; } } state = state==-1?4:state; monitorPoint.setState(state); } @Override public PageBean queryByPageBean(PageBean pageBean) { Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean); List criteriaList = example.getOredCriteria(); if(criteriaList!=null&&criteriaList.size()>0){ for(Example.Criteria cri : criteriaList){ cri.andNotEqualTo("isDelete", Constants.IS_DELETE_TRUE); } }else { example.or().andNotEqualTo("isDelete",Constants.IS_DELETE_TRUE); } PageHelper.startPage(pageBean.getPageIndex(),pageBean.getPageSize()); List monitorPointList = monitorPointMapper.selectWithAreaNameByExample(example); return new PageBean(monitorPointList); } @Override public MonitorPoint queryWithRelationById(Integer id){ Example example = new Example(ENTITY_CLASS); example.or().andEqualTo("id",id); List monitorPointList = monitorPointMapper.selectWithAreaNameByExample(example); return monitorPointList!=null&&monitorPointList.size()>0?monitorPointList.get(0):null; } @Override public void addOrModify(MonitorPoint monitorPoint) { try{ if(monitorPoint.getId()==null){ monitorPoint.setIsDelete(Constants.IS_DELETE_FALSE); monitorPointMapper.insertSelective(monitorPoint); }else{ monitorPointMapper.updateByPrimaryKeySelective(monitorPoint); } } catch (Exception ex){ throw ex; } } @Override public void deleteByIds(Integer... ids) { MonitorPoint monitorPoint = new MonitorPoint(); monitorPoint.setIsDelete(Constants.IS_DELETE_TRUE); if(ids!=null&&ids.length>0){ if(ids.length==1){ monitorPoint.setId(ids[0]); monitorPointMapper.updateByPrimaryKeySelective(monitorPoint); }else{ Example example = new Example(ENTITY_CLASS); example.or().andIn("id", Arrays.asList(ids)); monitorPointMapper.updateByExampleSelective(monitorPoint,example); } } } @Override public List getMonitorPointsByName(String name) { Example example = new Example(MonitorPoint.class); Criteria criteria = example.createCriteria(); criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andLike("name", "%" + name + "%"); example.or().andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andCondition("getPY(name) like ", "%" + name + "%"); List monitorPoints = monitorPointMapper.selectByExample(example); return monitorPoints; } }