mitao
2024-03-14 717811c825b998c3001ad0145bd1fd8f46d1a796
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
package com.ruoyi.system.service.impl;
 
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageVO;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.TbDept;
import com.ruoyi.system.handler.DeptVerifyHandler;
import com.ruoyi.system.mapper.TbDeptMapper;
import com.ruoyi.system.query.DeptQuery;
import com.ruoyi.system.service.TbDeptService;
import com.ruoyi.system.vo.DeptVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;
 
/**
 * <p>
 * 部门管理表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-03-13
 */
@Service
@RequiredArgsConstructor
public class TbDeptServiceImpl extends ServiceImpl<TbDeptMapper, TbDept> implements TbDeptService {
    private final DeptVerifyHandler deptVerifyHandler;
    private final HttpServletResponse response;
    @Override
    public PageVO<DeptVO> queryPage(DeptQuery query) {
        Page<TbDept> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<TbDept> queryWrapper = new LambdaQueryWrapper<TbDept>()
                .like(StringUtils.isNotEmpty(query.getAreaName()), TbDept::getAreaName, query.getAreaName())
                .like(StringUtils.isNotEmpty(query.getAccount()), TbDept::getAccount, query.getAccount())
                .like(StringUtils.isNotEmpty(query.getPhone()), TbDept::getPhone, query.getPhone())
                .orderByDesc(TbDept::getCreateTime);
        Page<TbDept> deptPage = this.baseMapper.selectPage(page, queryWrapper);
        PageVO<DeptVO> pageVO = new PageVO<>();
        pageVO.setPageNo(deptPage.getPages());
        pageVO.setPageSize(deptPage.getSize());
        pageVO.setTotalPages(deptPage.getTotal());
        pageVO.setTotalCount(deptPage.getTotal());
        IPage<DeptVO> convert = deptPage.convert(result -> {
            DeptVO deptVO = new DeptVO();
            BeanUtils.copyBeanProp(deptVO, result);
            return deptVO;
        });
        pageVO.setRecords(convert.getRecords());
        return pageVO;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws Exception {
        ImportParams importParams = new ImportParams();
        //表格标题行数,默认0
        importParams.setTitleRows(1);
        //是否需要校验上传的Excel
        importParams.setNeedVerify(true);
        //使用自定义校验规则
        importParams.setVerifyHandler(deptVerifyHandler);
        InputStream inputStream = file.getInputStream();
        ExcelImportResult<TbDept> result = ExcelImportUtil.importExcelMore(inputStream, TbDept.class, importParams);
        inputStream.close();
        List<TbDept> list = result.getList();
        if (Objects.requireNonNull(result).isVerfiyFail() || CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("文件校验失败,请检查数据填写是否完整");
        }
        List<String> strings = hasDuplicateAreaCode(list);
        if (!CollectionUtils.isEmpty(strings)) {
            throw new RuntimeException(String.format("区划代码%s重复,请修改后重新导入", String.join(",", strings)));
        }else {
            this.remove(null);
            this.saveBatch(list);
        }
    }
 
    /**
     * 校验区划代码是否重复
     * @param deptList
     * @return
     */
    public List<String> hasDuplicateAreaCode(List<TbDept> deptList) {
        Set<String> areaCodes = new HashSet<>();
        List<String> result = new ArrayList<>();
        for (TbDept dept : deptList) {
            if (!areaCodes.add(dept.getAreaCode())) {
                result.add(dept.getAreaCode());
            }
        }
        return result;
    }
}