evoToK3Cloud/ruoyi-system/src/main/java/com/ruoyi/system/controller/ImMaterialController.java

438 lines
18 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ruoyi.system.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.JdUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ImMaterial;
import com.ruoyi.system.domain.bo.ImMaterialBo;
import com.ruoyi.system.domain.vo.ImMaterialVo;
import com.ruoyi.system.mapper.ImMaterialMapper;
import com.ruoyi.system.service.IImMaterialService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.ruoyi.common.core.mapper.BaseMapperPlus.log;
/**
* 物料检索
*
* @author tzy
* @date 2024-03-12
*/
/*@Validated*/
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/material")
public class ImMaterialController extends BaseController {
private final IImMaterialService iImMaterialService;
@Resource
private final ImMaterialMapper imMaterialMapper;
/**
* 查询物料检索列表
*/
@SaCheckPermission("system:material:list")
@GetMapping("/list")
public TableDataInfo<ImMaterialVo> list(ImMaterialBo bo, PageQuery pageQuery) {
return iImMaterialService.queryPageList(bo, pageQuery);
}
@GetMapping("/list2")
public TableDataInfo<ImMaterialVo> list2(ImMaterialBo bo, PageQuery pageQuery) {
return iImMaterialService.queryPageList(bo, pageQuery);
}
/**
* 导出物料检索列表
*/
@SaCheckPermission("system:material:export")
@Log(title = "物料检索", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(ImMaterialBo bo, HttpServletResponse response) {
List<ImMaterialVo> list = iImMaterialService.queryList(bo);
ExcelUtil.exportExcel(list, "物料检索", ImMaterialVo.class, response);
}
/**
* 获取物料检索详细信息
*
* @param id 主键
*/
//@SaCheckPermission("system:material:query")
@GetMapping("/{id}")
public R<ImMaterialVo> getInfo(@NotNull(message = "主键不能为空")
@PathVariable Long id) {
return R.ok(iImMaterialService.queryById(id));
}
/**
* 新增物料检索
*/
@SaCheckPermission("system:material:add")
@Log(title = "物料检索", businessType = BusinessType.INSERT)
@RepeatSubmit()
@PostMapping()
public R<Void> add(@Validated(AddGroup.class) @RequestBody ImMaterialBo bo) {
return toAjax(iImMaterialService.insertByBo(bo));
}
/**
* 修改物料检索
*/
@SaCheckPermission("system:material:edit")
@Log(title = "物料检索", businessType = BusinessType.UPDATE)
@RepeatSubmit()
@PutMapping()
public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImMaterialBo bo) {
return toAjax(iImMaterialService.updateByBo(bo));
}
/**
* 删除物料检索
*
* @param ids 主键串
*/
@SaCheckPermission("system:material:remove")
@Log(title = "物料检索", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public R<Void> remove(@NotEmpty(message = "主键不能为空")
@PathVariable Long[] ids) {
return toAjax(iImMaterialService.deleteWithValidByIds(Arrays.asList(ids), true));
}
@PostMapping("/importTemplate")
public void importTemplate(HttpServletResponse response) {
ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", ImMaterialBo.class, response);
}
@SaCheckPermission("system:problemCreation:export")
@Log(title = "更新单重", businessType = BusinessType.EXPORT)
@PostMapping("/export1")
public void compareAndMerge() throws IOException {
//读取提供的单重信息
String pathB = "C:\\Users\\Administrator\\Desktop\\物料编码汇总表.xlsx";
String outputPath = "D:\\电子档案";
try (
FileInputStream fisB = new FileInputStream(pathB);
Workbook workbookB = new XSSFWorkbook(fisB);
) {
Sheet sheetB = workbookB.getSheetAt(0);
Map<String, Double> bMap = new HashMap<>();
for (Row row : sheetB) {
try {
Cell materialCell = row.getCell(0);
Cell weightCell = row.getCell(3);
if (materialCell == null || weightCell == null) {
continue; // 如果单元格为空,则跳过当前行
}
String materialCode;
Double weight;
if (materialCell.getCellType() == CellType.STRING) {
materialCode = materialCell.getStringCellValue();
} else {
continue; // 处理意外的单元格类型
}
if (weightCell.getCellType() == CellType.NUMERIC) {
weight = weightCell.getNumericCellValue();
} else {
continue; // 处理意外的单元格类型
}
bMap.put(materialCode, weight);
// 更新数据库中的物料单重(示例)
// updateMaterialWeight(materialCode, weight); // 实现这个方法
} catch (Exception e) {
// 记录或处理特定的异常
e.printStackTrace();
}
}
} catch (IOException e) {
// 处理文件IO异常
e.printStackTrace();
}
}
/**
* 导入数据
*
* @param file 导入文件
*/
@Log(title = "物料导入", businessType = BusinessType.IMPORT)
@SaCheckPermission("system:material:import")
@PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
ExcelResult<ImMaterialVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), ImMaterialVo.class, true);
List<ImMaterialVo> volist = excelResult.getList();
List<ImMaterial> list = BeanUtil.copyToList(volist, ImMaterial.class);
for (ImMaterial imMaterial : list) {
if (iImMaterialService.selectByCodeAndName(imMaterial.getMaterialCode(),imMaterial.getMaterialName()) != null) {
if (imMaterial.getMaterialCode().startsWith("009")) {
imMaterial.setImCategory("标准件");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
} else if (imMaterial.getMaterialCode().startsWith("002N") || imMaterial.getMaterialCode().startsWith("002Y")) {
imMaterial.setImCategory("刀具");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
} else if (imMaterial.getMaterialCode().startsWith("015")) {
imMaterial.setImCategory("原材料");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}else if (imMaterial.getMaterialCode().startsWith("005")) {
imMaterial.setImCategory("危化品");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}else if (imMaterial.getMaterialCode().startsWith("006")) {
imMaterial.setImCategory("生产耗材");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}else if (imMaterial.getMaterialCode().startsWith("017")) {
imMaterial.setImCategory("生产配件");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}else if (imMaterial.getMaterialCode().startsWith("001")) {
imMaterial.setImCategory("工具");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}else if (imMaterial.getMaterialCode().startsWith("003")) {
imMaterial.setImCategory("量具");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}else if (imMaterial.getMaterialCode().startsWith("004")) {
imMaterial.setImCategory("工装");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}else if (imMaterial.getMaterialCode().startsWith("008")) {
imMaterial.setImCategory("生产维修配件");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}else if (imMaterial.getMaterialCode().startsWith("016")) {
imMaterial.setImCategory("生产电料");
//设置对应编码
imMaterial.setClassificationNumber(imMaterial.getMaterialCode().substring(0,6));
}
iImMaterialService.updateByCode(imMaterial);
//iImMaterialService.selectByCodeAndName(imMaterial.getMaterialCode(),imMaterial.getMaterialName());
} else {
imMaterialMapper.insert(imMaterial);
}
}
return R.ok(excelResult.getAnalysis());
}
@XxlJob("updateMaterials")
public Boolean updateMaterials() throws Exception {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<ImMaterial> imMaterials = updateJdMaterial(sdf.format(date));
Boolean result = iImMaterialService.updateByFMid(imMaterials);
return result;
}
public List<ImMaterial> updateJdMaterial(String date) throws Exception {
K3CloudApi client = new K3CloudApi();
//请求参数要求为json字符串
JsonObject json = new JsonObject();
json.addProperty("FormId", "BD_MATERIAL");
json.addProperty("FieldKeys", "FMATERIALID,FNumber,FName,FCategoryID.FNumber,F_HBYT_DZ,F_SVRI_Assistant.FNumber,FErpClsID,FBaseUnitId.FName,FModifyDate,FForbidStatus");
JsonArray filterString = new JsonArray();
/*JsonObject filterObject = new JsonObject();
filterObject.addProperty("FieldName", "FForbidStatus"); // 使用传入的 fieldName
filterObject.addProperty("Compare", "105");
filterObject.addProperty("Value", "A");
filterObject.addProperty("Left", "");
filterObject.addProperty("Right", "");
filterObject.addProperty("Logic", 0);
filterString.add(filterObject);*/
JsonObject filterObject1 = new JsonObject();
filterObject1.addProperty("FieldName", "FCreateDate"); // 使用传入的 fieldName
filterObject1.addProperty("Compare", "93");
filterObject1.addProperty("Value", date);
filterObject1.addProperty("Left", "");
filterObject1.addProperty("Right", "");
filterObject1.addProperty("Logic", 1);
filterString.add(filterObject1);
JsonObject filterObject2 = new JsonObject();
filterObject2.addProperty("FieldName", "FModifyDate"); // 使用传入的 fieldName
filterObject2.addProperty("Compare", "93");
filterObject2.addProperty("Value", date);
filterObject2.addProperty("Left", "");
filterObject2.addProperty("Right", "");
filterObject2.addProperty("Logic", 0);
filterString.add(filterObject2);
log.debug("构建查询条件...");
json.add("FilterString", filterString);
json.addProperty("OrderString", "");
json.addProperty("TopRowCount", 0);
json.addProperty("Limit", 10000);
json.addProperty("SubSystemId", "");
int pageNo = 0;
json.addProperty("StartRow", pageNo);
// 通过无限循环来持续查询数据,直到没有更多数据为止
JSONArray list = new JSONArray();
JSONArray objects1;
while (true) {
// 将当前的JSON对象转换为字符串以符合查询接口的参数要求
String jsonData = json.toString();
try {
// 调用客户端的billQuery方法传入JSON字符串作为查询参数
String input = String.valueOf(client.billQuery(jsonData));
// 将返回的JSON字符串转换为JSONArray对象
objects1 = new JSONArray(input);
/// 如果返回的数组为空,则表示没有更多数据,退出循环
if (objects1.size() == 0) {
break;
}
// 将本次查询到的数据添加到总的查询结果数组中
list.addAll(objects1);
// 更新页码,为下一次查询准备
pageNo++;
// 更新JSON对象中的StartRow属性用于下一次查询的分页
json.addProperty("StartRow", pageNo * 10000);
} catch (Exception e) {
e.printStackTrace();
}
}
ObjectMapper objectMapper = new ObjectMapper();
List<ImMaterial> materialList = objectMapper.readValue(
list.toString(),
new TypeReference<List<ImMaterial>>() {
});
return materialList;
}
public List<ImMaterial> loadMaterial() throws JsonProcessingException {
K3CloudApi client = new K3CloudApi();
//请求参数要求为json字符串
JsonObject json = new JsonObject();
json.addProperty("FormId", "BD_MATERIAL");
json.addProperty("FieldKeys", "FMATERIALID,FNumber,FName,F_HBYT_PP,FCategoryID.FNumber,F_HBYT_DZ,F_SVRI_Assistant.FNumber,FErpClsID,FBaseUnitId.FName,FModifyDate,FForbidStatus,FIsVmiBusiness");
JsonArray filterString = new JsonArray();
log.debug("构建查询条件...");
json.add("FilterString", filterString);
json.addProperty("OrderString", "");
json.addProperty("TopRowCount", 0);
json.addProperty("Limit", 10000);
json.addProperty("SubSystemId", "");
int pageNo = 0;
json.addProperty("StartRow", pageNo);
// 通过无限循环来持续查询数据,直到没有更多数据为止
JSONArray list = new JSONArray();
JSONArray objects1;
while (true) {
// 将当前的JSON对象转换为字符串以符合查询接口的参数要求
String jsonData = json.toString();
try {
// 调用客户端的billQuery方法传入JSON字符串作为查询参数
String input = String.valueOf(client.billQuery(jsonData));
// 将返回的JSON字符串转换为JSONArray对象
objects1 = new JSONArray(input);
/// 如果返回的数组为空,则表示没有更多数据,退出循环
if (objects1.size() == 0) {
break;
}
// 将本次查询到的数据添加到总的查询结果数组中
list.addAll(objects1);
// 更新页码,为下一次查询准备
pageNo++;
// 更新JSON对象中的StartRow属性用于下一次查询的分页
json.addProperty("StartRow", pageNo * 10000);
} catch (Exception e) {
e.printStackTrace();
}
}
ObjectMapper objectMapper = new ObjectMapper();
List<ImMaterial> materialList = objectMapper.readValue(
list.toString(),
new TypeReference<List<ImMaterial>>() {
});
return materialList;
}
/**
* 保存所有的物料编码
* @return List<ImMaterial>
*/
@XxlJob("insertJDMaterial")
@GetMapping("/insertJDMaterial")
public List<ImMaterial> insertJDMaterial() throws Exception {
List<ImMaterial> materialList = loadMaterial();
iImMaterialService.insertJDMaterial(materialList);
return materialList;
}
@PostMapping("/updateInventory")
public String updateInventory() {
List<ImMaterial> imMaterialCodeList = imMaterialMapper.selectList();
iImMaterialService.updateInventory(imMaterialCodeList);
return "string";
}
/**
* 搜索物料信息
*/
/**
* 搜索物料信息
*/
@GetMapping("/search")
public TableDataInfo search(@RequestParam(value = "query", required = false) String query) {
return iImMaterialService.search(query);
}
}