文件上产下载修改

This commit is contained in:
lym
2024-05-28 18:17:14 +08:00
parent 924da20fe7
commit a2e57e91e7
15 changed files with 403 additions and 42 deletions
@@ -58,4 +58,6 @@ public interface ErrorCodeConstants {
ErrorCode PLATE_DATA_ERROR = new ErrorCode(1_002_029_012, "板材明细获取错误");
ErrorCode FILE_READ_SUCCESS = new ErrorCode(1_002_029_012, "文件读取成功");
ErrorCode FILE_READ_ERR = new ErrorCode(1_002_029_012, "文件读取错误");
ErrorCode FILE_ADD_ERR = new ErrorCode(1_002_029_012, "文件已经存在是否强制保存");
ErrorCode FILE_UPLOAD_ERR = new ErrorCode(1_002_029_012, "文件为空");
}
@@ -120,36 +120,9 @@ public class OrderController {
@GetMapping("/get-import-template")
@Operation(summary = "获得导入生产单模板")
@Parameter(name = "type", description = "文件类型", required = true, example = "0")
public void importTemplate(HttpServletResponse response, @RequestParam("type") Integer type) {
try {
// path是指想要下载的文件的路径
File file = new File(ResourceUtils.getURL("classpath:").getPath() + "\\files\\生产单新增Excel模板.xlsx");
log.info(file.getPath());
// 获取文件名
String filename = file.getName();
// 获取文件后缀名
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
log.info("文件后缀名:" + ext);
// 将文件写入输入流
FileInputStream fileInputStream = new FileInputStream(file);
InputStream fis = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
@Parameter(name = "value", description = "文件类型", required = true, example = "0")
public void importTemplate(HttpServletResponse response, @RequestParam("value") String value) {
orderService.exportTemplate(response, value);
}
// 清理生产单
@@ -289,8 +262,8 @@ public class OrderController {
// 判断是否读取有误
if (excelReadUtil.isErr() > 1) {
// 返回文件
excelReadUtil.write(response, (Map<Object, Object>) map.get("order"), (List<OrderPlateImportExcelVO>) map.get("plate"));
System.err.println(orderService.getSavePath()+ "生产单Excel导入错误导出模板.xlsx");
excelReadUtil.write(response, (Map<Object, Object>) map.get("order"), (List<OrderPlateImportExcelVO>) map.get("plate"),orderService.getSavePath()+ "\\"+ "生产单Excel导入错误导出模板.xlsx");
throw exception(FILE_READ_ERR);
}else {
// 获取数据
@@ -316,13 +289,22 @@ public class OrderController {
}
@GetMapping("/uploadFiles")
@Operation(summary = "导入生产数据文件模板")
@Operation(summary = "导入生产数据文件模板")
@Parameters({
@Parameter(name = "file", description = "文件", required = true),
@Parameter(name = "type", description = "文件类型名称", required = true)
@Parameter(name = "type", description = "是否强制上传", required = false)
})
@PreAuthorize("@ss.hasPermission('executor:order:upload')")
public CommonResult<List<OrderBodyRespVO>> getOrderBody(@RequestParam("orderId") Long orderId) {
return null;
public CommonResult<String> getOrderBody(@RequestPart("file") MultipartFile file,
@RequestParam(value = "type", required = false, defaultValue = "false") Boolean type) {
return success(orderService.importTemplate(file,type));
}
@GetMapping("/printing")
@Operation(summary = "打印/导出")
@PreAuthorize("@ss.hasPermission('executor:order:query')")
@Parameter(name = "type", description = "文件类型", required = true, example = "0")
public CommonResult<String> getOrderPrinting(@Valid OrderPageReqVO pageReqVO, @RequestParam("type") String type) {
return success(orderService.getPrintData(pageReqVO,type));
}
}
@@ -74,4 +74,6 @@ public class OrderPageReqVO extends PageParam {
private LocalDateTime[] createTime;
private Boolean deleted;
private Long organId;
}
@@ -0,0 +1,155 @@
package com.cf.imes.module.executor.controller.admin.plate.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import static com.cf.imes.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "管理后台 - 板件对应板材信息")
public class PlateGoodsRespVO {
@Schema(description = "生产单号", example = "11087")
private Long orderId;
@Schema(description = "板名称", example = "晨丰")
private String name;
@Schema(description = "自定义板编号,设计有板编号,则保留设计板编号;设计没有且在机台设置中设置了自定义板编号规则,则按规则自动生成")
private String plateNo;
@Schema(description = "板类型,0 层板 1 立板 2 背板", example = "1")
private Integer type;
@Schema(description = "商品 ID", example = "14195")
private String goodsId;
@Schema(description = "商品名称", example = "西西")
private String goodsName;
@Schema(description = "商品材质", example = "颗粒板")
private String material;
@Schema(description = "商品颜色", example = "白色")
private String color;
@Schema(description = "商品宽度", example = "14195")
private BigDecimal goodsWidth;
@Schema(description = "商品高度", example = "14195")
private BigDecimal goodsHeight;
@Schema(description = "商品厚度", example = "14195")
private BigDecimal goodsThickness;
@Schema(description = "商品价格", example = "14195")
private BigDecimal price;
@Schema(description = "商品品牌", example = "柏柏")
private String brand;
@Schema(description = "商品规格", example = "14195*21")
private String spec;
@Schema(description = "宽度")
private BigDecimal width;
@Schema(description = "高度")
private BigDecimal height;
@Schema(description = "厚度")
private BigDecimal thickness;
@Schema(description = "拆单宽度")
private BigDecimal splitWidth;
@Schema(description = "拆单高度")
private BigDecimal splitHeight;
@Schema(description = "拆单厚度")
private BigDecimal splitThickness;
@Schema(description = "左封边厚度")
private BigDecimal sealLeft;
@Schema(description = "右封边厚度")
private BigDecimal sealRight;
@Schema(description = "上封边厚度")
private BigDecimal sealUp;
@Schema(description = "下封边厚度")
private BigDecimal sealDown;
@Schema(description = "面积")
private BigDecimal area;
@Schema(description = "纹路类型,0 正纹 1 可翻转 2 反纹")
private Integer texture;
@Schema(description = "孔面类型,0 正面 1 反面 2 侧面")
private Integer holeFace;
@Schema(description = "排孔类型,0 正纹 1 反面 2 随意面")
private Integer holeArrange;
@Schema(description = "异型孔数量", example = "28477")
private Short unregularPointCount;
@Schema(description = "正面孔数量", example = "20823")
private Short frontHoleCount;
@Schema(description = "背面孔数量", example = "3441")
private Short backHoleCount;
@Schema(description = "侧面孔数量", example = "25509")
private Short sideHoleCount;
@Schema(description = "正面造型量", example = "31547")
private Short frontModelCount;
@Schema(description = "背面造型量", example = "26725")
private Short backModelCount;
@Schema(description = "是否门板")
private Boolean isDoor;
@Schema(description = "开门类型,0 无 1 左 2 右 3 上 4 下", example = "2")
private Integer openDoorType;
@Schema(description = "异型偏移 x")
private Double offsetX;
@Schema(description = "异型偏移 y")
private Double offsetY;
@Schema(description = "是否孤形对角")
private Boolean isArcAcross;
@Schema(description = "模块类型 ID", example = "12800")
private Long moduleTypeId;
@Schema(description = "排单过滤类型,1 有挖穿造型 2 有挖穿孔 4 有二维刀路 8 可叠", example = "1")
private Integer filterType;
@Schema(description = "备注", example = "你说的对")
private String remark;
@Schema(description = "是否作废")
private Boolean isCancel;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
}
@@ -45,6 +45,7 @@ public interface OrderMapper extends BaseMapperX<OrderDO> {
.eqIfPresent(OrderDO::getSplitter, reqVO.getSplitter())
.eqIfPresent(OrderDO::getRemark, reqVO.getRemark())
.eqIfPresent(OrderDO::getDeleted, reqVO.getDeleted())
.eqIfPresent(OrderDO::getOrganId, reqVO.getOrganId())
.betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(OrderDO::getId));
}
@@ -101,4 +101,6 @@ public interface PlateMapper extends BaseMapperX<PlateDO> {
List<OptimizePlateDetialRespVO> selectPlate(@Param("orderId") Long orderId);
List<OptimizePlateDetialRespVO> selectPlateListByOrderIds(@Param("orderIds") List<Long> orderIds);
List<PlateGoodsRespVO> selectPlateGoodsList(@Param("orderId") Long orderId, @Param("organId") Long organId);
}
@@ -1,7 +1,9 @@
package com.cf.imes.module.executor.service.order;
import javax.servlet.http.HttpServletResponse;
import javax.validation.*;
import com.alibaba.fastjson.JSONObject;
import com.cf.imes.module.executor.controller.admin.order.vo.order.*;
import com.cf.imes.module.executor.controller.admin.order.vo.product.OrderBodyRespVO;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsRespVO;
@@ -12,6 +14,8 @@ import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.executor.dal.dataobject.orderBody.OrderBodyDO;
import com.cf.imes.module.executor.dal.dataobject.orderModuleExtra.OrderModuleExtraDO;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateDO;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.multipart.MultipartFile;
import java.util.Collection;
import java.util.List;
@@ -135,7 +139,31 @@ public interface OrderService {
List<GoodsDO> getGoodsList(Long orderId);
/**
* 文件导入
* 文件数据导入
*/
void importExcelData(OrderDO orderDO, List<?> list);
/**
* 模板文件导入保存
*/
String importTemplate(MultipartFile file,Boolean type);
/**
* 生产导入文件模板下载
*/
void exportTemplate(HttpServletResponse response, String value);
/**
* 打印/导出数据获取
*/
String getPrintData(OrderPageReqVO orderPageReqVO, String type);
default String getSavePath() {
ApplicationHome applicationHome = new ApplicationHome(this.getClass());
// 保存目录位置根据项目需求可随意更改
return applicationHome.getDir().getParentFile()
.getParentFile().getAbsolutePath() + "\\src\\main\\resources\\type";
}
}
@@ -1,5 +1,6 @@
package com.cf.imes.module.executor.service.order;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.smallbun.screw.core.util.CollectionUtils;
@@ -7,6 +8,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.mybatis.core.generator.SnowFlakeGenerator;
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.module.executor.controller.admin.order.vo.order.*;
@@ -38,14 +40,18 @@ import com.cf.imes.module.executor.util.fileConversion.admin.api.webcad.ApiDataP
import com.cf.imes.module.executor.util.fileConversion.admin.api.webcad.ApiTypeRealize;
import com.cf.imes.module.executor.util.fileConversion.admin.files.excel.ExcelTypeRealize;
import com.cf.imes.module.system.api.application.ApplicationApi;
import com.cf.imes.module.system.api.dict.DictDataApi;
import com.cf.imes.module.system.api.user.AdminUserApi;
import com.cf.imes.module.system.enums.ErrorCodeConstants;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.annotation.Validated;
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
@@ -54,6 +60,11 @@ import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.executor.dal.mysql.order.OrderMapper;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@@ -66,6 +77,7 @@ import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.e
import static com.cf.imes.module.executor.enums.ErrorCodeConstants.*;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.web.multipart.MultipartFile;
/**
* 生产单表 order_{N} Service 实现类
@@ -121,6 +133,9 @@ public class OrderServiceImpl implements OrderService {
@Resource
private ApiDataProduction apiOrderService;
@Resource
private DictDataApi dictDataApi;
@Resource
private IdentifierGenerator identifierGenerator;
@@ -132,7 +147,7 @@ public class OrderServiceImpl implements OrderService {
private static final int DEFAULT_DAYS_TO_ADD = 30;
private static final int PARTS_PAGE_MAX = 100;
private static final int RANDOM_NUMBER_MAX = 10;
@Override
public Long createOrder(OrderSaveReqVO createReqVO) {
@@ -587,4 +602,112 @@ public class OrderServiceImpl implements OrderService {
}
@Override
public String importTemplate(MultipartFile file, Boolean type) {
// 接收文件
if (file.isEmpty()) {
throw exception(FILE_UPLOAD_ERR);
}
// 获取文件名称信息
String fileName = file.getOriginalFilename();
String value = fileName;
String label = fileName;
// 判断存放文件路径中是否有相同名称的文件
if (!type) {
Path path = Paths.get(getSavePath(), fileName);
if (Files.exists(path))
throw exception(FILE_ADD_ERR);
} else {
// 获取文件名称信息
value = fileName.substring(0, fileName.lastIndexOf(".")) +
getRandomNumber(RANDOM_NUMBER_MAX) +
fileName.substring(fileName.lastIndexOf("."));
fileName = value;
}
try {
// 新增字典查询
dictDataApi.addDictData("upload_file_type", label, value);
// 保存文件
File saveFile = new File(getSavePath() + "\\" + fileName);
file.transferTo(saveFile);
return value;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void exportTemplate(HttpServletResponse response, String value) {
// 根据type获取文件名称
try {
// path是指想要下载的文件的路径
File file = new File(getSavePath()+ "\\" + value);
// 获取文件名
String filename = file.getName();
// 获取文件后缀名
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
// 将文件写入输入流
FileInputStream fileInputStream = new FileInputStream(file);
InputStream fis = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public String getPrintData(OrderPageReqVO orderPageReqVO, String type) {
orderPageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
PageResult<OrderDO> pageResult = orderMapper.selectPage(orderPageReqVO.setOrganId(SecurityFrameworkUtils.getLoginUser().getOrganId()));
List<OrderDO> orderDOList = pageResult.getList();
if (orderDOList == null || orderDOList.size() < 0) {
throw exception(ORDER_NOT_EXISTS);
}
List<Map<String, Object>> list = new ArrayList<>();
for (OrderDO orderDO : orderDOList) {
list.add(getDataByOrderIdAndType(orderDO, type));
}
return JSON.toJSONString(list);
}
// 根据ype 与 orderId获取相关信息
public Map<String, Object> getDataByOrderIdAndType(OrderDO orderDO, String type) {
Map<String, Object> map = new HashMap<>();
map.put("order", orderDO);
switch (type) {
case "001":// 板材明细
System.err.println(orderDO.getId());
map.put("plateGoods", plateMapper.selectPlateGoodsList(orderDO.getId(),SecurityFrameworkUtils.getLoginUser().getOrganId()));
break;
case "002":// 配件明细
break;
default:
// 处理默认情况,可以选择忽略或者做其他处理
break;
}
return map;
}
public String getRandomNumber(int length) {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(random.nextInt(10));
}
return sb.toString();
}
}
@@ -134,10 +134,8 @@ public class ExcelReadUtil { // 文件读取
// 写excel
public void write(HttpServletResponse response,
Map<Object, Object> orderMap,
List<OrderPlateImportExcelVO> list) throws IOException {
// 加载模板
String filePathErr = ResourceUtils.getURL("classpath:").getPath() + "\\files\\生产单Excel导入错误导出模板.xlsx";
List<OrderPlateImportExcelVO> list,
String filePathErr) throws IOException {
//输入流
// InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/生产单新增Excel模板.xlsx");
InputStream inputStream = ResourceUtils.getURL(filePathErr).openStream();
@@ -252,5 +252,54 @@
</select>
<select id="selectPlateGoodsList" resultType="com.cf.imes.module.executor.controller.admin.plate.vo.PlateGoodsRespVO">
select op.id,
op.goods_id,
ogs.goods_name,
op.front_hole_count,
op.back_hole_count,
op.side_hole_count,
op.unregular_point_count,
op.front_model_count,
op.back_model_count,
op.hole_arrange,
op.hole_face,
op.texture,
op.type,
op.open_door_type,
op.filter_type,
op.seal_left,
op.seal_right,
op.seal_up,
op.seal_down,
op.offset_x,
op.offset_y,
op.is_arc_across,
op.is_door,
op.is_cancel,
op.remark,
op.plate_no,
op.split_height,
op.split_width,
op.split_thickness,
op.module_type_id,
op.height,
op.width,
op.thickness,
op.area,
ogs.price,
ogs.brand,
ogs.height as goods_height,
ogs.width as goods_width,
ogs.thickness as goods_thickness,
ogs.color,
ogs.material,
ogs.spec
from order_plate op
left join order_goods ogs on op.goods_id = ogs.id
where op.order_id = #{orderId}
and op.organ_id = #{organId}
</select>
</mapper>