package com.jsowell.pile.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.AdapayCallbackRecord;
import com.jsowell.pile.mapper.AdapayCallbackRecordMapper;
import com.jsowell.pile.service.AdapayCallbackRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
public class AdapayCallbackRecordServiceImpl implements AdapayCallbackRecordService {

    @Autowired
    private AdapayCallbackRecordMapper adapayCallbackRecordMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void saveAdapayCallbackRecord(AdapayCallbackRecord callbackRecord) {
        adapayCallbackRecordMapper.insert(callbackRecord);
    }

    @Override
    public void saveAdapayCallbackRecord(String data) throws JsonProcessingException {
        JSONObject jsonObject = JSON.parseObject(data);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        AdapayCallbackRecord adapayCallbackRecord = mapper.readValue(jsonObject.toJSONString(), AdapayCallbackRecord.class);
        adapayCallbackRecord.setExpend(jsonObject.getString("expend"));
        adapayCallbackRecord.setPaymentId(jsonObject.getString("id"));

        JSONObject descJson = JSON.parseObject(adapayCallbackRecord.getDescription());
        String type = descJson.getString("type");
        adapayCallbackRecord.setPayScenario(type);
        String memberId = descJson.getString("memberId");
        adapayCallbackRecord.setMemberId(memberId);
        String orderCode = descJson.getString("orderCode");
        adapayCallbackRecord.setOrderCode(orderCode);
        BigDecimal amount = adapayCallbackRecord.getPayAmt();

        // 保存到数据库
        this.saveAdapayCallbackRecord(adapayCallbackRecord);
    }

    /**
     * 根据订单号查询支付回调信息
     * @param orderCode
     * @return
     */
    @Override
    public AdapayCallbackRecord selectByOrderCode(String orderCode) {
        if (StringUtils.isBlank(orderCode)) {
            return null;
        }
        String redisKey = CacheConstants.QUERY_ORDER_CALLBACK + orderCode;
        AdapayCallbackRecord record =  redisCache.getCacheObject(redisKey);
        if (record != null) {
            return record;
        }
        // AdapayCallbackRecord record = null;
        try {
            record = adapayCallbackRecordMapper.selectByOrderCode(orderCode);
            if (record != null) {
                redisCache.setCacheObject(redisKey, record, CacheConstants.cache_expire_time_1d);
            }
        } catch (Exception e) {
            log.error("查询汇付支付回调记录失败", e);
        }
        // log.info("查询汇付支付回调记录 orderCode:{}, result:{}", orderCode, JSON.toJSONString(record));
        return record;
    }

    @Override
    public AdapayCallbackRecord selectByPaymentId(String paymentId) {
        return adapayCallbackRecordMapper.selectByPaymentId(paymentId);
    }
}
