package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dto.BusiEvaMonitorConfigDto;
import com.huatek.busi.dto.BusiSensorQueryParamDto;
import com.huatek.busi.entity.BusiCableSensor;
import com.huatek.busi.entity.BusiCableSubTree;
import com.huatek.busi.entity.BusiOtherSensor;
import com.huatek.busi.entity.BusiOtherSubTree;
import com.huatek.busi.mapper.BusiCableSensorMapper;
import com.huatek.busi.service.BusiEvaMonitorConfigService;
import com.huatek.busi.service.IBusiCableSensorService;
import com.huatek.busi.service.IBusiCableSubTreeService;
import com.huatek.busi.service.IBusiCableThresholdService;
import com.huatek.busi.vo.BusiEvaMonitorRefVo;
import com.huatek.busi.vo.BusiEvaMonitorUnitVo;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.torch.frame.exception.OwlException;
import com.huatek.torch.sql.beans.WherePrams;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.uuid.Uuid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Component
@Service(interfaceClass = IBusiCableSensorService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiCableSensorServiceImpl extends BaseDao<BusiCableSensor, String> implements IBusiCableSensorService {

    @Autowired
    private IBusiCableSubTreeService busiCableSubTreeService;

    @Autowired
    private BusiEvaMonitorConfigService busiEvaMonitorConfigService;

    @Reference(check = false)
    IBusiCableThresholdService busiCableThresholdService;

    @Resource
    private BusiCableSensorMapper busiCableSensorMapper;

    @Override
    public boolean delete(List<String> list) {
        list.forEach(pk -> {
            BusiCableSensor find = this.get(pk);
            if (null != find) {
                find.setStatus(SiteConstants.Status.DELETE.getValue());
                this.updateLocal(find);
                if (StringUtils.isNotEmpty(find.getSensorCode())) {
                    busiCableThresholdService.deleteCableThresholdBySensorCode(find.getSensorCode());
                }
            }
        });
        return true;
    }

    @Override
    public BusiCableSensor findSensorBySensorCode(String sensorCode){
        return this.get(new WherePrams().where("sensor_code", sensorCode).and("status", SiteConstants.Status.NORMAL.getValue()));
    }

    @Override
    public List<BusiSensorVo> findSensorList(BusiSensorQueryParamDto param) {
        return busiCableSensorMapper.findSensorList(param);
    }

    @Override
    public Page<BusiSensorVo> page(String equipmentId) {
        return busiCableSensorMapper.page(equipmentId);
    }

    @Override
    public boolean add(BusiSensorVo busiSensorVo) {
        BusiCableSensor entity = null;
        entity = this.buildData(busiSensorVo, entity);
        this.add(entity);
        return true;
    }

    @Override
    public BusiCableSensor findByPK(String pk) {
        return this.get(pk);
    }

    @Override
    public boolean update(String token, BusiSensorVo busiSensorVo) {
        BusiCableSensor find = this.findByPK(busiSensorVo.getId());
        if (null != find) {
            if (StringUtils.isNotEmpty(busiSensorVo.getSensorCode())) {
                Integer sensorCodeCount = busiCableSensorMapper.checkSensorCode(busiSensorVo.getSensorCode(), busiSensorVo.getId());
                if (sensorCodeCount > 0) {
                    throw new OwlException("当前sensor编码已经存在");
                }
            }
            String oldSensorCode = find.getSensorCode();
            find = this.buildData(busiSensorVo, find);
            this.updateLocal(find);
            if (StringUtils.isNotEmpty(busiSensorVo.getSensorCode())) {
                if (StringUtils.isNotEmpty(oldSensorCode) && !oldSensorCode.equals(busiSensorVo.getSensorCode())) {
                    // 电缆环流
                    busiCableThresholdService.updateCableThresholdBySensorCode(oldSensorCode, busiSensorVo.getSensorCode());
                }
                BusiEvaMonitorUnitVo busiEvaMonitorUnitVo = busiEvaMonitorConfigService.findBusiEvaMonitorUnit(find.getEquipmentType(), SiteConstants.ExternalTypeEnum.OTHER.getValue());
                if (!ObjectUtils.isEmpty(busiEvaMonitorUnitVo)) {
                    List<BusiEvaMonitorRefVo> busiEvaMonitorRefVoList = busiEvaMonitorConfigService.findBusiEvaMonitorRefList(busiEvaMonitorUnitVo.getId(), "");
                    if (!busiEvaMonitorRefVoList.isEmpty()) {
                        busiEvaMonitorRefVoList.stream().forEach(RedefinitionListenable -> {
                            BusiEvaMonitorConfigDto monitorConfig = new BusiEvaMonitorConfigDto();
                            monitorConfig.setMonitorTypeId(busiEvaMonitorUnitVo.getEqTypeId());
                            monitorConfig.setMonitorUnitId(busiEvaMonitorUnitVo.getId());
                            monitorConfig.setIndicatorsId(RedefinitionListenable.getIndicatorsId());
                            monitorConfig.setType(SiteConstants.ExternalTypeEnum.DL.getValue());
                            monitorConfig.setSensorId(busiSensorVo.getSensorCode());
                            busiEvaMonitorConfigService.saveOrUpdate("", monitorConfig);
                        });
                    }
                }
            }
            return true;
        }
        return false;
    }

    public BusiCableSensor buildData(BusiSensorVo busiSensorVo, BusiCableSensor entity) {
        if (null == entity) {
            entity = new BusiCableSensor();
            String equipmentId = busiSensorVo.getEquipmentId();
            entity.setId(Uuid.getUUID());
            entity.setStatus(SiteConstants.Status.NORMAL.getValue());
            entity.setCreateTime(LocalDateTime.now());

            BusiCableSubTree subTree = busiCableSubTreeService.findByPK(equipmentId);
            if (null != subTree) {
                entity.setSiteId(subTree.getSiteId());
                entity.setEquipmentId(equipmentId);
                entity.setEquipmentType(subTree.getEquipmentType());
            }
        }
        entity.setSensorBrand(busiSensorVo.getSensorBrand());
        entity.setSensorCode(busiSensorVo.getSensorCode());
        entity.setSensorName(busiSensorVo.getSensorName());
        entity.setPosition(busiSensorVo.getPosition());
        entity.setPositionDes(busiSensorVo.getPositionDes());
        entity.setManufactor(busiSensorVo.getManufactor());
        entity.setUseDate(busiSensorVo.getUseDate());
        entity.setProductionDate(busiSensorVo.getProductionDate());
        entity.setPdValue(busiSensorVo.getPdValue());
        return entity;
    }

    @Override
    public BusiSensorVo getBusiSensorVo(String sensorCode) {
        return busiCableSensorMapper.getBusiSensorVo(sensorCode);
    }

    @Override
    public BusiSensorVo getBusiSensorVoById(String id) {
        return busiCableSensorMapper.getBusiSensorVoById(id);
    }

    @Override
    public int findSensorCount(String equipmentType) {
        return busiCableSensorMapper.findSensorCount(equipmentType);
    }

}
