18516761980
2021-06-07 f5da19565cebc364d23a65ebe931b3c38cecfa09
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package com.slcf.service.impl;
 
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import com.slcf.dao.UserDao;
import com.slcf.pojo.UserBean;
import com.slcf.service.UserService;
 
@Service
public class UserServiceImpl implements UserService{
 
    @Resource
    UserDao userDao;
    
    /**
     * 分页查询所以user信息
     * @param spage
     * @param epage
     * @return
     */
    public Map<String,Object> getUserList(int pageNumber, int pageSize) {
        Map<String,Object>map=new HashMap<String, Object>();
        try {
            int count = userDao.getUserCount();
            // List<UserBean>ulist=userDao.getUserList((pageNumber-1)*pageSize,
            // pageNumber*pageSize);
            List<UserBean> ulist = userDao.getUserList((pageNumber - 1) * pageSize, pageSize);
            map.put("total", count);
            map.put("rows", ulist);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return map;
    }
 
    /**
     * 修改用户密码
     */
    public int upPass(int uid, String pass,HttpServletRequest request) {
        int i=0;
        try {
            i = userDao.upPass(uid, pass);
            // String name=(String)request.getSession().getAttribute("NAME");
            // if(i>0){
            // j=userDao.insertUserOpt(uid, "修改", name);
            // }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return i;
    }
 
    /**
     * 验证用户登录账号是否唯一
     */
    public boolean validUserAccount(String account, int uid) {
        boolean flag=true;
        try {
            List<UserBean> ulist = userDao.getUserByAccount(account);
            if (uid != 0) {// 修改
                if (ulist.size() == 1 && ulist.get(0).getUser_id() == uid) {
                    flag = true;
                } else if (ulist.size() == 0) {
                    flag = true;
                } else {
                    flag = false;
                }
            } else {// 添加
                if (ulist.size() > 0) {
                    flag = false;
                }
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return flag;
    }
 
    /**
     * 添加用户信息
     */
    public int saveUser(UserBean user,HttpServletRequest request) {        
        int i=0;
        try {
            i = userDao.saveUser(user);
//            String name = (String) request.getSession().getAttribute("NAME");
            if (i > 0) {
                List<UserBean> list = userDao.getUserByAccount(user.getUser_account());
                int x = userDao.saveUserRole(list.get(0).getUser_id(), 1);// 默认是普通员工
                // j=userDao.insertUserOpt(list.get(0).getUser_id(), "添加", name);
                if (x == 1) {
                    i = 1;
                } else {
                    i = 0;
                }
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return i;
    }
 
    /**
     * 删除用户
     */
    public int delUser(int uid) {
        int i = 0;
        try {
            i = userDao.delUser(uid);
            userDao.delUserRoleByUid(uid);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return i;
    }
 
    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    public UserBean queryUserById(int id) {
        UserBean user=new UserBean();
        try {
            user = userDao.queryUserById(id);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return user;
    }
 
    /**
     * 修改用户
     * @param user
     * @return
     */
    public int upUser(UserBean user,HttpServletRequest request) {
//        String name=(String)request.getSession().getAttribute("NAME");
//        int j=0;
        int i = 0; 
        try {
            i = userDao.upUser(user);
//            if (i > 0) {
//                j = userDao.insertUserOpt(user.getUser_id(), "修改", name);
//            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return i;
    }
 
    /**
     * 批量删除用户
     */
    public int delUsers(int[] ids) {
        int sum=0;
        int y=0;
        try {
            for (int i = 0; i < ids.length; i++) {
                // int j=userDao.delUserOpt(ids[i]);
                // if(j>0){
                int x = userDao.delUser(ids[i]);
                userDao.delUserRoleByUid(ids[i]);
                sum += x;
                // }
            }
            if (sum == ids.length) {
                y = 1;
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return y;
    }
 
    public List<Integer> getUserRole(int uid) {
        try {
            List<Integer> list=userDao.getUserRole(uid);
            return list;
        }catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }        
    }
 
    
    public boolean saveUserRole(int uid, String ridStr) {
        boolean flag=false;
        try {
            int sum = 0, x = 0;
            int count = userDao.getCount(uid);
            if (count >= 1) {
                x = userDao.delUserRoleByUid(uid);
            }
            if (count >= 1 && x > 0 || count == 0) {
                if (ridStr.contains(",")) {
                    String idStrs[] = ridStr.split(",");
                    int ids[] = new int[idStrs.length];
                    for (int i = 0; i < idStrs.length; i++) {
                        ids[i] = Integer.parseInt(idStrs[i]);
                    }
                    for (Integer s : ids) {
                        int j = userDao.saveUserRole(uid, s);
                        sum += j;
                    }
                    if (sum == ids.length) {
                        flag = true;
                    }
                } else {
                    int y = userDao.saveUserRole(uid, Integer.parseInt(ridStr));
                    if (y > 0) {
                        flag = true;
                    }
                }
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return flag;
    }
 
    public void ExportUserList(HttpServletResponse response) {
        //输出Excel文件  
        try {
            //HSSFWorkbook对象(excel的文档对象)  
            HSSFWorkbook workBook=new HSSFWorkbook();
            
            //sheet对象(excel的表单)
            HSSFSheet sheet=workBook.createSheet("用户表信息");
            
            //行数,参数为行索引(excel的行)
            HSSFRow rowHead=sheet.createRow(0);//第一行  可以是0~65535之间的任何一个
            
            //创建excel的单元格,参数为列索引,可以是0~255之间的任何一个 
            HSSFCell cellOne=rowHead.createCell(0);
            cellOne.setCellValue("用户信息");//表头
            
            //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列  
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,9));
            
            HSSFRow rows=sheet.createRow(1);//第二行
            //创建单元格并设置单元格内容
            rows.createCell(0).setCellValue("用户编号");
            rows.createCell(1).setCellValue("登陆账号");
            rows.createCell(2).setCellValue("用户姓名");
            rows.createCell(3).setCellValue("用户年龄");
            rows.createCell(4).setCellValue("用户性别");
            rows.createCell(5).setCellValue("用户地址");
            rows.createCell(6).setCellValue("出生日期");
            rows.createCell(7).setCellValue("Email");
            rows.createCell(8).setCellValue("联系电话");
            rows.createCell(9).setCellValue("所属部门");
        
            List<UserBean>userList=userDao.queryUserList();
            for(int i=0;i<userList.size();i++){
                HSSFRow row=sheet.createRow(i+2);//从第三行开始
                //创建单元格并设置单元格内容
                row.createCell(0).setCellValue(userList.get(i).getUser_id());
                row.createCell(1).setCellValue(userList.get(i).getUser_account());
                row.createCell(2).setCellValue(userList.get(i).getUser_name());
                row.createCell(3).setCellValue(userList.get(i).getUser_age());
                row.createCell(4).setCellValue(userList.get(i).getUser_sex());
                row.createCell(5).setCellValue(userList.get(i).getUser_address());
                row.createCell(6).setCellValue(userList.get(i).getUser_birth());
                row.createCell(7).setCellValue(userList.get(i).getEmail());
                row.createCell(8).setCellValue(userList.get(i).getUser_phone());
                row.createCell(9).setCellValue(userList.get(i).getDept_name());
            }
//        //输出Excel文件  
//        try {
            OutputStream output=response.getOutputStream();
            response.reset();  
            response.setHeader("Content-disposition", "attachment; filename=userinfo.xls");  
            response.setContentType("application/msexcel");          
            workBook.write(output);  
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }  
         
    }
 
    public Map<String,Object> importExcel(MultipartFile file) {
        List<UserBean>userList=new ArrayList<UserBean>();
        Map<String,Object>map=new HashMap<String, Object>();
        int x=0,y=0,sum=0;
        boolean flag=false;
        if(file!=null){
            //根据指定的文件输入流导入Excel从而产生Workbook对象  
            try {
                Workbook workBook=new HSSFWorkbook(file.getInputStream());
                //获取Excel文档中的第一个表单
                Sheet sheet=workBook.getSheetAt(0);
                //对Sheet中的每一行进行迭代  
                for (Row r : sheet) {  
                //如果当前行的行号(从0开始)未达到2(第三行)则从新循环  
                if(r.getRowNum()<2){  
                      continue;  
                     } 
                
                UserBean user=new UserBean();
                user.setUser_account(r.getCell(0).getStringCellValue());
                user.setUser_name(r.getCell(1).getStringCellValue());
                user.setUser_password(r.getCell(2).getStringCellValue());
                user.setUser_address(r.getCell(3).getStringCellValue());
                user.setUser_phone(r.getCell(4).getStringCellValue());
                userList.add(user);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            y=userList.size();
            for(UserBean u:userList){
                List<UserBean> user=userDao.getUserByAccount(u.getUser_account());
                if(user!=null&&user.size()>0){
                    x++;
                }else{
                    int i=userDao.saveUser(u);
                    sum+=i;
                }
            }
            if(sum==(y-x)){
                flag=true;
            }
        }
        map.put("msg", "共有"+y+"条数据,成功导入"+sum+"条数据,登陆账号重名的数据有"+x+"条");
        map.put("flag", flag);
        return map;
    }
 
    public Map<String, Object> getUserListByCon(int spage, int epage, int userId, String userName, int deptId) {
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            List<UserBean> userList = userDao.getUserListByCon(spage, epage, userId, userName, deptId);
            int count = userDao.getContByCon(userId, userName, deptId);
            map.put("rows", userList);
            map.put("total", count);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return map;
    }
 
    public Map<String, Object> getHightsInfo() {
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            List<UserBean> nanlist = userDao.getUserCharNan();
            List<UserBean> nvList = userDao.getUserCharNv();
            int naninfo[] = new int[12];
            int nvinfo[] = new int[12];
            // 男
            for (UserBean u : nanlist) {// 数据库月份信息
                String str[] = u.getMon().split("-");
                int j = Integer.parseInt(str[1]);
                for (int i = 1; i <= 12; i++) {
                    if (j == i) {
                        naninfo[i - 1] = u.getNum();
                    }
                }
            }
 
            for (UserBean u : nvList) {// 数据库月份信息
                String str[] = u.getMon().split("-");
                int j = Integer.parseInt(str[1]);
                for (int i = 1; i <= 12; i++) {
                    if (j == i) {
                        nvinfo[i - 1] = u.getNum();
                    }
                }
            }
            map.put("naninfo", naninfo);
            map.put("nvinfo", nvinfo);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return map;
    }
    
}