package com.zy.asrs.controller;
|
|
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
import com.baomidou.mybatisplus.plugins.Page;
|
import com.core.annotations.ManagerAuth;
|
import com.core.common.BaseRes;
|
import com.core.common.Cools;
|
import com.core.common.DateUtils;
|
import com.core.common.R;
|
import com.zy.asrs.entity.MatCode;
|
import com.zy.asrs.entity.MatCodePrint;
|
import com.zy.asrs.service.MatCodeService;
|
import com.zy.common.CodeRes;
|
import com.zy.common.utils.BarcodeUtils;
|
import com.zy.common.utils.QrCode;
|
import com.zy.common.utils.excel.matcode.MatCodeExcel;
|
import com.zy.common.utils.excel.matcode.MatCodeExcelListener;
|
import com.zy.common.web.BaseController;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.imageio.ImageIO;
|
import javax.servlet.http.HttpServletResponse;
|
import java.awt.image.BufferedImage;
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
@RestController
|
public class MatCodeController extends BaseController {
|
|
@Autowired
|
private MatCodeService matCodeService;
|
|
@RequestMapping(value = "/matCode/{id}/auth")
|
@ManagerAuth
|
public R get(@PathVariable("id") String id) {
|
return R.ok(matCodeService.selectById(String.valueOf(id)));
|
}
|
|
@RequestMapping(value = "/matCode/list/auth")
|
@ManagerAuth
|
public R list(@RequestParam(defaultValue = "1")Integer curr,
|
@RequestParam(defaultValue = "10")Integer limit,
|
@RequestParam(required = false)String orderByField,
|
@RequestParam(required = false)String orderByType,
|
@RequestParam Map<String, Object> param){
|
excludeTrash(param);
|
EntityWrapper<MatCode> wrapper = new EntityWrapper<>();
|
convert(param, wrapper);
|
if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));}
|
return R.ok(matCodeService.selectPage(new Page<>(curr, limit), wrapper));
|
}
|
|
private void convert(Map<String, Object> map, EntityWrapper wrapper){
|
for (Map.Entry<String, Object> entry : map.entrySet()){
|
if (entry.getKey().endsWith(">")) {
|
wrapper.ge(Cools.deleteChar(entry.getKey()), DateUtils.convert(String.valueOf(entry.getValue())));
|
} else if (entry.getKey().endsWith("<")) {
|
wrapper.le(Cools.deleteChar(entry.getKey()), DateUtils.convert(String.valueOf(entry.getValue())));
|
} else {
|
wrapper.like(entry.getKey(), String.valueOf(entry.getValue()));
|
}
|
}
|
}
|
|
@RequestMapping(value = "/matCode/add/auth")
|
@ManagerAuth
|
public R add(MatCode matCode) {
|
matCode.setModiUser(getUserId());
|
matCode.setModiTime(new Date());
|
matCode.setAppeUser(getUserId());
|
matCode.setAppeTime(new Date());
|
matCodeService.insert(matCode);
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/matCode/update/auth")
|
@ManagerAuth
|
public R update(MatCode matCode){
|
if (Cools.isEmpty(matCode) || null==matCode.getMatNo()){
|
return R.error();
|
}
|
matCode.setModiUser(getUserId());
|
matCode.setModiTime(new Date());
|
matCodeService.updateById(matCode);
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/matCode/delete/auth")
|
@ManagerAuth
|
public R delete(@RequestParam String param){
|
List<MatCode> list = JSONArray.parseArray(param, MatCode.class);
|
if (Cools.isEmpty(list)){
|
return R.error();
|
}
|
for (MatCode entity : list){
|
matCodeService.delete(new EntityWrapper<>(entity));
|
}
|
return R.ok();
|
}
|
|
// 导出
|
@RequestMapping(value = "/matCode/export/auth")
|
@ManagerAuth(value = ManagerAuth.Auth.NONE, memo = "物料编码数据导出")
|
public void export(@RequestParam(required = false) String fileName,
|
@RequestParam(required = false) Integer rowCount,
|
HttpServletResponse response) throws Exception {
|
List<MatCode> list = matCodeService.selectList(new EntityWrapper<>());
|
List<MatCodeExcel> excels = new ArrayList<>();
|
for (MatCode matCode : list) {
|
MatCodeExcel excel = new MatCodeExcel();
|
excel.setMatNo(matCode.getMatNo());
|
excel.setBarcode(matCode.getBarcode());
|
excel.setMatName(matCode.getMatName());
|
excel.setStr1(matCode.getStr1());
|
excel.setStr2(matCode.getStr2());
|
excels.add(excel);
|
if (rowCount != null && excels.size() >= rowCount){
|
break;
|
}
|
}
|
response.setContentType("application/vnd.ms-excel");
|
response.setCharacterEncoding("utf-8");
|
fileName = URLEncoder.encode(Cools.isEmpty(fileName)?"物料编码":fileName, "UTF-8");
|
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
EasyExcel.write(response.getOutputStream(), MatCodeExcel.class)
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
.sheet("表1")
|
.doWrite(excels);
|
}
|
|
// 导入
|
@RequestMapping(value = "/matCode/import/auth")
|
@ManagerAuth(memo = "物料编码数据导入")
|
public R matCodeImport(MultipartFile file) throws IOException, InterruptedException {
|
Thread.sleep(2000);
|
EasyExcel.read(file.getInputStream(), MatCodeExcel.class, new MatCodeExcelListener()).sheet().doRead();
|
return R.ok();
|
}
|
|
// 打印
|
@RequestMapping(value = "/macCode/print/auth")
|
@ManagerAuth(memo = "物料编码打印")
|
public R matCodePrint(@RequestParam(value = "param[]") String[] param) {
|
if(Cools.isEmpty(param)) {
|
return R.parse(CodeRes.EMPTY);
|
}
|
List<MatCodePrint> res = new ArrayList<>();
|
for (String matNo : param){
|
MatCode matCode = matCodeService.selectById(matNo);
|
// 打印数据注入
|
MatCodePrint print = new MatCodePrint();
|
print.setMatNo(matCode.getMatNo());
|
print.setBarcode(matCode.getBarcode());
|
print.setMatName(matCode.getMatName());
|
print.setStr1(matCode.getStr1());
|
print.setStr2(matCode.getStr2());
|
res.add(print);
|
}
|
return R.ok().add(res);
|
}
|
|
@RequestMapping(value = "/macCode/code/auth")
|
// @ManagerAuth(memo = "物料编码条形码获取(type:1(条形码);2(二维码)")
|
public R matCodeBarcode(@RequestParam(defaultValue = "1") Integer type
|
, @RequestParam String param
|
, HttpServletResponse response) throws Exception {
|
if (Cools.isEmpty(param)){
|
return R.parse(BaseRes.EMPTY);
|
}
|
BufferedImage img;
|
if (type == 1) {
|
img = BarcodeUtils.encode(param);
|
} else {
|
img = QrCode.createImg(param);
|
}
|
if (!ImageIO.write(img, "jpg", response.getOutputStream())) {
|
throw new IOException("Could not write an image of format jpg");
|
}
|
response.getOutputStream().flush();
|
response.getOutputStream().close();
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/matCodeQuery/auth")
|
@ManagerAuth
|
public R query(String condition) {
|
EntityWrapper<MatCode> wrapper = new EntityWrapper<>();
|
wrapper.like("mat_no", condition);
|
Page<MatCode> page = matCodeService.selectPage(new Page<>(0, 10), wrapper);
|
List<Map<String, Object>> result = new ArrayList<>();
|
for (MatCode matCode : page.getRecords()){
|
Map<String, Object> map = new HashMap<>();
|
map.put("id", matCode.getMatNo());
|
map.put("value", matCode.getMatNo());
|
result.add(map);
|
}
|
return R.ok(result);
|
}
|
|
@RequestMapping(value = "/matCode/check/column/auth")
|
@ManagerAuth
|
public R query(@RequestBody JSONObject param) {
|
Wrapper<MatCode> wrapper = new EntityWrapper<MatCode>().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val"));
|
if (null != matCodeService.selectOne(wrapper)){
|
return R.parse(BaseRes.REPEAT).add(getComment(MatCode.class, String.valueOf(param.get("key"))));
|
}
|
return R.ok();
|
}
|
|
}
|