package com.iformall.schedule;

import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.dto.WxSharingOrderDto;
import com.iformall.domain.po.WxProfitSharingOrder;
import com.iformall.enums.EnumProfitSharingOrderStatus;
import com.iformall.enums.EnumProfitSharingOrderType;
import com.iformall.mapper.WxPayOrderMapper;
import com.iformall.mapper.WxProfitSharingOrderMapper;
import com.iformall.service.WxProfitSharingOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SharingOrderRedoSchedule {

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

    @Autowired
    private WxProfitSharingOrderService wxProfitSharingOrderService;

    @Autowired
    private WxProfitSharingOrderMapper wxProfitSharingOrderMapper;


    @Scheduled(cron = "0 0 2 * * ?") // 每天凌晨02:00分账重试
    //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
    public void sharingOrderResultSchedule() {

        WxProfitSharingOrder wxProfitSharingOrder = new WxProfitSharingOrder();

        wxProfitSharingOrder.setType(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode());
        wxProfitSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_APPLY_FAILED.getCode());
        List<WxProfitSharingOrder> list = wxProfitSharingOrderMapper.findList(wxProfitSharingOrder);
        list.stream().forEach(sharingOrder->{
            try {
                ResultData result = wxProfitSharingOrderService.redoSharingOrder(sharingOrder);
                if (result.code != ResultData.SUCCESS) {
                    logger.error("分账重试失败:" + EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getMessage()
                            + " ID:" +sharingOrder.getId()
                            + " RES:" + result.message);
                }

            } catch (Exception e){
                logger.error("分账重试失败:" + EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getMessage()
                        + " ID:" +sharingOrder.getId()
                        + " MSG:" + e.getMessage());
            }
        });


        wxProfitSharingOrder.setType(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode());
        wxProfitSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_APPLY_FAILED.getCode());
        list = wxProfitSharingOrderMapper.findList(wxProfitSharingOrder);
        list.stream().forEach(sharingOrder->{
            try {
                ResultData result = wxProfitSharingOrderService.redoSharingOrder(sharingOrder);
                if (result.code != ResultData.SUCCESS) {
                    logger.error("分账重试失败:" + EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getMessage()
                            + " ID:" +sharingOrder.getId()
                            + " RES:" + result.message);
                }
            } catch (Exception e){
                logger.error("分账重试失败:" + EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getMessage()
                        + " ID:" +sharingOrder.getId()
                        + " MSG:" + e.getMessage());
            }
        });

    }
}