zss
2025-02-19 30db5b326ae164d1256726191bd36a0d24f5f335
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
package com.yuanchu.mom.service.impl;
 
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Pictures;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.dto.PersonSupervisePlanDto;
import com.yuanchu.mom.dto.PersonTrainingDetailedDto;
import com.yuanchu.mom.excel.PersonSupervisePlanDetailsListener;
import com.yuanchu.mom.excel.PersonSupervisePlanDetailsUpload;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.PersonSupervisePlan;
import com.yuanchu.mom.mapper.PersonSupervisePlanMapper;
import com.yuanchu.mom.pojo.PersonSupervisePlanDetails;
import com.yuanchu.mom.pojo.PersonTraining;
import com.yuanchu.mom.service.PersonSupervisePlanDetailsService;
import com.yuanchu.mom.service.PersonSupervisePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import com.yuanchu.mom.utils.DateImageUtil;
import com.yuanchu.mom.utils.Jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 监督计划 - 父 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2024-10-09 04:14:45
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PersonSupervisePlanServiceImpl extends ServiceImpl<PersonSupervisePlanMapper, PersonSupervisePlan> implements PersonSupervisePlanService {
 
    @Autowired
    private PersonSupervisePlanDetailsService personSupervisePlanDetailsService;
 
    @Autowired
    private GetLook getLook;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Autowired
    private UserMapper userMapper;
 
    public IPage<PersonSupervisePlanDto> yearPlanDtoIPage(Page page, String organizationPerson) {
        return baseMapper.pageByPerson(page, organizationPerson);
    }
 
    @Override
    public void yearPlanDetailImport(MultipartFile file) {
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectInsOrderParameter");
        // 年度计划父级新增数据
        PersonSupervisePlan personSupervisePlan = new PersonSupervisePlan();
        String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
        personSupervisePlan.setFileName(fileName);
        personSupervisePlan.setOrganizationPersonId(map1.get("userId"));
        personSupervisePlan.setOrganizationDate(LocalDateTime.now());
        baseMapper.insert(personSupervisePlan);
        // 年度计划详情 新增
        try {
            PersonSupervisePlanDetailsListener personSupervisePlanDetailsListener = new PersonSupervisePlanDetailsListener(personSupervisePlanDetailsService);
            personSupervisePlanDetailsListener.setPlanId(personSupervisePlan.getId());
            EasyExcel.read(file.getInputStream(), PersonSupervisePlanDetailsUpload.class, personSupervisePlanDetailsListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 导出人员监督计划
     * @param id
     * @param response
     */
    @Override
    public void exportSuperVisePlan(Integer id, HttpServletResponse response) {
        // 查询详情
        PersonSupervisePlan personSupervisePlan = baseMapper.selectById(id);
 
        //获取提交人的签名地址
        String organizationUrl = userMapper.selectById(personSupervisePlan.getOrganizationPersonId()).getSignatureUrl();
        if (StringUtils.isBlank(organizationUrl)) {
            throw new ErrorException("找不到检验人的签名");
        }
 
        //获取批准人的签名地址
        String approvalUrl = null;
        if (personSupervisePlan.getApprovalId() != null) {
            approvalUrl = userMapper.selectById(personSupervisePlan.getApprovalId()).getSignatureUrl();
            if (StringUtils.isBlank(approvalUrl)) {
                throw new ErrorException("找不到批准人的签名");
            }
        }
 
        // 查询详情
        List<PersonSupervisePlanDetails> detailedDtos = personSupervisePlanDetailsService.list(Wrappers.<PersonSupervisePlanDetails>lambdaQuery()
                .eq(PersonSupervisePlanDetails::getPlanId, id));
 
        int index = 1;
        SimpleDateFormat formatter = new  SimpleDateFormat("yyyy年MM月dd日");
        for (PersonSupervisePlanDetails detailedDto : detailedDtos) {
            detailedDto.setIndex(index);
            detailedDto.setSuperviseDateString(formatter.format(detailedDto.getSuperviseDate()));
            index++;
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/super-vise-plan.docx");
        Configure configure = Configure.builder()
                .bind("supervisePlanDetailsList", new HackLoopTableRenderPolicy())
                .build();
        String finalApprovalUrl = approvalUrl;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("supervisePlanDetailsList", detailedDtos);
                    put("organizationUrl", StringUtils.isNotBlank(organizationUrl) ? Pictures.ofLocal(imgUrl + "/" + organizationUrl).create() : null);
                    put("approvalUrl", StringUtils.isNotBlank(finalApprovalUrl) ? Pictures.ofLocal(imgUrl + "/" + finalApprovalUrl).create() : null);
                    put("writeDateUrl", personSupervisePlan.getOrganizationDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(personSupervisePlan.getOrganizationDate())).create() : null);
                    put("ratifyDateUrl", personSupervisePlan.getApprovalDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(personSupervisePlan.getApprovalDate())).create() : null);
                }});
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    "人员监督计划导出", "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
}