package com.dayou.controller;

import com.dayou.annotation.OperLog;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.controller.BaseController;
import com.dayou.service.IUserPostService;
import com.dayou.entity.UserPost;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.utils.HttpKit;
import com.dayou.exception.ErrorCode;

import org.springframework.web.multipart.MultipartFile;
/**
 * 用户岗位
 *
 * @author wucl
 * @since 2022-11-29
 */
@RestController
@RequestMapping("userPost")
@Slf4j
public class UserPostController extends BaseController {
    @Autowired
    private IUserPostService userPostService;

    /**
    * 用户岗位列表
    */
    @GetMapping("")
    public RestResponse<Page<UserPost>> page(UserPost userPost, Page page){
        Page<UserPost> pages=userPostService.selectPage(page,userPost);
        return RestResponse.data(pages);
    }

    /**
     * 用户岗位详情
     */
    @GetMapping("/{id}")
    public RestResponse<UserPost> detail(@PathVariable Long id){
        UserPost xUserPost =userPostService.detail(id);
        return RestResponse.data(xUserPost);
     }

    /**
     * 用户岗位新增
     */
    @OperLog(title = "新增用户岗位")
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody UserPost userPost) {
        Boolean ret = userPostService.add(userPost);
        return RestResponse.data(ret);
    }

    /**
     * 用户岗位更新
     */
    @OperLog(title = "更新用户岗位")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody UserPost userPost) {
        Boolean ret = userPostService.update(userPost);
        return RestResponse.data(ret);
    }

    /**
     * 用户岗位删除
     */
    @OperLog(title = "删除用户岗位")
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = userPostService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 用户岗位模板下载
     */
    @GetMapping(value = "/download/importTemplate")
    public void importTemplate() {
        try {
            XSSFWorkbook wb = userPostService.generateImportTemplate();

            String fileName = "用户岗位导入模板" + System.currentTimeMillis() + ".xlsx";

            writeXss2Response(HttpKit.getRequest(), HttpKit.getResponse(), wb, fileName);
        } catch (Exception e) {
            log.error("生成用户岗位导入模板异常", e);
            ErrorCode.throwBusinessException(ErrorCode.DEFAULTERROR);
        }
    }

    /**
     * 用户岗位导入
     */
    @PostMapping("/importExcel")
    public RestResponse<Boolean> importExcel(@RequestPart("file") MultipartFile file) {
        userPostService.importExcel(file);
        return RestResponse.data(true);
    }

    /**
     * 用户岗位导出
     */
    @GetMapping(value = "/download/exportExcel")
    public void export(UserPost userPost) {
        try {
            XSSFWorkbook wb =  userPostService.exportExcel(userPost);

            String fileName = "用户岗位" + System.currentTimeMillis() + ".xlsx";

            writeXss2Response(HttpKit.getRequest(), HttpKit.getResponse(), wb, fileName);
        } catch (Exception e) {
            log.error("导出用户岗位", e);
            ErrorCode.throwBusinessException(ErrorCode.DEFAULTERROR);
        }

    }



}

