package com.willalp.control.service.impl;

import cn.hutool.core.date.BetweenFormater;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.control.domain.HsControlStat;
import com.willalp.control.mapper.HsControlStatMapper;
import com.willalp.control.service.IHsControlStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备使用统计Service业务层处理
 *
 * @author willalp
 * @date 2022-03-02
 */
@Service
public class HsControlStatServiceImpl extends ServiceImpl<HsControlStatMapper, HsControlStat> implements IHsControlStatService {
    @Autowired
    private HsControlStatMapper hsControlStatMapper;

    /**
     * 查询设备使用统计列表
     *
     * @param hsControlStat 设备使用统计
     * @return 设备使用统计
     */
    @Override
    public List<HsControlStat> selectHsControlStatList(HsControlStat hsControlStat) {
        return hsControlStatMapper.selectHsControlStatList(hsControlStat);
    }

    @Override
    public List<HsControlStat> appControlStatList(String useNoId) {
        if (StringUtils.isBlank(useNoId)) {
            throw new BaseException("control.useNoId.null", new Object[]{useNoId});
        }
        List<HsControlStat> statYearMonthDates = this.baseMapper.getStatYearMonthDateByUseNoId(useNoId);
        statYearMonthDates.forEach((s) -> {
            String dosage = s.getDosage();
            s.setDosage(DateUtil.formatBetween(Long.parseLong(dosage), BetweenFormater.Level.SECOND));
        });
        return statYearMonthDates;
    }

    @Override
    public List<HsControlStat> appControlStatList(String useNoId, String yearMonth) {
        if (StringUtils.isBlank(useNoId)) {
            throw new BaseException("control.useNoId.null", new Object[]{useNoId});
        }
        if (StringUtils.isBlank(yearMonth)) {
            throw new BaseException("日期不能为空");
        }
        List<HsControlStat> dayList = this.baseMapper
                .getStatYearMonthDateByUseNoIdAndYearMonth(useNoId, yearMonth);
        dayList.forEach((d) -> {
            String dosage = d.getDosage();
            d.setDosage(DateUtil.formatBetween(Long.parseLong(dosage), BetweenFormater.Level.SECOND));
        });
        return dayList;
    }
}
