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

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.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.role.domain.MerchantRoleQuery;
import com.ruoyi.web.modules.role.dto.MerchantRoleCreateDTO;
import com.ruoyi.web.modules.role.dto.MerchantRoleUpdateDTO;
import com.ruoyi.web.modules.role.service.IMerchantRoleService;

/**
 * 商家端角色管理
 */
@RestController
@RequestMapping("/agri/seller/role")
public class MerchantRoleController extends BaseController
{
    @Autowired
    private IMerchantRoleService merchantRoleService;

    @PreAuthorize("@ss.hasPermi('agri:seller:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantRoleQuery query)
    {
        if (query == null)
        {
            query = new MerchantRoleQuery();
        }
        merchantRoleService.fillListScope(query, getUserId());
        startPage();
        return getDataTable(merchantRoleService.selectRoleList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:role:list')")
    @GetMapping("/menuTree")
    public AjaxResult menuTree()
    {
        return success(merchantRoleService.selectMenuTree(getUserId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:role:query')")
    @GetMapping("/{roleId}")
    public AjaxResult getInfo(@PathVariable("roleId") Long roleId)
    {
        return success(merchantRoleService.selectRoleDetail(roleId, getUserId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:role:add')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantRoleCreateDTO dto)
    {
        Long roleId = merchantRoleService.createRole(dto, getUserId(), getUsername());
        AjaxResult result = success();
        result.put("roleId", roleId);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantRoleUpdateDTO dto)
    {
        merchantRoleService.updateRole(dto, getUserId(), getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:role:remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable("roleIds") Long[] roleIds)
    {
        merchantRoleService.deleteRoles(roleIds, getUserId());
        return success();
    }
}
