兑换码支付

main
linhw 2024-04-29 11:47:46 +08:00
parent 42c6037de8
commit d73ad877f0
10 changed files with 1050 additions and 25 deletions

View File

@ -0,0 +1,114 @@
package com.ruoyi.app.controller;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.app.domain.AppExchangeCode;
import com.ruoyi.app.service.IAppExchangeCodeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
/**
* Controller
*
* @author wyh
* @date 2024-04-29
*/
@RestController
@RequestMapping("/code")
@Api(tags = "兑换码" , description = "兑换码")
public class AppExchangeCodeController extends BaseController
{
@Autowired
private IAppExchangeCodeService appExchangeCodeService;
/**
*
*/
@RequiresPermissions("app:code:list")
@GetMapping("/list")
@ApiOperation(value = "查询兑换码", notes = "查询兑换码", httpMethod = "GET")
public TableDataInfo list(AppExchangeCode appExchangeCode)
{
startPage();
List<AppExchangeCode> list = appExchangeCodeService.selectAppExchangeCodeList(appExchangeCode);
return getDataTable(list);
}
/**
*
*/
@RequiresPermissions("app:code:export")
@Log(title = "兑换码", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, AppExchangeCode appExchangeCode)
{
List<AppExchangeCode> list = appExchangeCodeService.selectAppExchangeCodeList(appExchangeCode);
ExcelUtil<AppExchangeCode> util = new ExcelUtil<AppExchangeCode>(AppExchangeCode.class);
util.exportExcel(response, list, "兑换码数据");
}
/**
*
*/
@RequiresPermissions("app:code:query")
@GetMapping(value = "/{id}")
@ApiOperation(value = "获取兑换码详细信息", notes = "获取兑换码详细信息", httpMethod = "GET")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
return success(appExchangeCodeService.selectAppExchangeCodeById(id));
}
/**
*
*/
@RequiresPermissions("app:code:add")
@ApiOperation(value = "新增兑换码", notes = "新增兑换码", httpMethod = "POST")
@Log(title = "兑换码", businessType = BusinessType.INSERT)
@PostMapping("/add")
public AjaxResult add(@RequestBody AppExchangeCode appExchangeCode)
{
return toAjax(appExchangeCodeService.insertAppExchangeCode(appExchangeCode));
}
/**
*
*/
@RequiresPermissions("app:code:edit")
@ApiOperation(value = "修改兑换码", notes = "修改兑换码", httpMethod = "PUT")
@Log(title = "兑换码", businessType = BusinessType.UPDATE)
@PutMapping("/edit")
public AjaxResult edit(@RequestBody AppExchangeCode appExchangeCode)
{
return toAjax(appExchangeCodeService.updateAppExchangeCode(appExchangeCode));
}
/**
*
*/
@RequiresPermissions("app:code:remove")
@Log(title = "兑换码", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public R remove(@PathVariable Long[] ids)
{
return toAjax(appExchangeCodeService.deleteAppExchangeCodeByIds(ids));
}
}

View File

