刘光辉
14 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package jnpf.limsController;
 
import cn.dev33.satoken.annotation.SaCheckLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.DictionaryDataApi;
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.vo.DownloadVO;
import jnpf.constant.FileTypeConstant;
import jnpf.exception.DataException;
import jnpf.file.FileUploadApi;
import jnpf.limsEntity.LimsWendingxingFenxiDataVo;
import jnpf.limsEntity.LimsWendingxingFenxiOptionsVo;
import jnpf.limsEntity.LimsWendingxingFenxiParam;
import jnpf.limsEntity.LimsWendingxingFenxiRowVo;
import jnpf.limsController.export.LimsWendingxingFenxiWorkbookBuilder;
import jnpf.limsService.LimsWendingxingFenxiService;
import jnpf.util.R;
import jnpf.util.ExcelUtil;
import jnpf.util.UploaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.dromara.x.file.storage.core.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
@Slf4j
@Tag(name = "lims稳定性数据分析", description = "")
@RestController
@SaCheckLogin
@RequestMapping("/lims/biz/wendingxing/fenxi")
public class LimsWendingxingFenxiController {
 
    @Autowired
    private LimsWendingxingFenxiService fenxiService;
 
    @Autowired
    private FileUploadApi fileUploadApi;
 
    @Autowired
    private DictionaryDataApi dictionaryDataApi;
 
    private final LimsWendingxingFenxiWorkbookBuilder workbookBuilder =
            new LimsWendingxingFenxiWorkbookBuilder();
 
    @Operation(summary = "稳定性分析筛选选项")
    @PostMapping("/options")
    public R<LimsWendingxingFenxiOptionsVo> options(
            @RequestBody(required = false) LimsWendingxingFenxiParam param) {
        try {
            return R.success(fenxiService.listOptions(
                    param == null ? new LimsWendingxingFenxiParam() : param));
        } catch (DataException e) {
            return R.error(e.getMessage());
        }
    }
 
    @Operation(summary = "稳定性分析数据")
    @PostMapping("/data")
    public R<LimsWendingxingFenxiDataVo> data(@RequestBody LimsWendingxingFenxiParam param) {
        String error = validateRequired(param);
        if (error != null) {
            return R.error(error);
        }
        try {
            return R.success(fenxiService.listData(param));
        } catch (DataException e) {
            return R.error(e.getMessage());
        }
    }
 
    @Operation(summary = "导出稳定性数据分析表")
    @PostMapping("/export")
    public R<DownloadVO> export(@RequestBody LimsWendingxingFenxiParam param) {
        String error = validateRequired(param);
        if (error != null) {
            return R.error(error);
        }
 
        try {
            List<LimsWendingxingFenxiRowVo> rows = fenxiService.listData(param).getRows();
            if (rows.isEmpty()) {
                throw new DataException("当前筛选条件没有可导出的已审核数据");
            }
            String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
            String fileName = "稳定性数据分析表_"
                    + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
                    + "_" + suffix + ".xls";
            FileInfo fileInfo;
            try (Workbook workbook = workbookBuilder.build(rows, listPeriodUnits())) {
                MultipartFile multipartFile =
                        ExcelUtil.workbookToCommonsMultipartFile(workbook, fileName);
                fileInfo = fileUploadApi.uploadFile(
                        multipartFile, FileTypeConstant.TEMPORARY, fileName);
            }
            if (fileInfo == null || !StringUtils.hasText(fileInfo.getFilename())) {
                throw new DataException("稳定性数据分析表上传文件服务失败");
            }
            DownloadVO download = DownloadVO.builder()
                    .name(fileName)
                    .url(UploaderUtil.uploaderFile(
                            fileInfo.getFilename() + "#" + FileTypeConstant.TEMPORARY))
                    .build();
            return R.success(download);
        } catch (DataException e) {
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error("稳定性数据分析表导出失败", e);
            return R.error("稳定性数据分析表导出失败");
        }
    }
 
    private List<DictionaryDataEntity> listPeriodUnits() {
        try {
            List<DictionaryDataEntity> units = dictionaryDataApi.getListByCode("zhouqiDanwei");
            return units == null ? Collections.<DictionaryDataEntity>emptyList() : units;
        } catch (Exception e) {
            log.warn("读取周期单位字典 zhouqiDanwei 失败,导出保留原始编码", e);
            return Collections.emptyList();
        }
    }
 
    private String validateRequired(LimsWendingxingFenxiParam param) {
        if (param == null) {
            return "请求参数不能为空";
        }
        if (!StringUtils.hasText(param.getFenleiBianma())) {
            return "分类不能为空";
        }
        if (!StringUtils.hasText(param.getYangpinId())) {
            return "品名不能为空";
        }
        if (param.getXiangmuIds() == null || param.getXiangmuIds().isEmpty()) {
            return "检验项目不能为空";
        }
        return null;
    }
 
}