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.Cybq;
import com.ruoyi.gysl.entity.request.CybqRequest;
import com.ruoyi.gysl.service.CybqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

/**
 * 产业标签(GyslCybq)表控制层
 *
 * @author makejava
 * @since 2025-04-02 11:02:34
 */
@Api(tags = "产业标签")
@RestController
@RequestMapping("/gysl/Cybq")
public class CybqController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private CybqService cybqService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param gyslCybq 查询实体
     * @return 所有数据
     */
    @ApiOperation("分页查询所有数据")
    @GetMapping("/page")
    public AjaxResult selectAll(Page<Cybq> page, CybqRequest gyslCybq) {
        return success(cybqService.page(page, gyslCybq));
    }

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

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

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

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

    /**
     * 产业标签批量导入
     */
    @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<Cybq> util = new ExcelUtil<>(Cybq.class);
        List<Cybq> proList = util.importExcel(file.getInputStream());
        StringBuilder successMsg = new StringBuilder();
        if (proList == null || proList.isEmpty()) {
            throw new ServiceException("产业标签导入数据不能为空");
        } else {
            cybqService.saveBatch(proList);
            successMsg.append("导入成功");
        }
        return AjaxResult.success(successMsg);
    }

    /**
     * 根据条件导出所有产业标签
     */
    @ApiOperation(value = "根据条件导出所有产业标签")
    @PostMapping(value = "/export")
    public void exportMl(HttpServletResponse response, CybqRequest req){
        List<Cybq> filteredList = cybqService.page(req);
        ExcelUtil<Cybq> util = new ExcelUtil<>(Cybq.class);
        util.exportExcel(response, filteredList, "产业标签");
    }
}

