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.Xfcygl;
import com.ruoyi.gysl.entity.request.XfcyglPageReq;
import com.ruoyi.gysl.service.XfcyglService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
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;

/**
 * 细分产业管理(Xfcygl)表控制层
 *
 * @author makejava
 * @since 2025-03-22 10:39:43
 */
@Api(tags ="细分产业管理" )
@RestController
@RequestMapping("/gysl/xfcygl")
@PreAuthorize("@ss.hasAnyRoles('admin,common,gov')")
public class XfcyglController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private XfcyglService xfcyglService;

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

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

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

    /**
     * 修改数据
     *
     * @param xfcygl 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改数据")
    @PutMapping("/edit")
    public AjaxResult update(@RequestBody Xfcygl xfcygl) {
        return success(xfcyglService.updateById(xfcygl));
    }

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


    /**
     * 根据条件导出所有细分产业
     */
    @ApiOperation(value = "根据条件导出所有细分产业")
    @PostMapping(value = "/exportXfcy")
    public void exportXfcy(HttpServletResponse response, XfcyglPageReq req) {
        List<Xfcygl> filteredList = xfcyglService.page(req);
        ExcelUtil<Xfcygl> util = new ExcelUtil<>(Xfcygl.class);
        util.exportExcel(response, filteredList, "细分产业数据");
    }

    /**
     * 导入已有的细分产业数据
     */
    @ApiOperation(value = "导入已有的细分产业数据")
    @PostMapping(value = "/imporXfcy", consumes = "multipart/form-data")
    public AjaxResult imporXfcy(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelUtil<Xfcygl> util = new ExcelUtil<>(Xfcygl.class);
        List<Xfcygl> proList = util.importExcel(file.getInputStream());
        StringBuilder successMsg = new StringBuilder();
        if (proList == null || proList.isEmpty()) {
            throw new ServiceException("导入数据不能为空");
        } else {
            xfcyglService.saveBatch(proList);
            successMsg.append("导入成功");
        }
        return AjaxResult.success(successMsg);
    }

    /**
     * 模板信息
     */
    @ApiOperation("导细分产业管理模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Xfcygl> util = new ExcelUtil<>(Xfcygl.class);
        util.importTemplateExcel(response, "细分产业管理");
    }
}

