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.ScUserRole;
import com.sunyard.ssp.modules.user.service.IScUserRoleService;
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("/userRole")
@Transactional
public class ScUserRoleController {

    @Autowired
    IScUserRoleService iScUserRoleService;

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

        ScUserRole entity = iScUserRoleService.getById(id);
        return new ResultUtil<ScUserRole>().setData(entity);
    }

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

        List<ScUserRole> list = iScUserRoleService.list();
        return new ResultUtil<List<ScUserRole>>().setData(list);
    }

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

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

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

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

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

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

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