package com.goldenwater.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.common.enums.BusinessType;
import com.goldenwater.system.domain.SysTemplate;
import com.goldenwater.system.service.ISysTemplateService;

/**
 * 系统模板 信息操作处理
 * 
 * @author goldenwater
 */
@RestController
@RequestMapping("/system/template")
public class SysTemplateController extends BaseController
{
    @Autowired
    private ISysTemplateService templateService;

    /**
     * 获取模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTemplate template, @RequestParam(required = false) String key)
    {
        if (key != null && !key.isEmpty())
        {
            template.setTplTitle("%" + key + "%");
        }
        startPage();
        List<SysTemplate> list = templateService.selectSysTemplateList(template);
        return getDataTable(list);
    }

    /**
     * 根据模板编码获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{tplCode}")
    public AjaxResult getInfo(@PathVariable String tplCode)
    {
        return success(templateService.selectSysTemplateById(tplCode));
    }

    /**
     * 新增模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "系统模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysTemplate template)
    {
        template.setCreateBy(getUsername());
        return toAjax(templateService.insertSysTemplate(template));
    }

    /**
     * 修改模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "系统模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysTemplate template)
    {
        template.setUpdateBy(getUsername());
        return toAjax(templateService.updateSysTemplate(template));
    }

    /**
     * 删除模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "系统模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tplCodes}")
    public AjaxResult remove(@PathVariable String[] tplCodes)
    {
        return toAjax(templateService.deleteSysTemplateByIds(tplCodes));
    }
}
