xufenglei
2017-12-07 dba72443e05e7b0a52ee85bfd9f4641aebc42c60
src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -1,35 +1,44 @@
package com.moral.service.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.moral.common.exception.BusinessException;
import com.moral.common.util.ValidateUtil;
import com.moral.entity.Device;
import com.moral.mapper.DeviceMapper;
import com.moral.service.AccountService;
import com.moral.service.DeviceService;
import tk.mybatis.mapper.entity.Example;
@Service
public class DeviceServiceImpl implements DeviceService {
   @Autowired
   @Resource
   private DeviceMapper deviceMapper;
   @Autowired
   @Resource
   private AccountService accountService;
   @Override
   public Map<String, Object> getDeviceStates(Map<String, Object> parameters) {
   public Map<String, Object> getDeviceStatesByAccount(Map<String, Object> parameters) {
      Map<String, Object> result = new HashMap<String, Object>();
      accountService.setOrgIdsByAccount(parameters);
      List<Map<String, Object>> list = deviceMapper.getDeviceStates(parameters);
      List<Map<String, Object>> list = deviceMapper.getDeviceStatesByAccount(parameters);
      Long all = 0L, normal = 0L, abnormal = 0L, stop = 0L;
      for (Map<String, Object> map : list) {
         Long count = (Long) map.get("count");
         all += count;
         switch ((Integer) map.get("state")) {
         switch (Integer.valueOf((String) map.get("state"))) {
         case 0:
            normal = count;
            break;
@@ -46,4 +55,40 @@
      result.put("stop", stop);
      return result;
   }
   @Override
   public List<Map<String, Object>> getSensorsByDevice(String mac) {
      return deviceMapper.getSensorsByDevice(mac);
   }
   @Override
   public void saveOrUpdateDevice(Device device) {
      ValidateUtil.notNull(device, "参数不可为空");
      ValidateUtil.notEmpty(device.getMac(), "参数不可为空");
      Example example = new Example(Device.class);
      example.or().andEqualTo("mac",device.getMac());
      List<Device> devices = deviceMapper.selectByExample(example);
      Date operateDate = new Date();
      device.setInstallTime(operateDate);
      if (ObjectUtils.isEmpty(devices)) {
         device.setCreateTime(operateDate);
         device.setState("4");
         deviceMapper.insertSelective(device);
      }else if (devices.size() > 1) {
         throw new BusinessException("设备信息异常,联系管理员!");
      }else {
         device.setId(devices.get(0).getId());
         deviceMapper.updateByPrimaryKeySelective(device);
      }
   }
   @Override
   public List<Device> getInstallDevicesByOperateUser(Integer uid, Integer pageIndex, Integer pageSize) {
      Example example = new Example(Device.class);
      example.or().andEqualTo("operateUserId",uid);
      PageHelper.startPage(pageIndex, pageSize);
      List<Device> devices = deviceMapper.selectByExample(example);
      return devices;
   }
}