package com.sqx.modules.job.task;

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.entity.WithdrawTypeEnum;
import com.sqx.modules.pay.wuyou.BaseResp;
import com.sqx.modules.pay.wuyou.WuyouPay;
import com.sqx.modules.utils.AliPayOrderUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

@Component
public class CashOutTask{

    @Resource
    private CashOutDao cashOutDao;
    @Resource
    private UserMoneyService userMoneyService;
    @Resource
    private UserMoneyDetailsService userMoneyDetailsService;
    @Resource
    private WuyouPay wuyouPay;

    private Logger logger = LoggerFactory.getLogger(getClass());

    public void run(String params) {
        logger.info("提现开始");
        List<CashOut> cashOuts = cashOutDao.selectYesterday();
        for (CashOut cashOut : cashOuts) {
            if (wuyouPay.checkCanCash(cashOut.getUserId(), WithdrawTypeEnum.RED_ENVELOPE, new BigDecimal(cashOut.getMoney()))) {
                cashOut.setState(4);
                String outOrderNo = AliPayOrderUtil.createOrderId();
                cashOut.setOrderNumber(outOrderNo);

                BaseResp baseResp = wuyouPay.extractOrder(outOrderNo, cashOut.getMoney(), cashOut.getZhifubao(), cashOut.getZhifubaoName());
                if (baseResp.getStatus() != null && baseResp.getStatus().equals(2)) {
                    cashOut.setState(1);
                } else {
                    cashOut.setState(2);
                    cashOut.setRefund(StringUtils.isNotBlank(baseResp.getMsg()) ? baseResp.getMsg() : baseResp.getErrorMsg());
                    if (cashOut.getRefund().contains("收款人账户号出款属性不匹配") || cashOut.getRefund().contains("已驳回")) {
                        cashOut.setRefund("提现失败,请检查支付宝账号与收款人姓名后,重试。");
                    } else {
                        cashOut.setRefund(baseResp.getErrorMsg());
                    }
                    UserMoneyDetails userMoneyDetails = new UserMoneyDetails(
                            cashOut.getUserId(), null, null, "提现失败", 4, 1, 1,
                            new BigDecimal(cashOut.getMoney()), "提现失败存入余额" + cashOut.getMoney() + "元", 1,cashOut.getId());

                    //存入余额 钱
                    userMoneyService.updateAmount(1, cashOut.getUserId(), Double.parseDouble(cashOut.getMoney()));
                    userMoneyDetailsService.save(userMoneyDetails);
                }
            }else {
                cashOut.setState(3);
            }
            cashOutDao.updateById(cashOut);
        }
        logger.info("提现结束");
    }


    public void test(String name, String age) {
        logger.info("name: {}, age: {}", name, age);
    }
}
