package com.gree.mall.manager.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.admin.AdminRoleLogic;
import com.gree.mall.manager.plus.entity.AdminRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(value = "系统角色管理", tags = {"系统角色管理"})
@RequestMapping(value = "/admin/role", produces = "application/json; charset=utf-8")
public class AdminRoleController {

    @Resource
    AdminRoleLogic adminRoleLogic;

    @GetMapping("/list")
    @ApiOperation(value = "角色列表")
    public ResponseHelper<IPage<AdminRole>> list(
            HttpServletRequest request,
            @ApiParam(value = "商户名称") @RequestParam(required = false) String companyWechatName,
            @ApiParam(value = "上级商户") @RequestParam(required = false) String adminCompanyId,
            @ApiParam(value = "适用机构 2:商家 1:商户 0:平台") @RequestParam(required = false) Integer type,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<AdminRole> adminRoleIPage = adminRoleLogic.listPage(request, companyWechatName, adminCompanyId, type, pageNum, pageSize);
        return ResponseHelper.success(adminRoleIPage);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "角色详情")
    public ResponseHelper<AdminRole> detail(
            @ApiParam(value = "角色id", required = true) @RequestParam(required = true) String adminRoleId
    ) throws RemoteServiceException {
        AdminRole detail = adminRoleLogic.detail(adminRoleId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    public ResponseHelper add(
            HttpServletRequest request,
            @RequestBody AdminRole adminRole
    ) throws RemoteServiceException {
        adminRoleLogic.add(request, adminRole);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改角色")
    public ResponseHelper update(@RequestBody AdminRole adminRole) throws RemoteServiceException {
        adminRoleLogic.update(adminRole);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    public ResponseHelper delete(
            @ApiParam(value = "角色id", required = true) @RequestParam String adminRoleId
    ) throws RemoteServiceException {
        adminRoleLogic.delete(adminRoleId);
        return ResponseHelper.success();
    }

}