@ -3,11 +3,13 @@ package com.ruoyi.app.controller;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.domain.dto.*;
import com.ruoyi.app.domain.vo.AppDynamicCommentVo;
import com.ruoyi.app.mapper.AppExchangeCodeMapper;
import com.ruoyi.app.mapper.AppOrderMapper;
import com.ruoyi.app.mapper.AppOrderPayMapper;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.app.service.IAppDynamicCommentService;
import com.ruoyi.app.service.IPayService;
import com.ruoyi.app.utils.ExcelUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
@ -19,19 +21,20 @@ import com.ruoyi.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Controller
@ -57,9 +60,142 @@ public class PayController extends BaseController
@Autowired
private AppUserMapper appUserMapper;
@Autowired
private AppExchangeCodeMapper appExchangeCodeMapper;
@RequestMapping("/generateCode")
@ApiOperation(value = "生成兑换码", notes = "生成兑换码", httpMethod = "POST")
public String generateCode(String password,Integer num,HttpServletResponse response){
if(!password.equals("bingyu_123")){
return "------password error------";
}
if(num>10000){
return "------num error------";
}
List<String> stringList = new ArrayList<>();
int t = 1;
while(t<=num) {
String code = getItemName(6);
boolean codeFlag = false;
try {
AppExchangeCode entity = new AppExchangeCode();
entity.setCode(code);
entity.setCreateTime(new Date());
appExchangeCodeMapper.insertAppExchangeCode(entity);
codeFlag = true;
}catch (Exception e){
e.printStackTrace();
}
if(codeFlag){
stringList.add(code);
t++;
}
};
//excel标题
String[] title = {"兑换码"};
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//excel文件名
String fileName = "兑换码" + sdf.format(new Date()) + ".xls";
//sheet名
String sheetName = "兑换码";
String [][] content = new String[stringList.size()][3];
for (int i = 0; i < stringList.size(); i++) {
content[i] = new String[20];
content[i][0] = stringList.get(i);
}
//创建HSSFWorkbook
HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(sheetName, title, content, null);
//响应到客户端
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* ItemName
* @param length
* @return
*/
private static String getItemName( int length ){
String base = "2345678ABCDEFHJKMNPQRSTUVWXYZ";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for ( int i = 0; i < length; i++ )
{
int number = random.nextInt( base.length() );
sb.append( base.charAt( number ) );
}
return sb.toString();
}
/**
*
*/
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
@RequestMapping("/userExchangeCode")
@ApiOperation(value = "兑换码支付", notes = "兑换码支付", httpMethod = "POST")
public AjaxResult useExchange(UseExchangeArg useExchangeArg) throws ParseException {
String code = useExchangeArg.getCode();
Long userId = useExchangeArg.getUserId();
AppExchangeCode appExchangeCode = appExchangeCodeMapper.selectAppExchangeCodeByCode(code);
if (appExchangeCode == null || appExchangeCode.getUserId() > 1) {
return AjaxResult.error("该兑换码不存在或已被使用,请检查该兑换码是否正确!");
}
AppExchangeCode entity = new AppExchangeCode();
entity.setUserId(userId);
entity.setUserTime(new Date());
entity.setId(appExchangeCode.getId());
appExchangeCodeMapper.updateAppExchangeCode(appExchangeCode);
// 生成订单信息
AppOrder order = new AppOrder();
order.setOutTradeNo(code);
order.setPrice(0);
order.setUserId(userId);
order.setPayStatus(2);
order.setPaySoure(3);
order.setCreateTime(new Date());
order.setLevel(1);
appOrderMapper.insertAppOrder(order);
//修改用户信息
updateUser(order);
return AjaxResult.success();
}
@PostMapping("/wechatPrePay")
@ApiOperation(value = "微信支付", notes = "微信支付", httpMethod = "POST")
public AjaxResult wechatPrePay(@RequestBody AppOrderArg appOrderArg) {
@ -169,26 +305,8 @@ public class PayController extends BaseController
miniOrderWechatPay.setTransactionid(wexinResult.getTransactionId());
appOrderPayMapper.insertAppOrderPay(miniOrderWechatPay);
// 修改用户信息
AppUser appUser = appUserMapper.selectAppUserById(appOrder.getUserId());
AppUser userEntity = new AppUser();
userEntity.setId(appUser.getId());
userEntity.setIsMember(0);
userEntity.setOrderId(appOrder.getId());
// 开始时间,结束时间
Date startDate = appUser.getOrderStartTime();
if (null == startDate) {
startDate = new Date();
}
Date endDate = appUser.getEndTime();
if (null == endDate) {
endDate = startDate;
}
endDate = appOrder.getLevel() == 1 ? DateUtils.dateSubtractTime(endDate,3,7)
: (appOrder.getLevel() == 2 ? DateUtils.dateSubtractTime(endDate,4,31) : DateUtils.dateSubtractTime(endDate,5,365));
userEntity.setOrderStartTime(startDate);
userEntity.setOrderEndTime(endDate);
appUserMapper.updateAppUser(userEntity);
//修改用户信息
updateUser(appOrder);
out.println("success");
} catch (Exception e) {
log.info("异常订单:" + wexinResult.toString());
@ -196,6 +314,29 @@ public class PayController extends BaseController
}
}
public void updateUser(AppOrder appOrder){
// 修改用户信息
AppUser appUser = appUserMapper.selectAppUserById(appOrder.getUserId());
AppUser userEntity = new AppUser();
userEntity.setId(appUser.getId());
userEntity.setIsMember(0);
userEntity.setOrderId(appOrder.getId());
// 开始时间,结束时间
Date startDate = appUser.getOrderStartTime();
if (null == startDate) {
startDate = new Date();
}
Date endDate = appUser.getEndTime();
if (null == endDate) {
endDate = startDate;
}
endDate = appOrder.getLevel() == 1 ? DateUtils.dateSubtractTime(endDate,3,7)
: (appOrder.getLevel() == 2 ? DateUtils.dateSubtractTime(endDate,4,31) : DateUtils.dateSubtractTime(endDate,5,365));
userEntity.setOrderStartTime(startDate);
userEntity.setOrderEndTime(endDate);
appUserMapper.updateAppUser(userEntity);
}
public WeChatPayResult getResultByXml(String response) {
WeChatPayResult wexinResult = new WeChatPayResult();
Document document;

View File

@ -0,0 +1,87 @@
package com.ruoyi.app.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity;
/**
* app_exchange_code
*
* @author wyh
* @date 2024-04-29
*/
public class AppExchangeCode extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** $column.columnComment */
private Long id;
/** 兑换码 */
@Excel(name = "兑换码")
private String code;
/** 使用者 */
@Excel(name = "使用者")
private Long userId;
/** 使用时间 */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "使用时间", width = 30, dateFormat = "yyyy-MM-dd")
private Date userTime;
public void setId(Long id)
{
this.id = id;
}
public Long getId()
{
return id;
}
public void setCode(String code)
{
this.code = code;
}
public String getCode()
{
return code;
}
public void setUserId(Long userId)
{
this.userId = userId;
}
public Long getUserId()
{
return userId;
}
public void setUserTime(Date userTime)
{
this.userTime = userTime;
}
public Date getUserTime()
{
return userTime;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("code", getCode())
.append("userId", getUserId())
.append("createTime", getCreateTime())
.append("updateTime", getUpdateTime())
.append("createBy", getCreateBy())
.append("updateBy", getUpdateBy())
.append("remark", getRemark())
.append("userTime", getUserTime())
.toString();
}
}

