package com.ruoyi.web.modules.template.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.modules.template.dto.AttrTemplateSaveDTO;
import com.ruoyi.web.modules.template.exception.AttrTemplateBatchDeleteException;
import com.ruoyi.web.modules.template.service.IAttrTemplateService;

/**
 * 商家端属性模版
 */
@RestController
@RequestMapping("/agri/seller/attrTemplate")
public class SellerAttrTemplateController extends BaseController
{
    @Autowired
    private IAttrTemplateService attrTemplateService;

    @PreAuthorize("@ss.hasPermi('agri:seller:attrTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "keyword", required = false) String keyword)
    {
        startPage();
        return getDataTable(attrTemplateService.selectTemplateList(keyword));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:attrTemplate:query')")
    @GetMapping("/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return success(attrTemplateService.selectTemplateDetail(templateId));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:attrTemplate:add')")
    @Log(title = "属性模版", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttrTemplateSaveDTO dto)
    {
        return toAjax(attrTemplateService.insertTemplate(dto, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:attrTemplate:edit')")
    @Log(title = "属性模版", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttrTemplateSaveDTO dto)
    {
        return toAjax(attrTemplateService.updateTemplate(dto, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:attrTemplate:remove')")
    @Log(title = "属性模版", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable("templateIds") Long[] templateIds)
    {
        try
        {
            attrTemplateService.deleteTemplateByIds(templateIds, getUsername());
            return success();
        }
        catch (AttrTemplateBatchDeleteException e)
        {
            Map<String, Object> data = new HashMap<>();
            data.put("reasons", e.getReasons());
            AjaxResult result = AjaxResult.error(e.getMessage());
            result.put(AjaxResult.DATA_TAG, data);
            return result;
        }
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:attrTemplate:list')")
    @GetMapping("/options")
    public AjaxResult options()
    {
        return success(attrTemplateService.selectOptions());
    }
}
