package com.qxgmat.controller.api; import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.Page; import com.nuliji.tools.PageMessage; import com.nuliji.tools.Response; import com.nuliji.tools.ResponseHelp; import com.nuliji.tools.Transform; import com.nuliji.tools.exception.ParameterException; import com.nuliji.tools.exception.SystemException; import com.qxgmat.data.constants.enums.SettingKey; import com.qxgmat.data.dao.entity.*; import com.qxgmat.data.relation.entity.UserHomeworkPreviewRelation; import com.qxgmat.dto.extend.UserHomeworkPreviewExtendDto; import com.qxgmat.dto.request.*; import com.qxgmat.dto.response.UserClassDetailDto; import com.qxgmat.dto.response.UserPrepareDetailDto; import com.qxgmat.dto.response.UserRealDto; import com.qxgmat.help.ShiroHelp; import com.qxgmat.service.HomeworkPreviewService; import com.qxgmat.service.UsersService; import com.qxgmat.service.inline.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.validation.Validator; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.util.Date; import java.util.List; import java.util.UUID; /** * Created by GaoJie on 2017/10/31. */ @RestController @RequestMapping("/api/auth") @Api(tags = "登录用户接口", description = "获取与操作当前用户信息", produces = MediaType.APPLICATION_JSON_VALUE) public class MyController { @Value("${upload.local_path}") private String localPath; @Value("${upload.web_url}") private String webUrl; @Autowired private Validator validator; @Autowired private ShiroHelp shiroHelp; @Autowired private UsersService usersService; @Autowired private UserMessageService userMessageService; @Autowired private SettingService settingService; @Autowired private StatDayService statDayService; @Autowired private UserClassService userClassService; @Autowired private UserServiceService userServiceService; @Autowired private UserCollectService userCollectService; @Autowired private UserNoteService userNoteService; @Autowired private UserQuestionService userQuestionService; @RequestMapping(value = "/email", method = RequestMethod.POST) @ApiOperation(value = "绑定邮箱", httpMethod = "POST") public Response email(@RequestBody @Validated UserEmailDto dto, HttpSession session, HttpServletRequest request) { User user = (User) shiroHelp.getLoginUser(); usersService.edit(User.builder() .id(user.getId()) .email(dto.getEmail()) .build()); return ResponseHelp.success(true); } @RequestMapping(value = "/info", method = RequestMethod.POST) @ApiOperation(value = "修改用户信息", httpMethod = "POST") public Response info(@RequestBody @Validated UserInfoDto dto){ User user = (User) shiroHelp.getLoginUser(); usersService.edit(User.builder() .id(user.getId()) .nickname(dto.getNickname()) .avatar(dto.getAvatar()) .build()); return ResponseHelp.success(true); } @RequestMapping(value = "/real", produces = MediaType.IMAGE_JPEG_VALUE, method = RequestMethod.POST) @ApiOperation(value = "实名认证", notes = "保存用户实名信息", httpMethod = "POST") public Response real(@RequestParam("file") MultipartFile multipartFile) { if (multipartFile.isEmpty()) { throw new ParameterException("上传文件为空"); } String contentType = multipartFile.getContentType(); if (!contentType.contains("")) { throw new ParameterException("文件类型错误"); } User user = (User) shiroHelp.getLoginUser(); UserRealDto dto = new UserRealDto(); // todo 随机文件名 String file = UUID.randomUUID().toString(); try { File dest = new File(localPath + File.separator+file); multipartFile.transferTo(dest); dto.setPhoto(webUrl+file); usersService.edit(User.builder() .id(user.getId()) .realAddress(dto.getAddress()) .realName(dto.getName()) .realIdentity(dto.getIdentity()) .realPhoto(dto.getPhoto()) .build()); return ResponseHelp.success(dto); } catch (IOException e) { e.printStackTrace(); return ResponseHelp.exception(new SystemException("图片上传失败")); } } @RequestMapping(value = "/message", method = RequestMethod.GET) @ApiOperation(value = "用户站内信", notes = "用户消息列表", httpMethod = "GET") public Response> message( @RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "100") int size, @RequestParam(required = false) String type, @RequestParam(required = false) Integer read ) { User user = (User) shiroHelp.getLoginUser(); Page p = userMessageService.select(page, size, user.getId(), type, read); return ResponseHelp.success(p, page, size, p.getTotal()); } @RequestMapping(value = "/message/read", method = RequestMethod.PUT) @ApiOperation(value = "读取消息", notes = "读取用户消息/全部", httpMethod = "PUT") public Response readMessage() { User user = (User) shiroHelp.getLoginUser(); userMessageService.clear(user.getId()); return ResponseHelp.success(true); } @RequestMapping(value = "/prepare", method = RequestMethod.PUT) @ApiOperation(value = "修改备考信息", notes = "修改用户备考信息", httpMethod = "PUT") public Response prepareEdit(@RequestBody @Validated UserPrepareDto dto) { User entity = Transform.dtoToEntity(dto); User user = (User) shiroHelp.getLoginUser(); userMessageService.clear(user.getId()); return ResponseHelp.success(true); } @RequestMapping(value = "/prepare", method = RequestMethod.GET) @ApiOperation(value = "获取备考信息", notes = "获取备考信息及分布", httpMethod = "GET") public Response prepareGet() { User user = (User) shiroHelp.getLoginUser(); User entity = usersService.get(user.getId()); UserPrepareDetailDto dto = Transform.convert(entity, UserPrepareDetailDto.class); Setting setting = settingService.getByKey(SettingKey.PREPARE_INFO); JSONObject value = setting.getValue(); return ResponseHelp.success(dto); } @RequestMapping(value = "/study", method = RequestMethod.GET) @ApiOperation(value = "获取学习记录", notes = "获取选择那天的做题信息", httpMethod = "GET") public Response studyTime( @RequestParam(required = false) String date ) { User user = (User) shiroHelp.getLoginUser(); try { Date day = DateFormat.getDateInstance().parse(date); } catch (ParseException e) { throw new ParameterException("日期格式错误"); } User entity = usersService.get(user.getId()); UserPrepareDetailDto dto = Transform.convert(entity, UserPrepareDetailDto.class); Setting setting = settingService.getByKey(SettingKey.PREPARE_INFO); JSONObject value = setting.getValue(); return ResponseHelp.success(dto); } @RequestMapping(value = "/collect", method = RequestMethod.PUT) @ApiOperation(value = "添加收藏", notes = "添加收藏", httpMethod = "PUT") public Response collectAdd(@RequestBody @Validated UserCollectDto dto) { UserCollect entity = Transform.dtoToEntity(dto); User user = (User) shiroHelp.getLoginUser(); userCollectService.addQuestion(user.getId(), entity.getQuestionNoId()); return ResponseHelp.success(true); } @RequestMapping(value = "/collect", method = RequestMethod.DELETE) @ApiOperation(value = "移除收藏", notes = "移除收藏", httpMethod = "DELETE") public Response collectDelete(int id) { User user = (User) shiroHelp.getLoginUser(); Boolean result = userCollectService.deleteQuestion(user.getId(), id); return ResponseHelp.success(result); } @RequestMapping(value = "/collect/list", method = RequestMethod.GET) @ApiOperation(value = "获取收藏列表", notes = "获取收藏列表", httpMethod = "GET") public Response> collectList( @RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "100") int size, @RequestParam(required = true) String module, @RequestParam(required = false) Number category, @RequestParam(required = false) String endTime, @RequestParam(required = false) Boolean finish ) { User user = (User) shiroHelp.getLoginUser(); Page p = userCollectService.list(page, size, category, user.getId(), endTime, finish); List pr = Transform.convert(p, UserHomeworkPreviewExtendDto.class); return ResponseHelp.success(pr, page, size, p.getTotal()); } @RequestMapping(value = "/error/list", method = RequestMethod.GET) @ApiOperation(value = "获取错题列表", notes = "获取错题列表", httpMethod = "GET") public Response> errorList( @RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "100") int size, @RequestParam(required = true) String module, @RequestParam(required = false) Number category, @RequestParam(required = false) String endTime, @RequestParam(required = false) Boolean finish ) { User user = (User) shiroHelp.getLoginUser(); Page p = userQuestionService.list(page, size, category, user.getId(), endTime, finish); List pr = Transform.convert(p, UserHomeworkPreviewExtendDto.class); return ResponseHelp.success(pr, page, size, p.getTotal()); } @RequestMapping(value = "/note", method = RequestMethod.PUT) @ApiOperation(value = "更新笔记", notes = "更新笔记", httpMethod = "PUT") public Response noteUpdate(@RequestBody @Validated UserNoteDto dto) { UserNote entity = Transform.dtoToEntity(dto); User user = (User) shiroHelp.getLoginUser(); entity.setUserId(user.getId()); userNoteService.update(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/note/list", method = RequestMethod.GET) @ApiOperation(value = "获取笔记列表", notes = "获取笔记列表", httpMethod = "GET") public Response> noteList( @RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "100") int size, @RequestParam(required = true) String module, @RequestParam(required = false) Number category, @RequestParam(required = false) String endTime, @RequestParam(required = false) Boolean finish ) { User user = (User) shiroHelp.getLoginUser(); Page p = userNoteService.list(page, size, category, user.getId(), endTime, finish); List pr = Transform.convert(p, UserHomeworkPreviewExtendDto.class); return ResponseHelp.success(pr, page, size, p.getTotal()); } @RequestMapping(value = "/report/list", method = RequestMethod.GET) @ApiOperation(value = "获取报告列表", notes = "获取报告列表", httpMethod = "GET") public Response> reportList( @RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "100") int size, @RequestParam(required = true) String module, @RequestParam(required = false) Number category, @RequestParam(required = false) String endTime, @RequestParam(required = false) Boolean finish ) { User user = (User) shiroHelp.getLoginUser(); Page p = userNoteService.list(page, size, category, user.getId(), endTime, finish); List pr = Transform.convert(p, UserHomeworkPreviewExtendDto.class); return ResponseHelp.success(pr, page, size, p.getTotal()); } }