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

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.pay.dao.CashOutDao;
import com.sqx.modules.pay.entity.CashOut;
import com.sqx.modules.pay.service.WuyouCallbackService;
import com.sqx.modules.pay.wuyou.BaseResp;
import com.sqx.modules.pay.wuyou.NotifyDto;
import com.sqx.modules.sys.entity.SysUserMoneyDetails;
import com.sqx.modules.sys.service.SysUserMoneyDetailsService;
import com.sqx.modules.sys.service.SysUserMoneyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author GYJoker
 */
@Service
public class WuyouCallbackServiceImpl implements WuyouCallbackService {
    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private SysUserMoneyService sysUserMoneyService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private SysUserMoneyDetailsService sysUserMoneyDetailsService;

    @Resource
    private CashOutDao cashOutDao;

    @Override
    @Transactional
    public int executeExtractCallback(CashOut cashOut, BaseResp resp) {
        int status = 0;
        if (resp.getStatus() != null && resp.getStatus().equals(2)) {
            status = 1;
        } else if (resp.getStatus() != null && (resp.getStatus().equals(3) || resp.getStatus().equals(99999))) {
            status = 2;
        }

        return executeExtractCallback(cashOut, status, resp.getMsg());
    }

    @Override
    @Transactional
    public void executeExtractCallback(CashOut cashOut, NotifyDto notifyDto) {
        int status = 0;
        if (notifyDto.getStatus() != null && "2".equals(notifyDto.getStatus())) {
            status = 1;
        } else if (notifyDto.getStatus() != null && "3".equals(notifyDto.getStatus())) {
            status = 2;
        }

        executeExtractCallback(cashOut, status, notifyDto.getMsg());
    }

    /**
     * 处理提现结果
     * @param cashOut 提现实体类
     * @param status 提现
     * @param reason
     * @return
     */
    @Override
    @Transactional
    public int executeExtractCallback(CashOut cashOut, int status, String reason) {
        if (StringUtils.isNotBlank(reason) && reason.contains("已驳回")) {
            reason = "提现失败,请检查支付宝账号与收款人姓名后重试。";
        }
        BigDecimal money = new BigDecimal(cashOut.getMoney());
        if (status == 1) {
            cashOut.setState(1);
            cashOut.setOutAt(DateUtil.now());
            cashOut.setRefund(null);
            cashOut.setOutAt(DateUtil.now());
            cashOutDao.update(cashOut, new LambdaQueryWrapper<CashOut>().eq(CashOut::getUserId, cashOut.getUserId()).eq(CashOut::getId, cashOut.getId()));

            return 1;
        }

        if (status == 2) {
            cashOut.setOutAt(DateUtil.date().toString());
            cashOut.setState(2);
            cashOut.setRefund(reason);

            if (cashOut.getUserType() != null && cashOut.getUserType().equals(2)) {
                int count = sysUserMoneyDetailsService.count(new LambdaQueryWrapper<SysUserMoneyDetails>()
                        .eq(SysUserMoneyDetails::getUserId, cashOut.getUserId())
                        .eq(SysUserMoneyDetails::getClassify, 4)
                        .eq(SysUserMoneyDetails::getState, 2)
                        .eq(SysUserMoneyDetails::getMoneyType, 1)
                        .eq(SysUserMoneyDetails::getType, 1)
                        .eq(SysUserMoneyDetails::getSourceId, cashOut.getId())
                );
                if (count > 0) {
                    return  0;
                }

                SysUserMoneyDetails details = new SysUserMoneyDetails(
                        cashOut.getUserId(), cashOut.getUserId(), null, "提现失败存入余额", 4, 1, 2,
                        money, "提现失败存入余额" + cashOut.getMoney() + "元", 1);
                sysUserMoneyDetailsService.save(details);

                sysUserMoneyService.updateSysMoney(1, cashOut.getUserId(), money.doubleValue());
            } else {
                int count = userMoneyDetailsService.count(new LambdaQueryWrapper<UserMoneyDetails>()
                        .eq(UserMoneyDetails::getUserId, cashOut.getUserId())
                        .eq(UserMoneyDetails::getClassify, 4)
                        .eq(UserMoneyDetails::getState, 2)
                        .eq(UserMoneyDetails::getMoneyType, 1)
                        .eq(UserMoneyDetails::getType, 1)
                        .eq(UserMoneyDetails::getSourceId, cashOut.getId())
                );
                if (count > 0) {
                    return 0;
                }

                UserMoneyDetails details = new UserMoneyDetails();
                details.setUserId(cashOut.getUserId());
                details.setContent("提现失败存入余额" + cashOut.getMoney() + "元");
                details.setClassify(4);
                details.setType(1);
                details.setState(2);
                details.setMoney(new BigDecimal(cashOut.getMoney()));
                details.setTitle("提现失败存入余额");
                details.setMoneyType(1);
                details.setCreateTime(DateUtil.date().toString());
                details.setSourceId(cashOut.getId());
                userMoneyDetailsService.save(details);

                userMoneyService.updateAmount(1, cashOut.getUserId(), money.doubleValue());
            }

            cashOutDao.update(cashOut, new LambdaQueryWrapper<CashOut>().eq(CashOut::getUserId, cashOut.getUserId()).eq(CashOut::getId, cashOut.getId()));
        }
        return 0;
    }
}
