Commit a1ea745a authored by 技术总监-万宁's avatar 技术总监-万宁

增加初始化内容

parent 7e024c9c
package com.ssy.lingxi.pay.config;
import com.ssy.lingxi.pay.service.IPayDataInitService;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* 服务启动后,执行数据初始化
* @author 万宁
* @version 2.0.0
* @date 2021-06-15
*/
@Configuration
public class ServiceInitConfig {
@Resource
private IPayDataInitService payDataInitService;
@PostConstruct
public void systemInit() {
//初始化支付服务数据
payDataInitService.initPayServiceData();
}
}
......@@ -20,7 +20,7 @@ public class PayWay {
*/
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE,generator="pay_pay_way_seq")
@SequenceGenerator(sequenceName="pay_pay_pay_way_seq", name="pay_pay_way_seq",allocationSize = 1)
@SequenceGenerator(sequenceName="pay_pay_way_seq", name="pay_pay_way_seq",allocationSize = 1)
private Long id;
/**
......
......@@ -18,7 +18,7 @@ public class PlatformPayWay {
*/
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE,generator="pay_platform_pay_way_seq")
@SequenceGenerator(sequenceName="pay_pay_platform_pay_way_seq", name="pay_platform_pay_way_seq",allocationSize = 1)
@SequenceGenerator(sequenceName="pay_platform_pay_way_seq", name="pay_platform_pay_way_seq",allocationSize = 1)
private Long id;
/**
......
package com.ssy.lingxi.pay.service;
/**
* 服务数据初始化
* @author 万宁
* @version 2.0.0
* @date 2021-06-15
*/
public interface IPayDataInitService {
/**
* 初始化支付服务数据
*/
void initPayServiceData();
}
package com.ssy.lingxi.pay.serviceimpl;
import com.ssy.lingxi.pay.entity.PayWay;
import com.ssy.lingxi.pay.entity.PlatformPayWay;
import com.ssy.lingxi.pay.repository.PayWayRepository;
import com.ssy.lingxi.pay.repository.PlatformPayWayRepository;
import com.ssy.lingxi.pay.service.IPayDataInitService;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* 服务数据初始化
* @author 万宁
* @version 2.0.0
* @date 2021-06-15
*/
@Service
public class PayDataInitServiceImpl implements IPayDataInitService {
@Resource
private JdbcTemplate jdbcTemplate;
@Resource
private PayWayRepository payWayRepository;
@Resource
private PlatformPayWayRepository platformPayWayRepository;
/**
* 初始化支付服务数据
*/
@Override
public void initPayServiceData() {
//支付方式
initPayWay();
//平台支付方式
initPlatformPayWay();
}
/**
* 初始化支付方式
*/
private void initPayWay() {
if(countTable("pay_pay_way") > 0) {
return;
}
alterSequence("pay_pay_way_seq");
List<PayWay> payWayList = new ArrayList<>();
PayWay payWay = new PayWay();
payWay.setIsPitchOn(1);
payWay.setPayType(1);
payWay.setWay("支付宝");
payWayList.add(payWay);
payWay = new PayWay();
payWay.setIsPitchOn(1);
payWay.setPayType(1);
payWay.setWay("微信");
payWayList.add(payWay);
payWay = new PayWay();
payWay.setIsPitchOn(1);
payWay.setPayType(1);
payWay.setWay("银联");
payWayList.add(payWay);
payWay = new PayWay();
payWay.setIsPitchOn(1);
payWay.setPayType(1);
payWay.setWay("余额支付");
payWayList.add(payWay);
payWay = new PayWay();
payWay.setIsPitchOn(1);
payWay.setPayType(2);
payWay.setWay("线下支付线上确认");
payWayList.add(payWay);
payWay = new PayWay();
payWay.setIsPitchOn(1);
payWay.setPayType(3);
payWay.setWay("授信额度支付");
payWayList.add(payWay);
payWay = new PayWay();
payWay.setIsPitchOn(1);
payWay.setPayType(4);
payWay.setWay("货到付款");
payWayList.add(payWay);
payWayRepository.saveAll(payWayList);
}
/**
* 初始化平台支付方式
*/
private void initPlatformPayWay() {
if(countTable("pay_platform_pay_way") > 0) {
return;
}
List<PayWay> payWayList = payWayRepository.findAll(Sort.by("id").ascending());
if(CollectionUtils.isEmpty(payWayList)) {
return;
}
alterSequence("pay_platform_pay_way_seq");
List<PlatformPayWay> platformPayWayList = new ArrayList<>();
for (PayWay payWay : payWayList) {
PlatformPayWay platformPayWay = new PlatformPayWay();
platformPayWay.setIsPitchOn(payWay.getIsPitchOn());
platformPayWay.setPayType(payWay.getPayType());
platformPayWay.setWay(payWay.getWay());
platformPayWay.setPayWayId(payWay.getId());
platformPayWayList.add(platformPayWay);
}
platformPayWayRepository.saveAll(platformPayWayList);
}
/**
* 执行 select count(id) from table 语句
* @param dbTableName 数据库的表名称
* @return 表数据行数
*/
private long countTable(String dbTableName) {
String sql = "select count(id) as total from " + dbTableName + ";";
SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet(sql);
long total = 0;
while (sqlRowSet.next()) {
total = sqlRowSet.getLong("total");
}
return total;
}
/**
* 更改序列值为1
* @param sequenceName 序列名称
*/
private void alterSequence(String sequenceName) {
String sql = "alter sequence " + sequenceName + " restart with 1";
jdbcTemplate.execute(sql);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment