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

matrix.business.pay.service.impl.MatrixPayServiceImpl 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.builder.response.QueryPayResponse;
import matrix.business.pay.constants.PayConstant;
import matrix.business.pay.entity.MatrixBusinessPayEntity;
import matrix.business.pay.service.MatrixPayService;
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.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void savePayEntity(MatrixBusinessPayEntity payEntity) {
        String createTime = DateUtil.formatDateStrByPattern(payEntity.getCreateTime() == null ? new Date() : payEntity.getCreateTime(), DateUtil.TimeFormat.LongTimeStandard);
        String updateTime = DateUtil.formatDateStrByPattern(payEntity.getUpdateTime() == null ? new Date() : payEntity.getUpdateTime(), DateUtil.TimeFormat.LongTimeStandard);
        jdbcTemplate.update(SqlUtil.getSql("insert_pay.sql"), payEntity.getTypeCode(), payEntity.getPayId(), payEntity.getOrderId(),
                payEntity.getPrice(), payEntity.getBody(), payEntity.getUrl(),
                payEntity.getActualPrice(), payEntity.getOutTradeNo(), payEntity.getPayMode(), payEntity.getPayChannel(),
                payEntity.getOrderBy(), createTime, updateTime, payEntity.getStatus());
    }

    @Override
    public List changePayed(List queryPayResponses) {
        Assert.state(!CollectionUtils.isEmpty(queryPayResponses), "queryPayResponses not be null");
        Object[] payIds = new Object[queryPayResponses.size()];
        List payIdsPlaceholder = new ArrayList<>();
        List params = new ArrayList<>();
        for (int i = 0; i < queryPayResponses.size(); i++) {
            payIdsPlaceholder.add("?");
            QueryPayResponse queryPayResponse = queryPayResponses.get(i);
            payIds[i] = queryPayResponse.getPayId();
            Object[] param = new Object[]{queryPayResponse.getOutTradeNo(),
                    BigDecimal.valueOf(Double.parseDouble(queryPayResponse.getActualPrice())).setScale(2, RoundingMode.HALF_UP),
                    PayConstant.PAYED, queryPayResponse.getPayId(), PayConstant.NO_PAY};
            params.add(param);
        }
        jdbcTemplate.batchUpdate(SqlUtil.getSql("update_pay.sql"), params);
        String querySql = String.format("%s WHERE pay_id IN (%s)", SqlUtil.getSql("select_pay.sql"), String.join(",", payIdsPlaceholder));
        return jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(MatrixBusinessPayEntity.class), payIds);
    }

    @Override
    public MatrixBusinessPayEntity getPayEntityByPayId(String payId) {
        List list = jdbcTemplate.query(String.format("%s WHERE pay_id = ?", SqlUtil.getSql("select_pay.sql")),
                new BeanPropertyRowMapper<>(MatrixBusinessPayEntity.class), payId);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public List getPayEntityByPayed(String orderId, String payChannel) {
        return jdbcTemplate.query(String.format("%s WHERE order_id = ? AND pay_channel = ? AND status = ?", SqlUtil.getSql("select_pay.sql")),
                new BeanPropertyRowMapper<>(MatrixBusinessPayEntity.class),
                orderId, payChannel, PayConstant.PAYED);
    }

    @Override
    public List getPayEntityByPayedForOutTradeNo(String outTradeNo, String payChannel) {
        return jdbcTemplate.query(String.format("%s WHERE out_trade_no = ? AND pay_channel = ? AND status = ?", SqlUtil.getSql("select_pay.sql")),
                new BeanPropertyRowMapper<>(MatrixBusinessPayEntity.class), outTradeNo, payChannel, PayConstant.PAYED);
    }

    @Override
    public List getPayEntityByNoPay(String orderId, String payChannel) {
        return jdbcTemplate.query(String.format("%s WHERE order_id = ? AND pay_channel = ? AND status = ?", SqlUtil.getSql("select_pay.sql")),
                new BeanPropertyRowMapper<>(MatrixBusinessPayEntity.class),
                orderId, payChannel, PayConstant.NO_PAY);
    }

    @Override
    public String getTypeCode(String payId) {
        MatrixBusinessPayEntity payEntity = getPayEntityByPayId(payId);
        AssertUtil.state(payEntity != null, String.format("%s typeCode not exists", payId));
        assert payEntity != null;
        return payEntity.getTypeCode();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy