package com.boying.controller;
|
|
import com.boying.common.BaseController;
|
import com.boying.common.SystemConfigProperties;
|
import com.boying.common.util.StringUtil;
|
import com.boying.entity.Dept;
|
import com.boying.entity.User;
|
import com.boying.service.DeptService;
|
import com.boying.service.StudyRecordService;
|
import com.boying.service.UserService;
|
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FilenameUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Sort;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.File;
|
import java.math.BigDecimal;
|
import java.util.List;
|
|
@RestController
|
@RequestMapping("user")
|
public class UserController extends BaseController {
|
|
@Autowired
|
private UserService userService;
|
@Autowired
|
private DeptService deptService;
|
@Autowired
|
private SystemConfigProperties systemConfigProperties;
|
@Autowired
|
private StudyRecordService studyRecordService;
|
|
@PostMapping("findPage")
|
public Object findPage(String name,int page,int pageSize,Long deptId) {
|
Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id");
|
Page<User> pages = userService.findPageHaveLike(pageable,1,"name",name,"deptId",deptId);
|
return success("",pages);
|
}
|
|
@PostMapping("save")
|
public Object save(User user) {
|
if(user.getId()==null){
|
user.setPassword(user.getLoginName());
|
}
|
if(user.getId()==null){
|
Dept dept = (Dept) deptService.findById(user.getDeptId());
|
dept.setNum(dept.getNum()+1);
|
deptService.save(dept);
|
}
|
userService.save(user);
|
return success("保存成功");
|
}
|
|
@PostMapping("delete")
|
public Object delete(Long id) {
|
User user = (User) userService.findById(id);
|
|
Dept dept = (Dept) deptService.findById(user.getDeptId());
|
dept.setNum(dept.getNum()-1);
|
if(dept.getNum()==0){
|
dept.setUserName(null);
|
dept.setUserId(null);
|
}
|
deptService.save(dept);
|
|
userService.delete(id);
|
return success("删除成功");
|
}
|
|
@PostMapping("login")
|
public Object login(String loginName,String password,String openid) {
|
if(StringUtil.isNullOrEmpty(loginName)||StringUtil.isNullOrEmpty(password)){
|
return error("用户名和密码必填");
|
}
|
List<User> all = userService.findAll("loginName",loginName,"password",password);
|
if(all.size()>0){
|
User user = all.get(0);
|
// user.setPdfs(studyRecordService.countByType(0,user.getId()));
|
// user.setVideos(studyRecordService.countByType(1,user.getId()));
|
// Integer time = studyRecordService.sumByTime(user.getId());
|
// if(time!=null){
|
// double d3 = (Double.valueOf(time))/60;
|
// double v = new BigDecimal(d3).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
|
// user.setTime(v);
|
// }
|
if(user.getDeptId()!=null){
|
Dept dept = (Dept)deptService.findById(user.getDeptId());
|
user.setDeptName(dept.getName());
|
}
|
user.setOpenid(openid);
|
userService.save(user);
|
return success("请求成功",user);
|
}
|
|
|
return error("用户名或密码不正确");
|
}
|
|
@PostMapping("updatePassword")
|
public Object updatePassword(Long userId,String oldPassword,String newPassword) {
|
List<User> all = userService.findAll("id",userId,"password",oldPassword);
|
if(all.size()>0){
|
User user = all.get(0);
|
user.setPassword(newPassword);
|
userService.save(user);
|
return success("请求成功");
|
}
|
return error("旧密码不正确");
|
}
|
|
@PostMapping("updateImg")
|
public Object updateImg(Long userId,HttpServletRequest request) throws Exception{
|
User user = (User) userService.findById(userId);
|
String path = "";
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
List<MultipartFile> imgs = multipartRequest.getFiles("img");
|
for(MultipartFile imageFile:imgs){
|
if (imageFile != null) {
|
String fileExtension = FilenameUtils.getExtension(imageFile.getOriginalFilename());//获取后缀
|
String newFileName = System.currentTimeMillis() + "." + fileExtension;//文件名称
|
path+=newFileName;
|
String newFilePath = systemConfigProperties.getUploadUserPath() + newFileName;//上传路径
|
File destFile = new File(newFilePath);
|
FileUtils.writeByteArrayToFile(destFile, imageFile.getBytes());
|
break;
|
}
|
}
|
user.setImg(path);
|
userService.save(user);
|
return success("修改成功",user);
|
}
|
|
@GetMapping("save2")
|
public Object save2() {
|
String a="130406197006010332\n" +
|
"130406198707282132\n" +
|
"130406196410172793\n" +
|
"13040619790226033x\n" +
|
"13040619661031061x\n" +
|
"130406197002150354\n" +
|
"130406196909290633\n" +
|
"130406198706200318\n" +
|
"130406197508130350\n" +
|
"130406196408062712\n" +
|
"130406196810170359\n" +
|
"130406198704090936\n" +
|
"130406197208130914\n" +
|
"130481197802093474\n" +
|
"130406197410260317\n" +
|
"130406196706020350\n" +
|
"130406197812210355\n" +
|
"130406197105012739\n" +
|
"130427198006051712\n" +
|
"130406197809020315\n" +
|
"130406196309110336\n" +
|
"130406197809080334\n" +
|
"130406197011150452\n" +
|
"130406198410130225\n" +
|
"130406196305112799\n" +
|
"130406197806072734\n" +
|
"130406198003200338\n" +
|
"130406196612140319\n" +
|
"130406198004032110\n" +
|
"130406198708062756\n" +
|
"130406197509120613\n" +
|
"130406198801180335\n" +
|
"130406196612010338\n" +
|
"130406197912070652";
|
String b = "靳文军\n" +
|
"马旦\n" +
|
"张爱军\n" +
|
"王海涛\n" +
|
"赵锡铎\n" +
|
"张丙洲\n" +
|
"王小红\n" +
|
"陈晓明\n" +
|
"梁志军\n" +
|
"李平安\n" +
|
"赵洪旺\n" +
|
"杜彦\n" +
|
"吝爱军\n" +
|
"赵延军\n" +
|
"陈志生\n" +
|
"张永军\n" +
|
"韩强\n" +
|
"庞显庆\n" +
|
"李广旭\n" +
|
"李长征\n" +
|
"蔡荣凯\n" +
|
"孙保功\n" +
|
"郜晓海\n" +
|
"马国萍\n" +
|
"房守民\n" +
|
"刘海燕\n" +
|
"刘明辉\n" +
|
"李鹏\n" +
|
"娄治海\n" +
|
"申先平\n" +
|
"范明晰\n" +
|
"刘龙\n" +
|
"李克荣\n" +
|
"高永亮";
|
String c = "15613065360\n" +
|
"13231001331\n" +
|
"15832018002\n" +
|
"13831088171\n" +
|
"13930086162\n" +
|
"16973103549\n" +
|
"13931022890\n" +
|
"17731088883\n" +
|
"15933889335\n" +
|
"15028060726\n" +
|
"15231838364\n" +
|
"13930064953\n" +
|
"13673106864\n" +
|
"15031073073\n" +
|
"18132976288\n" +
|
"13831039272\n" +
|
"13131081838\n" +
|
"13463105147\n" +
|
"15603100635\n" +
|
"13730015890\n" +
|
"13731030998\n" +
|
"15832080436\n" +
|
"13503103583\n" +
|
"13831071453\n" +
|
"13930090285\n" +
|
"13831036765\n" +
|
"13082100228\n" +
|
"15032023892\n" +
|
"13315005731\n" +
|
"15503011019\n" +
|
"13932037841\n" +
|
"18830887653\n" +
|
"13012115868\n" +
|
"15031055520";
|
String[] card = a.split("\n");
|
String[] name = b.split("\n");
|
String[] phone = c.split("\n");
|
for(int i=0;i<card.length;i++){
|
User user = new User();
|
user.setDeptId(Long.valueOf(20));
|
user.setPassword(phone[i]);
|
user.setName(name[i]);
|
user.setIdCard(card[i]);
|
user.setPhone(phone[i]);
|
user.setLoginName(phone[i]);
|
userService.save(user);
|
}
|
System.out.println(card.length);
|
return success("保存成功");
|
}
|
|
}
|