package com.ruoyi.web.controller.system;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ILcdInfoImportDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PostUser;
import com.ruoyi.system.service.IPostUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 权限模块 用户Controller
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Api(tags = "PostUserController",description = "用户表")
@RestController
@RequestMapping("/system/user")
public class PostUserController extends BaseController
{
    @Autowired
    private IPostUserService postUserService;

    @Autowired
    private ILcdInfoImportDataService iLcdInfoImportDataService;
    /**
     * 查询权限模块 用户列表
     */
    @ApiOperation("查询用户列表")
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostUser postUser)
    {
        startPage();
        List<PostUser> list = postUserService.selectPostUserList(postUser);
        return getDataTable(list);
    }

    /**
     * 导出权限模块 用户列表
     */
    @ApiOperation("导出用户列表")
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "权限模块 用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostUser postUser)
    {
        List<PostUser> list = postUserService.selectPostUserList(postUser);
        ExcelUtil<PostUser> util = new ExcelUtil<PostUser>(PostUser.class);
        util.exportExcel(response, list, "权限模块 用户数据");
    }

    @ApiOperation("导入用户列表")
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @RequestMapping(value = "/importData", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
    public AjaxResult importData(@RequestPart("fire") MultipartFile file, @RequestParam(value = "updateSupport",required=false)boolean updateSupport) throws Exception
    {
        ExcelUtil<PostUser> util = new ExcelUtil<PostUser>(PostUser.class);
        List<PostUser> stuList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = iLcdInfoImportDataService.importData(stuList, updateSupport, operName);
        return AjaxResult.success(message);
 }

    /**
     * 下载模板
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<PostUser> util = new ExcelUtil<PostUser>(PostUser.class);
        return util.importTemplateExcel("用户数据");
    }


    /**
     * 获取权限模块 用户详细信息
     */
    @ApiOperation("获取用户列表")
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(postUserService.selectPostUserById(id));
    }

    /**
     * 新增权限模块 用户
     */
    @ApiOperation("新增用户列表")
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "权限模块 用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostUser postUser)
    {
        return toAjax(postUserService.insertPostUser(postUser));
    }

    /**
     * 修改权限模块 用户
     */
    @ApiOperation("修改用户列表")
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "权限模块 用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostUser postUser)
    {
        return toAjax(postUserService.updatePostUser(postUser));
    }

    /**
     * 删除权限模块 用户
     */
    @ApiOperation("删除用户列表")
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "权限模块 用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(postUserService.deletePostUserByIds(ids));
    }
}
