package com.dayou.controller;

import com.dayou.annotation.OperLog;
import com.dayou.bo.PrivilegeTreeBO;
import com.dayou.enums.OperationTypeEnum;
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.IPostPrivilegeService;
import com.dayou.entity.PostPrivilege;
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;

import java.util.List;

/**
 * 岗位菜单权限
 *
 * @author wucl
 * @since 2022-11-29
 */
@RestController
@RequestMapping("postPrivilege")
@Slf4j
public class PostPrivilegeController extends BaseController {
    @Autowired
    private IPostPrivilegeService postPrivilegeService;

    /**
    * 岗位菜单权限列表
    */
    @GetMapping("")
    public RestResponse<Page<PostPrivilege>> page(PostPrivilege postPrivilege, Page page){
        Page<PostPrivilege> pages=postPrivilegeService.selectPage(page,postPrivilege);
        return RestResponse.data(pages);
    }

    /**
     * 岗位菜单权限详情
     */
    @GetMapping("/{id}")
    public RestResponse<PostPrivilege> detail(@PathVariable Long id){
        PostPrivilege xPostPrivilege =postPrivilegeService.detail(id);
        return RestResponse.data(xPostPrivilege);
     }

    /**
     * 岗位菜单权限新增
     */
    @OperLog(title = "新增岗位菜单权限")
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody PostPrivilege postPrivilege) {
        Boolean ret = postPrivilegeService.add(postPrivilege);
        return RestResponse.data(ret);
    }

    /**
     * 岗位菜单权限更新
     */
    @OperLog(title = "更新岗位菜单权限")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody PostPrivilege postPrivilege) {
        Boolean ret = postPrivilegeService.update(postPrivilege);
        return RestResponse.data(ret);
    }

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


    /**
     * 岗位菜单权限模板下载
     */
    @GetMapping(value = "/download/importTemplate")
    public void importTemplate() {
        try {
            XSSFWorkbook wb = postPrivilegeService.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) {
        postPrivilegeService.importExcel(file);
        return RestResponse.data(true);
    }

    /**
     * 岗位菜单权限导出
     */
    @GetMapping(value = "/download/exportExcel")
    public void export(PostPrivilege postPrivilege) {
        try {
            XSSFWorkbook wb =  postPrivilegeService.exportExcel(postPrivilege);

            String fileName = "岗位菜单权限" + System.currentTimeMillis() + ".xlsx";

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

    }

    /**
     * 权限树
     *
     * @param postId
     */
    @GetMapping(value = "/tree")
    public RestResponse<List<PrivilegeTreeBO>> postPrivilegeTree(Long postId) {
        List<PrivilegeTreeBO> list = postPrivilegeService.postPrivilegeTree(postId);
        return RestResponse.data(list);
    }

}

