package com.sqx.modules.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.pay.controller.app.AliPayController;
import com.sqx.modules.pay.dao.PayDetailsDao;
import com.sqx.modules.pay.entity.PayDetails;
import com.sqx.modules.pay.service.PayDetailsService;
import com.sqx.modules.pay.service.WxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 充值记录
 */
@Service
public class PayDetailsServiceImpl extends ServiceImpl<PayDetailsDao, PayDetails> implements PayDetailsService {

    /**
     * 充值记录
     */
    @Autowired
    private PayDetailsDao payDetailsDao;
    @Autowired
    private AliPayController aliPayController;
    @Autowired
    private WxService wxService;

    @Override
    public PageUtils selectPayDetails(int page, int limit, String startTime, String endTime, Long userId, Integer state) {
        Page<Map<String, Object>> pages = new Page<>(page, limit);
        if (state != null && state == -1) {
            state = null;
        }
        return new PageUtils(payDetailsDao.selectPayDetails(pages, startTime, endTime, userId, state));
    }

    public PageUtils wxPayJsApiMoneyDetails(int page, int limit,int classify,Long userId){
        Page<Map<String, Object>> pages = new Page<>(page, limit);
        return new PageUtils(payDetailsDao.wxPayJsApiMoneyDetails(pages,userId));
    }

    public void selectPyaDetailsByUserId(Long userId){
        new QueryWrapper<PayDetails>().eq("user_id", userId).eq("state", 1).eq("type", 1);

    }

    @Override
    public Result findTopUpMoney(Integer page, Integer limit, PayDetails payDetails, String startTime, String endTime) {
        Page<PayDetails> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(baseMapper.findTopUpMoney(pages,payDetails, startTime, endTime));
        return Result.success().put("data", pageUtils);
    }


    @Override
    public Double selectSumPay(String createTime, String endTime, Long userId) {
        if (userId == null || userId == -1) {
            return payDetailsDao.selectSumPay(createTime, endTime, null);
        }
        return payDetailsDao.selectSumPay(createTime, endTime, userId);
    }

    @Override
    public PageUtils payMemberAnalysis(int page, int limit, String time, Integer flag) {
        Page<Map<String, Object>> pages = new Page<>(page, limit);
        return new PageUtils(payDetailsDao.payMemberAnalysis(pages, time, flag));
    }

    @Override
    public PageUtils selectUserMemberList(int page, int limit, String phone) {
        Page<Map<String, Object>> pages = new Page<>(page, limit);
        return new PageUtils(payDetailsDao.selectUserMemberList(pages, phone));
    }

    @Override
    public Double selectSumMember(String time, Integer flag) {
        return payDetailsDao.selectSumMember(time, flag);
    }

    @Override
    public Double selectSumPayByState(String time, Integer flag, Integer state) {
        return payDetailsDao.selectSumPayByState(time, flag, state);
    }

    @Override
    public Double selectSumPayByClassify(String time, Integer flag, Integer classify, Integer payClassify) {
        return payDetailsDao.selectSumPayByClassify(time, flag, classify, payClassify);
    }

    @Override
    public Double instantselectSumPay(String date, Long userId) {
        return payDetailsDao.instantselectSumPay(date, userId);
    }

    @Override
    public BigDecimal getRechargeMoney(Integer type, String time, Integer flag) {

        return payDetailsDao.getRechargeMoney(type, time, flag);
    }

    @Override
    public BigDecimal getUserAccumulateMoney(Long userId) {


        return payDetailsDao.getUserAccumulateMoney(userId);
    }

    @Override
    public Result refund(Long id) {
        PayDetails payDetails = payDetailsDao.selectById(id);
        if (payDetails.getClassify() == 4 || payDetails.getClassify() == 5) {
           return Result.success().put("data", aliPayController.aliRefund(payDetails));
        } else {
            return wxService.wxRefund(payDetails);
        }

    }


}