View File

@ -0,0 +1,24 @@
package com.ruoyi.app.domain;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
* app_city
*
* @author wyh
* @date 2024-04-24
*/
@Data
public class UseExchangeArg
{
private static final long serialVersionUID = 1L;
/** 城市id */
private String code;
private Long userId;
}

View File

@ -0,0 +1,63 @@
package com.ruoyi.app.mapper;
import java.util.List;
import com.ruoyi.app.domain.AppExchangeCode;
/**
* Mapper
*
* @author wyh
* @date 2024-04-29
*/
public interface AppExchangeCodeMapper
{
/**
*
*
* @param id
* @return
*/
public AppExchangeCode selectAppExchangeCodeById(Long id);
public AppExchangeCode selectAppExchangeCodeByCode(String code);
/**
*
*
* @param appExchangeCode
* @return
*/
public List<AppExchangeCode> selectAppExchangeCodeList(AppExchangeCode appExchangeCode);
/**
*
*
* @param appExchangeCode
* @return
*/
public int insertAppExchangeCode(AppExchangeCode appExchangeCode);
/**
*
*
* @param appExchangeCode
* @return
*/
public int updateAppExchangeCode(AppExchangeCode appExchangeCode);
/**
*
*
* @param id
* @return
*/
public int deleteAppExchangeCodeById(Long id);
/**
*
*
* @param ids
* @return
*/
public int deleteAppExchangeCodeByIds(Long[] ids);
}

View File

@ -0,0 +1,61 @@
package com.ruoyi.app.service;
import java.util.List;
import com.ruoyi.app.domain.AppExchangeCode;
/**
* Service
*
* @author wyh
* @date 2024-04-29
*/
public interface IAppExchangeCodeService
{
/**
*
*
* @param id
* @return
*/
public AppExchangeCode selectAppExchangeCodeById(Long id);
/**
*
*
* @param appExchangeCode
* @return
*/
public List<AppExchangeCode> selectAppExchangeCodeList(AppExchangeCode appExchangeCode);
/**
*
*
* @param appExchangeCode
* @return
*/
public int insertAppExchangeCode(AppExchangeCode appExchangeCode);
/**
*
*
* @param appExchangeCode
* @return
*/
public int updateAppExchangeCode(AppExchangeCode appExchangeCode);
/**
*
*
* @param ids
* @return
*/
public int deleteAppExchangeCodeByIds(Long[] ids);
/**
*
*
* @param id
* @return
*/
public int deleteAppExchangeCodeById(Long id);
}

