package com.ho.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ho.business.entity.ColCount;
import com.ho.business.mapper.ColCountMapper;
import com.ho.business.service.ColCountService;
import com.ho.business.util.CountOffsetUtils;
import com.ho.business.vo.req.colCount.ColCountReq;
import com.ho.business.vo.resp.colCount.ColCountResp;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import com.ho.datacollect.api.constant.DataCollectConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ColCountServiceImpl implements ColCountService {

    @Autowired
    ColCountMapper colCountMapper;

    @Override
    public PageResult<ColCountResp> getPageList(ColCountReq vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<ColCountResp> list = colCountMapper.selectByParam(vo);
        return PageUtils.getPageResult(new PageInfo<>(list));
    }

    @Override
    public List<ColCountResp> selectByParam(ColCountReq vo) {
        List<ColCountResp> list = colCountMapper.selectByParam(vo);
        return list;
    }

    @Override
    public int insertColCount(ColCount vo) {
        vo.setCreateTime(new Date());
        int count = colCountMapper.insertColCount(vo);
        return count;
    }

    @Override
    public int insertList(List<ColCount> list) {
        int count = colCountMapper.insertList(list);
        return count;
    }

    @Override
    public int updateById(ColCount vo) {
        vo.setUpdateTime(new Date());
        int count = colCountMapper.updateById(vo);
        return count;
    }

    @Override
    public int deleteByList(List<Integer> ids) {
        int count = colCountMapper.deleteByList(ids);
        return count;
    }

    @Override
    public int deleteStationId(Integer stationId) {
        int count = colCountMapper.deleteStationId(stationId);
        return count;
    }

    /**
     * 计算偏差值
     * @param ele
     * @param list
     * @return
     */
    @Override
    public BigDecimal countOffset(BigDecimal ele, List<ColCountResp> list) {
        if(null != list && list.size()>0){
            ColCountResp colCountResp = list.get(0);
            if(colCountResp.getCountFlag()- CommonConstant.ONE>=0){
                //只需要计算一次的，用过之后将计算标志位设置为0，即不要计算
                if(CommonConstant.ONE.equals(colCountResp.getCountFlag())){
                    colCountResp.setUpdateTime(new Date());
                    colCountResp.setCountFlag(CommonConstant.ZERO);
                    updateById(colCountResp);
                }
                ele = CountOffsetUtils.countOffset(ele, list.get(0));
            }
        }
        return ele;
    }

    @Override
    public String getDeviceType(Integer stationId) {
        String deviceType = null;
        ColCountReq colCountReq = new ColCountReq();
        colCountReq.setStationId(stationId);
        colCountReq.setCol(DataCollectConstant.Elec_Meter.TOTAL_CHARGE);
        //总充
        List<ColCountResp> chargeList = selectByParam(colCountReq);
        if (null != chargeList && chargeList.size() > 0) {
            deviceType = chargeList.get(0).getDeviceType();
        }
        return deviceType;
    }
}
