package com.crm.ucard.service.impl;

import com.alibaba.fastjson.JSON;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.pojo.table.FinanceBalanceTable;
import com.crm.rely.backend.core.pojo.table.ucard.CardApplyTable;
import com.crm.rely.backend.core.pojo.table.ucard.CardGlobalCurrenciesTable;
import com.crm.rely.backend.core.pojo.table.ucard.UcardCardTable;
import com.crm.ucard.service.GlobalService;
import com.crm.ucard.service.UcardCardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class UcardMqServiceImpl {

    @Autowired
    private UcardCardService ucardCardService;
    @Autowired
    private GlobalService globalService;


    /**
     * 更新卡物流状态-1天消息处理
     */
    @JmsListener(destination = Constants.CARD_LOGISTICS_STATUS_UPDATE)
    @Transactional(rollbackFor = Exception.class)
    public void updateCardLogisticsStatus(String content) throws Exception {
        log.info(String.format("CARD_LOGISTICS_STATUS_UPDATE_ONE_HOUR,content:%s", content));
        UcardCardTable table = JSON.parseObject(content, UcardCardTable.class);
        if ("4".equals(table.getLogisticsStatus())) {
            return;
        }
        ucardCardService.updateCardLogisticsStatus(table);
    }


    /**
     * 手续费扣款回调
     */
    @JmsListener(destination = Constants.WASABI_CARD_APPLY_CALLBACK)
    @Transactional(rollbackFor = Exception.class)
    public void deductionCallback(String content) throws Exception {

        log.info(String.format("WASABI_CARD_APPLY_CALLBACK,content:%s", content));
        FinanceBalanceTable entity = JSON.parseObject(content, FinanceBalanceTable.class);

        if (entity == null || StringUtils.isEmpty(entity.getSerial()) || entity.getTradeStatus() == null) {
            log.error("WASABI_CARD_APPLY_CALLBACK,entity is null");
            return;
        }
        ucardCardService.deductionCallback(entity);
    }


    /**
     * 手续费退费回调
     */
    @JmsListener(destination = Constants.WASABI_CARD_REFUND_CALLBACK)
    @Transactional(rollbackFor = Exception.class)
    public void refundCallback(String content) throws Exception {

        log.info(String.format("WASABI_CARD_REFUND_CALLBACK,content:%s", content));
        FinanceBalanceTable entity = JSON.parseObject(content, FinanceBalanceTable.class);

        if (entity == null || StringUtils.isEmpty(entity.getSerial()) || entity.getTradeStatus() == null) {
            log.error("WASABI_CARD_REFUND_CALLBACK,entity is null");
            return;
        }
        ucardCardService.refundCallback(entity);
    }

    /**
     * 卡申请进度查询
     */
    @JmsListener(destination = Constants.WASABICARD_CARD_APPLY_PROGRESS)
    @Transactional(rollbackFor = Exception.class)
    public void callbackCardApplyProgress(String content) throws Exception {
        log.info(String.format("WASABICARD_CARD_APPLY_PROGRESS,content:%s", content));
        CardApplyTable table = JSON.parseObject(content, CardApplyTable.class);
        if (null == table) {
            log.error("WASABICARD_CARD_APPLY_PROGRESS,applyTable is null");
            return;
        }
        ucardCardService.callbackCardApplyProgress(table);
    }

    /**
     * 激活中数据查询卡信息并更新数据库
     */
    @JmsListener(destination = Constants.WASABICARD_CARD_INFO_REFRESH)
    @Transactional(rollbackFor = Exception.class)
    public void activateCallback(String content) throws Exception {
        log.info(String.format("WASABICARD_CARD_INFO_REFRESH,content:%s", content));
        UcardCardTable cardTable = JSON.parseObject(content, UcardCardTable.class);
        ucardCardService.activateCallback(cardTable);
    }

    /**
     * 同步速汇银行城市
     */
    @JmsListener(destination = Constants.SAVE_GLOBAL_BANK_CITIES)
    @Transactional(rollbackFor = Exception.class)
    public void saveGlobalBankCities(String content) throws Exception {
        log.info(String.format("SAVE_GLOBAL_BANK_CITIES,content:%s", content));
        List<CardGlobalCurrenciesTable> personList = JSON.parseArray(content, CardGlobalCurrenciesTable.class);
        if (CollectionUtils.isEmpty(personList)){
            log.error("SAVE_GLOBAL_BANK_CITIES,personList is null");
            return;
        }
        globalService.saveGlobalStates(personList);
    }

}
