You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

464 lines
19 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package jnpf.controller;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.exception.DataException;
import jnpf.permission.entity.UserEntity;
import jnpf.service.*;
import jnpf.entity.*;
import jnpf.util.*;
import jnpf.model.bom.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.*;
import jnpf.annotation.JnpfField;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.base.vo.DownloadVO;
import jnpf.config.ConfigValueUtil;
import jnpf.base.entity.ProvinceEntity;
import java.io.IOException;
import java.util.stream.Collectors;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.exception.WorkFlowException;
import org.springframework.web.multipart.MultipartFile;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import jnpf.onlinedev.model.ExcelImFieldModel;
import jnpf.onlinedev.model.OnlineImport.ImportDataModel;
import jnpf.onlinedev.model.OnlineImport.ImportFormCheckUniqueModel;
import jnpf.onlinedev.model.OnlineImport.ExcelImportModel;
import jnpf.onlinedev.model.OnlineImport.VisualImportModel;
import cn.xuyanwu.spring.file.storage.FileInfo;
import lombok.Cleanup;
import jnpf.model.visualJson.config.HeaderModel;
import jnpf.base.model.ColumnDataModel;
import jnpf.base.util.VisualUtils;
import org.springframework.transaction.annotation.Transactional;
/**
* bom
* @版本: V3.5
* @版权: 引迈信息技术有限公司https://www.jnpfsoft.com
* @作者: JNPF开发平台组
* @日期: 2024-01-30
*/
@Slf4j
@RestController
@Tag(name = "bom" , description = "scm")
@RequestMapping("/api/scm/Bom")
public class BomController {
@Autowired
private GeneraterSwapUtil generaterSwapUtil;
@Autowired
private UserProvider userProvider;
@Autowired
private BomService bomService;
@Autowired
private BomItemService bomItemService;
@Autowired
private BomItemValueService bomItemValueService;
@Autowired
private ConfigValueUtil configValueUtil;
/**
* 列表
*
* @param bomPagination
* @return
*/
@Operation(summary = "获取列表")
@PostMapping("/getList")
public ActionResult list(@RequestBody BomPagination bomPagination)throws IOException{
List<BomEntity> list= bomService.getList(bomPagination);
List<Map<String, Object>> realList=new ArrayList<>();
for (BomEntity entity : list) {
Map<String, Object> bomMap=JsonUtil.entityToMap(entity);
bomMap.put("id", bomMap.get("id"));
//副表数据
//子表数据
List<BomItemEntity> bomItemList = bomService.getBomItemList(entity.getId(),bomPagination);
bomMap.put("tableField106",JsonUtil.getJsonToList(JsonUtil.getListToJsonArray(bomItemList)));
List<BomItemValueEntity> bomItemValueList = bomService.getBomItemValueList(entity.getId(),bomPagination);
bomMap.put("tableField118",JsonUtil.getJsonToList(JsonUtil.getListToJsonArray(bomItemValueList)));
realList.add(bomMap);
}
//数据转换
realList = generaterSwapUtil.swapDataList(realList, BomConstant.getFormData(), BomConstant.getColumnData(), bomPagination.getModuleId(),false);
//流程状态添加
for(Map<String, Object> vo:realList){
FlowTaskEntity flowTaskEntity = generaterSwapUtil.getInfoSubmit(String.valueOf(vo.get("id")), FlowTaskEntity::getStatus);
if (flowTaskEntity!=null){
vo.put("flowState",flowTaskEntity.getStatus());
}else{
vo.put("flowState",null);
}
//添加流程id
String flowId="";
if(vo.get("flowid")!=null){
flowId = String.valueOf(vo.get("flowid"));
}
if(vo.get("flowid".toUpperCase())!=null){
flowId = String.valueOf(vo.get("flowid".toUpperCase()));
}
if(StringUtil.isNotEmpty(flowId)){
vo.put("flowId" ,flowId);
}
}
//返回对象
PageListVO vo = new PageListVO();
vo.setList(realList);
PaginationVO page = JsonUtil.getJsonToBean(bomPagination, PaginationVO.class);
vo.setPagination(page);
return ActionResult.success(vo);
}
/**
* 创建
*
* @param bomForm
* @return
*/
@PostMapping("/{id}")
@Operation(summary = "创建")
public ActionResult create(@PathVariable("id") String id, @RequestBody @Valid BomForm bomForm) {
String b = bomService.checkForm(bomForm,0);
if (StringUtil.isNotEmpty(b)){
return ActionResult.fail(b );
}
try{
bomService.saveOrUpdate(bomForm, id ,true);
}catch(Exception e){
return ActionResult.fail("新增数据失败");
}
return ActionResult.success("创建成功");
}
/**
* 导出Excel
*
* @return
*/
@Operation(summary = "导出Excel")
@PostMapping("/Actions/Export")
public ActionResult Export(@RequestBody BomPagination bomPagination) throws IOException {
if (StringUtil.isEmpty(bomPagination.getSelectKey())){
return ActionResult.fail("请选择导出字段");
}
List<BomEntity> list= bomService.getList(bomPagination);
List<Map<String, Object>> realList=new ArrayList<>();
for (BomEntity entity : list) {
Map<String, Object> bomMap=JsonUtil.entityToMap(entity);
bomMap.put("id", bomMap.get("id"));
//副表数据
//子表数据
List<BomItemEntity> bomItemList = bomService.getBomItemList(entity.getId(),bomPagination);
bomMap.put("tableField106",JsonUtil.getJsonToList(JsonUtil.getListToJsonArray(bomItemList)));
List<BomItemValueEntity> bomItemValueList = bomService.getBomItemValueList(entity.getId(),bomPagination);
bomMap.put("tableField118",JsonUtil.getJsonToList(JsonUtil.getListToJsonArray(bomItemValueList)));
realList.add(bomMap);
}
//数据转换
realList = generaterSwapUtil.swapDataList(realList, BomConstant.getFormData(), BomConstant.getColumnData(), bomPagination.getModuleId(),false);
String[]keys=!StringUtil.isEmpty(bomPagination.getSelectKey())?bomPagination.getSelectKey():new String[0];
UserInfo userInfo=userProvider.get();
DownloadVO vo=this.creatModelExcel(configValueUtil.getTemporaryFilePath(),realList,keys,userInfo);
return ActionResult.success(vo);
}
/**
* 导出表格方法
*/
public DownloadVO creatModelExcel(String path,List<Map<String, Object>>list,String[]keys,UserInfo userInfo){
DownloadVO vo=DownloadVO.builder().build();
List<ExcelExportEntity> entitys=new ArrayList<>();
if(keys.length>0){
ExcelExportEntity tableField106ExcelEntity = new ExcelExportEntity("","tableField106");
List<ExcelExportEntity> tableField106List = new ArrayList<>();
ExcelExportEntity tableField118ExcelEntity = new ExcelExportEntity("","tableField118");
List<ExcelExportEntity> tableField118List = new ArrayList<>();
for(String key:keys){
switch(key){
case "bomCode" :
entitys.add(new ExcelExportEntity("BOM编号" ,"bomCode"));
break;
case "bomName" :
entitys.add(new ExcelExportEntity("BOM名称" ,"bomName"));
break;
case "bomNm" :
entitys.add(new ExcelExportEntity("BOM简称" ,"bomNm"));
break;
case "bomStatus" :
entitys.add(new ExcelExportEntity("BOM状态" ,"bomStatus"));
break;
case "tableField106-productId":
tableField106List.add(new ExcelExportEntity("商品名称" ,"productId"));
break;
case "tableField106-creatorTime":
tableField106List.add(new ExcelExportEntity("商品规格" ,"creatorTime"));
break;
case "tableField106-creatorUserId":
tableField106List.add(new ExcelExportEntity("类型" ,"creatorUserId"));
break;
case "tableField106-lastModifyTime":
tableField106List.add(new ExcelExportEntity("库存单位" ,"lastModifyTime"));
break;
case "tableField106-deleteMark":
tableField106List.add(new ExcelExportEntity("销售单位" ,"deleteMark"));
break;
case "tableField118-productId":
tableField118List.add(new ExcelExportEntity("商品名称" ,"productId"));
break;
case "tableField118-creatorTime":
tableField118List.add(new ExcelExportEntity("存货类型" ,"creatorTime"));
break;
case "tableField118-creatorUserId":
tableField118List.add(new ExcelExportEntity("规格" ,"creatorUserId"));
break;
case "tableField118-lastModifyUserId":
tableField118List.add(new ExcelExportEntity("库存单位" ,"lastModifyUserId"));
break;
case "tableField118-deleteUserId":
tableField118List.add(new ExcelExportEntity("条码" ,"deleteUserId"));
break;
case "tableField118-unit":
tableField118List.add(new ExcelExportEntity("操作单位" ,"unit"));
break;
case "tableField118-proportionNum":
tableField118List.add(new ExcelExportEntity("比例数量" ,"proportionNum"));
break;
default:
break;
}
}
if(tableField106List.size() > 0){
tableField106ExcelEntity.setList(tableField106List);
entitys.add(tableField106ExcelEntity);
}
if(tableField118List.size() > 0){
tableField118ExcelEntity.setList(tableField118List);
entitys.add(tableField118ExcelEntity);
}
}
ExportParams exportParams = new ExportParams(null, "表单信息");
exportParams.setType(ExcelType.XSSF);
try{
@Cleanup Workbook workbook = new HSSFWorkbook();
if (entitys.size()>0){
if (list.size()==0){
list.add(new HashMap<>());
}
//去除空数据
List<Map<String, Object>> dataList = new ArrayList<>();
for (Map<String, Object> map : list) {
int i = 0;
for (String key : keys) {
//子表
if (key.toLowerCase().startsWith("tablefield")) {
String tableField = key.substring(0, key.indexOf("-" ));
String field = key.substring(key.indexOf("-" ) + 1);
Object o = map.get(tableField);
if (o != null) {
List<Map<String, Object>> childList = (List<Map<String, Object>>) o;
for (Map<String, Object> childMap : childList) {
if (childMap.get(field) != null) {
i++;
}
}
}
} else {
Object o = map.get(key);
if (o != null) {
i++;
}
}
}
if (i > 0) {
dataList.add(map);
}
}
//复杂表头-表头和数据处理
ColumnDataModel columnDataModel = JsonUtil.getJsonToBean(BomConstant.getColumnData(), ColumnDataModel.class);
List<HeaderModel> complexHeaderList = columnDataModel.getComplexHeaderList();
if (!Objects.equals(columnDataModel.getType(), 3) && !Objects.equals(columnDataModel.getType(), 5)) {
entitys = VisualUtils.complexHeaderHandel(entitys, complexHeaderList);
dataList = VisualUtils.complexHeaderDataHandel(dataList, complexHeaderList);
}
workbook = ExcelExportUtil.exportExcel(exportParams, entitys, dataList);
}
String fileName = "表单信息" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".xlsx";
MultipartFile multipartFile = ExcelUtil.workbookToCommonsMultipartFile(workbook, fileName);
String temporaryFilePath = configValueUtil.getTemporaryFilePath();
FileInfo fileInfo = FileUploadUtils.uploadFile(multipartFile, temporaryFilePath, fileName);
vo.setName(fileInfo.getFilename());
vo.setUrl(UploaderUtil.uploaderFile(fileInfo.getFilename() + "#" + "Temporary") + "&name=" + fileName);
} catch (Exception e) {
log.error("信息导出Excel错误:{}", e.getMessage());
e.printStackTrace();
}
return vo;
}
/**
* 批量删除
* @param ids
* @return
*/
@DeleteMapping("/batchRemove")
@Transactional
@Operation(summary = "批量删除")
public ActionResult batchRemove(@RequestBody String ids){
List<String> idList = JsonUtil.getJsonToList(ids, String.class);
List<String> columnIdList = new ArrayList<>(20);
int i =0;
String errInfo = "";
for (String allId : idList){
FlowTaskEntity taskEntity = generaterSwapUtil.getInfoSubmit(allId, FlowTaskEntity::getId, FlowTaskEntity::getStatus);
if (taskEntity==null){
columnIdList.add(allId);
this.delete(allId);
}else if (taskEntity.getStatus().equals(0) || taskEntity.getStatus().equals(4)){
try {
generaterSwapUtil.deleteFlowTask(taskEntity);
columnIdList.add(allId);
this.delete(allId);
i++;
} catch (WorkFlowException e) {
errInfo = e.getMessage();
e.printStackTrace();
}
}
}
if (i == 0 && columnIdList.size()==0){
return ActionResult.fail("流程已发起,无法删除");
}
if (StringUtil.isNotEmpty(errInfo)){
return ActionResult.fail(errInfo);
}
return ActionResult.success("删除成功");
}
/**
* 编辑
* @param id
* @param bomForm
* @return
*/
@PutMapping("/{id}")
@Operation(summary = "更新")
public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid BomForm bomForm,
@RequestParam(value = "isImport", required = false) boolean isImport){
bomForm.setId(id);
if (!isImport) {
String b = bomService.checkForm(bomForm,1);
if (StringUtil.isNotEmpty(b)){
return ActionResult.fail(b );
}
}
BomEntity entity= bomService.getInfo(id);
if(entity!=null){
try{
bomService.saveOrUpdate(bomForm,id,false);
}catch(Exception e){
return ActionResult.fail("修改数据失败");
}
return ActionResult.success("更新成功");
}else{
return ActionResult.fail("更新失败,数据不存在");
}
}
/**
* 删除
* @param id
* @return
*/
@Operation(summary = "删除")
@DeleteMapping("/{id}")
@Transactional
public ActionResult delete(@PathVariable("id") String id){
BomEntity entity= bomService.getInfo(id);
if(entity!=null){
FlowTaskEntity taskEntity = generaterSwapUtil.getInfoSubmit(id, FlowTaskEntity::getId, FlowTaskEntity::getStatus);
if (taskEntity != null) {
try {
generaterSwapUtil.deleteFlowTask(taskEntity);
} catch (WorkFlowException e) {
e.printStackTrace();
}
}
//假删除
entity.setDeleteMark(1);
bomService.update(id,entity);
}
return ActionResult.success("删除成功");
}
/**
* 表单信息(详情页)
* 详情页面使用-转换数据
* @param id
* @return
*/
@Operation(summary = "表单信息(详情页)")
@GetMapping("/detail/{id}")
public ActionResult detailInfo(@PathVariable("id") String id){
BomEntity entity= bomService.getInfo(id);
if(entity==null){
return ActionResult.fail("表单数据不存在!");
}
Map<String, Object> bomMap=JsonUtil.entityToMap(entity);
bomMap.put("id", bomMap.get("id"));
//副表数据
//子表数据
List<BomItemEntity> bomItemList = bomService.getBomItemList(entity.getId());
bomMap.put("tableField106",JsonUtil.getJsonToList(JsonUtil.getListToJsonArray(bomItemList)));
List<BomItemValueEntity> bomItemValueList = bomService.getBomItemValueList(entity.getId());
bomMap.put("tableField118",JsonUtil.getJsonToList(JsonUtil.getListToJsonArray(bomItemValueList)));
bomMap = generaterSwapUtil.swapDataDetail(bomMap,BomConstant.getFormData(),"522003185808710981",false);
return ActionResult.success(bomMap);
}
/**
* 获取详情(编辑页)
* 编辑页面使用-不转换数据
* @param id
* @return
*/
@Operation(summary = "信息")
@GetMapping("/{id}")
public ActionResult info(@PathVariable("id") String id){
BomEntity entity= bomService.getInfo(id);
if(entity==null){
return ActionResult.fail("表单数据不存在!");
}
Map<String, Object> bomMap=JsonUtil.entityToMap(entity);
bomMap.put("id", bomMap.get("id"));
//副表数据
//子表数据
List<BomItemEntity> bomItemList = bomService.getBomItemList(entity.getId());
bomMap.put("bomItemList",JsonUtil.getJsonToList(JsonUtil.getListToJsonArray(bomItemList)));
List<BomItemValueEntity> bomItemValueList = bomService.getBomItemValueList(entity.getId());
bomMap.put("bomItemValueList",JsonUtil.getJsonToList(JsonUtil.getListToJsonArray(bomItemValueList)));
bomMap = generaterSwapUtil.swapDataForm(bomMap,BomConstant.getFormData(),BomConstant.TABLEFIELDKEY,BomConstant.TABLERENAMES);
return ActionResult.success(bomMap);
}
}