liding
6 天以前 ef77cc9ff685f257e3d51944f98206f4e849829a
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
161
162
163
164
165
166
167
168
169
package com.ruoyi.manage.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
 
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.manage.mapper.ManageRecordTotalMapper;
import com.ruoyi.manage.mapper.ManageRecordVerifyMapper;
import com.ruoyi.manage.pojo.ManageRecordTotal;
import com.ruoyi.manage.pojo.ManageRecordVerify;
import com.ruoyi.manage.service.ManageRecordVerifyService;
 
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.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 IPage<ManageRecordVerify> pageManageRecordVerify(Page page, ManageRecordVerify manageRecordVerify) {
        if (ObjectUtils.isEmpty(manageRecordVerify.getManageRecordTotalId())) {
            ManageRecordTotal manageRecordTotal = getCurrentYearRecord();
            if (ObjectUtils.isEmpty(manageRecordTotal)) {
                return new Page<>();
            }
            manageRecordVerify.setManageRecordTotalId(manageRecordTotal.getId());
        }
        return manageRecordVerifyMapper.pageManageRecordVerify(page, QueryWrappers.queryWrappers(manageRecordVerify));
    }
 
    @Override
    public int addManageRecordVerify(ManageRecordVerify manageRecordVerify) {
        ManageRecordTotal manageRecordTotal = getOrCreateCurrentYearRecord();
        manageRecordVerify.setManageRecordTotalId(manageRecordTotal.getId());
        manageRecordVerifyMapper.insert(manageRecordVerify);
        manageRecordTotal.setTotalNum(1 + manageRecordTotal.getTotalNum());
        return manageRecordTotalMapper.updateById(manageRecordTotal);
    }
 
    private ManageRecordTotal getCurrentYearRecord() {
        //获取当前年份
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
        // 格式化当前日期
        String currentYear = currentDate.format(formatter);
        //查询历史
        return manageRecordTotalMapper.selectOne(Wrappers.<ManageRecordTotal>lambdaQuery().eq(ManageRecordTotal::getYear, currentYear));
    }
 
    private ManageRecordTotal getOrCreateCurrentYearRecord() {
        ManageRecordTotal manageRecordTotal = getCurrentYearRecord();
        if (ObjectUtils.isEmpty(manageRecordTotal)) {
            manageRecordTotal = new ManageRecordTotal();
            //获取当前年份
            LocalDate currentDate = LocalDate.now();
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
            // 格式化当前日期
            String currentYear = currentDate.format(formatter);
            manageRecordTotal.setYear(currentYear);
            manageRecordTotal.setTotalNum(0);
            manageRecordTotalMapper.insert(manageRecordTotal);
        }
        return 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");
        ManageRecordTotal manageRecordTotal = getOrCreateCurrentYearRecord();
        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) {
                        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;
    }
}