package com.ruoyi.base.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.base.vo.PicsVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.BasePics;
import com.ruoyi.base.service.IBasePicsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 图片库Controller
 * 
 * @author vinjor-m
 * @date 2025-06-24
 */
@RestController
@RequestMapping("/base/pics")
public class BasePicsController extends BaseController
{
    @Autowired
    private IBasePicsService basePicsService;

    /**
     * 查询图片库列表
     */
    @PreAuthorize("@ss.hasPermi('base:pics:list')")
    @GetMapping("/list")
    public AjaxResult list(BasePics basePics,
                                @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BasePics> page = new Page<>(pageNum, pageSize);
        IPage<BasePics> list = basePicsService.queryListPage(basePics,page);
        return success(list);
    }

    /**
     * 导出图片库列表
     */
    @PreAuthorize("@ss.hasPermi('base:pics:export')")
    @Log(title = "图片库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasePics basePics)
    {
        List<BasePics> list = basePicsService.list();
        ExcelUtil<BasePics> util = new ExcelUtil<BasePics>(BasePics.class);
        util.exportExcel(response, list, "图片库数据");
    }

    /**
     * 获取图片库详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:pics:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(basePicsService.getById(id));
    }

    /**
     * 新增图片库
     */
    @PreAuthorize("@ss.hasPermi('base:pics:add')")
    @Log(title = "图片库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PicsVO basePics)
    {
        List<BasePics> basePicsList = basePics.getFileList();
        basePicsList.forEach(item->{
            item.setFileType(basePics.getFileType());
            item.setTenantId(basePics.getTenantId());
        });
        return toAjax(basePicsService.saveBatch(basePicsList));
    }

    /**
     * 修改图片库
     */
    @PreAuthorize("@ss.hasPermi('base:pics:edit')")
    @Log(title = "图片库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasePics basePics)
    {
        return toAjax(basePicsService.updateById(basePics));
    }

    /**
     * 删除图片库
     */
    @PreAuthorize("@ss.hasPermi('base:pics:remove')")
    @Log(title = "图片库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(basePicsService.removeByIds(list));
    }
}
