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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.CwzzThresholdDao;
import com.huatek.busi.dao.Sf6EnvThresholdDao;
import com.huatek.busi.dto.CwzzThresholdDto;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.dto.Sf6EnvThresholdDto;
import com.huatek.busi.entity.CwzzThreshold;
import com.huatek.busi.entity.GisSf6EnvThreshold;
import com.huatek.busi.mapper.CwzzThresholdMapper;
import com.huatek.busi.mapper.Sf6EnvThresholdMapper;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.threshold.CwzzThresholdService;
import com.huatek.busi.service.threshold.Sf6EnvThresholdService;
import com.huatek.busi.vo.CwzzThresholdVo;
import com.huatek.busi.vo.Sf6EnvThresholdVo;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

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

/**
 * 测温阈值
 */
@Component
@Service(interfaceClass = Sf6EnvThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class Sf6EnvThresholdServiceImpl implements Sf6EnvThresholdService {

    @Autowired
    private Sf6EnvThresholdDao sf6EnvThresholdDao;

    @Autowired
    private Sf6EnvThresholdMapper sf6EnvThresholdMapper;

    @Autowired
    private BusiSubTreeService busiSubTreeService;

    @Override
    public Sf6EnvThresholdVo querySf6EnvThreshold(String sensorCode, String workStatus) {
        return sf6EnvThresholdMapper.querySf6EnvThreshold(sensorCode,workStatus);
    }

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

    @Override
    public int addSensorInsertSf6Env(String token, String sensorCode, String type) {
        int count = 0;
        // 判断当前sensorCode是否存在阈值
        List<Sf6EnvThresholdVo> cwzzList = sf6EnvThresholdMapper.querSf6EnvThresholdBySensorCode(sensorCode);
        if (cwzzList.isEmpty()) {
            EquipmentInfoDto equipmentInfoDto = busiSubTreeService.findEquipmentInfo(sensorCode, type);
            LocalUser user = UserUtil.getLocalUser(token);
            GisSf6EnvThreshold gisSf6EnvThreshold = new GisSf6EnvThreshold();
            gisSf6EnvThreshold.setSensorCode(sensorCode);
            gisSf6EnvThreshold.setType(type);
            gisSf6EnvThreshold.setCreator(user.getUid());
            String[] status = {"1", "2", "3"};
            for (int j = 0; j < 3; j++) {
                gisSf6EnvThreshold.setId(Uuid.getUUID());
                gisSf6EnvThreshold.setWorkStatus(status[j]);
                gisSf6EnvThreshold.setCreateTime(LocalDateTime.now());
                if(equipmentInfoDto != null) {
                    gisSf6EnvThreshold.setSiteId(equipmentInfoDto.getSiteId());
                    gisSf6EnvThreshold.setMainTreeId(equipmentInfoDto.getMainTreeId());
                }
                count += sf6EnvThresholdDao.add(gisSf6EnvThreshold);
            }
        }
        return count;
    }

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

    @Override
    public int updateSensorInsertSf6Env(String oldSensorCode,String newSensorCode) {
        int count =  sf6EnvThresholdMapper.updateSensorInsertSf6EnvBySensorCode(oldSensorCode, newSensorCode);
        return count;
    }

    @Override
    public List<Sf6EnvThresholdVo> querySf6EnvThresholdBySensorCode(String sensorCode) {
        return sf6EnvThresholdMapper.querSf6EnvThresholdBySensorCode(sensorCode);
    }
}
