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.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.jsowell.common.util.DateUtils;
import com.jsowell.pile.domain.AdapayCallbackRecord;
import com.jsowell.pile.domain.AdapayRefundRecord;
import com.jsowell.pile.mapper.AdapayRefundRecordMapper;
import com.jsowell.pile.service.AdapayCallbackRecordService;
import com.jsowell.pile.service.IAdapayRefundRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author jsowell
 * @date 2023-05-31
 */
@Service
public class AdapayRefundRecordServiceImpl implements IAdapayRefundRecordService {
    @Autowired
    private AdapayRefundRecordMapper adapayRefundRecordMapper;

    @Autowired
    private AdapayCallbackRecordService adapayCallbackRecordService;
    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public AdapayRefundRecord selectAdapayRefundRecordById(Integer id) {
        return adapayRefundRecordMapper.selectAdapayRefundRecordById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param adapayRefundRecord 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AdapayRefundRecord> selectAdapayRefundRecordList(AdapayRefundRecord adapayRefundRecord) {
        return adapayRefundRecordMapper.selectAdapayRefundRecordList(adapayRefundRecord);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param adapayRefundRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAdapayRefundRecord(AdapayRefundRecord adapayRefundRecord) {
        adapayRefundRecord.setCreateTime(DateUtils.getNowDate());
        return adapayRefundRecordMapper.insertAdapayRefundRecord(adapayRefundRecord);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param adapayRefundRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAdapayRefundRecord(AdapayRefundRecord adapayRefundRecord) {
        return adapayRefundRecordMapper.updateAdapayRefundRecord(adapayRefundRecord);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdapayRefundRecordByIds(Integer[] ids) {
        return adapayRefundRecordMapper.deleteAdapayRefundRecordByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdapayRefundRecordById(Integer id) {
        return adapayRefundRecordMapper.deleteAdapayRefundRecordById(id);
    }

    @Override
    public void saveAdapayRefundRecord(String data) throws JsonProcessingException {
        JSONObject jsonObject = JSON.parseObject(data);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        AdapayRefundRecord adapayRefundRecord = mapper.readValue(jsonObject.toJSONString(), AdapayRefundRecord.class);
        adapayRefundRecord.setRefundId(jsonObject.getString("id"));

        AdapayCallbackRecord adapayCallbackRecord = adapayCallbackRecordService.selectByPaymentId(adapayRefundRecord.getPaymentId());
        if (adapayCallbackRecord != null) {
            adapayRefundRecord.setMemberId(adapayCallbackRecord.getMemberId());
            adapayRefundRecord.setOrderCode(adapayCallbackRecord.getOrderCode());
        }

        // 保存到数据库
        this.insertAdapayRefundRecord(adapayRefundRecord);
    }
}
