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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.InfraredThresholdDao;
import com.huatek.busi.dao.WindingThresholdDao;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.dto.InfraredThresholdDto;
import com.huatek.busi.dto.WindingThresholdDto;
import com.huatek.busi.entity.InfraredThreshold;
import com.huatek.busi.entity.WindingThreshold;
import com.huatek.busi.mapper.InfraredThresholdMapper;
import com.huatek.busi.mapper.WindingThresholdMapper;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.threshold.InfraredThresholdService;
import com.huatek.busi.service.threshold.WindingThresholdService;
import com.huatek.busi.vo.InfraredThresholdVo;
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;

/**
 * 绕组测温阈值
 *
 * @author buck_guo
 * @date 2019/08/09
 */
@Component
@Service(interfaceClass = WindingThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class WindingThresholdServiceImpl implements WindingThresholdService {
    @Autowired
    private WindingThresholdDao windingThresholdDao;
    @Autowired
    private WindingThresholdMapper windingThresholdMapper;

    @Autowired
    private BusiSubTreeService busiSubTreeService;

    /**
     * 查询
     */
    @Override
    public WindingThresholdVo queryWindingThreshold(String sensorCode, String workStatus) {
        return windingThresholdMapper.queryWindingThreshold(sensorCode,workStatus);
    }

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

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

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

	@Override
	public int updateSensorInsertWinding(String oldSensorCode,String newSensorCode) {
        int count =  windingThresholdMapper.updateSensorInsertWindingBySensorCode(oldSensorCode, newSensorCode);
        return count;
	}

    @Override
    public List<WindingThresholdVo> queryWindingThresholdBySensorCode(String sensorCode) {
        return windingThresholdMapper.queryWindingThresholdBySensorCode(sensorCode);
    }
}
