package org.dromara.payment.consumer;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.enums.BusRole;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.payment.bill.domain.MerBill;
import org.dromara.payment.bill.domain.MerBillDetail;
import org.dromara.payment.bill.domain.vo.MerBillDetailVo;
import org.dromara.payment.bill.mapper.MerBillDetailMapper;
import org.dromara.payment.bill.mapper.MerBillMapper;
import org.dromara.payment.bill.service.IMerBillDetailService;
import org.dromara.payment.bill.service.IMerBillService;
import org.dromara.payment.common.domain.BusAccounts;
import org.dromara.payment.common.domain.TranLog;
import org.dromara.payment.common.mapper.TranLogMapper;
import org.dromara.payment.merchant.domain.BusMerchantAccount;
import org.dromara.payment.merchant.domain.vo.BusMerchantAccountVo;
import org.dromara.payment.merchant.domain.vo.BusMerchantVo;
import org.dromara.payment.merchant.mapper.BusMerchantAccountMapper;
import org.dromara.payment.merchant.mapper.BusMerchantMapper;
import org.dromara.payment.operator.domain.BusOperatorAccount;
import org.dromara.payment.operator.domain.vo.BusOperatorAccountVo;
import org.dromara.payment.operator.domain.vo.BusOperatorVo;
import org.dromara.payment.operator.mapper.BusOperatorAccountMapper;
import org.dromara.payment.operator.mapper.BusOperatorMapper;
import org.dromara.payment.sys.domain.SysSyd;
import org.dromara.payment.sys.domain.SysSydAccount;
import org.dromara.payment.sys.mapper.SysSydAccountMapper;
import org.dromara.payment.sys.mapper.SysSydMapper;
import org.dromara.payment.sys.service.ISysSydService;
import org.dromara.payment.task.domain.MerTask;
import org.dromara.payment.worker.domain.UserWorkerAccount;
import org.dromara.payment.worker.domain.vo.UserWorkerAccountVo;
import org.dromara.payment.worker.mapper.UserWorkerAccountMapper;
import org.dromara.settlement.api.RemoteAccountService;
import org.dromara.settlement.api.domain.Account;
import org.dromara.settlement.api.domain.Result;
import org.dromara.settlement.api.domain.TransactionLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

@Slf4j
@Component
@RequiredArgsConstructor
public class BillPayStreamConsumer {

    private final IMerBillService billService;

    private final IMerBillDetailService billDetailService;

    @Bean
    Consumer<JSONObject> bill() {
        log.info("订阅代发任务队列");
        return param -> {
            log.info("通过stream消费到代发任务消息 => {}", param);
            if (!param.containsKey("billId") && !param.containsKey("billDetailId")) {
                return;
            }
            Long billId = param.getLong("billId");
            Long billDetailId = null;
            try {
                if(param.containsKey("billDetailId")){
                    billDetailId = param.getLong("billDetailId");
                    billService.paymentToWorker(billDetailId.intValue());
                }else{
                    billService.paymentToService(billId.intValue());
                }
            } catch (Exception e) {
                String errorType = "代发任务";
                if(e instanceof ServiceException){
                    ServiceException ex = (ServiceException) e;
                    errorType += "-" + ex.getDetailMessage();
                    try {
                        billDetailService.failBillDetail(billId,billDetailId,ex.getMessage(),0,null);
                    } catch (Exception exc) {
                        log.error("保存"+errorType+"失败结果出错：（"+param.toJSONString()+"）",exc);
                    }
                }
                log.error(errorType+"出错：（"+param.toJSONString()+"）",e);
            }
        };
    }
}
