package cn.com.ctop.finance.payment.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.finance.payment.entity.FinancePaymentApplication;
import cn.com.ctop.finance.payment.entity.FinancePaymentPool;
import cn.com.ctop.finance.payment.entity.FinancePaymentRechargeApplication;
import cn.com.ctop.finance.payment.mapper.FinancePaymentPoolMapper;
import cn.com.ctop.finance.payment.mapper.FinancePaymentRechargeAccountMapper;
import cn.com.ctop.finance.payment.service.IFinancePaymentPoolService;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 款项池表
 *
 * @author jeecg-boot
 * 2023-04-11
 * @version V1.0
 */
@Service
public class FinancePaymentPoolServiceImpl extends ServiceImpl<FinancePaymentPoolMapper, FinancePaymentPool> implements IFinancePaymentPoolService {

    @Autowired
    private FinancePaymentRechargeAccountMapper accountMapperl;

    @Override
    public void add(FinancePaymentRechargeApplication recharge, FinancePaymentApplication application) {
        if (Check.isNull(recharge)) {
            FinancePaymentPool pool = baseMapper.queryFinancePaymentPool(application.getAdvertiserName(), application.getProductId(), application.getPaymentMedia(), application.getPaymentPort());
            if (Check.isNull(pool)) {
                FinancePaymentPool paymentPool = new FinancePaymentPool();
                BeanUtil.copyProperties(application, paymentPool);
                paymentPool.setId(null);
                paymentPool.setCreateTime(null);
                paymentPool.setUpdateTime(null);
                this.save(paymentPool);
            } else {
                BigDecimal cashAmount = pool.getCashAmount();
                BigDecimal displayAmount = pool.getDisplayAmount();
                pool.setCashAmount(cashAmount.add(application.getCashAmount()));
                pool.setDisplayAmount(displayAmount.add(application.getDisplayAmount()));
                this.updateById(pool);
            }
        } else {
            FinancePaymentPool pool = baseMapper.queryFinancePaymentPool(recharge.getAdvertiserName(), recharge.getProductId(), recharge.getPaymentMedia(), recharge.getPaymentPort());
            if (!Check.isNull(pool)) {
                JSONObject amounts = accountMapperl.queryAmounts(recharge.getId());
                BigDecimal cashAmount = pool.getCashAmount();
                BigDecimal displayAmount = pool.getDisplayAmount();
                pool.setCashAmount(cashAmount.add(amounts.getBigDecimal("cashAmount")));
                pool.setDisplayAmount(displayAmount.add(amounts.getBigDecimal("displayAmount")));
                this.updateById(pool);
            }
        }
    }

    @Override
    public FinancePaymentPool getIdByUniqueKey(String advertiserName, Long productId, String paymentMedia, String paymentPort) {
        return baseMapper.queryFinancePaymentPool(advertiserName, productId, paymentMedia, paymentPort);
    }

    @Override
    public Result<Object> querySheetIdAndNameList(JSONObject data) {
        String tableparameters = data.getString("table");
        List<String> list = Arrays.asList(tableparameters.split(","));
        if (list.size() != 3) {
            return Result.error("入参格式不正确");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("table", list.get(0));
        map.put("id", list.get(1));
        map.put("name", list.get(2));
        map.put("groupBy", data.getString("groupBy"));
        map.put("paramters", data.getJSONArray("paramters"));
        return Result.ok(baseMapper.queryIdAndName(map));
    }

    @Override
    public void reduce(FinancePaymentRechargeApplication recharge, FinancePaymentApplication application) {
        if (Check.isNull(application)) {
            FinancePaymentPool pool = baseMapper.queryFinancePaymentPool(recharge.getAdvertiserName(), recharge.getProductId(), recharge.getPaymentMedia(), recharge.getPaymentPort());
            if (!Check.isNull(pool)) {
                JSONObject amounts = accountMapperl.queryAmounts(recharge.getId());

                BigDecimal cashAmount = pool.getCashAmount();
                BigDecimal displayAmount = pool.getDisplayAmount();
                pool.setCashAmount(cashAmount.subtract(amounts.getBigDecimal("cashAmount")));
                pool.setDisplayAmount(displayAmount.subtract(amounts.getBigDecimal("displayAmount")));
                this.updateById(pool);
            }
        } else {
            FinancePaymentPool pool = baseMapper.queryFinancePaymentPool(application.getAdvertiserName(), application.getProductId(), application.getPaymentMedia(), application.getPaymentPort());
            if (!Check.isNull(pool)) {
                BigDecimal cashAmount = pool.getCashAmount();
                BigDecimal displayAmount = pool.getDisplayAmount();
                pool.setCashAmount(cashAmount.subtract(application.getCashAmount()));
                pool.setDisplayAmount(displayAmount.subtract(application.getDisplayAmount()));
                this.updateById(pool);
            }
        }
    }

    @Override
    public Result<Object> queryPoolProjectList(String advertiserId, Long productId, String type) {
        List<String> advertiserIds = Arrays.asList(advertiserId.split(","));
        return Result.ok(baseMapper.queryPoolProjectList(advertiserIds, productId, type));
    }


    @Override
    public Result<Object> queryPoolAccountList(String advertiserId, Long projectId, String type) {
        List<String> advertiserIds = Arrays.asList(advertiserId.split(","));
        return Result.ok(baseMapper.queryPoolAccountList(advertiserIds, projectId, type));
    }

    @Override
    public Result<Object> queryPoolAmountById(String advertiserId, Long productId, String type, String paymentPort) {
        return Result.ok(baseMapper.queryFinancePaymentPoolById(advertiserId, productId, type, paymentPort));
    }

    @Override
    public Result<Object> queryPaymentPort(String advertiserId, Long productId, String paymentMedia) {
        return Result.ok(baseMapper.queryPaymentPort(advertiserId, productId, paymentMedia));
    }
}
