package com.zcxk.rmcp.web.service.impl;

import com.zcxk.core.common.pojo.TotalResult;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.core.utils.DateUtil;
import com.zcxk.rmcp.api.vo.MeterReadWaterRecordVo;
import com.zcxk.rmcp.core.dao.MeterReadWaterRecordMapper;
import com.zcxk.rmcp.web.service.MeterReadWaterRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author Andy
 * @version V1.0
 * @description: 抄表记录
 * @date 2021/7/22
 **/
@Service
public class MeterReadWaterRecordServiceImpl implements MeterReadWaterRecordService {

    @Resource
    private MeterReadWaterRecordMapper meterReadWaterRecordMapper;

    @Override
    public TotalResult statisticsRecent15Days() {
        LocalDate localDate = DateUtil.parseLocalDate(new Date());
        int endDate   = Integer.parseInt(localDate.plusDays(-1).toString().replaceAll("-",""));
        int beginDate = Integer.parseInt(localDate.plusDays(-16).toString().replaceAll("-",""));
        return listWaterRecord(beginDate, endDate);
    }

    @Override
    public TotalResult listWaterRecord(int beginDate, int endDate) {
        BigDecimal total = new BigDecimal("0");
        List<MeterReadWaterRecordVo> list = meterReadWaterRecordMapper.listWaterRecord(beginDate, endDate, UserUtil.getCurrentUser().getUserCondition());
        for (MeterReadWaterRecordVo meterReadWaterRecordVo : list) {
            total = total.add(meterReadWaterRecordVo.getWaterConsumption());
        }
        return new TotalResult<>(list, total);
    }
}
