package com.sckw.payment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.BeanUtils;
import com.sckw.payment.dao.KwpWalletRefundMapper;
import com.sckw.payment.model.KwpWalletRefund;
import com.sckw.payment.model.constant.RefundEnum;
import com.sckw.payment.model.vo.req.page.RefundPage;
import com.sckw.payment.model.vo.res.WalletRefundVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 钱包订单相关业务
 *
 * @author xucaiqin
 * @date 2023-09-18 19:20:02
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WalletOrderService {
    private final KwpWalletRefundMapper kwpWalletRefundMapper;

    /**
     * 预付、预收 查询退款中金额
     *
     * @param change  true-预收，需要转换双方企业
     * @param channel huifu-汇付渠道
     */
    public List<KwpWalletRefund> query(boolean change, String uid, String filter, String channel) {
        LambdaQueryWrapper<KwpWalletRefund> wrapper = new LambdaQueryWrapper<>();
        //预收时，切换预收预付的双方人员
        if (change) {
            wrapper.eq(KwpWalletRefund::getUid, filter).eq(KwpWalletRefund::getFilter, uid);
        } else {
            wrapper.eq(KwpWalletRefund::getUid, uid).eq(KwpWalletRefund::getFilter, filter);
        }
        wrapper.eq(KwpWalletRefund::getChannel, channel)
                .eq(KwpWalletRefund::getStatus, RefundEnum.REFUNDING.getStatus());
        return kwpWalletRefundMapper.selectList(wrapper);
    }

    /**
     * 查询退款中的订单
     *
     * @param uid
     * @param filter
     * @param channel
     * @return
     */
    public List<KwpWalletRefund> queryList(String uid, String filter, String channel, RefundEnum refundEnum) {
        LambdaQueryWrapper<KwpWalletRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpWalletRefund::getUid, uid).eq(KwpWalletRefund::getFilter, filter);
        wrapper.eq(KwpWalletRefund::getChannel, channel)
                .eq(KwpWalletRefund::getStatus, refundEnum.getStatus());
        return kwpWalletRefundMapper.selectList(wrapper);
    }

    public KwpWalletRefund queryOne(String uid, String filter, String channel, RefundEnum refundEnum) {
        LambdaQueryWrapper<KwpWalletRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpWalletRefund::getUid, uid).eq(KwpWalletRefund::getFilter, filter);
        wrapper.eq(KwpWalletRefund::getChannel, channel)
                .eq(KwpWalletRefund::getStatus, refundEnum.getStatus()).last("limit 1");
        return kwpWalletRefundMapper.selectOne(wrapper);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    public KwpWalletRefund queryById(Long id) {
        return kwpWalletRefundMapper.selectById(id);
    }

    /**
     * 更新
     *
     * @param kwpWalletRefund
     * @return
     */
    public void updateById(KwpWalletRefund kwpWalletRefund) {
        kwpWalletRefundMapper.updateById(kwpWalletRefund);
    }

    /**
     * 分页查询
     *
     * @param refundPage
     * @return
     */
    public PageRes<WalletRefundVo> queryRefundList(RefundPage refundPage) {
        PageHelper.startPage(refundPage.getPage(), refundPage.getPageSize());
        List<KwpWalletRefund> kwpWalletRefunds = kwpWalletRefundMapper.pageList(refundPage);
        PageInfo<KwpWalletRefund> pageInfo = new PageInfo<>(kwpWalletRefunds);
        List<WalletRefundVo> list = kwpWalletRefunds.stream().map(a -> {
            WalletRefundVo walletRefundVo = new WalletRefundVo();
            BeanUtils.copyProperties(a, walletRefundVo);
            return walletRefundVo;
        }).toList();
        return PageRes.build(pageInfo, list);
    }
}
