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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.BusiConstant.TemperPointType;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.RptTemperDao;
import com.huatek.busi.dto.RptTemperDto;
import com.huatek.busi.entity.BusiTemperPoint;
import com.huatek.busi.entity.CwgTemperature;
import com.huatek.busi.entity.RptTemper;
import com.huatek.busi.mapper.BusiTemperPointMapper;
import com.huatek.busi.mapper.RptTemperMapper;
import com.huatek.busi.service.temperature.RptTemperService;
import com.huatek.busi.vo.RptTemperVo;

import lombok.extern.slf4j.Slf4j;

/**
 * 测温柜-实时监测数据 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 1, 2019 4:56:50 PM
 */
@Slf4j
@Component
@Service(interfaceClass = RptTemperService.class)
@Transactional(rollbackFor = Exception.class)
public class RptTemperServiceImpl implements RptTemperService {

    @Autowired
    private RptTemperMapper rptTemperMapper;
    @Autowired
    private BusiTemperPointMapper busiTemperPointMapper;

    @Autowired
    private RptTemperDao rptTemperDao;

    @Override
    public PageInfo<RptTemperVo> findRptTemperPage(RptTemperDto rptTemperDto) {
        return PageHelper.startPage(rptTemperDto.getPage(), rptTemperDto.getLimit())
            .doSelectPageInfo(() -> rptTemperMapper.selectRptTemperPage(rptTemperDto));
    }

    @Override
    public synchronized int saveRptTemper(CwgTemperature cacEntity) {
        // 测温点
        BusiTemperPoint busiTemperPoint = busiTemperPointMapper.getBusiTemperPoint(cacEntity.getSiteId(),
                cacEntity.getSensorId());
        if (busiTemperPoint == null) {
            return -1;
        }
        RptTemper rptTemper = rptTemperMapper.getRptTemper(cacEntity.getSiteId(), cacEntity.getEquipmentCode());
        if (rptTemper == null) {
            rptTemper = new RptTemper();
        }
        rptTemper.setCreateTime(LocalDateTime.now());
        rptTemper.setEqmId(cacEntity.getEquipmentCode());
        rptTemper.setSiteId(cacEntity.getSiteId());
        TemperPointType temperPointType = TemperPointType.from(busiTemperPoint.getPosition());
        if(temperPointType == null) {
            log.debug("安装位置 为空");
            return rptTemperDao.saveOrUpdate(rptTemper);
        }
        switch(temperPointType) {
            case A_I:
                rptTemper.setAITemper(transData(cacEntity.getTmp()));
                rptTemper.setAISensorId(cacEntity.getSensorId());
                break;
            case A_O:
                rptTemper.setAOTemper(transData(cacEntity.getTmp()));
                rptTemper.setAOSensorId(cacEntity.getSensorId());
                break;
            case B_I:
                rptTemper.setBITemper(transData(cacEntity.getTmp()));
                rptTemper.setBISensorId(cacEntity.getSensorId());
                break;
            case B_O:
                rptTemper.setBOTemper(transData(cacEntity.getTmp()));
                rptTemper.setBOSensorId(cacEntity.getSensorId());
                break;
            case C_I:
                rptTemper.setCITemper(transData(cacEntity.getTmp()));
                rptTemper.setCISensorId(cacEntity.getSensorId());
                break;
            case C_O:
                rptTemper.setCOTemper(transData(cacEntity.getTmp()));
                rptTemper.setCOSensorId(cacEntity.getSensorId());
                break;
            case IN_SIDE:
                rptTemper.setBoxTemper(transData(cacEntity.getTmp()));
                rptTemper.setBoxSensorId(cacEntity.getSensorId());
                break;
            case OUT_SIDE:
                rptTemper.setBoxOutsideTemper(transData(cacEntity.getTmp()));
                rptTemper.setBoxOutsideSensorId(cacEntity.getSensorId());
                break;
            default:
                break;
        }
        return rptTemperDao.saveOrUpdate(rptTemper);
    }

    private static BigDecimal transData(String val) {
        return DecimalUtil.getVal(val).setScale(2, RoundingMode.HALF_DOWN);
    }
}