yuhdemo/cms/jnpf-mes/jnpf-example-controller/src/main/java/jnpf/controller/YysClassesController.java

323 lines
12 KiB
Java
Raw Normal View History

2026-01-30 16:22:13 +08:00
package jnpf.controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.xuyanwu.spring.file.storage.FileInfo;
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.base.model.ColumnDataModel;
import jnpf.base.util.VisualUtils;
import jnpf.base.vo.DownloadVO;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.config.ConfigValueUtil;
import jnpf.entity.YysClassesEntity;
import jnpf.model.visualJson.config.HeaderModel;
import jnpf.model.yysclasses.YysClassesConstant;
import jnpf.model.yysclasses.YysClassesForm;
import jnpf.model.yysclasses.YysClassesPagination;
import jnpf.service.YysClassesService;
import jnpf.util.*;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
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.validation.Valid;
import java.io.IOException;
import java.util.*;
/**
* yysClasses
* @版本 V3.5
* @版权 引迈信息技术有限公司https://www.jnpfsoft.com
* @作者 JNPF开发平台组
* @日期 2024-08-05
*/
@Slf4j
@RestController
@Tag(name = "yysClasses" , description = "example")
@RequestMapping("/api/example/YysClasses")
public class YysClassesController {
@Autowired
private GeneraterSwapUtil generaterSwapUtil;
@Autowired
private UserProvider userProvider;
@Autowired
private YysClassesService yysClassesService;
@Autowired
private ConfigValueUtil configValueUtil;
/**
* 列表
*
* @param yysClassesPagination
* @return
*/
@Operation(summary = "获取列表")
@PostMapping("/getList")
public ActionResult list(@RequestBody YysClassesPagination yysClassesPagination)throws IOException{
List<YysClassesEntity> list= yysClassesService.getList(yysClassesPagination);
List<Map<String, Object>> realList=new ArrayList<>();
for (YysClassesEntity entity : list) {
Map<String, Object> yysClassesMap=JsonUtil.entityToMap(entity);
yysClassesMap.put("id", yysClassesMap.get("id"));
//副表数据
//子表数据
realList.add(yysClassesMap);
}
//数据转换
realList = generaterSwapUtil.swapDataList(realList, YysClassesConstant.getFormData(), YysClassesConstant.getColumnData(), yysClassesPagination.getModuleId(),false);
//返回对象
PageListVO vo = new PageListVO();
vo.setList(realList);
PaginationVO page = JsonUtil.getJsonToBean(yysClassesPagination, PaginationVO.class);
vo.setPagination(page);
return ActionResult.success(vo);
}
/**
* 创建
*
* @param yysClassesForm
* @return
*/
@PostMapping()
@Operation(summary = "创建")
public ActionResult create(@RequestBody @Valid YysClassesForm yysClassesForm) {
String b = yysClassesService.checkForm(yysClassesForm,0);
if (StringUtil.isNotEmpty(b)){
return ActionResult.fail(b );
}
try{
yysClassesService.saveOrUpdate(yysClassesForm, null ,true);
}catch(Exception e){
return ActionResult.fail("新增数据失败");
}
return ActionResult.success("创建成功");
}
/**
* 导出Excel
*
* @return
*/
@Operation(summary = "导出Excel")
@PostMapping("/Actions/Export")
public ActionResult Export(@RequestBody YysClassesPagination yysClassesPagination) throws IOException {
if (StringUtil.isEmpty(yysClassesPagination.getSelectKey())){
return ActionResult.fail("请选择导出字段");
}
List<YysClassesEntity> list= yysClassesService.getList(yysClassesPagination);
List<Map<String, Object>> realList=new ArrayList<>();
for (YysClassesEntity entity : list) {
Map<String, Object> yysClassesMap=JsonUtil.entityToMap(entity);
yysClassesMap.put("id", yysClassesMap.get("id"));
//副表数据
//子表数据
realList.add(yysClassesMap);
}
//数据转换
realList = generaterSwapUtil.swapDataList(realList, YysClassesConstant.getFormData(), YysClassesConstant.getColumnData(), yysClassesPagination.getModuleId(),false);
String[]keys=!StringUtil.isEmpty(yysClassesPagination.getSelectKey())?yysClassesPagination.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){
for(String key:keys){
switch(key){
case "classesName" :
entitys.add(new ExcelExportEntity("班次名称" ,"classesName"));
break;
case "startTime" :
entitys.add(new ExcelExportEntity("开始时间" ,"startTime"));
break;
case "endTime" :
entitys.add(new ExcelExportEntity("结束时间" ,"endTime"));
break;
case "classesDuration" :
entitys.add(new ExcelExportEntity("班次时长h)" ,"classesDuration"));
break;
case "enabledStatus" :
entitys.add(new ExcelExportEntity("启用状态" ,"enabledStatus"));
break;
default:
break;
}
}
}
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(YysClassesConstant.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 id
* @param yysClassesForm
* @return
*/
@PutMapping("/{id}")
@Operation(summary = "更新")
public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid YysClassesForm yysClassesForm,
@RequestParam(value = "isImport", required = false) boolean isImport){
yysClassesForm.setId(id);
if (!isImport) {
String b = yysClassesService.checkForm(yysClassesForm,1);
if (StringUtil.isNotEmpty(b)){
return ActionResult.fail(b );
}
}
YysClassesEntity entity= yysClassesService.getInfo(id);
if(entity!=null){
try{
yysClassesService.saveOrUpdate(yysClassesForm,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){
YysClassesEntity entity= yysClassesService.getInfo(id);
if(entity!=null){
//主表数据删除
yysClassesService.delete(entity);
}
return ActionResult.success("删除成功");
}
/**
* 表单信息(详情页)
* 详情页面使用-转换数据
* @param id
* @return
*/
@Operation(summary = "表单信息(详情页)")
@GetMapping("/detail/{id}")
public ActionResult detailInfo(@PathVariable("id") String id){
YysClassesEntity entity= yysClassesService.getInfo(id);
if(entity==null){
return ActionResult.fail("表单数据不存在!");
}
Map<String, Object> yysClassesMap=JsonUtil.entityToMap(entity);
yysClassesMap.put("id", yysClassesMap.get("id"));
//副表数据
//子表数据
yysClassesMap = generaterSwapUtil.swapDataDetail(yysClassesMap,YysClassesConstant.getFormData(),"590163980832475013",false);
return ActionResult.success(yysClassesMap);
}
/**
* 获取详情(编辑页)
* 编辑页面使用-不转换数据
* @param id
* @return
*/
@Operation(summary = "信息")
@GetMapping("/{id}")
public ActionResult info(@PathVariable("id") String id){
YysClassesEntity entity= yysClassesService.getInfo(id);
if(entity==null){
return ActionResult.fail("表单数据不存在!");
}
Map<String, Object> yysClassesMap=JsonUtil.entityToMap(entity);
yysClassesMap.put("id", yysClassesMap.get("id"));
//副表数据
//子表数据
yysClassesMap = generaterSwapUtil.swapDataForm(yysClassesMap,YysClassesConstant.getFormData(),YysClassesConstant.TABLEFIELDKEY,YysClassesConstant.TABLERENAMES);
return ActionResult.success(yysClassesMap);
}
@Operation(summary = "根据id获取信息")
@GetMapping("/getInfoById/{id}")
public ActionResult getInfoById(@PathVariable String id) {
return ActionResult.success(yysClassesService.getInfoById(id));
}
}