package com.hfkj.schedule;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.entity.BsOrderSettle;
import com.hfkj.entity.BsOrderSettleLedger;
import com.hfkj.entity.BsUserSpreadAccount;
import com.hfkj.pay.HuiPayService;
import com.hfkj.service.gas.BsGasOrderSpreadService;
import com.hfkj.service.order.BsOrderSettleLedgerService;
import com.hfkj.service.order.BsOrderSettleService;
import com.hfkj.service.spread.BsUserSpreadAccountService;
import com.hfkj.sysenum.order.OrderSettleLedgerStatusEnum;
import com.hfkj.sysenum.order.OrderStatusEnum;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单结算业务
 * @className: OrderSettleService
 * @author: HuRui
 * @date: 2024/6/25
 **/
@Component
public class OrderSettleSchedule {
    @Resource
    private BsOrderSettleService orderSettleService;
    @Resource
    private BsOrderSettleLedgerService orderSettleLedgerService;
    @Resource
    private BsUserSpreadAccountService userSpreadAccountService;
    @Resource
    private BsGasOrderSpreadService gasOrderSpreadService;

    @Scheduled(cron = "0 0 0 * * ?")   // 每日凌晨00:00:00执行一次
    public void userSpread() {
        List<BsUserSpreadAccount> accountList = userSpreadAccountService.getList(new HashMap<>());
        for (BsUserSpreadAccount account : accountList) {
            try {
                gasOrderSpreadService.settleOrder(account.getUserId());
            } catch (Exception e) {
                System.out.println("用户ID："+account.getUserId()+"，账户结算异常");
            }
        }
    }

    /**
     * 发起分账【补偿】
     * 默认支付回调后发起MQ分账，消费端不一定100%能消费到
     */
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void startSeparate() {
        Calendar createTimeE = Calendar.getInstance();
        createTimeE.add(Calendar.MINUTE,-3);

        Map<String,Object> param = new HashMap<>();
        param.put("ledgerStatus", OrderSettleLedgerStatusEnum.status0.getCode());
        param.put("orderStatus", OrderStatusEnum.status3.getCode());
        param.put("createTimeE", createTimeE.getTime());
        List<BsOrderSettle> orderSettleList = orderSettleService.getList(param);
        for (BsOrderSettle orderSettle : orderSettleList) {
           try {

               orderSettleService.separate(orderSettle);

           } catch (Exception e){

           }
        }
    }

    /**
     * 分账结果
     */
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void separateResult() {
        Map<String,Object> param = new HashMap<>();
        param.put("ledgerStatus", OrderSettleLedgerStatusEnum.status2.getCode());
        List<BsOrderSettle> orderSettleList = orderSettleService.getList(param);
        for (BsOrderSettle orderSettle : orderSettleList) {
            // 查询分账处理结果
            JSONObject obj = HuiPayService.profitSharingQuery(orderSettle);
            if (obj.getString("return_code").equals("000000")) {
                JSONObject data = obj.getJSONObject("return_data");
                // 1：已完成 2：处理中 3：已受理 4：失败
                if (data.getInteger("status") == 1) {
                    orderSettle.setLedgerStatus(OrderSettleLedgerStatusEnum.status1.getCode());
                    orderSettleService.editData(orderSettle);
                    // 分账接收方
                    List<BsOrderSettleLedger> ledgerList = orderSettleLedgerService.getListBySettleId(orderSettle.getId());
                    for (BsOrderSettleLedger ledger : ledgerList) {
                        ledger.setStatus(1);
                        orderSettleLedgerService.editData(ledger);
                    }

                } else if (data.getInteger("status") == 4) {
                    orderSettle.setLedgerStatus(OrderSettleLedgerStatusEnum.status4.getCode());
                    orderSettleService.editData(orderSettle);
                }
            }
        }
    }

    /**
     * 分账撤销结果
     */
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void revokeLedgerResult() {
        Map<String,Object> param = new HashMap<>();
        param.put("ledgerStatus", OrderSettleLedgerStatusEnum.status5.getCode());
        List<BsOrderSettle> orderSettleList = orderSettleService.getList(param);
        for (BsOrderSettle orderSettle : orderSettleList) {
            // 查询分账处理结果
            // 重新设置请求单号
            orderSettle.setLedgerOrderNo(orderSettle.getRevokeOrderNo());
            JSONObject obj = HuiPayService.profitSharingQuery(orderSettle);
            if (obj.getString("return_code").equals("000000")) {
                JSONObject data = obj.getJSONObject("return_data");
                // 1：已完成 2：处理中 3：已受理 4：失败
                if (data.getInteger("status") == 1) {
                    orderSettle.setLedgerStatus(OrderSettleLedgerStatusEnum.status6.getCode());
                    orderSettleService.editData(orderSettle);

                } else if (data.getInteger("status") == 4) {
                    orderSettle.setLedgerStatus(OrderSettleLedgerStatusEnum.status4.getCode());
                    orderSettleService.editData(orderSettle);
                }
            }
        }
    }

}
