package com.jkcredit.invoice.hub.service.consumptionStatistics;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.mapper.ConsumptionStatisticsMapper;
import com.jkcredit.invoice.hub.model.dto.record.ConsumptionStatisticsDto;
import com.jkcredit.invoice.hub.model.po.record.ConsumptionStatisticsPo;
import com.jkcredit.invoice.hub.model.vo.record.ConsumptionStatisticsVo;
import com.jkcredit.invoice.hub.service.base.BaseService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/20 21:28
 * @version: V1.0
 **/
@Slf4j
@Service
public class ConsumptionStatisticsServiceImpl extends BaseService implements ConsumptionStatisticsService {
    @Resource
    ConsumptionStatisticsMapper mapper;

    @Override
    public IPage<ConsumptionStatisticsVo> getConsumptionStatistics(Page page, ConsumptionStatisticsDto dto) {
        try {
            IPage<ConsumptionStatisticsPo> poIPage = mapper.getConsumptionStatistics(page, dto);
            return poIPage.convert(this::convert);
        } catch (Exception e) {
            log.error("获取消费统计列表失败，失败原因：{}", e.getMessage());
            throw new ServiceException("获取消费统计列表失败");
        }
    }

    private ConsumptionStatisticsVo convert(ConsumptionStatisticsPo po) {
        ConsumptionStatisticsVo vo = new ConsumptionStatisticsVo();
        BeanUtil.copyProperties(vo, po);
        // 将数据库存储的decimal转double
//        if (po.getPrice() != null) {
//            vo.setPrice(CommonUtil.parseDouble(po.getPrice()));
//        }
//        if (po.getBalance() != null) {
//            vo.setBalance(CommonUtil.parseDouble(po.getBalance()));
//        }
        return vo;
    }
}
