package com.jsowell.pile.service.impl;

import com.github.pagehelper.PageInfo;
import com.jsowell.common.core.page.PageResponse;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.PageUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.SettleOrderReport;
import com.jsowell.pile.dto.MerchantOrderReportDTO;
import com.jsowell.pile.mapper.SettleOrderReportMapper;
import com.jsowell.pile.service.ISettleOrderReportService;
import com.jsowell.pile.vo.uniapp.MemberWalletLogVO;
import com.jsowell.pile.vo.web.MerchantOrderReportVO;
import com.jsowell.pile.vo.web.SettleOrderReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 结算订单报Service业务层处理
 *
 * @author jsowell
 * @date 2023-06-05
 */
@Service
public class SettleOrderReportServiceImpl implements ISettleOrderReportService {
    @Autowired
    private SettleOrderReportMapper settleOrderReportMapper;

    /**
     * 查询结算订单报
     *
     * @param id 结算订单报主键
     * @return 结算订单报
     */
    @Override
    public SettleOrderReport selectSettleOrderReportById(Long id) {
        return settleOrderReportMapper.selectSettleOrderReportById(id);
    }

    /**
     * 查询结算订单报列表
     *
     * @param settleOrderReport 结算订单报
     * @return 结算订单报
     */
    @Override
    public List<SettleOrderReport> selectSettleOrderReportList(SettleOrderReport settleOrderReport) {
        return settleOrderReportMapper.selectSettleOrderReportList(settleOrderReport);
    }

    /**
     * 新增结算订单报
     *
     * @param settleOrderReport 结算订单报
     * @return 结果
     */
    @Override
    public int insertSettleOrderReport(SettleOrderReport settleOrderReport) {
        settleOrderReport.setCreateTime(DateUtils.getNowDate());
        return settleOrderReportMapper.insertSettleOrderReport(settleOrderReport);
    }

    /**
     * 修改结算订单报
     *
     * @param settleOrderReport 结算订单报
     * @return 结果
     */
    @Override
    public int updateSettleOrderReport(SettleOrderReport settleOrderReport) {
        return settleOrderReportMapper.updateSettleOrderReport(settleOrderReport);
    }

    /**
     * 批量删除结算订单报
     *
     * @param ids 需要删除的结算订单报主键
     * @return 结果
     */
    @Override
    public int deleteSettleOrderReportByIds(Long[] ids) {
        return settleOrderReportMapper.deleteSettleOrderReportByIds(ids);
    }

    /**
     * 删除结算订单报信息
     *
     * @param id 结算订单报主键
     * @return 结果
     */
    @Override
    public int deleteSettleOrderReportById(Long id) {
        return settleOrderReportMapper.deleteSettleOrderReportById(id);
    }

    /**
     * 根据运营商id查询订单报表
     * @param dto
     * @return
     */
    @Override
    public MerchantOrderReportVO getMerchantOrderReport(MerchantOrderReportDTO dto) {
        int pageNum = dto.getPageNum() == null ? 1 : dto.getPageNum();
        int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize();
        if (StringUtils.isBlank(dto.getStartTime())) {
            dto.setStartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.addDays(new Date(), -7)));
        }
        if (StringUtils.isBlank(dto.getEndTime())) {
            dto.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, new Date()));
        }
        MerchantOrderReportVO vo = new MerchantOrderReportVO();
        MerchantOrderReportVO.MerchantOrderReport report = new MerchantOrderReportVO.MerchantOrderReport();
        PageUtils.startPage(pageNum, pageSize);
        List<SettleOrderReportVO> pageList = settleOrderReportMapper.getMerchantOrderReport(dto);
        if (CollectionUtils.isEmpty(pageList)) {
            return null;
        }
        List<SettleOrderReportVO> list = settleOrderReportMapper.getMerchantOrderReport(dto);
        PageInfo<SettleOrderReportVO> pageInfo = new PageInfo<>(pageList);
        vo.setMerchantId(dto.getMerchantId());
        // vo.setReportList(pageInfo.getList());

        PageResponse pageResponse = PageResponse.builder()
                .pageSize(pageSize)
                .pageNum(pageNum)
                .pages(pageInfo.getPages())
                .total(pageInfo.getTotal())
                .list(pageInfo.getList())

                .build();
        vo.setPageResponse(pageResponse);
        // 进行数据汇总
        // 用电度数
        report.setUseElectricity(list.stream().map(SettleOrderReportVO::getUseElectricity).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 充电次数
        report.setChargeNum(list.stream().map(SettleOrderReport -> new BigDecimal(SettleOrderReport.getChargeNum())).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 充电时长
        report.setChargeTime(list.stream().map(SettleOrderReport -> new BigDecimal(SettleOrderReport.getChargeTime())).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 电费金额
        report.setElectricityAmount(list.stream().map(SettleOrderReportVO::getElectricityAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 服务费金额
        report.setServiceAmount(list.stream().map(SettleOrderReportVO::getServiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 收入金额
        report.setTotalAmount(list.stream().map(SettleOrderReportVO::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 虚拟金额
        report.setVirtualAmount(list.stream().map(SettleOrderReportVO::getVirtualAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 交易金额
        report.setTradeAmount(list.stream().map(SettleOrderReportVO::getTradeAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 交易手续费
        report.setTradeFee(list.stream().map(SettleOrderReportVO::getTradeFee).reduce(BigDecimal.ZERO, BigDecimal::add));

        vo.setMerchantOrderReport(report);
        return vo;
    }
}
