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

import java.util.List;
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.account.domain.PlatformAdminQuery;
import com.ruoyi.web.modules.account.dto.PlatformAdminCreateDTO;
import com.ruoyi.web.modules.account.dto.PlatformAdminUpdateDTO;
import com.ruoyi.web.modules.account.service.IPlatformAdminService;

/**
 * 平台管理员列表
 */
@RestController
@RequestMapping("/agri/platformAdmin")
public class PlatformAdminController extends BaseController
{
    @Autowired
    private IPlatformAdminService platformAdminService;

    @PreAuthorize("@ss.hasPermi('agri:platformAdmin:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlatformAdminQuery query)
    {
        startPage();
        return getDataTable(platformAdminService.selectAdminList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:platformAdmin:list')")
    @GetMapping("/roleOptions")
    public AjaxResult roleOptions()
    {
        return success(platformAdminService.selectRoleOptions());
    }

    @PreAuthorize("@ss.hasPermi('agri:platformAdmin:query')")
    @GetMapping("/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(platformAdminService.selectAdminDetail(userId));
    }

    @PreAuthorize("@ss.hasPermi('agri:platformAdmin:add')")
    @Log(title = "管理员列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformAdminCreateDTO dto)
    {
        Long userId = platformAdminService.createAdmin(dto, getUsername());
        return success(userId);
    }

    @PreAuthorize("@ss.hasPermi('agri:platformAdmin:edit')")
    @Log(title = "管理员列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformAdminUpdateDTO dto)
    {
        platformAdminService.updateAdmin(dto, getUserId(), getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:platformAdmin:remove')")
    @Log(title = "管理员列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable("userIds") Long[] userIds)
    {
        platformAdminService.deleteAdmin(userIds, getUserId());
        return success();
    }
}
