Commit 830fe74e authored by licc's avatar licc

防伪二维码接口实现2

parent e456e6ea
...@@ -2,8 +2,10 @@ package cn.wisenergy.mapper; ...@@ -2,8 +2,10 @@ package cn.wisenergy.mapper;
import cn.wisenergy.model.app.AntiFake; import cn.wisenergy.model.app.AntiFake;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author 86187 * @author 86187
...@@ -35,9 +37,34 @@ public interface AntiFakeMapper extends BaseMapper<AntiFake> { ...@@ -35,9 +37,34 @@ public interface AntiFakeMapper extends BaseMapper<AntiFake> {
/** /**
* 批量创建防伪二维码 * 批量创建防伪二维码
*
* @param list 入参list * @param list 入参list
* @return list.size() * @return list.size()
*/ */
int creates(List<AntiFake> list); int creates(List<AntiFake> list);
/**
* 通过产品编号 获取二维码信息
*
* @param productNo
* @return
*/
AntiFake getByProductNo(@Param("productNo") String productNo);
/**
* 根据条件获取二维码数量
*
* @param map 条件参数
* @return 数量
*/
int count(Map<String, Object> map);
/**
* 获取二维码列表
*
* @param map 条件参数
* @return 二维码列表
*/
List<AntiFake> getList(Map<String, Object> map);
} }
...@@ -85,16 +85,48 @@ ...@@ -85,16 +85,48 @@
<include refid="table"/> <include refid="table"/>
</select> </select>
<!-- 批量创建接口 --> <!-- 批量创建接口 -->
<insert id="creates" parameterType="list"> <insert id="creates" parameterType="list">
INSERT INTO INSERT INTO
<include refid="table"/> <include refid="table"/>
(<include refid="cols_exclude_id"/>) (<include refid="cols_exclude_id"/>)
VALUES VALUES
<foreach collection="list" item="i" index="index" separator=","> <foreach collection="list" item="i" index="index" separator=",">
(<include refid="createsVal"/>) (<include refid="createsVal"/>)
</foreach> </foreach>
</insert> </insert>
<select id="getByProductNo" resultType="cn.wisenergy.model.app.AntiFake">
select
<include refid="cols_all"/>
from
<include refid="table"/>
<where>
product_no=#{productNo}
</where>
</select>
<select id="count" resultType="java.lang.Integer">
select count(1)
from
<include refid="table"/>
<where>
status=0
</where>
</select>
<select id="getList" resultType="cn.wisenergy.model.app.AntiFake">
select
<include refid="cols_all"/>
from
<include refid="table"/>
<where>
status=0
order by create_time desc
limit #{startNum},#{endNum}
</where>
</select>
</mapper> </mapper>
\ No newline at end of file
package cn.wisenergy.model.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author 86187
*/
@Data
@ApiModel("AntiFakeQuery")
public class AntiFakeQuery {
/**
* 页码
*/
@ApiModelProperty(value = "页码", name = "pageNo")
private Integer pageNo;
/**
* 页条数
*/
@ApiModelProperty(value = "页条数", name = "pageSize")
private Integer pageSize;
private Integer startNum;
private Integer endNum;
}
package cn.wisenergy.service.app; package cn.wisenergy.service.app;
import cn.wisenergy.common.utils.R; import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.app.AntiFake;
import cn.wisenergy.model.dto.AntiFakeQuery;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -17,6 +20,7 @@ public interface AntiFakeService { ...@@ -17,6 +20,7 @@ public interface AntiFakeService {
* *
* @param number 二维码数量 * @param number 二维码数量
* @return true or false * @return true or false
* @throws Exception 异常
*/ */
R<Boolean> createCode(Integer number) throws Exception; R<Boolean> createCode(Integer number) throws Exception;
...@@ -29,5 +33,27 @@ public interface AntiFakeService { ...@@ -29,5 +33,27 @@ public interface AntiFakeService {
*/ */
R<String> decode(MultipartFile file) throws Exception; R<String> decode(MultipartFile file) throws Exception;
/**
* 测试二维码图片上传到服务器
*
* @return 1
* @throws Exception 异常
*/
R<String> uploadImage() throws Exception; R<String> uploadImage() throws Exception;
/**
* 扫描防伪二维码
*
* @param produceNo 产品编号
* @return 扫描结果
*/
R<String> scanCode(String produceNo);
/**
* 获取防伪二维码列表
*
* @param query 条件参数
* @return 列表
*/
R<PageInfo<AntiFake>> getList(AntiFakeQuery query);
} }
package cn.wisenergy.service.app.impl; package cn.wisenergy.service.app.impl;
import cn.wisenergy.common.constant.CommonAttributes;
import cn.wisenergy.common.utils.DateUtil; import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.R; import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.AntiFakeMapper; import cn.wisenergy.mapper.AntiFakeMapper;
import cn.wisenergy.model.app.AccountInfo;
import cn.wisenergy.model.app.AntiFake; import cn.wisenergy.model.app.AntiFake;
import cn.wisenergy.model.dto.AccountInfoQuery;
import cn.wisenergy.model.dto.AntiFakeQuery;
import cn.wisenergy.service.app.AntiFakeService; import cn.wisenergy.service.app.AntiFakeService;
import cn.wisenergy.service.util.CodeUtils; import cn.wisenergy.service.util.CodeUtils;
import cn.wisenergy.service.util.FileUtils; import cn.wisenergy.service.util.FileUtils;
import cn.wisenergy.service.util.QRCodeUtils; import cn.wisenergy.service.util.QRCodeUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -16,9 +21,7 @@ import org.springframework.stereotype.Service; ...@@ -16,9 +21,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
/** /**
* @author 86187 * @author 86187
...@@ -57,7 +60,6 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i ...@@ -57,7 +60,6 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
antiFake.setBatchNo(String.valueOf(batchNo)); antiFake.setBatchNo(String.valueOf(batchNo));
antiFake.setBatchNumber(batchNumber); antiFake.setBatchNumber(batchNumber);
antiFake.setProduceTime(realTime); antiFake.setProduceTime(realTime);
antiFake.setScanTime(new Date());
//生成产品编码 //生成产品编码
String productNo = CodeUtils.createProductNo(batchNumber, antiFake.getBatchNo()); String productNo = CodeUtils.createProductNo(batchNumber, antiFake.getBatchNo());
...@@ -98,4 +100,76 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i ...@@ -98,4 +100,76 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
String url = QRCodeUtils.encode("2021-04022", DEST_PATH); String url = QRCodeUtils.encode("2021-04022", DEST_PATH);
return R.ok(url); return R.ok(url);
} }
@Override
public R<String> scanCode(String produceNo) {
log.info("AntiFakeServiceImpl[]scanCode[]input.param.produceNo:" + produceNo);
if (StringUtils.isBlank(produceNo)) {
return R.error("入参不能为空!");
}
//获取二维码信息
AntiFake antiFake = antiFakeMapper.getByProductNo(produceNo);
if (null == antiFake) {
return R.error("无效二维码");
}
if (antiFake.getStatus() == 1) {
return R.ok("该产品防伪码已于" + antiFake.getScanTime() + "被扫描,批次号为:"
+ antiFake.getBatchNumber() + ",编号为:" + antiFake.getBatchNo() + "");
}
antiFake.setStatus(1);
antiFake.setScanTime(new Date());
int count = antiFakeMapper.edit(antiFake);
if (count == 0) {
return R.error("扫描失败!");
}
return R.ok("该防伪二维码第一次被扫描!");
}
@Override
public R<PageInfo<AntiFake>> getList(AntiFakeQuery query) {
log.info("AntiFakeServiceImpl[]getList[]input.param.query:" + query);
if (null == query) {
return R.error("入参不能为空!");
}
pageHandle(query);
Map<String, Object> map = new HashMap<>(8);
int total = antiFakeMapper.count(map);
map.put("startNum", query.getStartNum());
map.put("endNum", query.getEndNum());
List<AntiFake> list = antiFakeMapper.getList(map);
PageInfo<AntiFake> info = new PageInfo<>();
info.setPageSize(query.getPageSize());
info.setPageNum(query.getPageNo());
info.setTotal(total);
info.setList(list);
return R.ok(info);
}
/**
* 分页处理方法
*
* @param schemeVo 参数
*/
private void pageHandle(AntiFakeQuery schemeVo) {
Integer pageNum = schemeVo.getPageNo();
Integer pageSize = schemeVo.getPageSize();
if (null == pageSize || pageSize == 0) {
pageSize = 10;
}
if (null == pageNum || pageNum == 0) {
pageNum = 1;
}
Integer endNum = pageSize;
Integer startNum = (pageNum - CommonAttributes.NUM_ONE) * pageSize;
schemeVo.setEndNum(endNum);
schemeVo.setStartNum(startNum);
schemeVo.setPageNo(pageNum);
schemeVo.setPageSize(pageSize);
}
} }
...@@ -26,6 +26,9 @@ import com.google.zxing.common.BitMatrix; ...@@ -26,6 +26,9 @@ import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer; import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* @author 86187
*/
public class QRCodeUtils { public class QRCodeUtils {
private static final String CHARSET = "utf-8"; private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "JPG"; private static final String FORMAT_NAME = "JPG";
...@@ -36,6 +39,8 @@ public class QRCodeUtils { ...@@ -36,6 +39,8 @@ public class QRCodeUtils {
// LOGO高度 // LOGO高度
private static final int HEIGHT = 60; private static final int HEIGHT = 60;
private static final String PATH = "/upload";
private static BufferedImage createImage(String content, String imgPath, private static BufferedImage createImage(String content, String imgPath,
boolean needCompress) throws Exception { boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
...@@ -123,7 +128,7 @@ public class QRCodeUtils { ...@@ -123,7 +128,7 @@ public class QRCodeUtils {
mkdirs(destPath); mkdirs(destPath);
String file = new Random().nextInt(99999999) + ".jpg"; String file = new Random().nextInt(99999999) + ".jpg";
ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file)); ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
return destPath + "/" + file; return PATH + "/" + file;
} }
/** /**
......
package cn.wisenergy.web.admin.controller.app; package cn.wisenergy.web.admin.controller.app;
import cn.wisenergy.common.utils.R; import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.app.AntiFake;
import cn.wisenergy.model.dto.AntiFakeQuery;
import cn.wisenergy.service.app.AntiFakeService; import cn.wisenergy.service.app.AntiFakeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/** /**
* @author 86187 * @author 86187
...@@ -37,9 +40,29 @@ public class AntiFakeController { ...@@ -37,9 +40,29 @@ public class AntiFakeController {
return antiFakeService.createCode(number); return antiFakeService.createCode(number);
} }
@ApiOperation(value = "上传防伪码图片接口", notes = "上传防伪码图片接口", httpMethod = "POST", produces = "application/json; charset=UTF-8") @ApiOperation(value = "扫描防伪二维码", notes = "扫描防伪二维码", httpMethod = "POST")
@RequestMapping(value = "/admin/uploadImage", method = RequestMethod.POST) @ApiImplicitParam(name = "productNo", value = "产品编号", dataType = "String")
public R<String> uploadImage() throws Exception { @PostMapping("/admin/scanCode")
return antiFakeService.uploadImage(); public R<String> scanCode(String productNo) {
log.info("shop-mall[]AntiFakeController[]scanCode[]input.param.productNo:" + productNo);
if (StringUtils.isBlank(productNo)) {
return R.error("入参为空!");
}
return antiFakeService.scanCode(productNo);
}
@ApiOperation(value = "获取防伪二维码分页列表", notes = "获取防伪二维码分页列表", httpMethod = "GET")
@ApiImplicitParam(name = "query", value = "查询条件", dataType = "AccountInfoQuery")
@GetMapping("admin/code/getList")
public R<PageInfo<AntiFake>> getList(AntiFakeQuery query) {
log.info("shop-mall[]AntiFakeController[]scanCode[]input.param.query:" + query);
if (null == query) {
return R.error("入参为空!");
}
return antiFakeService.getList(query);
} }
} }
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