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.GeneratorAmplThresholdDao;
import com.huatek.busi.dto.CwzzThresholdDto;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.dto.GeneratorAmplThresholdDto;
import com.huatek.busi.dto.TransformerIcgeThresholdDto;
import com.huatek.busi.entity.CwzzThreshold;
import com.huatek.busi.entity.GeneratorAmplThreshold;
import com.huatek.busi.entity.InfraredThreshold;
import com.huatek.busi.mapper.CwzzThresholdMapper;
import com.huatek.busi.mapper.GeneratorAmplThresholdMapper;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.threshold.CwzzThresholdService;
import com.huatek.busi.service.threshold.GeneratorAmplThresholdService;
import com.huatek.busi.vo.CwzzThresholdVo;
import com.huatek.busi.vo.GeneratorAmplThresholdVo;
import com.huatek.busi.vo.InfraredThresholdVo;
import com.huatek.busi.vo.TransformerIcgeThresholdVo;
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;
import java.util.Map;

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

//    @Autowired
//    private CwzzThresholdDao cwzzThresholdDao;

    @Autowired
    private GeneratorAmplThresholdDao generatorAmplThresholdDao;

//    @Autowired
//    private CwzzThresholdMapper cwzzThresholdMapper;

    @Autowired
    private GeneratorAmplThresholdMapper generatorAmplThresholdMapper;

    @Autowired
    private BusiSubTreeService busiSubTreeService;

    @Override
    public GeneratorAmplThresholdVo queryGeneratorAmplThreshold(String sensorCode, String workStatus) {
//        return cwzzThresholdMapper.queryCwzzThreshold(sensorCode,workStatus);
        return generatorAmplThresholdMapper.queryGeneratorAmplThreshold(sensorCode, workStatus);
    }

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

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

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

    @Override
    public int updateSensorInsertGeneratorAmpl(String oldSensorCode,String newSensorCode) {
        int count =  generatorAmplThresholdMapper.updateSensorInsertGeneratorAmplBySensorCode(oldSensorCode, newSensorCode);
        return count;
    }

    @Override
    public Map<String, Object> findType(String sensorCode) {
        return generatorAmplThresholdMapper.findType(sensorCode);
    }

    @Override
    public List<GeneratorAmplThresholdVo> queryGeneratorAmplThresholdBySensorCode(String sensorCode) {
        return generatorAmplThresholdMapper.queryGeneratorAmplThresholdBySensorCode(sensorCode);
    }
}
