/**
 * 
 */
package com.huatek.busi.service.impl.generator;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dao.BusiGeneratorSensorDao;
import com.huatek.busi.dao.BusiTransformerSensorDao;
import com.huatek.busi.dto.BusiEvaMonitorConfigDto;
import com.huatek.busi.dto.BusiSensorQueryParamDto;
import com.huatek.busi.dto.TransformerIcgeThresholdDto;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.BusiGeneratorSensorMapper;
import com.huatek.busi.mapper.BusiTransformerSensorMapper;
import com.huatek.busi.service.BusiEvaMonitorConfigService;
import com.huatek.busi.service.BusiGeneratorSubTreeService;
import com.huatek.busi.service.BusiTransfromerSubTreeService;
import com.huatek.busi.service.generatro.BusiGeneratorSensorService;
import com.huatek.busi.service.threshold.GeneratorAmplThresholdService;
import com.huatek.busi.service.threshold.InfraredThresholdService;
import com.huatek.busi.service.transformer.BusiTransformerSensorService;
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.LocalUser;
import com.huatek.torch.sql.beans.WherePrams;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;
import org.apache.commons.lang3.ObjectUtils;
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 java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: BusiTransformerSensorServiceImpl
 * @Description: 发电机外挂设备传感器服务
 * @version: 1.0
 */
@Component
@Service(interfaceClass = BusiGeneratorSensorService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiGeneratorSensorServiceImpl extends BaseDao<BusiGeneratorSensor, String>  implements BusiGeneratorSensorService {
	@Autowired
	private BusiGeneratorSensorDao busiGeneratorSensorDao;
	@Autowired
	private BusiGeneratorSensorMapper busiGeneratorSensorMapper;
	@Autowired
	private BusiGeneratorSubTreeService busiGeneratorSubTreeService;
	@Autowired
	private InfraredThresholdService infraredThresholdService;
	@Autowired
	private GeneratorAmplThresholdService generatorAmplThresholdService;
	@Autowired
	private BusiEvaMonitorConfigService busiEvaMonitorConfigService;

	@Override
	public BusiGeneratorSensor findByPK(String pk) {
		return busiGeneratorSensorDao.get(pk);
	}

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

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

	public BusiGeneratorSensor buildData(BusiSensorVo busiSensorVo, BusiGeneratorSensor entity) {
		if (null == entity) {
			entity = new BusiGeneratorSensor();
			String equipmentId = busiSensorVo.getEquipmentId();
			entity.setId(Uuid.getUUID());
			entity.setStatus(SiteConstants.Status.NORMAL.getValue());
			entity.setCreateTime(LocalDateTime.now());
			BusiGeneratorSubTree subTree = busiGeneratorSubTreeService.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());
		entity.setAmplType(busiSensorVo.getAmplType());
		return entity;
	}
	@Override
	public boolean delete(List<String> list) {
		list.forEach(pk -> {
			BusiGeneratorSensor find = this.findByPK(pk);
			if (null != find) {
				find.setStatus(SiteConstants.Status.DELETE.getValue());
				this.busiGeneratorSensorDao.updateLocal(find);
				infraredThresholdService.deleteSensorInsertInfrared(find.getSensorCode());
			}
		});
		return true;
	}

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

	}

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

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

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

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

	@Override
	public boolean update(String token, BusiSensorVo busiSensorVo) {
		BusiGeneratorSensor find = this.findByPK(busiSensorVo.getId());
		if (null != find) {
			if (StringUtils.isNotEmpty(busiSensorVo.getSensorCode())) {
				Integer sensorCodeCount = busiGeneratorSensorMapper.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())) {
					// 已经设置过sensorCode后，并且修改sensorCode后进行更新传感器，需要对阈值表中的sensorCode进行重新关联
					// 振动
					if (find.getEquipmentType().equals(SiteConstants.GeneratorTreeEnum.ZD_EQM.getValue())) {
						generatorAmplThresholdService.updateSensorInsertGeneratorAmpl(oldSensorCode, busiSensorVo.getSensorCode());
					}
					else if (find.getEquipmentType().equals(SiteConstants.GeneratorTreeEnum.YL_EQM.getValue())) {
						generatorAmplThresholdService.updateSensorInsertGeneratorAmpl(oldSensorCode, busiSensorVo.getSensorCode());
					}
				}
				else {
					// 初次设置sensorCode更新传感器，新增阈值基础数据
					// 振动
					if (find.getEquipmentType().equals(SiteConstants.GeneratorTreeEnum.ZD_EQM.getValue())) {
						generatorAmplThresholdService.addSensorInsertGeneratorAmpl(token, busiSensorVo.getSensorCode(), busiSensorVo.getType());
					}
					else if (find.getEquipmentType().equals(SiteConstants.GeneratorTreeEnum.YL_EQM.getValue())) {
						generatorAmplThresholdService.addSensorInsertGeneratorAmpl(token, busiSensorVo.getSensorCode(), busiSensorVo.getType());
					}
				}
				// 维护考核指标
				if (StringUtils.isNotEmpty(busiSensorVo.getSensorCode())) {
					BusiEvaMonitorUnitVo busiEvaMonitorUnitVo = busiEvaMonitorConfigService.findBusiEvaMonitorUnit(find.getEquipmentType(), SiteConstants.ExternalTypeEnum.GEN.getValue());
					if(!org.apache.commons.lang3.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.GEN.getValue());
								monitorConfig.setSensorId(busiSensorVo.getSensorCode());
								LocalUser user = UserUtil.getLocalUser(token);
								if (ObjectUtils.isNotEmpty(user)) {
									monitorConfig.setCreator(user.getUid());
								}
								busiEvaMonitorConfigService.saveOrUpdate("", monitorConfig);
							});
						}
					}
				}
			}
			return true;
		}
		return false;
	}


}