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

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

import com.huatek.busi.entity.InfraredThreshold;
import com.huatek.busi.vo.InfraredThresholdVo;
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 com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.dao.ArresterInsulationMonitorThresholdDao;
import com.huatek.busi.dto.ArresterInsulationMonitorThresholdDto;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.ArresterInsulationMonitorThreshold;
import com.huatek.busi.mapper.ArresterInsulationMonitorThresholdMapper;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.threshold.ArresterInsulationMonitorThresholdService;
import com.huatek.busi.vo.ArresterInsulationMonitorThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;

/**
 * 避雷器-绝缘监测-阈值表：023001 ServiceImpl
 *
 * @author anker_cheng
 * @date Aug 5, 2019 5:29:30 PM
 */
@Component
@Service(interfaceClass = ArresterInsulationMonitorThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class ArresterInsulationMonitorThresholdServiceImpl implements ArresterInsulationMonitorThresholdService {

    @Autowired
    private ArresterInsulationMonitorThresholdMapper arresterInsulationMonitorThresholdMapper;
    @Autowired
    private ArresterInsulationMonitorThresholdDao arresterInsulationMonitorThresholdDao;

    @Autowired
    private BusiSubTreeService busiSubTreeService;

    @Override
    public ArresterInsulationMonitorThreshold getArresterInsulationMonitorThreshold(String mainTreeId, String state,
        String sensorCode) {
        return arresterInsulationMonitorThresholdMapper.getArresterInsulationMonitorThreshold(mainTreeId, state,
            sensorCode);
    }

    @Override
    public ArresterInsulationMonitorThresholdVo getThresholdVo(String state, String sensorCode) {
        return arresterInsulationMonitorThresholdMapper.getThresholdVo(state, sensorCode);
    }

    @Override
    public int addSensorInsertInsulation(String token, String sensorCode, String type) {
        int count = 0;
        // 判断当前sensorCode是否存在阈值
        List<ArresterInsulationMonitorThresholdVo> infraredList = arresterInsulationMonitorThresholdMapper.queryInsulationThresholdBySensorCode(sensorCode);
        if (infraredList.isEmpty()) {
            EquipmentInfoDto equipmentInfoDto = busiSubTreeService.findEquipmentInfo(sensorCode, type);
            LocalUser user = UserUtil.getLocalUser(token);
            ArresterInsulationMonitorThreshold arresterInsulationMonitorThreshold = new ArresterInsulationMonitorThreshold();
            arresterInsulationMonitorThreshold.setSensorCode(sensorCode);
            arresterInsulationMonitorThreshold.setCreator(user.getUid());
            String[] status = {"1", "2", "3"};
            for (int j = 0; j < 3; j++) {
                arresterInsulationMonitorThreshold.setId(Uuid.getUUID());
                arresterInsulationMonitorThreshold.setRunState(status[j]);
                arresterInsulationMonitorThreshold.setCreatedTime(LocalDateTime.now());
                if(equipmentInfoDto != null) {
                    arresterInsulationMonitorThreshold.setSiteId(equipmentInfoDto.getSiteId());
                    arresterInsulationMonitorThreshold.setMainTreeId(equipmentInfoDto.getMainTreeId());
                }
                count += arresterInsulationMonitorThresholdDao.add(arresterInsulationMonitorThreshold);
            }
        }
        return count;
    }

    @Override
    public int saveOrUpdate(String token, ArresterInsulationMonitorThresholdDto arresterInsulationMonitorThresholdDto) {
        ArresterInsulationMonitorThreshold threshold = new ArresterInsulationMonitorThreshold();
        BeanUtils.copyProperties(arresterInsulationMonitorThresholdDto, threshold, "createdTime", "creator",
            "modifiedTime", "modifier");
        LocalUser user = UserUtil.getLocalUser(token);
        if (StringUtils.isEmpty(threshold.getId())) {
            threshold.setCreatedTime(LocalDateTime.now());
            if (user != null) {
                threshold.setCreator(user.getUid());
            }
        }
        threshold.setModifiedTime(LocalDateTime.now());
        if (user != null) {
            threshold.setModifier(user.getUid());
        }
        EquipmentInfoDto equipmentInfoDto = busiSubTreeService
            .findEquipmentInfo(arresterInsulationMonitorThresholdDto.getSensorCode(), SiteTreeEnum.BLQ.getValue());
        if(equipmentInfoDto != null) {
            threshold.setSiteId(equipmentInfoDto.getSiteId());
            threshold.setMainTreeId(equipmentInfoDto.getMainTreeId());
        }
        return arresterInsulationMonitorThresholdDao.saveOrUpdate(threshold);
    }

    @Override
    public List<ArresterInsulationMonitorThresholdVo> getThresholdVoBysensorCode(String sensorCode) {
        return arresterInsulationMonitorThresholdMapper.getThresholdVoBysensorCode(sensorCode);
    }
}