package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.SysMenuPower;
import com.ssm.manage.query.MenuPowerRequest;
import com.ssm.manage.query.PowerRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmSysMenuPowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(value="权限controller",tags={"权限相关接口"})
@RestController
@RequestMapping("/demay/ssm/menupower")
@Slf4j
public class SsmSysMenuPowerController {
    @Autowired
    private SsmSysMenuPowerService ssmSysMenuPowerService;

    @ApiOperation(value = "菜单权限查询",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
    public GenericResponse<List<SysMenuPower>> list(@RequestBody MenuPowerRequest menuPowerRequest){
        GenericResponse<List<SysMenuPower>> genericResponse = new GenericResponse();
        List<SysMenuPower> list = new ArrayList();
        try {
            list = ssmSysMenuPowerService.listMenuPower(menuPowerRequest);
        }catch (Exception e){
            log.error("SsmSysMenuPowerController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
    @ApiOperation(value = "菜单权限查询",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/listNew")
    public GenericResponse<List<SysMenuPower>> listNew(@RequestBody MenuPowerRequest menuPowerRequest){
        GenericResponse<List<SysMenuPower>> genericResponse = new GenericResponse();
        List<SysMenuPower> list = new ArrayList();
        try {
            list = ssmSysMenuPowerService.listNew(menuPowerRequest);
        }catch (Exception e){
            log.error("SsmSysMenuPowerController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
    @ApiOperation(value = "用户菜单权限查询",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/listbyStaffId")
    public GenericResponse<List<SysMenuPower>> listbyStaffId(@RequestBody MenuPowerRequest menuPowerRequest){
        GenericResponse<List<SysMenuPower>> genericResponse = new GenericResponse();
        List<SysMenuPower> list = new ArrayList();
        try {
            list = ssmSysMenuPowerService.listbyStaffId(menuPowerRequest);
        }catch (Exception e){
            log.error("SsmSysMenuPowerController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    //权限菜单新增  sys_power  sys_menu_power
    @ApiOperation(value = "菜单权限查询",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody PowerRequest menuPowerRequest){
        GenericResponse genericResponse = new GenericResponse();
        try {
             ssmSysMenuPowerService.addMenuPower(menuPowerRequest);
        }
        catch (Exception e){
            log.error("SsmSysMenuPowerController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "菜单权限修改",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody PowerRequest menuPowerRequest){
        GenericResponse genericResponse = new GenericResponse();
        try {
            ssmSysMenuPowerService.update(menuPowerRequest);
        }catch (Exception e){
            log.error("SsmSysMenuPowerController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "菜单权限删除",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody PowerRequest menuPowerRequest){
        GenericResponse genericResponse = new GenericResponse();
        try {
            ssmSysMenuPowerService.delete(menuPowerRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }
        catch (Exception e){
            log.error("SsmSysMenuPowerController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }



    @ApiOperation(value = "当前角色拥有菜单权限查询",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/listByRole")
    public GenericResponse<List<SysMenuPower>> listByRole(@RequestBody MenuPowerRequest menuPowerRequest){
        GenericResponse<List<SysMenuPower>> genericResponse = new GenericResponse();
        List<SysMenuPower> list = new ArrayList();
        try {
            list = ssmSysMenuPowerService.listByRole(menuPowerRequest);
        }catch (Exception e){
            log.error("SsmSysMenuPowerController.listByRole",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "菜单权限查询",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/selectAll")
    public GenericResponse<List<SysMenuPower>> selectAll(@RequestBody MenuPowerRequest menuPowerRequest){
        GenericResponse<List<SysMenuPower>> genericResponse = new GenericResponse();
        List<SysMenuPower> list = new ArrayList();
        try {
            list = ssmSysMenuPowerService.selectAll(menuPowerRequest);
        }catch (Exception e){
            log.error("SsmSysMenuPowerController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

}
