package com.ruoyi.web.controller.material;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.system.domain.BomanMaterial;
import com.ruoyi.system.service.IBomanMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 素材库Controller
 * 
 * @author boman
 * @date 2023-11-02
 */
@RestController
@RequestMapping("/system/material")
public class BomanMaterialController extends BaseController
{
    @Autowired
    private IBomanMaterialService bomanMaterialService;

    /**
     * 查询素材库列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(BomanMaterial bomanMaterial)
    {
        startPage();
        List<BomanMaterial> list = bomanMaterialService.selectBomanMaterialList(bomanMaterial);
        return getDataTable(list);
    }

    /**
     * 导出素材库列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:export')")
    @Log(title = "素材库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BomanMaterial bomanMaterial)
    {
        List<BomanMaterial> list = bomanMaterialService.selectBomanMaterialList(bomanMaterial);
        ExcelUtil<BomanMaterial> util = new ExcelUtil<BomanMaterial>(BomanMaterial.class);
        util.exportExcel(response, list, "素材库数据");
    }

    /**
     * 获取素材库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return success(bomanMaterialService.selectBomanMaterialByMaterialId(materialId));
    }

    /**
     * 新增素材库
     */
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @Log(title = "素材库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BomanMaterial bomanMaterial)
    {
        List<BomanMaterial> bomanMaterialList = bomanMaterial.getBomanMaterialList();
        if (bomanMaterialList != null && bomanMaterialList.size() > 0){
            for (BomanMaterial material : bomanMaterialList) {
                material.setKeywords(bomanMaterial.getKeywords());
                material.setReleaseTime(bomanMaterial.getReleaseTime());
                material.setType(bomanMaterial.getType());
            }
        }
        return toAjax(bomanMaterialService.insertBomanMaterial(bomanMaterialList));
    }

    /**
     * 修改素材库
     */
    @PreAuthorize("@ss.hasPermi('system:material:edit')")
    @Log(title = "素材库", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody BomanMaterial bomanMaterial)
    {
        return toAjax(bomanMaterialService.updateBomanMaterial(bomanMaterial));
    }

    /**
     * 删除素材库
     */
    @PreAuthorize("@ss.hasPermi('system:material:remove')")
    @Log(title = "素材库", businessType = BusinessType.DELETE)
	@GetMapping("/delete/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(bomanMaterialService.deleteBomanMaterialByMaterialIds(materialIds));
    }
}
