郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.gk.hotwork.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.RiskAnaUnit;
import com.gk.hotwork.Domain.RiskControlMeasure;
import com.gk.hotwork.Domain.RiskEvent;
import com.gk.hotwork.Domain.UserInfo;
import com.gk.hotwork.Domain.Utils.BeanUtils;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Domain.Vo.RiskEventExportVo;
import com.gk.hotwork.Mapper.RiskEventMapper;
import com.gk.hotwork.Service.RiskAnaUnitService;
import com.gk.hotwork.Service.RiskControlMeasureService;
import com.gk.hotwork.Service.RiskEventService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service("riskEventService")
public class RiskEventServiceImpl extends ServiceImpl<RiskEventMapper,RiskEvent> implements RiskEventService {
 
    @Autowired
    private RiskEventMapper riskEventMapper;
    @Autowired
    private RiskControlMeasureService riskControlMeasureService;
    @Autowired
    private RiskAnaUnitService riskAnaUnitService;
 
 
    /**
    * @Description: 分页
    */
    @Override
    public IPage<RiskEvent> selectPage(Page<RiskEvent> page, Map<String, Object> filter, UserInfo user) {
        return riskEventMapper.selectPages(page,filter);
    }
 
 
    /**
    * @Description: 新增
    */
    @Override
    public void addOne(RiskEvent riskEventVo, UserInfo user) {
        if(StringUtils.isBlank(riskEventVo.getRiskEventName())) throw new BusinessException("请填写安全风险事件名称");
        if (riskEventVo.getRiskUnitId() == null) throw new BusinessException("请选择安全风险单位");
        int count = riskAnaUnitService.count(new LambdaQueryWrapper<RiskAnaUnit>()
                .eq(RiskAnaUnit::getId, riskEventVo.getRiskUnitId())
                .eq(RiskAnaUnit::getValidFlag, Boolean.TRUE));
        if (count < 1 ) throw new BusinessException("安全风险单位不存在");
 
        Date date = new Date();
        String username = user.getRealname();
        riskEventVo.setValidFlag(Boolean.TRUE);
        riskEventVo.setUpdateBy(username);
        riskEventVo.setCreateBy(username);
        riskEventVo.setUpdateTime(date);
        riskEventVo.setCreateTime(date);
        this.save(riskEventVo);
    }
 
 
    /**
    * @Description: 修改
    */
    @Override
    public void modOne(RiskEvent riskEventVo, UserInfo user) {
        if (riskEventVo.getId() == null) throw new BusinessException("id传参不能为空");
        int eventCount = this.count(new LambdaQueryWrapper<RiskEvent>().eq(RiskEvent::getId, riskEventVo.getId()));
        if (eventCount < 1) throw new BusinessException("找不到对应实体");
        if(StringUtils.isBlank(riskEventVo.getRiskEventName())) throw new BusinessException("请填写安全风险事件名称");
        if (riskEventVo.getRiskUnitId() == null) throw new BusinessException("请选择安全风险单位");
        int unitCount = riskAnaUnitService.count(new LambdaQueryWrapper<RiskAnaUnit>()
                .eq(RiskAnaUnit::getId, riskEventVo.getRiskUnitId())
                .eq(RiskAnaUnit::getValidFlag, Boolean.TRUE));
        if (unitCount < 1 ) throw new BusinessException("安全风险单位不存在");
 
        riskEventVo.setUpdateTime(new Date());
        riskEventVo.setUpdateBy(user.getRealname());
        this.updateById(riskEventVo);
    }
 
 
    /**
    * @Description: 删除
    */
    @Override
    public void delOne(Long id, UserInfo user) {
        if (id == null) throw new BusinessException("id传参不能为空");
        RiskEvent riskEvent = this.getById(id);
        if (riskEvent ==  null) throw new BusinessException("找不到对应实体");
        int count = riskControlMeasureService.count(new LambdaQueryWrapper<RiskControlMeasure>()
                .eq(RiskControlMeasure::getRiskEventId, riskEvent.getId())
                .eq(RiskControlMeasure::getValidFlag, Boolean.TRUE));
        if (count > 0) throw new BusinessException("当前风险事件仍然绑定风险管控措施,无法删除");
        RiskEvent delOne = new RiskEvent();
        delOne.setId(id);
        delOne.setUpdateTime(new Date());
        delOne.setUpdateBy(user.getRealname());
        delOne.setValidFlag(Boolean.FALSE);
        this.updateById(delOne);
    }
 
 
    /**
     * @Description: 导出
     */
    @SneakyThrows
    @Override
    public List<RiskEventExportVo> getRiskEventList(HttpServletRequest request, HttpServletResponse response) {
 
        List<RiskEvent> risklist = riskEventMapper.getRiskEventList();
        List<RiskEventExportVo> list = new ArrayList<>();
 
        for (RiskEvent riskEvent : risklist) {
            RiskAnaUnit byId = riskAnaUnitService.getById(riskEvent.getRiskUnitId());
            RiskEventExportVo copy = BeanUtils.copy(riskEvent, RiskEventExportVo.class);
            copy.setRiskUnitName(byId.getRiskUnitName());
            list.add(copy);
        }
 
        return list;
 
//        //第一步创建一个webbook,对应一个Excel文件
//        HSSFWorkbook wb=new HSSFWorkbook();
//        //第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
//        HSSFSheet sheet=wb.createSheet("管控措施");
//        //第三步,在sheet中添加表头第0行
//        HSSFRow row = sheet.createRow(0);
//        //第四步,创建单元格,并设置表头居中
//        HSSFCellStyle style = wb.createCellStyle();
//        style.setAlignment(HorizontalAlignment.CENTER);//居中格式
//
//        HSSFCell cell = row.createCell(0);
//        cell.setCellValue("安全风险事件名称");
//        cell.setCellStyle(style);
//
//        cell=row.createCell((short)1);
//        cell.setCellValue("安全风险单元名称");
//        cell.setCellStyle(style);
//
//        for (int i = 0; i < list.size(); i++) {
//            //创建单元格,并赋值
//            row=sheet.createRow(i+1);
//            RiskEvent riskEvent = list.get(i);
//            RiskAnaUnit  riskAnaUnit = riskAnaUnitService.getById(riskEvent.getRiskUnitId());
//
//            row.createCell((short) 0).setCellValue(riskEvent.getRiskEventName());
//            row.createCell((short)1).setCellValue(riskAnaUnit.getRiskUnitName());
//
//        }
//        //第六步,下载Excel
//        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\风险事件.xls"));
//        wb.write(fileOutputStream);
 
    }
 
