zss
6 天以前 51ec98113c6d49d0f7eec4e3c030e55e337e97db
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
151
152
153
154
155
156
157
158
159
160
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.common.PrintChina;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.ManageRecordTotalMapper;
import com.yuanchu.mom.pojo.ManageRecordTotal;
import com.yuanchu.mom.pojo.ManageRecordVerify;
import com.yuanchu.mom.mapper.ManageRecordVerifyMapper;
import com.yuanchu.mom.service.ManageRecordVerifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.QueryWrappers;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 外来文件确认记录 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-11-12 10:29:44
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ManageRecordVerifyServiceImpl extends ServiceImpl<ManageRecordVerifyMapper, ManageRecordVerify> implements ManageRecordVerifyService {
 
    @Resource
    private ManageRecordVerifyMapper manageRecordVerifyMapper;
 
    @Resource
    private ManageRecordTotalMapper manageRecordTotalMapper;
 
    @Override
    public Map<String, Object> pageManageRecordVerify(Page page, ManageRecordVerify manageRecordVerify) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", PrintChina.printChina(ManageRecordVerify.class));
        if (ObjectUtils.isEmpty(manageRecordVerify.getManageRecordTotalId())) {
            //获取当前年份
            LocalDate currentDate = LocalDate.now();
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
            // 格式化当前日期
            String currentMonth = currentDate.format(formatter);
            //查询历史
            ManageRecordTotal manageRecordTotal = manageRecordTotalMapper.selectOne(Wrappers.<ManageRecordTotal>lambdaQuery().eq(ManageRecordTotal::getYear, currentMonth));
            manageRecordVerify.setManageRecordTotalId(manageRecordTotal.getId());
        }
        map.put("body", manageRecordVerifyMapper.pageManageRecordVerify(page, QueryWrappers.queryWrappers(manageRecordVerify)));
        return map;
    }
 
    @Override
    public int addManageRecordVerify(ManageRecordVerify manageRecordVerify) {
        //获取当前年份
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
        // 格式化当前日期
        String currentMonth = currentDate.format(formatter);
        ManageRecordTotal manageRecordTotal = manageRecordTotalMapper.selectOne(Wrappers.<ManageRecordTotal>lambdaQuery().eq(ManageRecordTotal::getYear, currentMonth));
        manageRecordVerify.setManageRecordTotalId(manageRecordTotal.getId());
        manageRecordVerifyMapper.insert(manageRecordVerify);
        manageRecordTotal.setTotalNum(1 + manageRecordTotal.getTotalNum());
        return manageRecordTotalMapper.updateById(manageRecordTotal);
    }
 
    @Override
    public int delManageRecordVerify(Integer id) {
        ManageRecordVerify manageRecordVerify = manageRecordVerifyMapper.selectById(id);
        manageRecordVerifyMapper.deleteById(id);
        ManageRecordTotal manageRecordTotal = manageRecordTotalMapper.selectById(manageRecordVerify.getManageRecordTotalId());
        manageRecordTotal.setTotalNum(manageRecordTotal.getTotalNum() - 1);
        return manageRecordTotalMapper.updateById(manageRecordTotal);
    }
 
    @Override
    public int exportManageRecordVerify(MultipartFile file) {
        List<ManageRecordVerify> manageRecordVerifyList = new ArrayList<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //获取当前年份
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
        // 格式化当前日期
        String currentMonth = currentDate.format(formatter);
        ManageRecordTotal manageRecordTotal = manageRecordTotalMapper.selectOne(Wrappers.<ManageRecordTotal>lambdaQuery().eq(ManageRecordTotal::getYear, currentMonth));
        try {
            InputStream inputStream = file.getInputStream();
            XWPFDocument document = new XWPFDocument(inputStream);
            List<XWPFTable> tables = document.getTables();
            if (tables.isEmpty()) {
                throw new ErrorException("文档中没有表格");
            }
 
            for (XWPFTable table : tables) {
                List<XWPFTableRow> rows = table.getRows();
                if (rows.size() <= 1) {
                    throw new ErrorException("表格没有数据行");
                }
                for (int i = 1; i < rows.size(); i++) { // 从第二行开始,跳过表头
                    XWPFTableRow row = rows.get(i);
                    if (row.getTableCells().size() != 8) {
                        System.out.println("行 " + (i + 1) + " 的列数不匹配,跳过该行");
                        continue;
                    }
                    if (ObjectUtils.isNotEmpty(row.getCell(1).getText())) {
                        ManageRecordVerify manageRecordVerify = new ManageRecordVerify();
                        manageRecordVerify.setDocumentName(row.getCell(1).getText());
                        manageRecordVerify.setDocumentCode(row.getCell(2).getText());
                        manageRecordVerify.setStandardName(row.getCell(3).getText());
                        manageRecordVerify.setStandardCode(row.getCell(4).getText());
                        try {
                            manageRecordVerify.setEffectiveDate(LocalDate.parse(row.getCell(5).getText(), dateTimeFormatter));
                        } catch (Exception e) {
                            manageRecordVerify.setEffectiveDate(null);
                        }
                        try {
                            manageRecordVerify.setCancelDate(LocalDate.parse(row.getCell(6).getText(), dateTimeFormatter));
                        } catch (Exception e) {
                            manageRecordVerify.setCancelDate(null);
                        }
                        manageRecordVerify.setNote(row.getCell(7).getText());
                        manageRecordVerify.setManageRecordTotalId(manageRecordTotal.getId());
                        if (manageRecordVerifyMapper.selectCount(Wrappers.<ManageRecordVerify>lambdaQuery()
                                .eq(ManageRecordVerify::getDocumentCode, manageRecordVerify.getDocumentCode())
                                .eq(ManageRecordVerify::getDocumentName, manageRecordVerify.getDocumentName())
                                .eq(ManageRecordVerify::getStandardName, manageRecordVerify.getStandardName())
                                .eq(ManageRecordVerify::getStandardCode, manageRecordVerify.getStandardCode())
                                .eq(ManageRecordVerify::getManageRecordTotalId, manageRecordVerify.getManageRecordTotalId())) <= 0) {
                            manageRecordVerifyList.add(manageRecordVerify);
                        }
                    }
                }
            }
            saveBatch(manageRecordVerifyList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
}