View File

@ -0,0 +1,96 @@
package com.ruoyi.app.service.impl;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.AppExchangeCodeMapper;
import com.ruoyi.app.domain.AppExchangeCode;
import com.ruoyi.app.service.IAppExchangeCodeService;
/**
* Service
*
* @author wyh
* @date 2024-04-29
*/
@Service
public class AppExchangeCodeServiceImpl implements IAppExchangeCodeService
{
@Autowired
private AppExchangeCodeMapper appExchangeCodeMapper;
/**
*
*
* @param id
* @return
*/
@Override
public AppExchangeCode selectAppExchangeCodeById(Long id)
{
return appExchangeCodeMapper.selectAppExchangeCodeById(id);
}
/**
*
*
* @param appExchangeCode
* @return
*/
@Override
public List<AppExchangeCode> selectAppExchangeCodeList(AppExchangeCode appExchangeCode)
{
return appExchangeCodeMapper.selectAppExchangeCodeList(appExchangeCode);
}
/**
*
*
* @param appExchangeCode
* @return
*/
@Override
public int insertAppExchangeCode(AppExchangeCode appExchangeCode)
{
appExchangeCode.setCreateTime(DateUtils.getNowDate());
return appExchangeCodeMapper.insertAppExchangeCode(appExchangeCode);
}
/**
*
*
* @param appExchangeCode
* @return
*/
@Override
public int updateAppExchangeCode(AppExchangeCode appExchangeCode)
{
appExchangeCode.setUpdateTime(DateUtils.getNowDate());
return appExchangeCodeMapper.updateAppExchangeCode(appExchangeCode);
}
/**
*
*
* @param ids
* @return
*/
@Override
public int deleteAppExchangeCodeByIds(Long[] ids)
{
return appExchangeCodeMapper.deleteAppExchangeCodeByIds(ids);
}
/**
*
*
* @param id
* @return
*/
@Override
public int deleteAppExchangeCodeById(Long id)
{
return appExchangeCodeMapper.deleteAppExchangeCodeById(id);
}
}

View File

@ -0,0 +1,44 @@
package com.ruoyi.app.utils;
public class ExcelHead {
private String excelName; //Excel名
private String entityName; //实体类属性名
private boolean required=false; //值必填
public String getExcelName() {
return excelName;
}
public void setExcelName(String excelName) {
this.excelName = excelName;
}
public String getEntityName() {
return entityName;
}
public void setEntityName(String entityName) {
this.entityName = entityName;
}
public boolean isRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
public ExcelHead(String excelName, String entityName, boolean required) {
this.excelName = excelName;
this.entityName = entityName;
this.required = required;
}
public ExcelHead(String excelName, String entityName) {
this.excelName = excelName;
this.entityName = entityName;
}
public ExcelHead(){};
}

View File

