package com.ruoyi.web.modules.freight.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.freight.dto.FreightTemplateSaveDTO;
import com.ruoyi.web.modules.freight.exception.FreightTemplateBatchDeleteException;
import com.ruoyi.web.modules.freight.service.IFreightTemplateService;

/**
 * 商家端运费模版
 */
@RestController
@RequestMapping("/agri/seller/freightTemplate")
public class SellerFreightTemplateController extends BaseController
{
    @Autowired
    private IFreightTemplateService freightTemplateService;

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

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

    @PreAuthorize("@ss.hasPermi('agri:seller:freightTemplate:add')")
    @Log(title = "运费模版", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FreightTemplateSaveDTO dto)
    {
        return toAjax(freightTemplateService.insertTemplate(dto, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:freightTemplate:edit')")
    @Log(title = "运费模版", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FreightTemplateSaveDTO dto)
    {
        return toAjax(freightTemplateService.updateTemplate(dto, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:freightTemplate:edit')")
    @Log(title = "运费模版", businessType = BusinessType.UPDATE)
    @PutMapping("/default/{templateId}")
    public AjaxResult setDefault(@PathVariable("templateId") Long templateId)
    {
        return success(freightTemplateService.setDefaultTemplate(templateId, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:freightTemplate:remove')")
    @Log(title = "运费模版", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable("templateIds") Long[] templateIds)
    {
        try
        {
            freightTemplateService.deleteTemplateByIds(templateIds, getUsername());
            return success();
        }
        catch (FreightTemplateBatchDeleteException 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:freightTemplate:list')")
    @GetMapping("/options")
    public AjaxResult options()
    {
        return success(freightTemplateService.selectOptions());
    }
}
