债务表单+二维码生成(联调暂没测试)

master
yqy 2 years ago
parent cab4585959
commit 06c1e5489a

@ -156,6 +156,18 @@
<artifactId>alipay-easysdk</artifactId>
</dependency>
<!-- 谷歌二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<!-- 配置插件 -->

@ -1,11 +1,17 @@
package com.baiye.modules.system.domain;
import com.baiye.model.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
*
@ -17,7 +23,8 @@ import javax.validation.constraints.NotBlank;
@Entity
@Table(name = "tb_debt_form")
@ApiModel(value = "债务表单表")
public class Debt {
@EntityListeners(AuditingEntityListener.class)
public class Debt implements Serializable {
private static final long serialVersionUID = 8623354712013889005L;
@ -25,6 +32,7 @@ public class Debt {
@ApiModelProperty(value = "ID")
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull(groups = BaseEntity.Update.class)
private Long id;
@ApiModelProperty(value = "邀请用户id")
@ -50,4 +58,26 @@ public class Debt {
@NotBlank(message = "请选择额度")
@Column(name = "quota")
private Integer quota;
@ApiModelProperty(value = "创建人")
@Column(name = "create_by")
private Long createBy;
@ApiModelProperty(value = "创建时间")
@Column(name = "create_time")
@CreationTimestamp
private java.util.Date createTime;
@ApiModelProperty(value = "更新时间")
@Column(name = "update_time")
@LastModifiedDate
private java.util.Date updateTime;
@ApiModelProperty(value = "版本号")
@Transient
private Long optimisticVersion;
@ApiModelProperty(value = "token")
@Transient
private String userToken;
}

@ -25,7 +25,6 @@ public class FormUser extends BaseEntity implements Serializable {
@Id
@Column(name = "id")
@NotNull(groups = BaseEntity.Update.class)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(value = "ID", hidden = true)
private Long id;
@ -45,4 +44,8 @@ public class FormUser extends BaseEntity implements Serializable {
@ApiModelProperty(value = "最后登录时间")
private Date lastLoginTime;
@Column(name = "optimistic_version")
@ApiModelProperty(value = "版本号")
private Long optimisticVersion;
}

@ -1,10 +1,16 @@
package com.baiye.modules.system.rest;
import com.baiye.http.CommonResponse;
import com.baiye.http.ResponseCode;
import com.baiye.modules.system.domain.Debt;
import com.baiye.modules.system.service.DebtFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* @author YQY
@ -16,4 +22,13 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/api/debt")
@RequiredArgsConstructor
public class DebtFormController {
private final DebtFormService debtFormService;
@ApiOperation("新建债务表单")
@PostMapping("/saveDebtForm")
public ResponseEntity<Object> saveTask(@RequestBody Debt debt){
debtFormService.saveDebtForm(debt);
return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK);
}
}

@ -2,10 +2,14 @@ package com.baiye.modules.system.rest;
import com.baiye.http.CommonResponse;
import com.baiye.modules.system.service.FormUserService;
import com.baiye.modules.system.service.dto.FormUserQueryCriteria;
import com.baiye.modules.system.service.dto.UserLoginDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -44,5 +48,11 @@ public class FormUserController {
return formUserService.getUserDetail(userToken);
}
@ApiOperation("查询人员列表(分页)")
@GetMapping("/queryAll")
public ResponseEntity<Object> queryAll(FormUserQueryCriteria formUserQueryCriteria, Pageable pageable) {
return new ResponseEntity<>(formUserService.queryAll(formUserQueryCriteria, pageable), HttpStatus.OK);
}
}

@ -1,4 +1,13 @@
package com.baiye.modules.system.service;
import com.baiye.modules.system.domain.Debt;
public interface DebtFormService {
/**
*
* @param debt
* @return
*/
void saveDebtForm(Debt debt);
}

@ -1,11 +0,0 @@
package com.baiye.modules.system.service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@Slf4j
public class DebtFormServiceImpl implements DebtFormService{
}

@ -2,7 +2,9 @@ package com.baiye.modules.system.service;
import com.baiye.http.CommonResponse;
import com.baiye.modules.system.domain.FormUser;
import com.baiye.modules.system.service.dto.FormUserQueryCriteria;
import com.baiye.modules.system.service.dto.UserLoginDTO;
import org.springframework.data.domain.Pageable;
import java.util.Map;
@ -39,4 +41,12 @@ public interface FormUserService {
* @return
*/
FormUser findUserById(Long userId);
/**
*
* @param formUserQueryCriteria
* @param pageable
* @return
*/
Map<String, Object> queryAll(FormUserQueryCriteria formUserQueryCriteria, Pageable pageable);
}

@ -16,6 +16,7 @@
package com.baiye.modules.system.service.dto;
import com.baiye.model.base.BaseDTO;
import com.baiye.modules.system.domain.FormUser;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
@ -23,6 +24,8 @@ import lombok.Setter;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author Zheng Jie
@ -36,7 +39,7 @@ public class FormUserDto extends BaseDTO implements Serializable {
private static final long serialVersionUID = -2035716596706011470L;
@ApiModelProperty(value = "id")
private Integer id;
private Long id;
@ApiModelProperty(value = "二维码地址")
private String qrCodePath;
@ -47,6 +50,8 @@ public class FormUserDto extends BaseDTO implements Serializable {
@ApiModelProperty(value = "最后登录时间")
private Date lastLoginTime;
private List<FormUser> userList;

@ -0,0 +1,14 @@
package com.baiye.modules.system.service.dto;
import com.baiye.annotation.Query;
import lombok.Data;
/**
*
*/
@Data
public class FormUserQueryCriteria {
@Query
private Long id;
}

@ -0,0 +1,39 @@
package com.baiye.modules.system.service.impl;
import com.baiye.exception.BadRequestException;
import com.baiye.manager.UserTokenManager;
import com.baiye.modules.system.domain.Debt;
import com.baiye.modules.system.domain.FormUser;
import com.baiye.modules.system.repository.DebtFormRepository;
import com.baiye.modules.system.repository.FormUserRepository;
import com.baiye.modules.system.service.DebtFormService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Service
@RequiredArgsConstructor
@Slf4j
public class DebtFormServiceImpl implements DebtFormService {
private final DebtFormRepository debtFormRepository;
private final FormUserRepository formUserRepository;
@Override
@Transactional(rollbackOn = Exception.class)
public void saveDebtForm(Debt debt) {
debt.setCreateBy(UserTokenManager.getUserId(debt.getUserToken()));
debtFormRepository.save(debt);
FormUser formUser = formUserRepository.findById(debt.getUserId()).orElseGet(FormUser::new);
if (debt.getOptimisticVersion() == null || formUser.getOptimisticVersion() != debt.getOptimisticVersion()){
log.info("=================================版本号不一致=========================================");
throw new BadRequestException("刷新后重试");
}
formUser.setInvitationNums(formUser.getInvitationNums() + 1);
formUser.setOptimisticVersion(formUser.getOptimisticVersion() + 1);
formUserRepository.save(formUser);
}
}

@ -3,33 +3,40 @@ package com.baiye.modules.system.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baiye.constant.DefaultNumberConstants;
import com.baiye.exception.BadRequestException;
import com.baiye.http.CommonResponse;
import com.baiye.manager.UserTokenManager;
import com.baiye.model.enums.ResponseCode;
import com.baiye.modules.report.entity.TaskReport;
import com.baiye.modules.system.domain.Debt;
import com.baiye.modules.system.domain.FormUser;
import com.baiye.modules.system.repository.DebtFormRepository;
import com.baiye.modules.system.repository.FormUserRepository;
import com.baiye.modules.system.service.FormUserService;
import com.baiye.modules.system.service.dto.DebtQueryResult;
import com.baiye.modules.system.service.dto.FormUserDto;
import com.baiye.modules.system.service.dto.FormUserQueryCriteria;
import com.baiye.modules.system.service.dto.UserLoginDTO;
import com.baiye.util.MobileUtil;
import com.baiye.util.SmsUtil;
import com.baiye.modules.system.service.mapstruct.FormUserMapper;
import com.baiye.util.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* @author Enzo
@ -48,6 +55,17 @@ public class FormUserServiceImpl implements FormUserService {
private final SmsUtil smsUtil;
private final FormUserMapper formUserMapper;
@Value("${snowflake.workerId}")
private int workerId;
@Value("${snowflake.datacenterId}")
private int datacenterId;
@Value("${debt.qrcode}")
String qrcodeUrl;
@Value("${debt.visitUrl}")
String visitUrl;
@Override
public CommonResponse<String> createRandomCodeByPhone(String phone) {
@ -89,8 +107,17 @@ public class FormUserServiceImpl implements FormUserService {
}
// TODO 生成二维码
formUser = new FormUser();
Long formUserId = IdUtil.getSnowflake(workerId, datacenterId).nextId();
formUser.setId(formUserId);
formUser.setPhone(phone);
formUser.setQrCodePath("");
formUser.setOptimisticVersion(0l);
try {
String visit = visitUrl + formUserId;
QRCodeUtil.encode(visit, qrcodeUrl + "qrcode.jpg", qrcodeUrl,formUserId.toString(), true);
}catch (Exception e){
throw new BadRequestException("生成二维码错误");
}
formUser.setQrCodePath(qrcodeUrl + formUserId + ".jpg");
formUser.setLastLoginTime(DateUtil.date());
FormUser user = formUserRepository.save(formUser);
// token
@ -130,5 +157,26 @@ public class FormUserServiceImpl implements FormUserService {
return formUserRepository.findById(userId).orElseGet(FormUser::new);
}
@Override
public Map<String, Object> queryAll(FormUserQueryCriteria formUserQueryCriteria, Pageable pageable) {
Page<FormUser> debtPage = formUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, formUserQueryCriteria, criteriaBuilder), pageable);
Page<FormUserDto> map = debtPage.map(formUserMapper::toDto);
Map<Long, List<Debt>> listMap = debtFormRepository.findAll().stream().collect(Collectors.groupingBy(Debt::getUserId, Collectors.toList()));
for (FormUserDto formUserDto : map) {
List<Long> list = new ArrayList<>();
if (listMap.containsKey(formUserDto.getId())){
List<Debt> debtList = listMap.get(formUserDto.getId());
for (Debt debt : debtList) {
list.add(debt.getCreateBy());
}
}
if (CollUtil.isNotEmpty(list)){
List<FormUser> formUsersAll = formUserRepository.findAllById(list);
formUserDto.setUserList(formUsersAll);
}
}
return PageUtil.toPage(map);
}
}

@ -0,0 +1,11 @@
package com.baiye.modules.system.service.mapstruct;
import com.baiye.model.base.BaseMapper;
import com.baiye.modules.system.domain.Debt;
import com.baiye.modules.system.service.dto.DebtFormDto;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface DebtFormMapper extends BaseMapper<DebtFormDto, Debt> {
}

@ -0,0 +1,272 @@
package com.baiye.util;
import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Random;
/**
*
*
* @author YQY
* @date 2022-04-18
*/
public class QRCodeUtil {
private static final String CHARSET = "utf-8";
private static final String FORMAT = "JPG";
// 二维码尺寸
private static final int QRCODE_SIZE = 300;
// LOGO宽度
private static final int LOGO_WIDTH = 60;
// LOGO高度
private static final int LOGO_HEIGHT = 60;
private static BufferedImage createImage(String content, String logoPath, boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (logoPath == null || "".equals(logoPath)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, logoPath, needCompress);
return image;
}
/**
* LOGO
*
* @param source
*
* @param logoPath
* LOGO
* @param needCompress
*
* @throws Exception
*/
private static void insertImage(BufferedImage source, String logoPath, boolean needCompress) throws Exception {
File file = new File(logoPath);
if (!file.exists()) {
throw new Exception("logo file not found.");
}
Image src = ImageIO.read(new File(logoPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > LOGO_WIDTH) {
width = LOGO_WIDTH;
}
if (height > LOGO_HEIGHT) {
height = LOGO_HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* (LOGO)
*
*
* @param content
*
* @param logoPath
* LOGO
* @param destPath
*
* @param needCompress
* LOGO
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);
mkdirs(destPath);
String fileName = new Random().nextInt(99999999) + "." + FORMAT.toLowerCase();
ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));
return fileName;
}
/**
* (LOGO)
*
*
* @param content
*
* @param logoPath
* LOGO
* @param destPath
*
* @param fileName
*
* @param needCompress
* LOGO
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath, String fileName, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);
mkdirs(destPath);
fileName = fileName.substring(0, fileName.indexOf(".")>0?fileName.indexOf("."):fileName.length())
+ "." + FORMAT.toLowerCase();
ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));
return fileName;
}
/**
* mkdirsmkdir
* (mkdir)
* @param destPath
*
*/
public static void mkdirs(String destPath) {
File file = new File(destPath);
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
/**
* (LOGO)
*
* @param content
*
* @param logoPath
* LOGO
* @param destPath
*
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath) throws Exception {
return QRCodeUtil.encode(content, logoPath, destPath, false);
}
/**
*
*
* @param content
*
* @param destPath
*
* @param needCompress
* LOGO
* @throws Exception
*/
public static String encode(String content, String destPath, boolean needCompress) throws Exception {
return QRCodeUtil.encode(content, null, destPath, needCompress);
}
/**
*
*
* @param content
*
* @param destPath
*
* @throws Exception
*/
public static String encode(String content, String destPath) throws Exception {
return QRCodeUtil.encode(content, null, destPath, false);
}
/**
* (LOGO)
*
* @param content
*
* @param logoPath
* LOGO
* @param output
*
* @param needCompress
* LOGO
* @throws Exception
*/
public static void encode(String content, String logoPath, OutputStream output, boolean needCompress)
throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);
ImageIO.write(image, FORMAT, output);
}
/**
*
*
* @param content
*
* @param output
*
* @throws Exception
*/
public static void encode(String content, OutputStream output) throws Exception {
QRCodeUtil.encode(content, null, output, false);
}
/**
*
*
* @param file
*
* @return
* @throws Exception
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
/**
*
*
* @param path
*
* @return
* @throws Exception
*/
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
}
}

@ -163,3 +163,8 @@ template:
# ConnectTimeout: 50000
# #建立连接之后,读取响应资源超时时间
# ReadTimeout: 50000
#债务表单二维码扫描地址及二维码存放路径
debt:
visitUrl: http://118.178.137.129:8866/api/pppppppppppp?visitUrl=
qrcode: /home/eladmin/qrcode/

@ -163,3 +163,8 @@ alipay:
template:
download-personnel-list: https://baiyee.vip/file/example.xlsx
#债务表单二维码扫描地址及二维码存放路径
debt:
visitUrl: https://baiyee.vip/api/pppppppppppp?visitUrl=
qrcode: /home/eladmin/qrcode/
Loading…
Cancel
Save