package com.dayou.controller;

import com.dayou.annotation.OperLog;
import com.dayou.vo.PostVO;
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 org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IPostService;
import com.dayou.entity.Post;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.HttpKit;
import com.dayou.exception.ErrorCode;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.multipart.MultipartFile;
/**
 * 岗位
 *
 * @author wucl
 * @since 2022-11-29
 */
@RestController
@RequestMapping("post")
@Slf4j
public class PostController extends BaseController {
    @Autowired
    private IPostService postService;

    /**
    * 岗位列表
    */
    @GetMapping("")
    public RestResponse<Page<PostVO>> page(PostVO postVO, Page page){
        Page<PostVO> pages=postService.selectPage(page,postVO);
        return RestResponse.data(pages);
    }

    /**
     * 岗位详情
     */
    @GetMapping("/{id}")
    public RestResponse<Post> detail(@PathVariable Long id){
        Post xPost =postService.detail(id);
        return RestResponse.data(xPost);
     }

    /**
     * 岗位新增
     */
    @OperLog(title = "新增岗位")
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody Post post) {
        Boolean ret = postService.add(post);
        return RestResponse.data(ret);
    }

    /**
     * 岗位更新
     */
    @OperLog(title = "更新岗位")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody Post post) {
        Boolean ret = postService.update(post);
        return RestResponse.data(ret);
    }

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


    /**
     * 岗位下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<Post> lambdaQueryWrapper = new LambdaQueryWrapper<Post>()
        .select(Post::getId,Post::getName)
        .eq(Post::getDeleted,false);
        List<Post> list= postService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 岗位下拉列表（含部门名称）
     * @return
     */
    @GetMapping("/xSimpleAll")
    public RestResponse<List<PullDownModel>> xSimpleAll(){
        List<PullDownModel > result = postService.xSimpleAll();
        return RestResponse.data(result);
    }
    /**
     * 岗位模板下载
     */
    @GetMapping(value = "/download/importTemplate")
    public void importTemplate() {
        try {
            XSSFWorkbook wb = postService.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) {
        postService.importExcel(file);
        return RestResponse.data(true);
    }

    /**
     * 岗位导出
     */
    @GetMapping(value = "/download/exportExcel")
    public void export(Post post) {
        try {
            XSSFWorkbook wb =  postService.exportExcel(post);

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

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

    }

    /**
     * 岗位列表不分页
     * @param postVO
     * @return
     */
    @GetMapping("/list")
    public RestResponse<List<Post>> getPostList(PostVO postVO){
        List<Post> posts = postService.getPostList(postVO);
        return RestResponse.data(posts);
    }


}

