package com.huatek.busi.service.impl.threshold;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.InfraredThresholdDao;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.dto.InfraredThresholdDto;
import com.huatek.busi.entity.InfraredThreshold;
import com.huatek.busi.mapper.InfraredThresholdMapper;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.threshold.InfraredThresholdService;
import com.huatek.busi.vo.InfraredThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.frame.exception.OwlException;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.sql.beans.WherePrams;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;

/**
 * 红外测温阈值
 * 
 * @author buck_guo
 * @date 2019/08/09
 */
@Component
@Service(interfaceClass = InfraredThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class InfraredThresholdServiceImpl implements InfraredThresholdService {
    @Autowired
    private InfraredThresholdDao infraredThresholdDao;
    @Autowired
    private InfraredThresholdMapper infraredThresholdMapper;

    @Autowired
    private BusiSubTreeService busiSubTreeService;

    /**
     * 查询
     */
    @Override
    public InfraredThresholdVo queryInfraredThreshold(String sensorCode,String workStatus) {
        return infraredThresholdMapper.queryInfraredThreshold(sensorCode,workStatus);
    }

    /**
     * 批量更新或者新增数据
     */
    @Override
    public int insertOrUpdate(String token,InfraredThresholdDto[] infraredThresholdDto) {
        if(infraredThresholdDto == null || infraredThresholdDto.length == 0) {
            return 0;
        }
        LocalUser user = UserUtil.getLocalUser(token); 
        int count = 0;
        for(InfraredThresholdDto dto : infraredThresholdDto) {
            if(StringUtils.isEmpty(dto.getSensorCode()) || StringUtils.isEmpty(dto.getWorkStatus())) {
                throw new OwlException("阈值保存失败，传感器编码和工作状态不能为空");
            }
            InfraredThreshold infraredThreshold = new InfraredThreshold();
            BeanUtils.copyPropertiesWithIgnoreNull(dto, infraredThreshold);
            if(StringUtils.isEmpty(infraredThreshold.getId())) {
                infraredThreshold.setCreatedTime(LocalDateTime.now());
                if (user != null) {
                    infraredThreshold.setCreator(user.getUid());
                }
            }else {
                infraredThreshold.setModifiedTime(LocalDateTime.now());
                if(user != null) {
                    infraredThreshold.setModifierId(user.getUid());
                }
            }
            //
            EquipmentInfoDto equipmentInfoDto = busiSubTreeService.findEquipmentInfo(dto.getSensorCode(), dto.getType());
            if(equipmentInfoDto != null) {
                infraredThreshold.setSiteId(equipmentInfoDto.getSiteId());
                infraredThreshold.setMainTreeId(equipmentInfoDto.getMainTreeId());
            }
            count += infraredThresholdDao.saveOrUpdate(infraredThreshold);
        }
        return count;
    }

    @Override
    public int addSensorInsertInfrared(String token, String sensorCode, String type) {
        int count = 0;
        // 判断当前sensorCode是否存在阈值
        List<InfraredThresholdVo> infraredList = infraredThresholdMapper.queryInfraredThresholdBySensorCode(sensorCode);
        if (infraredList.isEmpty()) {
            EquipmentInfoDto equipmentInfoDto = busiSubTreeService.findEquipmentInfo(sensorCode, type);
            LocalUser user = UserUtil.getLocalUser(token);
            InfraredThreshold infraredThreshold = new InfraredThreshold();
            infraredThreshold.setSensorCode(sensorCode);
            infraredThreshold.setType(type);
            infraredThreshold.setCreator(user.getUid());
            String[] status = {"1", "2", "3"};
            for (int j = 0; j < 3; j++) {
                infraredThreshold.setId(Uuid.getUUID());
                infraredThreshold.setWorkStatus(status[j]);
                infraredThreshold.setCreatedTime(LocalDateTime.now());
                infraredThreshold.setRegionPosition(1);
                if(equipmentInfoDto != null) {
                    infraredThreshold.setSiteId(equipmentInfoDto.getSiteId());
                    infraredThreshold.setMainTreeId(equipmentInfoDto.getMainTreeId());
                }
                count += infraredThresholdDao.add(infraredThreshold);
            }
        }
        return count;
    }

	@Override
	public int deleteSensorInsertInfrared(String sensorCode) {
		return infraredThresholdDao.del(new WherePrams().where("sensor_code",sensorCode));
	}

	@Override
	public int updateSensorInsertInfrared(String oldSensorCode,String newSensorCode) {
        int count =  infraredThresholdMapper.updateSensorInsertInfraredBySensorCode(oldSensorCode, newSensorCode);
        return count;
	}

    @Override
    public List<InfraredThresholdVo> queryInfraredThresholdBySensorCode(String sensorCode) {
        return infraredThresholdMapper.queryInfraredThresholdBySensorCode(sensorCode);
    }
}
