All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.business.pay.service.impl.MatrixRefundServiceImpl Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.business.pay.service.impl;

import matrix.boot.common.utils.AssertUtil;
import matrix.boot.jdbc.annotation.TargetDataSource;
import matrix.business.pay.constants.PayConstant;
import matrix.business.pay.entity.MatrixBusinessRefundEntity;
import matrix.business.pay.service.MatrixRefundService;
import matrix.business.common.utils.DateUtil;
import matrix.business.common.utils.SqlUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author WangCheng
 */
@Service
@TargetDataSource("${matrix.business.pay.db}")
public class MatrixRefundServiceImpl implements MatrixRefundService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void saveRefundEntity(MatrixBusinessRefundEntity refundEntity) {
        String createTime = DateUtil.formatDateStrByPattern(refundEntity.getCreateTime() == null ? new Date() : refundEntity.getCreateTime(), DateUtil.TimeFormat.LongTimeStandard);
        String updateTime = DateUtil.formatDateStrByPattern(refundEntity.getUpdateTime() == null ? new Date() : refundEntity.getUpdateTime(), DateUtil.TimeFormat.LongTimeStandard);
        jdbcTemplate.update(SqlUtil.getSql("insert_refund.sql"), refundEntity.getTypeCode(), refundEntity.getRefundId(), refundEntity.getOrderId(),
                refundEntity.getPayId(), refundEntity.getRefundPrice(),
                refundEntity.getOrderBy(), createTime, updateTime, refundEntity.getStatus());
    }

    @Override
    public List changeRefundStatus(List refundIds, Integer originRefundStatus, Integer newRefundStatus) {
        Assert.state(!CollectionUtils.isEmpty(refundIds), "refundIds not be null");
        List refundIdsPlaceholder = new ArrayList<>();
        for (int i = 0; i < refundIds.size(); i++) {
            refundIdsPlaceholder.add("?");
        }
        List params = new ArrayList<>();
        params.add(newRefundStatus);
        params.addAll(refundIds);
        params.add(originRefundStatus);
        jdbcTemplate.update(String.format("%s WHERE refund_id IN (%s) AND status = ?",
                SqlUtil.getSql("update_refund.sql"), String.join(",", refundIdsPlaceholder)), params.toArray());
        String querySql = String.format("%s WHERE refund_id IN (%s)",
                SqlUtil.getSql("select_refund.sql"), String.join(",", refundIdsPlaceholder));
        return jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(MatrixBusinessRefundEntity.class), refundIds.toArray());
    }

    @Override
    public List getRefundEntityByNoRefund(String payId) {
        String querySql = String.format("%s WHERE pay_id = ? AND status != ?", SqlUtil.getSql("select_refund.sql"));
        return jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(MatrixBusinessRefundEntity.class),
                payId, PayConstant.REFUNDED);
    }

    @Override
    public List getRefundEntityByRefunded(String payId) {
        String querySql = String.format("%s WHERE pay_id = ? AND status = ?", SqlUtil.getSql("select_refund.sql"));
        return jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(MatrixBusinessRefundEntity.class),
                payId, PayConstant.REFUNDED);
    }

    @Override
    public MatrixBusinessRefundEntity getRefundEntityByRefundId(String refundId) {
        String querySql = String.format("%s WHERE refund_id = ? AND status != ?", SqlUtil.getSql("select_refund.sql"));
        List result = jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(MatrixBusinessRefundEntity.class),
                refundId, PayConstant.REFUNDED);
        return CollectionUtils.isEmpty(result) ? null : result.get(0);
    }

    @Override
    public String getTypeCode(String refundId) {
        MatrixBusinessRefundEntity refundEntity = getRefundEntityByRefundId(refundId);
        AssertUtil.state(refundEntity != null, String.format("%s typeCode not exists", refundId));
        assert refundEntity != null;
        return refundEntity.getTypeCode();
    }
}