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.common.DateUtils;
|
import com.core.exception.CoolException;
|
import com.zy.asrs.entity.BomMat;
|
import com.zy.asrs.service.BomMatService;
|
import com.core.annotations.ManagerAuth;
|
import com.core.common.BaseRes;
|
import com.core.common.Cools;
|
import com.core.common.R;
|
import com.zy.common.entity.MatExcel;
|
import com.zy.common.web.BaseController;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.ss.usermodel.DataFormatter;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
@Slf4j
|
@RestController
|
public class BomMatController extends BaseController {
|
|
@Autowired
|
private BomMatService bomMatService;
|
|
@RequestMapping(value = "/bomMat/{id}/auth")
|
@ManagerAuth
|
public R get(@PathVariable("id") String id) {
|
return R.ok(bomMatService.selectById(String.valueOf(id)));
|
}
|
|
@RequestMapping(value = "/bomMat/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){
|
EntityWrapper<BomMat> wrapper = new EntityWrapper<>();
|
excludeTrash(param);
|
convert(param, wrapper);
|
if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));}
|
return R.ok(bomMatService.selectPage(new Page<>(curr, limit), wrapper));
|
}
|
|
private <T> void convert(Map<String, Object> map, EntityWrapper<T> wrapper){
|
for (Map.Entry<String, Object> entry : map.entrySet()){
|
String val = String.valueOf(entry.getValue());
|
if (val.contains(RANGE_TIME_LINK)){
|
String[] dates = val.split(RANGE_TIME_LINK);
|
wrapper.ge(entry.getKey(), DateUtils.convert(dates[0]));
|
wrapper.le(entry.getKey(), DateUtils.convert(dates[1]));
|
} else {
|
wrapper.like(entry.getKey(), val);
|
}
|
}
|
}
|
|
@RequestMapping(value = "/bomMat/add/auth")
|
@ManagerAuth
|
public R add(BomMat bomMat) {
|
Date now = new Date();
|
bomMat.setZpalletAnfme(bomMat.getBomAnfme() * bomMat.getBomCount());
|
bomMat.setModiUser(getUserId());
|
bomMat.setModiTime(now);
|
bomMat.setAppeUser(getUserId());
|
bomMat.setAppeTime(now);
|
bomMatService.insert(bomMat);
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/bomMat/update/auth")
|
@ManagerAuth
|
public R update(BomMat bomMat){
|
if (Cools.isEmpty(bomMat) || null==bomMat.getId()){
|
return R.error();
|
}
|
bomMat.setZpalletAnfme(bomMat.getBomAnfme() * bomMat.getBomCount());
|
bomMat.setModiUser(getUserId());
|
bomMat.setModiTime(new Date());
|
bomMatService.updateById(bomMat);
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/bomMat/delete/auth")
|
@ManagerAuth
|
public R delete(@RequestParam(value="ids[]") Long[] ids){
|
for (Long id : ids){
|
bomMatService.deleteById(id);
|
}
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/bomMat/export/auth")
|
@ManagerAuth
|
public R export(@RequestBody JSONObject param){
|
EntityWrapper<BomMat> wrapper = new EntityWrapper<>();
|
List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class);
|
Map<String, Object> map = excludeTrash(param.getJSONObject("bomMat"));
|
convert(map, wrapper);
|
List<BomMat> list = bomMatService.selectList(wrapper);
|
return R.ok(exportSupport(list, fields));
|
}
|
|
@RequestMapping(value = "/bomMatQuery/auth")
|
@ManagerAuth
|
public R query(String condition) {
|
EntityWrapper<BomMat> wrapper = new EntityWrapper<>();
|
wrapper.like("id", condition);
|
Page<BomMat> page = bomMatService.selectPage(new Page<>(0, 10), wrapper);
|
List<Map<String, Object>> result = new ArrayList<>();
|
for (BomMat bomMat : page.getRecords()){
|
Map<String, Object> map = new HashMap<>();
|
map.put("id", bomMat.getId());
|
map.put("value", bomMat.getId());
|
result.add(map);
|
}
|
return R.ok(result);
|
}
|
|
@RequestMapping(value = "/bomMat/check/column/auth")
|
@ManagerAuth
|
public R query(@RequestBody JSONObject param) {
|
Wrapper<BomMat> wrapper = new EntityWrapper<BomMat>().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val"));
|
if (null != bomMatService.selectOne(wrapper)){
|
return R.parse(BaseRes.REPEAT).add(getComment(BomMat.class, String.valueOf(param.get("key"))));
|
}
|
return R.ok();
|
}
|
|
/**
|
* excel导入
|
*/
|
@PostMapping(value = "/bomMat/excel/import/auth")
|
@ManagerAuth(memo = "组件档案excel导入")
|
@Transactional
|
public R cstmrExcelImport(MultipartFile file) throws IOException {
|
InputStream inStream = file.getInputStream();
|
String fileMime = file.getContentType();
|
int excelVersion = 2007;
|
if ("application/vnd.ms-excel".equals(fileMime)) {
|
excelVersion = 2003;
|
}
|
Workbook book = null;
|
try {
|
if (excelVersion == 2003) {
|
book = new HSSFWorkbook(inStream);
|
}
|
else { // 当 excel 是 2007 时
|
book = new XSSFWorkbook(inStream);
|
}
|
} catch (Exception e) {
|
log.error("fail", e);
|
return R.error("导入文件格式错误,请使用xls后缀的文件!");
|
}
|
|
Sheet sheet = book.getSheetAt(0);
|
int totalRows = sheet.getLastRowNum() + 1; // 总
|
Long userId = getUserId();
|
Date now = new Date();
|
DataFormatter dataFormatter = new DataFormatter();
|
|
for (int i = 1; i < totalRows; i++) {
|
Row row = sheet.getRow(i);
|
// 部件品号
|
String unitNum = dataFormatter.formatCellValue(row.getCell(0));
|
// 部件品名
|
String unitName = dataFormatter.formatCellValue(row.getCell(1));
|
// 部件规格
|
String unitSpace = dataFormatter.formatCellValue(row.getCell(2));
|
// 组件品号
|
String bomNum = dataFormatter.formatCellValue(row.getCell(3));
|
// 组件品名
|
String bomName = dataFormatter.formatCellValue(row.getCell(4));
|
// 组件规格
|
String bomSpace = dataFormatter.formatCellValue(row.getCell(5));
|
// 元件品号
|
String elementNum = dataFormatter.formatCellValue(row.getCell(6));
|
// 元件品名
|
String elementName = dataFormatter.formatCellValue(row.getCell(7));
|
// 元件规格
|
String elementSpace = dataFormatter.formatCellValue(row.getCell(8));
|
// 备注
|
String memo = dataFormatter.formatCellValue(row.getCell(9));
|
// 套数
|
Double bomCount = Double.parseDouble(dataFormatter.formatCellValue(row.getCell(10)));
|
// 组成用量
|
Double bomAnfme = Double.parseDouble(dataFormatter.formatCellValue(row.getCell(11)));
|
BomMat bomMat = bomMatService.selectByThreeCode(unitNum, bomNum, elementNum);
|
// 没有就新建,有就更新
|
if (Cools.isEmpty(bomMat)) {
|
createBomMat(unitNum, unitName, unitSpace, bomNum, bomName, bomSpace, elementNum, elementName, elementSpace, memo, bomCount, bomAnfme,getUserId());
|
} else {
|
updateBomMat(bomMat,unitName,unitSpace,bomName,bomSpace,elementName,elementSpace,memo,bomCount,bomAnfme,userId);
|
}
|
|
}
|
return R.ok();
|
}
|
|
/**
|
* excel导入模板下载
|
*/
|
@RequestMapping(value = "/bomMat/excel/import/mould")
|
public void matExcelImportMould(HttpServletResponse response) throws IOException {
|
List<BomMat> excels = new ArrayList<>();
|
response.setContentType("application/vnd.ms-excel");
|
response.setCharacterEncoding("utf-8");
|
String fileName = URLEncoder.encode("组件档案Excel导入模板", "UTF-8");
|
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
EasyExcel.write(response.getOutputStream(), BomMat.class)
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
.sheet("sheet1")
|
.doWrite(excels);
|
}
|
|
private void createBomMat(String unitNum,String unitName,String unitSpace,
|
String bomNum,String bomName,String bomSpace,
|
String elementNum, String elementName,String elementSpace,
|
String memo,Double bomCount,Double bomAnfme,Long userId) {
|
Date now = new Date();
|
BomMat bomMat = new BomMat();
|
bomMat.setUnitNum(unitNum);
|
bomMat.setUnitName(unitName);
|
bomMat.setUnitSpace(unitSpace);
|
|
bomMat.setBomNum(bomNum);
|
bomMat.setBomName(bomName);
|
bomMat.setBomSpace(bomSpace);
|
|
bomMat.setElementNum(elementNum);
|
bomMat.setElementName(elementName);
|
bomMat.setElementSpace(elementSpace);
|
|
bomMat.setMemo(memo);
|
bomMat.setBomCount(bomCount);
|
bomMat.setBomAnfme(bomAnfme);
|
bomMat.setZpalletAnfme(bomAnfme * bomCount);
|
|
bomMat.setModiUser(userId);
|
bomMat.setModiTime(now);
|
bomMat.setAppeUser(userId);
|
bomMat.setAppeTime(now);
|
|
bomMat.setIsDeleted((short)0);
|
if (!bomMatService.insert(bomMat)) {
|
throw new CoolException("新建组件档案失败!");
|
}
|
}
|
private void updateBomMat(BomMat bomMat,String unitName,String unitSpace,String bomName,String bomSpace,String elementName,String elementSpace,String memo,Double bomCount,Double bomAnfme,Long userId) {
|
Date now = new Date();
|
bomMat.setUnitName(unitName);
|
bomMat.setUnitSpace(unitSpace);
|
bomMat.setBomName(bomName);
|
bomMat.setBomSpace(bomSpace);
|
bomMat.setElementName(elementName);
|
bomMat.setElementSpace(elementSpace);
|
bomMat.setMemo(memo);
|
bomMat.setBomCount(bomCount);
|
bomMat.setBomAnfme(bomAnfme);
|
bomMat.setZpalletAnfme(bomCount*bomAnfme);
|
bomMat.setModiUser(userId);
|
bomMat.setModiTime(now);
|
if (!bomMatService.update(bomMat,new EntityWrapper<BomMat>().eq("unit_num",bomMat.getUnitNum()).eq("bom_num",bomMat.getBomNum()).eq("element_num",bomMat.getElementNum()))) {
|
throw new CoolException("更新组件档案失败!");
|
}
|
|
}
|
}
|