【admin】新增#设备管理
parent
a4e765e8d1
commit
c641fc8d37
|
@ -3,7 +3,7 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
<!-- 父工程 -->
|
<!-- 父工程 -->
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>charging_pile</artifactId>
|
<artifactId>charging_pile</artifactId>
|
||||||
<groupId>org.hcy</groupId>
|
<groupId>org.hcy</groupId>
|
||||||
|
@ -44,6 +44,15 @@
|
||||||
<version>2.0.12</version>
|
<version>2.0.12</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hcy</groupId>
|
||||||
|
<artifactId>common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tomcat.embed</groupId>
|
||||||
|
<artifactId>tomcat-embed-core</artifactId>
|
||||||
|
<version>9.0.83</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<!-- 插件管理 -->
|
<!-- 插件管理 -->
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.hcy.admin.controller.QrCode;
|
||||||
|
|
||||||
|
import com.hcy.admin.service.QRCode.QRCodeService;
|
||||||
|
import com.hcy.common.utils.QRCodeUtil;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("qr/code")
|
||||||
|
public class QrCodeController{
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private QRCodeService qrCodeService;
|
||||||
|
|
||||||
|
private static final String RootPath="E:\\shFiles\\QRCode";
|
||||||
|
private static final String FileFormat=".png";
|
||||||
|
private static final ThreadLocal<SimpleDateFormat> LOCALDATEFORMAT=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));
|
||||||
|
//生成二维码并将其存放于本地目录
|
||||||
|
@PostMapping("generate/v1")
|
||||||
|
public void generateV1(String content){
|
||||||
|
System.out.println(content);
|
||||||
|
final String fileName=LOCALDATEFORMAT.get().format(new Date());
|
||||||
|
QRCodeUtil.createQRCodeToFile(content,new File(RootPath),fileName+FileFormat);
|
||||||
|
}
|
||||||
|
//生成二维码并将其返回给前端调用者
|
||||||
|
@PostMapping("generate/v2")
|
||||||
|
public void generateV2(String content, HttpServletResponse servletResponse) throws IOException {
|
||||||
|
/*QRCodeUtil.createQRCodeToOutputStream(content,servletResponse.getOutputStream());*/
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/toFile")
|
||||||
|
public void toFile(String content) {
|
||||||
|
qrCodeService.generateFile(content, new File("./test.png"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/toStream")
|
||||||
|
public void toStream(String content, HttpServletResponse response) {
|
||||||
|
try {
|
||||||
|
qrCodeService.generateStream(content, response.getOutputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,124 @@
|
||||||
|
package com.hcy.admin.controller.equipment;
|
||||||
|
|
||||||
|
import com.hcy.admin.config.aop.Log;
|
||||||
|
import com.hcy.admin.service.equipment.IEquipmentService;
|
||||||
|
import com.hcy.admin.validate.commonFault.CommonFaultParam;
|
||||||
|
import com.hcy.admin.validate.equipment.EquipmentParam;
|
||||||
|
import com.hcy.admin.validate.common.PageParam;
|
||||||
|
import com.hcy.admin.vo.equipment.EquipmentListVo;
|
||||||
|
import com.hcy.admin.vo.equipment.EquipmentDetailVo;
|
||||||
|
import com.hcy.common.core.AjaxResult;
|
||||||
|
import com.hcy.common.core.PageResult;
|
||||||
|
import com.hcy.common.utils.QRCodeUtil;
|
||||||
|
import com.hcy.common.validator.annotation.IDMust;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理管理
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("api/equipment")
|
||||||
|
public class EquipmentController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
IEquipmentService iEquipmentService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理列表
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param pageParam 分页参数
|
||||||
|
* @param params 搜索参数
|
||||||
|
* @return Object
|
||||||
|
*/
|
||||||
|
@GetMapping("/list")
|
||||||
|
public Object list(@Validated PageParam pageParam,
|
||||||
|
@RequestParam Map<String, String> params) {
|
||||||
|
PageResult<EquipmentListVo> list = iEquipmentService.list(pageParam, params);
|
||||||
|
return AjaxResult.success(list);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 设备管理详情
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param id 主键ID
|
||||||
|
* @return Object
|
||||||
|
*/
|
||||||
|
@GetMapping("/detail")
|
||||||
|
public Object detail(@Validated @IDMust() @RequestParam("id") Integer id) {
|
||||||
|
EquipmentDetailVo detail = iEquipmentService.detail(id);
|
||||||
|
return AjaxResult.success(detail);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理新增
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param equipmentParam 参数
|
||||||
|
* @return Object
|
||||||
|
*/
|
||||||
|
@Log(title = "设备管理新增")
|
||||||
|
@PostMapping("/add")
|
||||||
|
public Object add(@Validated(value = EquipmentParam.create.class) @RequestBody EquipmentParam equipmentParam) {
|
||||||
|
iEquipmentService.add(equipmentParam);
|
||||||
|
return AjaxResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理编辑
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param equipmentParam 参数
|
||||||
|
* @return Object
|
||||||
|
*/
|
||||||
|
@Log(title = "设备管理编辑")
|
||||||
|
@PostMapping("/edit")
|
||||||
|
public Object edit(@Validated(value = EquipmentParam.update.class) @RequestBody EquipmentParam equipmentParam) {
|
||||||
|
iEquipmentService.edit(equipmentParam);
|
||||||
|
return AjaxResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理删除
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param equipmentParam 参数
|
||||||
|
* @return Object
|
||||||
|
*/
|
||||||
|
@Log(title = "设备管理删除")
|
||||||
|
@PostMapping("/del")
|
||||||
|
public Object del(@Validated(value = EquipmentParam.delete.class) @RequestBody EquipmentParam equipmentParam) {
|
||||||
|
iEquipmentService.del(Math.toIntExact(equipmentParam.getId()));
|
||||||
|
return AjaxResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备停用
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param equipmentParam 参数
|
||||||
|
* @return Object
|
||||||
|
*/
|
||||||
|
@Log(title = "常见故障管理改变状态")
|
||||||
|
@PostMapping("/stateEdit")
|
||||||
|
public Object stateEdit(@RequestBody EquipmentParam equipmentParam) {
|
||||||
|
iEquipmentService.stateEdit(equipmentParam.getIds());
|
||||||
|
return AjaxResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成设备编号二维码并将其返回给前端调用者
|
||||||
|
* @param number
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@PostMapping("generate/v2")
|
||||||
|
public String generateV2(String number) {
|
||||||
|
return QRCodeUtil.createQRCodeToOutputStream(number);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.hcy.admin.service.QRCode;
|
||||||
|
|
||||||
|
import cn.hutool.extra.qrcode.QrCodeUtil;
|
||||||
|
import cn.hutool.extra.qrcode.QrConfig;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class QRCodeService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private QrConfig qrConfig;
|
||||||
|
|
||||||
|
public void generateFile(String content, File file) {
|
||||||
|
//生成到本地文件
|
||||||
|
QrCodeUtil.generate(content, qrConfig, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generateStream(String content, OutputStream outputStream) {
|
||||||
|
//输出到流
|
||||||
|
QrCodeUtil.generate(content, qrConfig, "png", outputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
package com.hcy.admin.service.equipment;
|
||||||
|
|
||||||
|
import com.hcy.admin.validate.common.PageParam;
|
||||||
|
import com.hcy.admin.validate.equipment.EquipmentParam;
|
||||||
|
import com.hcy.admin.vo.equipment.EquipmentListVo;
|
||||||
|
import com.hcy.admin.vo.equipment.EquipmentDetailVo;
|
||||||
|
import com.hcy.common.core.PageResult;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理服务接口类
|
||||||
|
*/
|
||||||
|
public interface IEquipmentService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理列表
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param pageParam 分页参数
|
||||||
|
* @param params 搜索参数
|
||||||
|
* @return PageResult<EquipmentVo>
|
||||||
|
*/
|
||||||
|
PageResult<EquipmentListVo> list(PageParam pageParam, Map<String, String> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理详情
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param id 主键ID
|
||||||
|
* @return Equipment
|
||||||
|
*/
|
||||||
|
EquipmentDetailVo detail(Integer id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理新增
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param equipmentParam 参数
|
||||||
|
*/
|
||||||
|
void add(EquipmentParam equipmentParam);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理编辑
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param equipmentParam 参数
|
||||||
|
*/
|
||||||
|
void edit(EquipmentParam equipmentParam);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理删除
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param id 主键ID
|
||||||
|
*/
|
||||||
|
void del(Integer id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备停用
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
void stateEdit(List<Integer> ids);
|
||||||
|
}
|
|
@ -0,0 +1,213 @@
|
||||||
|
package com.hcy.admin.service.equipment.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.github.yulichang.query.MPJQueryWrapper;
|
||||||
|
import com.hcy.admin.service.equipment.IEquipmentService;
|
||||||
|
import com.hcy.admin.validate.common.PageParam;
|
||||||
|
import com.hcy.admin.validate.equipment.EquipmentParam;
|
||||||
|
import com.hcy.admin.vo.equipment.EquipmentListVo;
|
||||||
|
import com.hcy.admin.vo.equipment.EquipmentDetailVo;
|
||||||
|
import com.hcy.common.constant.GlobalConstant;
|
||||||
|
import com.hcy.common.core.PageResult;
|
||||||
|
import com.hcy.common.entity.commonFault.CommonFault;
|
||||||
|
import com.hcy.common.entity.equipment.Equipment;
|
||||||
|
import com.hcy.common.entity.sparePart.SparePart;
|
||||||
|
import com.hcy.common.enums.commonFault.CommonFaultEnum;
|
||||||
|
import com.hcy.common.enums.equipment.EquipmentStateEnum;
|
||||||
|
import com.hcy.common.mapper.equipment.EquipmentMapper;
|
||||||
|
import com.hcy.common.utils.ArrayUtil;
|
||||||
|
import com.hcy.common.utils.TimeUtil;
|
||||||
|
import com.hcy.common.utils.UrlUtil;
|
||||||
|
import com.hcy.common.config.GlobalConfig;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理实现类
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class EquipmentServiceImpl implements IEquipmentService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
EquipmentMapper equipmentMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理列表
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param pageParam 分页参数
|
||||||
|
* @param params 搜索参数
|
||||||
|
* @return PageResult<EquipmentListVo>
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public PageResult<EquipmentListVo> list(PageParam pageParam, Map<String, String> params) {
|
||||||
|
Integer page = pageParam.getPageNo();
|
||||||
|
Integer limit = pageParam.getPageSize();
|
||||||
|
|
||||||
|
QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("is_delete", 0);
|
||||||
|
queryWrapper.orderByDesc("id");
|
||||||
|
|
||||||
|
equipmentMapper.setSearch(queryWrapper, params, new String[]{
|
||||||
|
"like:number:str",
|
||||||
|
"like:name:str",
|
||||||
|
"=:deviceStatus@device_status:int",
|
||||||
|
"datetime:createTimeStart-createTimeEnd@create_time:str",
|
||||||
|
});
|
||||||
|
|
||||||
|
IPage<Equipment> iPage = equipmentMapper.selectPage(new Page<>(page, limit), queryWrapper);
|
||||||
|
|
||||||
|
List<EquipmentListVo> list = new LinkedList<>();
|
||||||
|
for(Equipment item : iPage.getRecords()) {
|
||||||
|
EquipmentListVo vo = new EquipmentListVo();
|
||||||
|
BeanUtils.copyProperties(item, vo);
|
||||||
|
vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
|
||||||
|
list.add(vo);
|
||||||
|
}
|
||||||
|
|
||||||
|
return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理详情
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param id 主键参数
|
||||||
|
* @return Equipment
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public EquipmentDetailVo detail(Integer id) {
|
||||||
|
Equipment model = equipmentMapper.selectOne(
|
||||||
|
new QueryWrapper<Equipment>()
|
||||||
|
.eq("id", id)
|
||||||
|
.eq("is_delete", 0)
|
||||||
|
.last("limit 1"));
|
||||||
|
|
||||||
|
Assert.notNull(model, "数据不存在");
|
||||||
|
|
||||||
|
EquipmentDetailVo vo = new EquipmentDetailVo();
|
||||||
|
BeanUtils.copyProperties(model, vo);
|
||||||
|
return vo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理新增
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param equipmentParam 参数
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void add(EquipmentParam equipmentParam) {
|
||||||
|
Equipment model = new Equipment();
|
||||||
|
|
||||||
|
model.setNumber(equipmentParam.getNumber());
|
||||||
|
model.setName(equipmentParam.getName());
|
||||||
|
model.setModel(equipmentParam.getModel()); // 设备型号
|
||||||
|
model.setManufacturers(equipmentParam.getManufacturers()); // 设备厂家
|
||||||
|
model.setSpecification(equipmentParam.getSpecification()); // 设备规格
|
||||||
|
model.setDeviceStatus(EquipmentStateEnum.NORMAL.getStatus()); // 设备状态(0=停用,1=正常,2=保修中,3=检修中)
|
||||||
|
model.setLongitude(equipmentParam.getLongitude()); // 经度
|
||||||
|
model.setLatitude(equipmentParam.getLatitude()); // 纬度
|
||||||
|
model.setProvinceId(equipmentParam.getProvinceId()); // 省id
|
||||||
|
model.setDistrictId(equipmentParam.getDistrictId()); // 区id
|
||||||
|
model.setCityId(equipmentParam.getCityId()); // 市id
|
||||||
|
model.setDetailedAddress(equipmentParam.getDetailedAddress()); // 详细地址
|
||||||
|
model.setInspectionCycle(equipmentParam.getInspectionCycle()); // 巡检周期方式(0=天数间隔,1=固定日期)
|
||||||
|
model.setDailyAudit(equipmentParam.getDailyAudit()); // 每隔几天巡检
|
||||||
|
model.setCreateTime(System.currentTimeMillis() / 1000);
|
||||||
|
//model.setDeviceCode(equipmentParam.getDeviceCode()); // 设备码
|
||||||
|
equipmentMapper.insert(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理编辑
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param equipmentParam 参数
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void edit(EquipmentParam equipmentParam) {
|
||||||
|
Equipment model = equipmentMapper.selectOne(
|
||||||
|
new QueryWrapper<Equipment>()
|
||||||
|
.eq("id", equipmentParam.getId())
|
||||||
|
.eq("is_delete", 0)
|
||||||
|
.last("limit 1"));
|
||||||
|
|
||||||
|
Assert.notNull(model, "数据不存在!");
|
||||||
|
|
||||||
|
model.setId(equipmentParam.getId());
|
||||||
|
model.setUserId(equipmentParam.getUserId());
|
||||||
|
model.setModuleId(equipmentParam.getModuleId());
|
||||||
|
model.setNumber(equipmentParam.getNumber());
|
||||||
|
model.setName(equipmentParam.getName());
|
||||||
|
model.setModel(equipmentParam.getModel());
|
||||||
|
model.setManufacturers(equipmentParam.getManufacturers());
|
||||||
|
model.setSpecification(equipmentParam.getSpecification());
|
||||||
|
model.setDeviceStatus(equipmentParam.getDeviceStatus());
|
||||||
|
model.setLongitude(equipmentParam.getLongitude());
|
||||||
|
model.setLatitude(equipmentParam.getLatitude());
|
||||||
|
model.setProvinceId(equipmentParam.getProvinceId());
|
||||||
|
model.setDistrictId(equipmentParam.getDistrictId());
|
||||||
|
model.setCityId(equipmentParam.getCityId());
|
||||||
|
model.setDetailedAddress(equipmentParam.getDetailedAddress());
|
||||||
|
model.setInspectionCycle(equipmentParam.getInspectionCycle());
|
||||||
|
model.setDailyAudit(equipmentParam.getDailyAudit());
|
||||||
|
model.setDeviceCode(equipmentParam.getDeviceCode());
|
||||||
|
model.setUpdateTime(System.currentTimeMillis() / 1000);
|
||||||
|
equipmentMapper.updateById(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理删除
|
||||||
|
*
|
||||||
|
* @author hcy
|
||||||
|
* @param id 主键ID
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void del(Integer id) {
|
||||||
|
Equipment model = equipmentMapper.selectOne(
|
||||||
|
new QueryWrapper<Equipment>()
|
||||||
|
.eq("id", id)
|
||||||
|
.eq("is_delete", 0)
|
||||||
|
.last("limit 1"));
|
||||||
|
|
||||||
|
Assert.notNull(model, "数据不存在!");
|
||||||
|
|
||||||
|
model.setIsDelete(1);
|
||||||
|
model.setDeleteTime(System.currentTimeMillis() / 1000);
|
||||||
|
equipmentMapper.updateById(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备停用
|
||||||
|
*
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stateEdit(List<Integer> ids) {
|
||||||
|
List<Equipment> modelList = equipmentMapper.selectList(
|
||||||
|
new QueryWrapper<Equipment>()
|
||||||
|
.in("id", ids));
|
||||||
|
|
||||||
|
Assert.notNull(modelList, "数据不存在!");
|
||||||
|
|
||||||
|
for (Equipment equipment : modelList) {
|
||||||
|
// 设备状态(0=停用,1=正常,2=保修中,3=检修中)
|
||||||
|
if(equipment.getDeviceStatus() == EquipmentStateEnum.STOPWORD.getStatus()){
|
||||||
|
equipment.setDeviceStatus(EquipmentStateEnum.NORMAL.getStatus());
|
||||||
|
}else{
|
||||||
|
equipment.setDeviceStatus(EquipmentStateEnum.STOPWORD.getStatus());
|
||||||
|
}
|
||||||
|
equipment.setUpdateTime(System.currentTimeMillis() / 1000);
|
||||||
|
equipmentMapper.updateById(equipment);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
package com.hcy.admin.validate.equipment;
|
||||||
|
|
||||||
|
import com.hcy.common.entity.equipment.Equipment;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
import org.hibernate.validator.constraints.Length;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import javax.validation.constraints.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理参数
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = false)
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class EquipmentParam implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public interface create{}
|
||||||
|
public interface update{}
|
||||||
|
public interface delete{}
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0", message = "userId参数值不能少于0", groups = {create.class, update.class})
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0", message = "moduleId参数值不能少于0", groups = {create.class, update.class})
|
||||||
|
private Long moduleId;
|
||||||
|
|
||||||
|
@Length(max = 255, message = "number参数不能超出255个字符", groups = {create.class, update.class})
|
||||||
|
private String number;
|
||||||
|
|
||||||
|
@Length(max = 255, message = "name参数不能超出255个字符", groups = {create.class, update.class})
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Length(max = 255, message = "model参数不能超出255个字符", groups = {create.class, update.class})
|
||||||
|
private String model;
|
||||||
|
|
||||||
|
@Length(max = 255, message = "manufacturers参数不能超出255个字符", groups = {create.class, update.class})
|
||||||
|
private String manufacturers;
|
||||||
|
|
||||||
|
@Length(max = 255, message = "specification参数不能超出255个字符", groups = {create.class, update.class})
|
||||||
|
private String specification;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0", message = "deviceStatus参数值不能少于0", groups = {create.class, update.class})
|
||||||
|
private Integer deviceStatus;
|
||||||
|
|
||||||
|
@Length(max = 24, message = "longitude参数不能超出24个字符", groups = {create.class, update.class})
|
||||||
|
private String longitude;
|
||||||
|
|
||||||
|
@Length(max = 24, message = "latitude参数不能超出24个字符", groups = {create.class, update.class})
|
||||||
|
private String latitude;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0", message = "provinceId参数值不能少于0", groups = {create.class, update.class})
|
||||||
|
private Long provinceId;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0", message = "districtId参数值不能少于0", groups = {create.class, update.class})
|
||||||
|
private Long districtId;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0", message = "cityId参数值不能少于0", groups = {create.class, update.class})
|
||||||
|
private Long cityId;
|
||||||
|
|
||||||
|
@Length(max = 255, message = "detailedAddress参数不能超出255个字符", groups = {create.class, update.class})
|
||||||
|
private String detailedAddress;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0", message = "inspectionCycle参数值不能少于0", groups = {create.class, update.class})
|
||||||
|
private Integer inspectionCycle;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0", message = "dailyAudit参数值不能少于0", groups = {create.class, update.class})
|
||||||
|
private Integer dailyAudit;
|
||||||
|
|
||||||
|
@Length(max = 255, message = "deviceCode参数不能超出255个字符", groups = {create.class, update.class})
|
||||||
|
private String deviceCode;
|
||||||
|
|
||||||
|
private List<Integer> ids; //批量停用
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.hcy.admin.vo.equipment;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EquipmentVo
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class EquipmentDetailVo implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private Long id; // 主键id
|
||||||
|
private Long userId; // 用户id
|
||||||
|
private Long moduleId; // 模块id
|
||||||
|
private String number; // 设备编号
|
||||||
|
private String name; // 设备名称
|
||||||
|
private String model; // 设备型号
|
||||||
|
private String manufacturers; // 设备厂家
|
||||||
|
private String specification; // 设备规格
|
||||||
|
private Integer deviceStatus; // 设备状态(0=停用,1=正常,2=保修中,3=检修中)
|
||||||
|
private String longitude; // 经度
|
||||||
|
private String latitude; // 纬度
|
||||||
|
private Long provinceId; // 省id
|
||||||
|
private Long districtId; // 区id
|
||||||
|
private Long cityId; // 市id
|
||||||
|
private String detailedAddress; // 详细地址
|
||||||
|
private Integer inspectionCycle; // 巡检周期方式(0=天数间隔,1=固定日期)
|
||||||
|
private Integer dailyAudit; // 每隔几天巡检
|
||||||
|
private String deviceCode; // 设备码
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.hcy.admin.vo.equipment;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EquipmentVo
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class EquipmentListVo implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private Long id; // 主键id
|
||||||
|
private Long userId; // 用户id
|
||||||
|
private Long moduleId; // 模块id
|
||||||
|
private String number; // 设备编号
|
||||||
|
private String name; // 设备名称
|
||||||
|
private String model; // 设备型号
|
||||||
|
private String manufacturers; // 设备厂家
|
||||||
|
private String specification; // 设备规格
|
||||||
|
private Integer deviceStatus; // 设备状态(0=停用,1=正常,2=保修中,3=检修中)
|
||||||
|
private String longitude; // 经度
|
||||||
|
private String latitude; // 纬度
|
||||||
|
private Long provinceId; // 省id
|
||||||
|
private Long districtId; // 区id
|
||||||
|
private Long cityId; // 市id
|
||||||
|
private String detailedAddress; // 详细地址
|
||||||
|
private Integer inspectionCycle; // 巡检周期方式(0=天数间隔,1=固定日期)
|
||||||
|
private Integer dailyAudit; // 每隔几天巡检
|
||||||
|
private String deviceCode; // 设备码
|
||||||
|
private String createTime; // 创建时间
|
||||||
|
|
||||||
|
}
|
|
@ -245,6 +245,25 @@
|
||||||
<version>5.8.16</version>
|
<version>5.8.16</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--zxing生成二维码-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.zxing</groupId>
|
||||||
|
<artifactId>core</artifactId>
|
||||||
|
<version>3.3.3</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.zxing</groupId>
|
||||||
|
<artifactId>javase</artifactId>
|
||||||
|
<version>3.3.3</version>
|
||||||
|
</dependency>
|
||||||
|
<!--Hutool工具包生成-->
|
||||||
|
<!--hutool工具包内部也是使用了ZXing库,但是帮我们封装好了,还提供了配置类,使用上更加简单。-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.2.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.hcy.common.config;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import cn.hutool.extra.qrcode.QrConfig;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
@Configuration
|
||||||
|
public class QRCodeConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public QrConfig qrConfig() {
|
||||||
|
QrConfig qrConfig = new QrConfig();
|
||||||
|
qrConfig.setBackColor(Color.white);
|
||||||
|
qrConfig.setForeColor(Color.black);
|
||||||
|
qrConfig.setCharset(Charset.forName("UTF-8"));
|
||||||
|
qrConfig.setMargin(1);
|
||||||
|
qrConfig.setWidth(400);
|
||||||
|
qrConfig.setHeight(400);
|
||||||
|
//logo图片
|
||||||
|
//qrConfig.setImg(new File("./logo.jpeg"));
|
||||||
|
//logo缩放比例
|
||||||
|
qrConfig.setRatio(6);
|
||||||
|
return qrConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.hcy.common.entity.equipment;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理实体
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Equipment implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@TableId(value="id", type= IdType.AUTO)
|
||||||
|
private Long id; // 主键id
|
||||||
|
private Long userId; // 用户id
|
||||||
|
private Long moduleId; // 模块id
|
||||||
|
private String number; // 设备编号
|
||||||
|
private String name; // 设备名称
|
||||||
|
private String model; // 设备型号
|
||||||
|
private String manufacturers; // 设备厂家
|
||||||
|
private String specification; // 设备规格
|
||||||
|
private Integer deviceStatus; // 设备状态(0=停用,1=正常,2=保修中,3=检修中)
|
||||||
|
private String longitude; // 经度
|
||||||
|
private String latitude; // 纬度
|
||||||
|
private Long provinceId; // 省id
|
||||||
|
private Long districtId; // 区id
|
||||||
|
private Long cityId; // 市id
|
||||||
|
private String detailedAddress; // 详细地址
|
||||||
|
private Integer inspectionCycle; // 巡检周期方式(0=天数间隔,1=固定日期)
|
||||||
|
private Integer dailyAudit; // 每隔几天巡检
|
||||||
|
private String deviceCode; // 设备码
|
||||||
|
private Integer isDelete; // 是否删除: [0=否, 1=是]
|
||||||
|
private Long createTime; // 创建时间
|
||||||
|
private Long updateTime; // 更新时间
|
||||||
|
private Long deleteTime; // 删除时间
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.hcy.common.enums.equipment;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 审核状态
|
||||||
|
* @author dabin
|
||||||
|
*/
|
||||||
|
|
||||||
|
public enum EquipmentStateEnum {
|
||||||
|
// 设备状态(0=停用,1=正常,2=保修中,3=检修中)
|
||||||
|
STOPWORD(0,"停用"),
|
||||||
|
NORMAL(1, "正常"),
|
||||||
|
UNDERWARRANTY(2,"保修中"),
|
||||||
|
UNDEROVERHAUL(3, "检修中"),
|
||||||
|
// 巡检周期方式(0=天数间隔,1=固定日期)
|
||||||
|
DAYINTERVAL(0,"天数间隔"),
|
||||||
|
FIXEDDATE(1, "固定日期");
|
||||||
|
private final int status;
|
||||||
|
private final String desc;
|
||||||
|
|
||||||
|
EquipmentStateEnum(int status, String desc) {
|
||||||
|
this.status = status;
|
||||||
|
this.desc = desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取状态码
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
* @author dabin
|
||||||
|
*/
|
||||||
|
public int getStatus() {
|
||||||
|
return this.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取提示
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
* @author dabin
|
||||||
|
*/
|
||||||
|
public String getDesc() {
|
||||||
|
return this.desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<Integer, String> getMap() {
|
||||||
|
Map<Integer, String> map = Maps.newHashMap();
|
||||||
|
for (EquipmentStateEnum auditStateEnum : EquipmentStateEnum.values()) {
|
||||||
|
map.put(auditStateEnum.status, auditStateEnum.desc);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.hcy.common.mapper.equipment;
|
||||||
|
|
||||||
|
|
||||||
|
import com.hcy.common.core.basics.IBaseMapper;
|
||||||
|
import com.hcy.common.entity.equipment.Equipment;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理Mapper
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface EquipmentMapper extends IBaseMapper<Equipment> {
|
||||||
|
}
|
|
@ -0,0 +1,119 @@
|
||||||
|
package com.hcy.common.utils;
|
||||||
|
import com.google.zxing.BarcodeFormat;
|
||||||
|
import com.google.zxing.EncodeHintType;
|
||||||
|
import com.google.zxing.MultiFormatWriter;
|
||||||
|
import com.google.zxing.WriterException;
|
||||||
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.filechooser.FileSystemView;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 二维码工具类
|
||||||
|
* 1)、通用生成方法
|
||||||
|
* 此种方式可以生成ZXing库支持的各种类型二维码,且可以自定义二维码颜色
|
||||||
|
*/
|
||||||
|
public class CodeUtil {
|
||||||
|
|
||||||
|
//二维码类型,枚举类
|
||||||
|
private static final BarcodeFormat CODE_TYPE = BarcodeFormat.QR_CODE;
|
||||||
|
//二维码宽度,单位像素
|
||||||
|
private static final int CODE_WIDTH = 400;
|
||||||
|
//二维码高度,单位像素
|
||||||
|
private static final int CODE_HEIGHT = 400;
|
||||||
|
//二维码前景色,0x000000表示黑色
|
||||||
|
private static final int FRONT_COLOR = 0x000000;
|
||||||
|
//二维码背景色,0xFFFFFF表示白色
|
||||||
|
private static final int BACKGROUND_COLOR = 0xFFFFFF;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成二维码并保存为文件
|
||||||
|
* @param content 二维码内容
|
||||||
|
* @param codeImgFileSaveDir 生成的二维码图片存储位置
|
||||||
|
* @param fileName 二维码图片文件名
|
||||||
|
*/
|
||||||
|
public static void createCodeToFile(String content, File codeImgFileSaveDir, String fileName) {
|
||||||
|
if (ObjectUtils.isEmpty(content) || ObjectUtils.isEmpty(fileName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
content = content.trim();
|
||||||
|
if (codeImgFileSaveDir == null || codeImgFileSaveDir.isFile()) {
|
||||||
|
//二维码图片存储目录为空,默认放在桌面
|
||||||
|
FileSystemView.getFileSystemView().getHomeDirectory();
|
||||||
|
}
|
||||||
|
if (!codeImgFileSaveDir.exists()) {
|
||||||
|
//二维码图片存储目录不存在,则创建
|
||||||
|
codeImgFileSaveDir.mkdirs();
|
||||||
|
}
|
||||||
|
//生成二维码
|
||||||
|
try {
|
||||||
|
BufferedImage bufferedImage = getBufferedImage(content);
|
||||||
|
File codeImgFile = new File(codeImgFileSaveDir, fileName);
|
||||||
|
ImageIO.write(bufferedImage, "png", codeImgFile);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成二维码并输出到输出流,通常用于输出到网页上进行显示
|
||||||
|
* @param content 二维码内容
|
||||||
|
* @param outputStream 输出流
|
||||||
|
*/
|
||||||
|
public static void createCodeToOutputStream(String content, OutputStream outputStream) {
|
||||||
|
if (ObjectUtils.isEmpty(content)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
content = content.trim();
|
||||||
|
//生成二维码
|
||||||
|
try {
|
||||||
|
BufferedImage bufferedImage = getBufferedImage(content);
|
||||||
|
ImageIO.write(bufferedImage, "png", outputStream);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 核心代码,生成二维码
|
||||||
|
* @param content
|
||||||
|
* @return BufferedImage
|
||||||
|
* @throws WriterException
|
||||||
|
*/
|
||||||
|
private static BufferedImage getBufferedImage(String content) throws WriterException {
|
||||||
|
//com.google.zxing.EncodeHintType:编码提示类,枚举类型
|
||||||
|
Map<EncodeHintType, Object> hints = new HashMap<>();
|
||||||
|
//EncodeHintType.CHARACTER_SET:设置字符编码类型
|
||||||
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
||||||
|
//EncodeHintType.ERROR_CORRECTION:设置纠错级别
|
||||||
|
//ErrorCorrectionLevel:纠错级别,【L:%7】【M:15%】【Q:25%】[H:30%]
|
||||||
|
//默认为L级别,纠错级别不同,生成的图案不同,但扫描结果一致
|
||||||
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
||||||
|
//EncodeHintType.MARGIN:设置二维码边距,单位像素
|
||||||
|
hints.put(EncodeHintType.MARGIN, 1);
|
||||||
|
|
||||||
|
//创建工厂类
|
||||||
|
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
|
||||||
|
//指定二维码类型和参数,返回对应的Writter进行编码后的二维码对象
|
||||||
|
BitMatrix bitMatrix = multiFormatWriter.encode(content, CODE_TYPE, CODE_WIDTH, CODE_HEIGHT, hints);
|
||||||
|
//创建图像缓冲区
|
||||||
|
BufferedImage bufferedImage = new BufferedImage(CODE_WIDTH, CODE_HEIGHT, BufferedImage.TYPE_INT_BGR);
|
||||||
|
//填充
|
||||||
|
for (int x = 0; x < CODE_WIDTH; x++) {
|
||||||
|
for (int y = 0; y < CODE_HEIGHT; y++) {
|
||||||
|
//bitMatrix.get(x, y)返回true,表示黑色即前景色
|
||||||
|
bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACKGROUND_COLOR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bufferedImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,116 @@
|
||||||
|
package com.hcy.common.utils;
|
||||||
|
import com.google.zxing.BarcodeFormat;
|
||||||
|
import com.google.zxing.EncodeHintType;
|
||||||
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
||||||
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
import com.google.zxing.qrcode.QRCodeWriter;
|
||||||
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
import javax.swing.filechooser.FileSystemView;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* QRCode工具类
|
||||||
|
* 2)、生成QRCode
|
||||||
|
*/
|
||||||
|
public class QRCodeUtil {
|
||||||
|
|
||||||
|
//二维码宽度,单位像素
|
||||||
|
private static final int CODE_WIDTH = 400;
|
||||||
|
//二维码高度,单位像素
|
||||||
|
private static final int CODE_HEIGHT = 400;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成QRCode并保存为文件
|
||||||
|
* @param content 二维码内容
|
||||||
|
* @param codeImgFileSaveDir 生成的二维码图片存储位置
|
||||||
|
* @param fileName 二维码图片文件名
|
||||||
|
*/
|
||||||
|
public static void createQRCodeToFile(String content, File codeImgFileSaveDir, String fileName) {
|
||||||
|
if (ObjectUtils.isEmpty(content) || ObjectUtils.isEmpty(fileName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
content = content.trim();
|
||||||
|
if (codeImgFileSaveDir == null || codeImgFileSaveDir.isFile()) {
|
||||||
|
//二维码图片存储目录为空,默认放在桌面
|
||||||
|
FileSystemView.getFileSystemView().getHomeDirectory();
|
||||||
|
}
|
||||||
|
if (!codeImgFileSaveDir.exists()) {
|
||||||
|
//二维码图片存储目录不存在,则创建
|
||||||
|
codeImgFileSaveDir.mkdirs();
|
||||||
|
}
|
||||||
|
//生成QRCode
|
||||||
|
//配置
|
||||||
|
Map<EncodeHintType, Object> hints = new HashMap<>();
|
||||||
|
//设置字符编码类型
|
||||||
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
||||||
|
//设置纠错级别
|
||||||
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
||||||
|
//设置二维码边距,单位像素
|
||||||
|
hints.put(EncodeHintType.MARGIN, 1);
|
||||||
|
//设置QRCode版本,1-40
|
||||||
|
hints.put(EncodeHintType.QR_VERSION, 1);
|
||||||
|
//直接使用QRCodeWriter
|
||||||
|
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
||||||
|
try {
|
||||||
|
//编码为灰度的位矩阵
|
||||||
|
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);
|
||||||
|
//获取Path
|
||||||
|
Path path = Paths.get(codeImgFileSaveDir.getPath(), fileName);
|
||||||
|
//写入文件
|
||||||
|
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成QRCode并输出到输出流,通常用于输出到网页上进行显示
|
||||||
|
* @param content 二维码内容
|
||||||
|
* @param outputStream 输出流
|
||||||
|
*/
|
||||||
|
public static String createQRCodeToOutputStream(String content) {
|
||||||
|
if (ObjectUtils.isEmpty(content)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
content = content.trim();
|
||||||
|
//生成QRCode
|
||||||
|
//配置
|
||||||
|
Map<EncodeHintType, Object> hints = new HashMap<>();
|
||||||
|
//设置字符编码类型
|
||||||
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
||||||
|
//设置纠错级别
|
||||||
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
||||||
|
//设置二维码边距,单位像素
|
||||||
|
hints.put(EncodeHintType.MARGIN, 1);
|
||||||
|
//设置QRCode版本,1-40
|
||||||
|
hints.put(EncodeHintType.QR_VERSION, 1);
|
||||||
|
//直接使用QRCodeWriter
|
||||||
|
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
//编码为灰度的位矩阵
|
||||||
|
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);
|
||||||
|
//写入输出流
|
||||||
|
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
|
||||||
|
byte[] imageInByte = outputStream.toByteArray();
|
||||||
|
|
||||||
|
//对图像字节流进行Base64编码
|
||||||
|
return "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(imageInByte);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue