package com.ruoyi.app.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PoseOrderZalopay;
import com.ruoyi.system.service.IPoseOrderZalopayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/*
 * ZaloPay通用方法
 * @Description:
 */
@Component
public class ZaloPayCommonService {

    @Autowired
    private IPoseOrderZalopayService zalopayService;
    /**
     *  记录ZaloPay支付信息
     */

    public void setZaloPayOrder(String ddId,String createResponse,String callbackResponse,String refundResponse,String mRefundId,String payType) {
        PoseOrderZalopay poseOrderZalopay =zalopayService.getOne(new QueryWrapper<PoseOrderZalopay>().eq("dd_id", ddId));
        PoseOrderZalopay zaloResopen = new PoseOrderZalopay();
        if(StringUtils.isNotEmpty(createResponse)){
            zaloResopen.setCreateResponse(createResponse);
            zaloResopen.setCreateResponseTime(new Date());
        }
        if(StringUtils.isNotEmpty(callbackResponse)){
            zaloResopen.setCallbackResponse(callbackResponse);
            zaloResopen.setCallbackResponseTime(new Date());
        }
        if(StringUtils.isNotEmpty(refundResponse)){
            zaloResopen.setRefundResponse(refundResponse);
            zaloResopen.setRefundResponseTime(new Date());
        }
        if(StringUtils.isNotEmpty(mRefundId)){
            zaloResopen.setMRefundId(mRefundId);
        }
        if(StringUtils.isNotEmpty(payType)){
            zaloResopen.setPayType(payType);
        }
        if(poseOrderZalopay==null){
            zaloResopen.setDdId(ddId);
            zalopayService.save(zaloResopen);
        }else{
            zaloResopen.setId(poseOrderZalopay.getId());
            zalopayService.updateById(zaloResopen);
        }
    }

}
