package com.jeequan.jeepay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.bill.ChannelBill;
import com.jeequan.jeepay.core.utils.DateKit;
import com.jeequan.jeepay.db.config.dynamic.DataSourceSwitch;
import com.jeequan.jeepay.db.config.dynamic.DynamicDataSource;
import com.jeequan.jeepay.db.entity.CheckChannelBill;
import com.jeequan.jeepay.service.mapper.CheckChannelBillMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 渠道对账单表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2022-09-09
 */
@Service
public class CheckChannelBillService extends ServiceImpl<CheckChannelBillMapper, CheckChannelBill> {

    @DataSourceSwitch(DynamicDataSource.DataSourceTypeEnum.SLAVE)
    public List<CheckChannelBill> selectListByCheckBatchNo(String batchNo) {
        return this.list(CheckChannelBill.gw().eq(CheckChannelBill::getBatchNo, batchNo));
    }

    public void saveChannelBills(List<ChannelBill> channelBillList) {
        if (CollUtil.isEmpty(channelBillList)) {
            return;
        }

        List<CheckChannelBill> checkChannelBillList = covertCheckChannelBillList(channelBillList);
        boolean result = this.saveBatch(checkChannelBillList);
        if (!result) {
            throw new BizException("保存渠道对账单失败");
        }
    }

    private List<CheckChannelBill> covertCheckChannelBillList(List<ChannelBill> checkChannelBillList) {
        return checkChannelBillList.stream().map(orderBill -> {

            CheckChannelBill dbCheckChannelBill = new CheckChannelBill();
            BeanUtil.copyProperties(orderBill, dbCheckChannelBill);

            return dbCheckChannelBill;
        }).collect(Collectors.toList());
    }

    /**  渠道账单统计 */
    @DataSourceSwitch(DynamicDataSource.DataSourceTypeEnum.SLAVE)
    public JSONObject billCount(CheckChannelBill channelBill, JSONObject paramJSON) {

        HashMap<String, Object> paramsMap = new HashMap<>();

        String dateRange = paramJSON.getString("queryDateRange");

        if (!StringUtils.isEmpty(dateRange)) {
            // 获取查询时间请求参数  [开始时间,  结束时间]
            Date[] queryDateRangeArray = DateKit.getQueryDateRange(dateRange);

            String createdStart = DateUtil.formatDateTime(queryDateRangeArray[0]);
            String createdEnd = DateUtil.formatDateTime(queryDateRangeArray[1]);

            if (StrUtil.isNotBlank(createdStart)) {
                paramsMap.put("createTimeStart", createdStart);
            }
            if (StrUtil.isNotBlank(createdEnd)) {
                paramsMap.put("createTimeEnd", createdEnd);
            }
        }

        if (channelBill.getChannelState() != null) {
            paramsMap.put("channelState", channelBill.getChannelState());
        }
        if (StrUtil.isNotBlank(channelBill.getIfCode())) {
            paramsMap.put("ifCode", channelBill.getIfCode());
        }
        if (StrUtil.isNotBlank(channelBill.getBatchNo())) {
            paramsMap.put("batchNo", channelBill.getBatchNo());
        }

        Map resMap = baseMapper.channelBillCount(paramsMap);
        // 返回对象
        return new JSONObject(resMap);

    }
}
