fengxiang
2018-05-04 4a41bd4e105385b5460e5a81c8b67e5f701a262b
首页 工作台
12 files modified
169 ■■■■ changed files
src/main/java/com/moral/common/convert/StringToDateConverter.java 28 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/common/util/StringUtils.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/AlarmController.java 18 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/DeviceController.java 10 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/AlarmMapper.java 4 ●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/DeviceMapper.java 5 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/AlarmService.java 8 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/DeviceService.java 7 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/AlarmServiceImpl.java 18 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/DeviceServiceImpl.java 28 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/AlarmMapper.xml 22 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/DeviceMapper.xml 12 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/common/convert/StringToDateConverter.java
@@ -1,8 +1,11 @@
package com.moral.common.convert;
import com.moral.common.exception.BusinessException;
import com.moral.common.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
@@ -11,18 +14,31 @@
 * @version $Id: StringToDateConverter.java, v 0.1 2015年9月24日 下午7:19:41 byshome Exp $
 */
public class StringToDateConverter implements Converter<String, Date> {
//    private static final String dateFormat      = "yyyy-MM-dd HH:mm:ss";
      Logger log = Logger.getLogger(StringToDateConverter.class);
    private static final String dateFormat      = "yyyy-MM-dd HH:mm:ss";
//    private static final String shortDateFormat = "yyyy-MM-dd";
      private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
    /** 
     * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
     */
    @Override
    public Date convert(String source) {
          if(StringUtils.isNullOrEmpty(source)){
              throw new RuntimeException(String.format("parser %s to Date fail", source));
          };
          return  new Date(Long.parseLong(source));
              throw new BusinessException(String.format("parser %s to Date fail", source));
          }
          if("null".equals(source)){
              return  null;
          }
          if(StringUtils.isNumericZidai(source)){
              return  new Date(Long.parseLong(source));
          }
        try {
            return  simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }
}
src/main/java/com/moral/common/util/StringUtils.java
@@ -132,4 +132,13 @@
        sbuffer.append(suffix);
        return sbuffer.toString();
    }
    public static boolean isNumericZidai(String str) {
        for (int i = 0; i < str.length(); i++) {
            System.out.println(str.charAt(i));
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
src/main/java/com/moral/controller/AlarmController.java
@@ -1,26 +1,24 @@
package com.moral.controller;
import com.alibaba.fastjson.JSON;
import com.moral.common.bean.ResultBean;
import com.moral.entity.Alarm;
import com.moral.entity.Device;
import com.moral.entity.Sensor;
import com.moral.entity.alarm.AlarmConfig;
import com.moral.entity.alarm.AlarmConfigValue;
import com.moral.service.AlarmConfigService;
import com.moral.service.DeviceService;
import com.moral.service.HistoryService;
import com.moral.service.SensorService;
import com.moral.entity.charts.TimeUnits;
import com.moral.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@RestController
@RequestMapping("alarm")
@@ -34,6 +32,12 @@
    HistoryService historyService;
    @Resource
    AlarmConfigService alarmConfigService;
    @Resource
    AlarmService alarmService;
    @RequestMapping(value = "/count-by-times", method = RequestMethod.GET)
    public ResultBean<List<Map>> countByTimes(Date start, Date end,@RequestParam(value = "timeUnits")Optional<TimeUnits> timeUnits){
      return  new ResultBean<>(alarmService.countByTimes(start,end,timeUnits.isPresent()?timeUnits.get():null));
    }
    /**
     *
     * @param alarm
src/main/java/com/moral/controller/DeviceController.java
@@ -6,7 +6,9 @@
import com.moral.service.DeviceService;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
@@ -16,6 +18,14 @@
public class DeviceController {
    @Resource
    DeviceService deviceService;
    @GetMapping("count-by-example")
    public ResultBean<Integer> countByExample(PageBean pageBean){
        return  new ResultBean<Integer>(deviceService.countByExample(pageBean));
    }
    @GetMapping("count-by-times")
    public ResultBean<List<Map>> countByTimes(Date start, Date end){
        return  new ResultBean<List<Map>>(deviceService.countByTimes(start,end,"%Y-%m"));
    }
    @GetMapping("page-list")
    public PageBean pageList(PageBean pageBean) {
        return deviceService.queryByPageBean(pageBean);
src/main/java/com/moral/mapper/AlarmMapper.java
@@ -1,13 +1,15 @@
package com.moral.mapper;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.moral.common.mapper.BaseMapper;
import com.moral.entity.Alarm;
import org.apache.ibatis.annotations.Param;
public interface AlarmMapper extends BaseMapper<Alarm> {
    
    List<Map<String, Object>> getAlarmData(Map<String, Object> parameters);
    List<Map> countByTimes(@Param("start")Date start, @Param("end")Date end, @Param("format")String format);
}
src/main/java/com/moral/mapper/DeviceMapper.java
@@ -1,5 +1,6 @@
package com.moral.mapper;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -22,9 +23,7 @@
    List<Device> selectByOrgIdAndMpId(@Param("orgId")Integer orgId,@Param("mpId")Integer mpId);
    List<Device> selectByMap(Map<String, Object> params);
    Device selectWithOrgIdsByMac(String mac);
    List<Integer> getDeviceVersionIdByAreaCode(Map<String, Object> parameters);
    Integer getDeviceCountByRegion(Map<String, Object> parameters);
    List<Map> countByTimes(@Param("start")Date start,@Param("end")Date end,@Param("format")String format);
}
src/main/java/com/moral/service/AlarmService.java
@@ -1,10 +1,16 @@
package com.moral.service;
import com.moral.entity.charts.TimeUnits;
import java.util.Date;
import java.util.List;
import java.util.Map;
@SuppressWarnings("rawtypes")
public interface AlarmService {
    Map getPieData(Map<String, Object> parameters);
    List<Map> countByTimes(Date start, Date end, TimeUnits timeUnits);
    Map getPieData(Map<String, Object> parameters);
}
src/main/java/com/moral/service/DeviceService.java
@@ -1,5 +1,6 @@
package com.moral.service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.moral.common.bean.PageBean;
@@ -22,12 +23,16 @@
    PageResult query(Integer orgId, Integer mpId, Integer pageSize, Integer pageNo);
    PageBean queryByPageBean(PageBean pageBean);
    int countByExample(PageBean pageBean);
    PageBean queryByPageBean(PageBean pageBean);
    void deleteByIds(Integer[] ids);
    void addOrModify(Device device);
    List<Map> countByTimes(Date start, Date end, String format);
    List<Device> getDevicesByMonitorPointId(Integer monitorPointId);
    List<Map<String,String>> queryDevicesState(List<String> macList,Boolean withData);
src/main/java/com/moral/service/impl/AlarmServiceImpl.java
@@ -1,14 +1,11 @@
package com.moral.service.impl;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import javax.annotation.Resource;
import com.moral.entity.charts.TimeUnits;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@@ -25,7 +22,18 @@
    @Resource
    private HistoryMinutelyService historyMinutelyService;
    @Override
    public List<Map> countByTimes(Date start, Date end, TimeUnits timeUnits){
        String format = null;
        if(timeUnits!=null){
            switch (timeUnits){
                case MONTH: format = "%Y-%m";
                case DAY: format = "%Y-%m-%d";
            }
        }
        return alarmMapper.countByTimes(start, end, format);
    }
    @Override
    public Map getPieData(Map<String, Object> parameters) {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -10,12 +10,14 @@
import com.moral.common.bean.Constants;
import com.moral.common.bean.PageBean;
import com.moral.common.bean.PageResult;
import com.moral.common.exception.BusinessException;
import com.moral.common.util.ExampleUtil;
import com.moral.common.util.RedisUtils;
import com.moral.mapper.MonitorPointMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
@@ -30,6 +32,7 @@
@Service
public class DeviceServiceImpl implements DeviceService {
    Logger log = Logger.getLogger(DeviceServiceImpl.class);
    //-----------------------redis key前缀-开始---------------------------------
    private static String AlARM = "alarm";//警报阀值储存key前缀
    private static String ADJUST="adjust";//校准值存储key前缀
@@ -268,8 +271,17 @@
         }
    }
    @Override
    public PageBean queryByPageBean(PageBean pageBean) {
    public int countByExample(PageBean pageBean){
        Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
        addDeletesToExample(example);
        return deviceMapper.selectCountByExample(example);
    }
    /**
     * 增加删除条件过滤
     * @param example
     */
    private void addDeletesToExample(Example example){
        List<Example.Criteria> criteriaList = example.getOredCriteria();
        if(criteriaList!=null&&criteriaList.size()>0){
            for(Example.Criteria cri : criteriaList){
@@ -278,6 +290,11 @@
        }else {
            example.or().andNotEqualTo("isDelete",Constants.IS_DELETE_TRUE);
        }
    }
    @Override
    public PageBean queryByPageBean(PageBean pageBean) {
        Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
        addDeletesToExample(example);
        if(pageBean.getPageSize()>0){
            PageHelper.startPage(pageBean.getPageIndex(),pageBean.getPageSize());
        }
@@ -323,7 +340,14 @@
            throw  ex;
        }
    }
    @Override
    public List<Map> countByTimes(Date start,Date end,String format){
        if(start==null||end==null||StringUtils.isBlank(format)){
            log.error("some  params is null");
            throw new BusinessException("some  params is null");
        }
        return  deviceMapper.countByTimes(start, end, format);
    }
    @Override
    public List<Device> getDevicesByMonitorPointId(Integer monitorPointId) {
        Device device = new Device();
src/main/resources/mapper/AlarmMapper.xml
@@ -75,5 +75,25 @@
        </if>
            
    </select>
    <select id="countByTimes"   resultType="java.util.Map">
        select
        <if test="format !=null">
            DATE_FORMAT(time,#{format}) as time,
        </if>
        COUNT(*) as count from alarm
        <where>
            <if test="start != null">
                time >= #{start}
            </if>
            <if test="end != null">
                and time
                <![CDATA[
                <=
                ]]> #{end}
            </if>
        </where>
        <if test="format !=null">
            GROUP BY DATE_FORMAT(time,#{format}) ;
        </if>
    </select>
</mapper>
src/main/resources/mapper/DeviceMapper.xml
@@ -62,6 +62,18 @@
    id, name, address, longitude, latitude, mac, operate_user_id, state, is_delete, create_time, 
    install_time, monitor_point_id, device_version_id
  </sql>
    <select id="countByTimes"   resultType="java.util.Map">
        select DATE_FORMAT(create_time,#{format}) as time, COUNT(*) as count from device
        where create_time >= #{start}
        and create_time
        <![CDATA[
        <=
        ]]> #{end}
        and is_delete <![CDATA[
        <>
        ]]> 1
        GROUP BY DATE_FORMAT(create_time,#{format}) ;
    </select>
  <select id="selectWithRelationData" parameterType="tk.mybatis.mapper.entity.Example" resultMap="BaseResultMap">
    select
    dev.*,