    @Transactional
    @Override
    public void importRiskEventData(List<RiskEventExportVo> param, UserInfo user) {
        if (CollectionUtils.isEmpty(param)){
            throw new BusinessException("文件不能为空");
        }
 
        for (RiskEventExportVo riskEventExportVo : param) {
            //取出风险单元名称,查出对应风险单元信息
            RiskAnaUnit riskUnitInfo = riskAnaUnitService.getInfoByRiskUnitName(riskEventExportVo.getRiskUnitName());
            if (riskEventExportVo.getRiskUnitName() == null || ObjectUtils.isEmpty(riskUnitInfo)) {
                throw new BusinessException("请确认文件格式是否正确或风险单元是否存在");
            }
            if (riskEventExportVo.getRiskEventName() == null){
                throw new BusinessException("请填写风险事件名称");
            }
 
            //取出风险事件名称,将数据写入RiskEvent对象
            RiskEvent riskEvent = new RiskEvent();
 
            riskEvent.setRiskUnitId(riskUnitInfo.getId());
            riskEvent.setRiskEventName(riskEventExportVo.getRiskEventName());
            riskEvent.setValidFlag(Boolean.TRUE);
            riskEvent.setUpdateBy(user.getRealname());
            riskEvent.setCreateBy(user.getRealname());
            riskEvent.setCreateTime(new Date());
            riskEvent.setUpdateTime(new Date());
 
            this.save(riskEvent);
            }
    }
 
    @Override
    public RiskEvent getByRiskEventName(String riskEventName) {
 
        return riskEventMapper.getByRiskEventName(riskEventName);
    }
}