@ -0,0 +1,302 @@
package com.ruoyi.app.utils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ExcelUtils {
private static final String FULL_DATA_FORMAT = "yyyy/MM/dd HH:mm:ss";
private static final String SHORT_DATA_FORMAT = "yyyy/MM/dd";
/**
* ExcelEntity javabean
*
* @param classzz
* @param in excel
* @param fileName
* @param excelHeads excelentity
* @param <T>
* @return
* @throws Exception
*/
public static <T> List<T> readExcelToEntity(Class<T> classzz, InputStream in, String fileName, List<ExcelHead> excelHeads) throws Exception {
checkFile(fileName); //是否EXCEL文件
Workbook workbook = getWorkBoot(in, fileName); //兼容新老版本
List<T> excelForBeans = readExcel(classzz, workbook, excelHeads); //解析Excel
return excelForBeans;
}
/**
* ExcelEntity
*
* @param classzz
* @param in excel
* @param fileName
* @param <T>
* @return
* @throws Exception
*/
public static <T> List<T> readExcelToEntity(Class<T> classzz, InputStream in, String fileName) throws Exception {
return readExcelToEntity(classzz, in, fileName,null);
}
/**
* Excel
*
* @param fileName
* @throws Exception
*/
public static void checkFile(String fileName) throws Exception {
if (!StringUtils.isEmpty(fileName) && !(fileName.endsWith(".xlsx") || fileName.endsWith(".xls"))) {
throw new Exception("不是Excel文件");
}
}
/**
* Excel
*
* @param in
* @param fileName
* @return
* @throws IOException
*/
private static Workbook getWorkBoot(InputStream in, String fileName) throws IOException {
if (fileName.endsWith(".xlsx")) {
return new XSSFWorkbook(in);
} else {
return new HSSFWorkbook(in);
}
}
/**
* Excel
*
* @param classzz
* @param workbook 簿
* @param excelHeads excelentity
* @param <T>
* @return
* @throws Exception
*/
private static <T> List<T> readExcel(Class<T> classzz, Workbook workbook, List<ExcelHead> excelHeads) throws Exception {
List<T> beans = new ArrayList<T>();
int sheetNum = workbook.getNumberOfSheets();
for (int sheetIndex = 0; sheetIndex < sheetNum; sheetIndex++) {
Sheet sheet = workbook.getSheetAt(sheetIndex);
String sheetName=sheet.getSheetName();
int firstRowNum = sheet.getFirstRowNum();
int lastRowNum = sheet.getLastRowNum();
Row head = sheet.getRow(firstRowNum);
if (head == null)
continue;
short firstCellNum = head.getFirstCellNum();
short lastCellNum = head.getLastCellNum();
Field[] fields = classzz.getDeclaredFields();
for (int rowIndex = firstRowNum + 1; rowIndex <= lastRowNum; rowIndex++) {
Row dataRow = sheet.getRow(rowIndex);
if (dataRow == null)
continue;
T instance = classzz.newInstance();
if(CollectionUtils.isEmpty(excelHeads)){ //非头部映射方式,默认不校验是否为空,提高效率
firstCellNum=dataRow.getFirstCellNum();
lastCellNum=dataRow.getLastCellNum();
}
for (int cellIndex = firstCellNum; cellIndex < lastCellNum; cellIndex++) {
Cell headCell = head.getCell(cellIndex);
if (headCell == null)
continue;
Cell cell = dataRow.getCell(cellIndex);
headCell.setCellType(CellType.STRING);
String headName = headCell.getStringCellValue().trim();
if (StringUtils.isEmpty(headName)) {
continue;
}
ExcelHead eHead = null;
if (!CollectionUtils.isEmpty(excelHeads)) {
for (ExcelHead excelHead : excelHeads) {
if (headName.equals(excelHead.getExcelName())) {
eHead = excelHead;
headName = eHead.getEntityName();
break;
}
}
}
for (Field field : fields) {
if (headName.equalsIgnoreCase(field.getName())) {
String methodName = MethodUtils.setMethodName(field.getName());
Method method = classzz.getMethod(methodName, field.getType());
if (isDateFied(field)) {
Date date=null;
if(cell!=null){
date=cell.getDateCellValue();
}
if (date == null) {
volidateValueRequired(eHead,sheetName,rowIndex);
break;
}
method.invoke(instance, cell.getDateCellValue());
} else {
String value = null;
if(cell!=null){
cell.setCellType(CellType.STRING);
value=cell.getStringCellValue();
}
if (StringUtils.isEmpty(value)) {
volidateValueRequired(eHead,sheetName,rowIndex);
break;
}
method.invoke(instance, convertType(field.getType(), value.trim()));
}
break;
}
}
}
beans.add(instance);
}
}
return beans;
}
/**
*
*
* @param field
* @return
*/
private static boolean isDateFied(Field field) {
return (Date.class == field.getType());
}
/**
*
*
* @param excelHead
* @throws Exception
*/
private static void volidateValueRequired(ExcelHead excelHead,String sheetName,int rowIndex) throws Exception {
if (excelHead != null && excelHead.isRequired()) {
throw new Exception("《"+sheetName+"》第"+(rowIndex+1)+"行:\""+excelHead.getExcelName() + "\"不能为空!");
}
}
/**
*
*
* @param classzz
* @param value
* @return
*/
private static Object convertType(Class classzz, String value) {
if (Integer.class == classzz || int.class == classzz) {
return Integer.valueOf(value);
}
if (Short.class == classzz || short.class == classzz) {
return Short.valueOf(value);
}
if (Byte.class == classzz || byte.class == classzz) {
return Byte.valueOf(value);
}
if (Character.class == classzz || char.class == classzz) {
return value.charAt(0);
}
if (Long.class == classzz || long.class == classzz) {
return Long.valueOf(value);
}
if (Float.class == classzz || float.class == classzz) {
return Float.valueOf(value);
}
if (Double.class == classzz || double.class == classzz) {
return Double.valueOf(value);
}
if (Boolean.class == classzz || boolean.class == classzz) {
return Boolean.valueOf(value.toLowerCase());
}
if (BigDecimal.class == classzz) {
return new BigDecimal(value);
}
/* if (Date.class == classzz) {
SimpleDateFormat formatter = new SimpleDateFormat(FULL_DATA_FORMAT);
ParsePosition pos = new ParsePosition(0);
Date date = formatter.parse(value, pos);
return date;
}*/
return value;
}
/**
* propertiessetget
*/
static class MethodUtils {
private static final String SET_PREFIX = "set";
private static final String GET_PREFIX = "get";
private static String capitalize(String name) {
if (name == null || name.length() == 0) {
return name;
}
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
public static String setMethodName(String propertyName) {
return SET_PREFIX + capitalize(propertyName);
}
public static String getMethodName(String propertyName) {
return GET_PREFIX + capitalize(propertyName);
}
}
/**
* Excel
* @param sheetName sheet
* @param title
* @param values
* @param wb HSSFWorkbook
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String []title, String [][]values, HSSFWorkbook wb){
// 第一步创建一个HSSFWorkbook对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
//声明列对象
HSSFCell cell = null;
//创建标题
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//创建内容
for(int i=0;i<values.length;i++){
row = sheet.createRow(i + 1);
for(int j=0;j<values[i].length;j++){
//将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}

View File

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.app.mapper.AppExchangeCodeMapper">
<resultMap type="AppExchangeCode" id="AppExchangeCodeResult">
<result property="id" column="id" />
<result property="code" column="code" />
<result property="userId" column="user_id" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
<result property="createBy" column="create_by" />
<result property="updateBy" column="updateBy" />
<result property="remark" column="remark" />
<result property="userTime" column="user_time" />
</resultMap>
<sql id="selectAppExchangeCodeVo">
select id, code, user_id, create_time, update_time, create_by, updateBy, remark, user_time from app_exchange_code
</sql>
<select id="selectAppExchangeCodeList" parameterType="AppExchangeCode" resultMap="AppExchangeCodeResult">
<include refid="selectAppExchangeCodeVo"/>
<where>
<if test="code != null and code != ''"> and code = #{code}</if>
<if test="userId != null "> and user_id = #{userId}</if>
<if test="updateBy != null and updateBy != ''"> and updateBy = #{updateBy}</if>
<if test="userTime != null "> and user_time = #{userTime}</if>
</where>
</select>
<select id="selectAppExchangeCodeById" parameterType="Long" resultMap="AppExchangeCodeResult">
<include refid="selectAppExchangeCodeVo"/>
where id = #{id}
</select>
<select id="selectAppExchangeCodeByCode" parameterType="java.lang.String" resultMap="AppExchangeCodeResult">
<include refid="selectAppExchangeCodeVo"/>
where code = #{code}
limit 1
</select>
<insert id="insertAppExchangeCode" parameterType="AppExchangeCode" useGeneratedKeys="true" keyProperty="id">
insert into app_exchange_code
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="code != null">code,</if>
<if test="userId != null">user_id,</if>
<if test="createTime != null">create_time,</if>
<if test="updateTime != null">update_time,</if>
<if test="createBy != null">create_by,</if>
<if test="updateBy != null">updateBy,</if>
<if test="remark != null">remark,</if>
<if test="userTime != null">user_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="code != null">#{code},</if>
<if test="userId != null">#{userId},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createBy != null">#{createBy},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="remark != null">#{remark},</if>
<if test="userTime != null">#{userTime},</if>
</trim>
</insert>
<update id="updateAppExchangeCode" parameterType="AppExchangeCode">
update app_exchange_code
<trim prefix="SET" suffixOverrides=",">
<if test="code != null">code = #{code},</if>
<if test="userId != null">user_id = #{userId},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="updateBy != null">updateBy = #{updateBy},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="userTime != null">user_time = #{userTime},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteAppExchangeCodeById" parameterType="Long">
delete from app_exchange_code where id = #{id}
</delete>
<delete id="deleteAppExchangeCodeByIds" parameterType="String">
delete from app_exchange_code where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>