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.Ml;
import com.ruoyi.gysl.entity.Xfcygl;
import com.ruoyi.gysl.entity.request.MlPageReq;
import com.ruoyi.gysl.service.MlService;
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;

/**
 * 目录管理(Ml)表控制层
 *
 * @author makejava
 * @since 2025-03-22 09:21:57
 */
@RestController
@RequestMapping("/gysl/ml")
@Api(tags = "目录管理")
@PreAuthorize("@ss.hasAnyRoles('admin,common,gov')")
public class MlController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MlService mlService;

    /**
     * 分页查询所有目录
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询所有目录",response = Ml.class )
    public AjaxResult selectAll(Page<Ml> page, MlPageReq req) {
        return success(mlService.page(page,req));
    }

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

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

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

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

    /**
     * 根据条件导出所有目录
     */
    @ApiOperation(value = "根据条件导出所有目录")
    @PostMapping(value = "/exportMl")
    public void exportMl(HttpServletResponse response, MlPageReq req) throws Exception {
        List<Ml> filteredList = mlService.page(req);
        ExcelUtil<Ml> util = new ExcelUtil<>(Ml.class);
        util.exportExcel(response, filteredList, "目录数据");
    }

    /**
     * 导入已有的目录数据
     */
    @ApiOperation(value = "导入已有的目录数据")
    @PostMapping(value = "/importMl", consumes = "multipart/form-data")
    public AjaxResult importMl(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelUtil<Ml> util = new ExcelUtil<>(Ml.class);
        List<Ml> proList = util.importExcel(file.getInputStream());
        StringBuilder successMsg = new StringBuilder();
        if (proList == null || proList.isEmpty()) {
            throw new ServiceException("导入数据不能为空");
        } else {
            mlService.saveBatch(proList);
            successMsg.append("导入成功");
        }
        return AjaxResult.success(successMsg);
    }
    /**
     * 模板信息
     */
    @ApiOperation("目录管理模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Ml> util = new ExcelUtil<>(Ml.class);
        util.importTemplateExcel(response, "目录管理模板");
    }

}

