cn.mypandora.controller.BaseUserController.java Source code

Java tutorial

Introduction

Here is the source code for cn.mypandora.controller.BaseUserController.java

Source

/**
 * Copyright  2015.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.mypandora.controller;

import cn.mypandora.po.BaseUser;
import cn.mypandora.po.UploadFile;
import cn.mypandora.service.BaseUserService;
import cn.mypandora.util.MyDateUtils;
import cn.mypandora.util.MyExcelUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>User: hankaibo
 * <p>Date: 2016/9/26
 * <p>Version: 1.0
 */
@Api(value = "user-api", description = "CURD?")
@Controller
@RequestMapping(value = "/users")
public class BaseUserController {
    private final static String XLS = ".xls";
    private final static String XLSX = ".xlsx";

    @Resource
    private BaseUserService baseUserService;

    /**
     * ?
     */
    @RequestMapping(value = "/toList", method = RequestMethod.GET)
    public String toList() {
        return "user/list";
    }

    /**
     * ??
     *
     * @return ??
     */
    @ApiOperation(value = "??", notes = "", response = BaseUser.class)
    @RequestMapping(value = { "" }, method = RequestMethod.GET)
    public @ResponseBody PageInfo<BaseUser> list(HttpServletRequest request) {
        //?jqGrid?????
        int currentPage = Integer.parseInt(request.getParameter("page"));
        int pageSize = Integer.parseInt(request.getParameter("rows"));
        String sortName = request.getParameter("sidx");
        String orderName = request.getParameter("sord");
        String username = request.getParameter("username");

        PageInfo<BaseUser> page = new PageInfo<>();
        page.setPageNum(currentPage);
        page.setPageSize(pageSize);

        Map<String, Object> params = new HashMap<>();
        if (null != sortName && StringUtils.isNotBlank(sortName)) {
            params.put("sortName", sortName);
        }
        if (null != orderName && StringUtils.isNotBlank(orderName)) {
            params.put("orderName", orderName);
        }
        if (null != username && StringUtils.isNotBlank(username)) {
            params.put("username", username);
        }

        page = baseUserService.findPageUserByCondition("pageUsers", params, page);
        return page;
    }

    /**
     * ?
     */
    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAdd() {
        return "user/add";
    }

    /**
     * 
     */
    @ApiOperation(value = "", notes = "", response = BaseUser.class)
    @RequestMapping(method = RequestMethod.POST)
    public String add(BaseUser baseUser) {
        baseUserService.addUser(baseUser);
        return "user/list";
    }

    /**
     * 
     */
    @ApiOperation(value = "")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public @ResponseBody Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        baseUserService.deleteUser(id);
        result.put("code", 200);
        result.put("msg", "OK");
        return result;
    }

    /**
     * ?
     */
    @ApiOperation(value = "?")
    @RequiresRoles("admin")
    @RequestMapping(value = "/batch/{ids}", method = RequestMethod.DELETE)
    public @ResponseBody Map<String, Object> delete(@PathVariable Long[] ids) {
        Map<String, Object> result = new HashMap<>();
        baseUserService.deleteBatchUser(ids);
        result.put("code", 200);
        result.put("msg", "OK");
        return result;
    }

    /**
     * ?
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String update(@PathVariable Long id, ModelMap model) {
        BaseUser baseUser = baseUserService.findUserById(id);
        model.put("user", baseUser);
        return "user/edit";
    }

    /**
     * 
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ApiResponses(value = { @ApiResponse(code = 200, message = "?", response = BaseUser.class),
            @ApiResponse(code = 404, message = "??"),
            @ApiResponse(code = 500, message = "") })
    public String update(BaseUser baseUser) {
        baseUserService.updateUser(baseUser);
        return "user/list";
    }

    @RequestMapping(value = "/up", method = RequestMethod.POST)
    public void up(@RequestParam("FileDate") Part part) {
        UploadFile file = new UploadFile();
        file.setFileSize(part.getSize());

    }

    /**
     * 
     */
    @ApiOperation(value = "")
    @RequestMapping(value = "/down/{currentPage}", method = RequestMethod.GET)
    public ResponseEntity<byte[]> down(@PathVariable int currentPage, HttpServletRequest request)
            throws IOException {
        // ???Excel?
        PageInfo<BaseUser> page = new PageInfo<>();
        page.setPageNum(currentPage);
        page = baseUserService.findPageUserByCondition("pageUsers", null, page);
        // ???
        String rootpath = request.getSession().getServletContext().getRealPath("/");
        String fileName = MyDateUtils.getCurrentDate() + XLSX;
        MyExcelUtil.writeExcel(rootpath + "download" + fileName, "sheet1", "ID,??,,,",
                page.getList(), BaseUser.class, "id,username,sex,birthday,credits");
        // 
        File file = new File(rootpath + "download" + fileName);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", fileName);
        return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
    }

    /*********************** ? **********************/
    /**
     * ?
     */
    @ApiOperation(value = "?")
    @RequestMapping(value = "/me", method = RequestMethod.GET)
    public String myInfo(ModelMap model) {
        BaseUser userself = new BaseUser();
        // TODO IDsession?
        userself = baseUserService.findUserById(5L);
        model.put("userself", userself);
        return "user/myinfo";
    }

    /**
     * ??
     */
    @RequestMapping(value = "/me/{id}", method = RequestMethod.GET)
    public String updateMe(@PathVariable Long id, ModelMap model) {
        BaseUser baseUser = baseUserService.findUserById(id);
        model.put("userself", baseUser);
        return "user/myedit";
    }

    /**
     * ?
     */
    @ApiOperation(value = "?")
    @RequestMapping(value = "/me", method = RequestMethod.PUT)
    public String updateMe(BaseUser baseUser) {
        baseUserService.updateUser(baseUser);
        return "redirect:/user/me";
    }

    /*********************** ? **********************/
    /**
     * ?
     */
    @RequestMapping(value = "/analysis", method = RequestMethod.GET)
    public String userAnalysis(ModelMap model) {
        //??
        //        String curMonth=MyDateUtils.getCurrentMonth();
        //        List<Map<String,Object>> userCount=baseUserService.findUserCount(curMonth);
        //        model.put("userCount", userCount);
        //        return "user/useranalysis";

        Map<String, Object> sexCount = baseUserService.findUserSexCount();
        model.put("sexCount", sexCount);
        return "user/analysis";
    }

}