package com.sqx.modules.job.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.sqx.modules.pay.dao.CashOutDao;
import com.sqx.modules.pay.entity.CashOut;
import com.sqx.modules.pay.wuyou.BaseResp;
import com.sqx.modules.pay.wuyou.WuyouPay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

@Component
public class TempCashOutTask{

    @Resource
    private CashOutDao cashOutDao;
    @Resource
    private WuyouPay wuyouPay;

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

    public void run(String params) {
        logger.info("提现开始");
        List<CashOut> cashOuts = cashOutDao.selectTemp();
        for (CashOut cashOut : cashOuts) {
            BaseResp baseResp = wuyouPay.queryExtractOrder(cashOut.getOrderNumber(), cashOut.getMoney());
            logger.info("baseResp：{} ", JSONUtil.toJsonStr(baseResp));
            if (baseResp.getStatus() != null && (baseResp.getStatus().equals(2) || baseResp.getStatus().equals(10000))){
                logger.info("success：{} ", cashOut.getOrderNumber());
                cashOut.setState(1);
                cashOut.setOutAt(DateUtil.now());
                cashOut.setRefund(null);
                cashOut.setOutAt(DateUtil.now());
                cashOutDao.updateById(cashOut);
            }
        }
        logger.info("提现结束");
    }
}
