package com.huatek.busi.service.impl;

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

import com.huatek.busi.service.cable.BusiOtherThresholdService;
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 com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.dao.BusiOtherSensorDao;
import com.huatek.busi.dto.BusiEvaMonitorConfigDto;
import com.huatek.busi.dto.BusiSensorQueryParamDto;
import com.huatek.busi.entity.BusiOtherSensor;
import com.huatek.busi.entity.BusiOtherSubTree;
import com.huatek.busi.mapper.BusiOtherSensorMapper;
import com.huatek.busi.service.BusiEvaMonitorConfigService;
import com.huatek.busi.service.BusiOtherSensorService;
import com.huatek.busi.service.BusiOtherSubTreeService;
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.util.uuid.Uuid;

@Component
@Service(interfaceClass = BusiOtherSensorService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOtherSensorServiceImpl implements BusiOtherSensorService {

    @Autowired
    private BusiOtherSensorDao busiOtherSensorDao;

    @Autowired
    private BusiOtherSubTreeService busiOtherSubTreeService;

    @Autowired
    private BusiOtherSensorMapper busiOtherSensorMapper;

    @Autowired
    private BusiOtherThresholdService busiOtherThresholdService;

    @Autowired
    private BusiEvaMonitorConfigService busiEvaMonitorConfigService;

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

    @Override
    public BusiOtherSensor findByPK(String pk) {
        return busiOtherSensorDao.get(pk);
    }

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

    @Override
    public boolean update(String token, BusiSensorVo busiSensorVo) {
        BusiOtherSensor find = this.findByPK(busiSensorVo.getId());
        if (null != find) {
            if (StringUtils.isNotEmpty(busiSensorVo.getSensorCode())) {
                Integer sensorCodeCount = busiOtherSensorMapper.checkSensorCode(busiSensorVo.getSensorCode(), busiSensorVo.getId());
                if (sensorCodeCount > 0) {
                    throw new OwlException("当前sensor编码已经存在");
                }
            }
            String oldSensorCode = find.getSensorCode();
            find = this.buildData(busiSensorVo, find);
            this.busiOtherSensorDao.updateLocal(find);
            if (StringUtils.isNotEmpty(busiSensorVo.getSensorCode())) {
                if (StringUtils.isNotEmpty(oldSensorCode) && !oldSensorCode.equals(busiSensorVo.getSensorCode())) {
                    // 电缆环流
                    busiOtherThresholdService.updateOtherThresholdBySensorCode(oldSensorCode, busiSensorVo.getSensorCode());
                }
                BusiEvaMonitorUnitVo busiEvaMonitorUnitVo = busiEvaMonitorConfigService.findBusiEvaMonitorUnit(find.getEquipmentType(), 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(ExternalTypeEnum.OTHER.getValue());
                            monitorConfig.setSensorId(busiSensorVo.getSensorCode());
                            busiEvaMonitorConfigService.saveOrUpdate("", monitorConfig);
                        });
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(List<String> list) {
        list.forEach(pk -> {
            BusiOtherSensor find = this.findByPK(pk);
            if (null != find) {
                find.setStatus(SiteConstants.Status.DELETE.getValue());
                this.busiOtherSensorDao.updateLocal(find);
                if (StringUtils.isNotEmpty(find.getSensorCode())) {
                    // 电缆环流
                    busiOtherThresholdService.deleteOtherThresholdBySensorCode(find.getSensorCode());
                }
            }
        });
        return true;
    }

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

            BusiOtherSubTree subTree = busiOtherSubTreeService.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 BusiOtherSensor findSensorBySensorCode(String sensorCode) {
        return busiOtherSensorDao.get(new WherePrams().where("sensor_code", sensorCode).and("status", SiteConstants.Status.NORMAL.getValue()));
    }

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

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

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

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

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