package com.ruoyi.label.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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.label.domain.TbLabelModelInfo;
import com.ruoyi.label.service.ITbLabelModelInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 标签模板信息Controller
 * 
 * @author 原动力
 * @date 2023-11-24
 */
@RestController
@RequestMapping("/label/labelModel")
public class TbLabelModelInfoController extends BaseController
{
    @Autowired
    private ITbLabelModelInfoService tbLabelModelInfoService;

    /**
     * 查询标签模板信息列表
     */
    @PreAuthorize("@ss.hasPermi('label:labelModel:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbLabelModelInfo tbLabelModelInfo)
    {
        startPage();
        List<TbLabelModelInfo> list = tbLabelModelInfoService.selectTbLabelModelInfoList(tbLabelModelInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('label:labelModel:query')")
    @GetMapping("/select")
    public AjaxResult select()
    {
        List<TbLabelModelInfo> list = tbLabelModelInfoService.selectTbLabelModelInfoList(null);
        List<Map<String, String>> select = list.stream().map(item -> {
            Map<String, String> map = new HashMap<>();
            map.put("label", item.getName());
            map.put("value", item.getLabelCode());
            return map;
        }).collect(Collectors.toList());
        return AjaxResult.success(select);
    }

    /**
     * 导出标签模板信息列表
     */
    @PreAuthorize("@ss.hasPermi('label:labelModel:export')")
    @Log(title = "标签模板信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbLabelModelInfo tbLabelModelInfo)
    {
        List<TbLabelModelInfo> list = tbLabelModelInfoService.selectTbLabelModelInfoList(tbLabelModelInfo);
        ExcelUtil<TbLabelModelInfo> util = new ExcelUtil<TbLabelModelInfo>(TbLabelModelInfo.class);
        util.exportExcel(response, list, "标签模板信息数据");
    }

    /**
     * 获取标签模板信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('label:labelModel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbLabelModelInfoService.selectTbLabelModelInfoById(id));
    }

    /**
     * 新增标签模板信息
     */
    @PreAuthorize("@ss.hasPermi('label:labelModel:add')")
    @Log(title = "标签模板信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbLabelModelInfo tbLabelModelInfo)
    {
        String username = getUsername();
        tbLabelModelInfo.setCreateBy(username);
        return toAjax(tbLabelModelInfoService.insertTbLabelModelInfo(tbLabelModelInfo));
    }

    /**
     * 修改标签模板信息
     */
    @PreAuthorize("@ss.hasPermi('label:labelModel:edit')")
    @Log(title = "标签模板信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbLabelModelInfo tbLabelModelInfo)
    {
        return toAjax(tbLabelModelInfoService.updateTbLabelModelInfo(tbLabelModelInfo));
    }

    /**
     * 删除标签模板信息
     */
    @PreAuthorize("@ss.hasPermi('label:labelModel:remove')")
    @Log(title = "标签模板信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbLabelModelInfoService.deleteTbLabelModelInfoByIds(ids));
    }
}
