多单位版国产化地质资料管理系统
zhai
2025-09-17 7a8a84577894b766a95c6cbfa5ac7b51e54ac242
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
package com.zbooksoft.gdmis.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruili.wcp.common.EncryptUtil;
import com.ruili.wcp.data.entity.management.Dept;
import com.ruili.wcp.data.entity.management.Role;
import com.ruili.wcp.data.entity.management.User;
import com.ruili.wcp.service.config.ImportLogService;
import com.ruili.wcp.service.management.DeptService;
import com.ruili.wcp.service.management.RoleService;
import com.ruili.wcp.service.management.UserService;
import com.ruili.wcp.web.common.LicenseException;
import com.ruili.wcp.web.model.AjaxResponse;
import com.ruili.wcp.web.model.ErrorInfo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import static com.ruili.wcp.web.controller.general.DataImportController.getExcelValue;
 
/**
 * @Description:
 * @Author: zhai
 * @Date: 2025/11/25
 **/
@Controller
@RequestMapping("/ghUser")
public class GhUserController {
    @Autowired
    DeptService deptService;
 
    @Autowired
    UserService userService;
 
    @Autowired
    RoleService roleService;
    private static final Logger logger = LoggerFactory.getLogger(GhUserController.class);
 
    /**
     * xml导入页面
     *
     * @return
     */
    @RequestMapping(value = "/userIndex")
    @RequiresUser
    public ModelAndView wasm() {
        ModelAndView mv = new ModelAndView("gh/user/userIndex");
        return mv;
    }
 
    /**
     * @param file 0 导入转孔信息  1 导入图幅信息 2 导入实物案卷信息
     * @return
     * @throws LicenseException
     */
 
    @RequestMapping({"/importExcel"})
    @ResponseBody
    @RequiresUser
    public Object importExcel(@RequestParam(value = "file", required = false) MultipartFile file) throws LicenseException {
        try {
            readExcel(file);
            return new AjaxResponse(true);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new AjaxResponse(new ErrorInfo("导入失败"), false);
        }
    }
 
    private void readExcel(MultipartFile file) throws IOException {
 
        String originalFilename = file.getOriginalFilename();
 
        InputStream inputStream = file.getInputStream();
        if (originalFilename.matches("^.+\\.(?i)(xls)$")) {
            //创建工作簿对象
//            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
//            HashMap<String, Object> map = readProject2003(hssfWorkbook);
//            readExcel2003(hssfWorkbook, map);
 
        }
        if (originalFilename.matches("^.+\\.(?i)(xlsx)$")) {
            //创建工作簿对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
            insertDept(xssfWorkbook.getSheetAt(1));
            insertUser(xssfWorkbook.getSheetAt(0));
 
        }
    }
 
 
    private void insertDept(Sheet sheet) {
        int maxRow = sheet.getLastRowNum();
        for (int j = 1; j <= maxRow; j++) {
            try {
                Row newRow = sheet.getRow(j);
 
                Dept dept = new Dept();
                dept.setIcon("fa fa-address-card");
                dept.setDeptType(0);
                dept.setSortNum(j);
 
                Cell deptName = newRow.getCell(1);
                String deptNameString = getExcelValue(deptName);
                String[] split1 = deptNameString.split("\\.");
                dept.setDeptName(split1[split1.length - 1]);
                dept.setDeptExtendStringField5(deptNameString);
 
                Cell deptNum = newRow.getCell(2);
                dept.setDeptNum(getExcelValue(deptNum));
 
                Cell deptCode = newRow.getCell(3);
                String deptCodeString = getExcelValue(deptCode);
                dept.setDeptCode(deptCodeString);
 
                String[] split = deptCodeString.split("\\.");
                String parentDeptCode = "";
                if (split.length > 1) {
                    if (split.length == 2) {
                        parentDeptCode = split[0];
                    }
                    if (split.length == 3) {
                        parentDeptCode = split[0] + "." + split[1];
                    }
                    QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
                    deptQueryWrapper.eq("dept_code", parentDeptCode);
                    List<Dept> list = deptService.list(deptQueryWrapper);
                    if (list.size() > 0) {
                        dept.setParentId(list.get(0).getDeptId());
                    }
                } else {
                    dept.setParentId(0L);
                }
                Cell oaDeptId = newRow.getCell(0);
                dept.setDeptExtendStringField1(getExcelValue(oaDeptId));
                deptService.saveOrUpdate(dept);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
 
    private void insertUser(Sheet sheet) {
        int maxRow = sheet.getLastRowNum();
        for (int j = 1; j <= maxRow; j++) {
            Row newRow = sheet.getRow(j);
            User user = new User();
            Cell userName = newRow.getCell(1);
            user.setUserName(getExcelValue(userName));
            Cell mobile = newRow.getCell(4);
            user.setMobilePhone(getExcelValue(mobile));
            String mobileString = getExcelValue(mobile);
            if (mobileString.length() >= 4) {
                String pwd = mobileString.substring(mobileString.length() - 4, mobileString.length());
                String newPwd = "gh@" + pwd;
                String md5Pwd = EncryptUtil.getInstance().MD5(newPwd);
                user.setPassword(md5Pwd);
            } else {
                String newPwd = "gh@0000";
                String md5Pwd = EncryptUtil.getInstance().MD5(newPwd);
                user.setPassword(md5Pwd);
            }
            Cell trueName = newRow.getCell(2);
            user.setTrueName(getExcelValue(trueName));
            Byte enableLogin = 0;
            user.setEnableLogin(enableLogin);
            user.setUserType(1);
 
            user.setCreateTime(new Date());
            Cell deptName = newRow.getCell(3);
            String deptNameString = getExcelValue(deptName);
//            String[] split = deptNameString.split("\\.");
//            String deptCode = split[split.length - 1];
            LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dept::getDeptExtendStringField5, deptNameString);
            Dept dept = deptService.getOne(wrapper);
            user.setDeptId(dept.getDeptId());
            user.setDeptName(dept.getDeptName());
            Cell oldId = newRow.getCell(0);
            user.setUserExtendStringField1(getExcelValue(oldId));
            //插入系统用户表
            userService.saveUser(user);
            //初始化用户默认角色
            List<Role> roles = roleService.addUserToDefaultRole(user.getUserId());
        }
    }
}