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

import java.time.LocalDateTime;

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.huatek.busi.common.SiteConstants;
import com.huatek.busi.dao.PhotoThresholdDao;
import com.huatek.busi.dto.PhotoThresholdDto;
import com.huatek.busi.entity.BusiArresterSensor;
import com.huatek.busi.entity.BusiExternalEquipment;
import com.huatek.busi.entity.BusiGisSensor;
import com.huatek.busi.entity.BusiTransformerSensor;
import com.huatek.busi.entity.PhotoThreshold;
import com.huatek.busi.mapper.PhotoThresholdMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.arrester.BusiArresterSensorService;
import com.huatek.busi.service.gis.BusiGisSensorService;
import com.huatek.busi.service.threshold.PhotoThresholdService;
import com.huatek.busi.service.transformer.BusiTransformerSensorService;
import com.huatek.busi.vo.PhotoThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;

/**
 * 拍照阈值
 * 
 * @author buck_guo
 * @date 2019/08/09
 */
@Component
@Service(interfaceClass = PhotoThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class PhotoThresholdServiceImpl implements PhotoThresholdService {

    @Autowired
    private PhotoThresholdDao photoThresholdDao;

    @Autowired
    private PhotoThresholdMapper photoThresholdMapper;
    @Autowired
    private BusiArresterSensorService busiArresterSensorService;
    @Autowired
    private BusiGisSensorService busiGisSensorService;
    @Autowired
    private BusiTransformerSensorService busiTransformerSensorService;
    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Override
    public PhotoThresholdVo queryPhotoThreshold(String sensorCode, String workStatus, String type) {

        String equipmentId="";
        /**
                        * 根据sensorCode和type分别从busi_arrester_sensor、busi_gis_sensor、busi_transformer_sensor中获取获取外挂设备ID，
                        * 根据外挂设备ID获取外挂设备信息
         */
        if(SiteConstants.SiteTreeEnum.BYQ.getValue().equals(type))
        {
            BusiTransformerSensor busiTransformerSensor = busiTransformerSensorService.findSensorBySensorCode(sensorCode);
            if(busiTransformerSensor!=null)
            {
                equipmentId= busiTransformerSensor.getEquipmentId();
            }
        }
        else if(SiteConstants.SiteTreeEnum.BLQ.getValue().equals(type))
        {
            BusiArresterSensor busiArresterSensor = busiArresterSensorService.findSensorBySensorCode(sensorCode);
            if(busiArresterSensor!=null)
            {
                equipmentId= busiArresterSensor.getEquipmentId();
            }
        }
        else if(SiteConstants.SiteTreeEnum.GIS.getValue().equals(type))
        {
            BusiGisSensor busiGisSensor = busiGisSensorService.findSensorBySensorCode(sensorCode);
            if(busiGisSensor!=null)
            {
                equipmentId= busiGisSensor.getEquipmentId();
            }
        }
        
        /**
                         * 根据设备ID查询外挂设备信息
         */
        BusiExternalEquipment busiExternalEquipment = busiExternalEquipmentService.getByEquipmentId(equipmentId);
        
       
        PhotoThresholdVo photoThresholdVo = photoThresholdMapper.queryPhotoThreshold(sensorCode, workStatus);
        if(busiExternalEquipment != null)
        {
            if(photoThresholdVo != null)
            {
                photoThresholdVo.setEquipmentName(busiExternalEquipment.getEquipmentName());
            }
        }
        return photoThresholdVo;
    }

    @Override
    public int insertOrUpdate(String token, PhotoThresholdDto photoThresholdDto) {
        int count = 0;
        PhotoThreshold photoThreshold = new PhotoThreshold();
        LocalUser user = UserUtil.getLocalUser(token);
        BeanUtils.copyPropertiesWithIgnoreNull(photoThresholdDto, photoThreshold);
        if (ObjectUtils.isEmpty(photoThreshold.getId())) {
            photoThreshold.setId(Uuid.getUUID());
            photoThreshold.setCreator(user.getUid());
            photoThreshold.setCreatedTime(LocalDateTime.now());
            count = photoThresholdDao.add(photoThreshold);
        } else {
            photoThreshold.setModifierId(user.getUid());
            photoThreshold.setModifiedTime(LocalDateTime.now());
            count = photoThresholdDao.updateLocal(photoThreshold);
        }
        return count;
    }

}
