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

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.util.DecimalUtil;
import com.huatek.busi.dao.RptTermperRecordDao;
import com.huatek.busi.dto.RptTermperRecordDto;
import com.huatek.busi.entity.BusiTemperPoint;
import com.huatek.busi.entity.CwgTemperature;
import com.huatek.busi.entity.RptTermperRecord;
import com.huatek.busi.entity.TemperIncreaseRate;
import com.huatek.busi.mapper.BusiTemperPointMapper;
import com.huatek.busi.mapper.RptTermperRecordMapper;
import com.huatek.busi.mapper.TemperIncreaseRateMapper;
import com.huatek.busi.service.temperature.RptTermperRecordService;
import com.huatek.busi.vo.RptTermperRecordVo;

import lombok.extern.slf4j.Slf4j;

/**
 * 测温柜-全部测温点汇总表 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 1, 2019 4:05:05 PM
 */
@Slf4j
@Component
@Service(interfaceClass = RptTermperRecordService.class)
@Transactional(rollbackFor = Exception.class)
public class RptTermperRecordServiceImpl implements RptTermperRecordService {

    @Autowired
    private RptTermperRecordMapper rptTermperRecordMapper;
    @Autowired
    private BusiTemperPointMapper busiTemperPointMapper;

    @Autowired
    private RptTermperRecordDao rptTermperRecordDao;
    @Autowired
    private TemperIncreaseRateMapper temperIncreaseRateMapper;
    @Override
    public PageInfo<RptTermperRecordVo> findRptTermperRecordPage(RptTermperRecordDto rptTermperRecordDto) {
        PageInfo<RptTermperRecordVo> pageList =
            PageHelper.startPage(rptTermperRecordDto.getPage(), rptTermperRecordDto.getLimit())
                .doSelectPageInfo(() -> rptTermperRecordMapper.selectRptTermperRecordPage(rptTermperRecordDto));

        pageList.getList().stream().forEach(rptTermperRecordVo -> {
            // 设置检测值
            TemperIncreaseRate temperIncreaseRate =  temperIncreaseRateMapper.getNearlyItem(rptTermperRecordVo.getSensorId(), rptTermperRecordVo.getDTime().toLocalDate().minusDays(1));
            if(temperIncreaseRate != null)
            {
                rptTermperRecordVo.setDailyGrowthRate(temperIncreaseRate.getDailyGrowthRate());
                rptTermperRecordVo.setMonthlyGrowthRate(temperIncreaseRate.getMonthlyGrowthRate());
            }
        });
        return pageList;
    }

    @Override
    public int saveRptTermperRecord(CwgTemperature cacEntity) {
        // 测温点
        BusiTemperPoint busiTemperPoint =
            busiTemperPointMapper.getBusiTemperPoint(cacEntity.getSiteId(), cacEntity.getSensorId());
        if (busiTemperPoint == null) {
            log.warn("测温点不存在，siteId: {}, sensorCode：{}", cacEntity.getSiteId(), cacEntity.getSensorId());
            return 0;
        }
        RptTermperRecord rptTermperRecord = rptTermperRecordMapper.getRptTermperRecord(cacEntity);
        if (rptTermperRecord == null) {
            rptTermperRecord = new RptTermperRecord();
            rptTermperRecord.setCreateTime(LocalDateTime.now());
        }
        rptTermperRecord.setMainTreeId(cacEntity.getMainTreeId());
        rptTermperRecord.setSensorId(cacEntity.getSensorId());
        rptTermperRecord.setSiteId(cacEntity.getSiteId());
        rptTermperRecord.setEqmId(cacEntity.getEquipmentCode());
        rptTermperRecord.setRunState(cacEntity.getRunState());
        rptTermperRecord.setPhase(cacEntity.getPhase());
        rptTermperRecord.setDTime(cacEntity.getTimeStamp());
        rptTermperRecord.setTemper(DecimalUtil.getVal(cacEntity.getTmp()));
        rptTermperRecord.setPosition(
            rptTermperRecord.getTemper().subtract(DecimalUtil.getVal(busiTemperPoint.getWarningValue())).toString());
        rptTermperRecord.setModifiedTime(LocalDateTime.now());
        return rptTermperRecordDao.saveOrUpdate(rptTermperRecord);
    }
}