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.WindingThresholdDao;
import com.huatek.busi.dto.CwzzThresholdDto;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.dto.WindingThresholdDto;
import com.huatek.busi.entity.CwzzThreshold;
import com.huatek.busi.entity.WindingThreshold;
import com.huatek.busi.mapper.CwzzThresholdMapper;
import com.huatek.busi.mapper.WindingThresholdMapper;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.threshold.CwzzThresholdService;
import com.huatek.busi.service.threshold.WindingThresholdService;
import com.huatek.busi.vo.CwzzThresholdVo;
import com.huatek.busi.vo.WindingThresholdVo;
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 = CwzzThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class CwzzThresholdServiceImpl implements CwzzThresholdService {

    @Autowired
    private CwzzThresholdDao cwzzThresholdDao;

    @Autowired
    private CwzzThresholdMapper cwzzThresholdMapper;

    @Autowired
    private BusiSubTreeService busiSubTreeService;

    @Override
    public CwzzThresholdVo queryCwzzThreshold(String sensorCode, String workStatus) {
        return cwzzThresholdMapper.queryCwzzThreshold(sensorCode,workStatus);
    }

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

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

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

    @Override
    public int updateSensorInsertCwzz(String oldSensorCode,String newSensorCode) {
        int count =  cwzzThresholdMapper.updateSensorInsertCwzzBySensorCode(oldSensorCode, newSensorCode);
        return count;
    }

    @Override
    public List<CwzzThresholdVo> queryCwzzThresholdBySensorCode(String sensorCode) {
        return cwzzThresholdMapper.queryCwzzThresholdBySensorCode(sensorCode);
    }
}
