package com.ruoyi.gysl.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gysl.entity.Xmzsk;
import com.ruoyi.gysl.entity.request.XmzskPageReq;
import com.ruoyi.gysl.service.XmzskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * 项目知识库(Xmzsk)表控制层
 *
 * @author makejava
 * @since 2025-02-24 14:31:17
 */
@Api(tags = "项目知识库")
@RestController
@RequestMapping("/gysl/xmzsk")
@PreAuthorize("@ss.hasAnyRoles('admin,common,gov')")
public class XmzskController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private XmzskService xmzskService;

    /**
     * 分页查询所有数据
     *
     * @param page  分页对象
     * @return 所有数据
     */
    @GetMapping("/page")
    @ApiOperation("分页查询所有数据")
    public AjaxResult selectAll(Page<Xmzsk> page,XmzskPageReq req) {
        return success(xmzskService.page(page,req));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @ApiOperation("通过主键查询单条数据")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(xmzskService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param xmzsk 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增数据")
    public AjaxResult insert(@RequestBody Xmzsk xmzsk) {
        return success(xmzskService.save(xmzsk));
    }

    /**
     * 修改数据
     *
     * @param xmzsk 实体对象
     * @return 修改结果
     */
    @PostMapping("/edit")
    @ApiOperation("修改数据")
    public AjaxResult update(@RequestBody Xmzsk xmzsk) {
        return success(xmzskService.updateById(xmzsk));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除数据")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(xmzskService.removeByIds(idList));
    }

    /**
     * 导出项目数据
     */
//    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation("导出项目数据")
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response,XmzskPageReq req) {
        List<Xmzsk> pE = xmzskService.page(req);
        ExcelUtil<Xmzsk> util = new ExcelUtil<>(Xmzsk.class);
        util.exportExcel(response, pE, "项目知识库数据");
    }



    /**
     * 模板信息
     */
    @ApiOperation("导出基本信息模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Xmzsk> util = new ExcelUtil<>(Xmzsk.class);
        util.importTemplateExcel(response, "项目知识库数据");
    }

    /**
     * 项目知识库信息批量导入
     */
    @ApiOperation(value = "项目知识库信息批量导入")
//    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @PostMapping(value = "/import", consumes = "multipart/form-data")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importTemplateProject(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelUtil<Xmzsk> util = new ExcelUtil<>(Xmzsk.class);
        List<Xmzsk> proList = util.importExcel(file.getInputStream());
        StringBuilder successMsg = new StringBuilder();
        if (proList == null || proList.isEmpty()) {
            throw new ServiceException("项目知识库导入数据不能为空");
        } else {
            xmzskService.saveBatch(proList);
            successMsg.append("导入成功");
        }
        return AjaxResult.success(successMsg);
    }
}

