zhanghua
2023-03-30 dfea866e55c725bd601ff84c92dff30f83057571
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
package com.ycl.utils.listener;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ycl.dto.UmsDepartDto;
import com.ycl.entity.depart.UmsDepart;
import com.ycl.entity.dict.DataDictionary;
import com.ycl.mapper.depart.UmsDepartMapper;
import com.ycl.mapper.dict.DataDictionary2Mapper;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.Objects;
 
@Component
public class DepartListener implements ReadListener<UmsDepartDto> {
 
    @Resource
    private UmsDepartMapper sccgDepartMapper;
    @Resource
    private DataDictionary2Mapper dataDictionary2Mapper;
 
//    private static DepartListener departListener;
//
//    @PostConstruct
//    public void init(){
//        departListener = this;
//        departListener.sccgDepartMapper = this.sccgDepartMapper;
//        departListener.dataDictionaryMapper = this.dataDictionaryMapper;
//    }
 
    @Override
    public void invoke(UmsDepartDto umsDepartDto, AnalysisContext analysisContext) {
        //  不能重复插入
        String departTypeName = umsDepartDto.getDepartTypeName();
        UmsDepart one = sccgDepartMapper.selectOne(new LambdaQueryWrapper<UmsDepart>().eq(UmsDepart::getDepartName, departTypeName));
        if (Objects.nonNull(one)){
            throw new RuntimeException("该部门已经存在");
        }
 
        UmsDepart umsDepart = new UmsDepart();
 
        //  设置部门名字
        umsDepart.setDepartName(umsDepart.getDepartName());
 
        //  设置部门描述
        umsDepart.setDepartDes(umsDepartDto.getDepartDes());
 
        //  设置部门类型
        DataDictionary dataDictionary = dataDictionary2Mapper.selectOne(new LambdaQueryWrapper<DataDictionary>().eq(DataDictionary::getName, umsDepartDto.getDepartType()));
        if (Objects.isNull(dataDictionary)){
            throw new RuntimeException("部门类型不存在");
        }else {
            umsDepart.setDepartType(new Long(dataDictionary.getId()).intValue());
        }
 
        //  设置父id
        UmsDepart two = sccgDepartMapper.selectOne(new LambdaQueryWrapper<UmsDepart>().eq(UmsDepart::getDepartName, umsDepartDto.getParentDepartName()));
        if (Objects.nonNull(two)){
            umsDepart.setParentId(two.getParentId());
        }
 
        int insert = sccgDepartMapper.insert(umsDepart);
        if (insert < 1){
            throw new RuntimeException("插入失败");
        }
 
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
 
    }
}