package com.sunyard.ssp.modules.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.ssp.common.PageVo;
import com.sunyard.ssp.common.Result;
import com.sunyard.ssp.modules.user.entity.ScRolePermission;
import com.sunyard.ssp.modules.user.service.IScRolePermissionService;
import com.sunyard.ssp.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tsz
 * @since 2020-03-11
 */
@RestController
@Slf4j
@RequestMapping("/rolePermission")
@Transactional
public class ScRolePermissionController {

    @Autowired
    IScRolePermissionService iScRolePermissionService;

    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    @ResponseBody
    public Result<ScRolePermission> get(@RequestParam String id){

        ScRolePermission entity = iScRolePermissionService.getById(id);
        return new ResultUtil<ScRolePermission>().setData(entity);
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    @ResponseBody
    public Result<List<ScRolePermission>> getAll(){

        List<ScRolePermission> list = iScRolePermissionService.list();
        return new ResultUtil<List<ScRolePermission>>().setData(list);
    }

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public Result<IPage<ScRolePermission>> getByPage(@ModelAttribute PageVo page){
        Page<ScRolePermission> data = new Page<>( page.getPageNumber(),page.getPageSize());
        IPage<ScRolePermission> iPage =  iScRolePermissionService.page(data);
        return new ResultUtil<IPage<ScRolePermission>>().setData(iPage);
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public Result<ScRolePermission> save(@ModelAttribute ScRolePermission entity){

        boolean e = iScRolePermissionService.save(entity);
        if ( !e ){
            return new ResultUtil<ScRolePermission>().setErrorMsg("添加失败");
        }
        return new ResultUtil<ScRolePermission>().setSuccessMsg("添加成功");
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ResponseBody
    public Result<UpdateChainWrapper<ScRolePermission>> update(@ModelAttribute ScRolePermission entity){

        boolean e = iScRolePermissionService.updateById(entity);
        if ( !e ){
            return new ResultUtil<UpdateChainWrapper<ScRolePermission>>().setErrorMsg("更新失败");
        }
        return new ResultUtil<UpdateChainWrapper<ScRolePermission>>().setSuccessMsg("更新成功");
    }

    @RequestMapping(value = "/deleteByIds",method = RequestMethod.DELETE)
    @ResponseBody
    public Result<Object> delAllByIds(@RequestParam String[] ids){

        for(String id:ids){
            iScRolePermissionService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
