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.ScRoleDepartment;
import com.sunyard.ssp.modules.user.service.IScRoleDepartmentService;
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-12
 */
@RestController
@Slf4j
@RequestMapping("/roleDepartment")
@Transactional
public class ScRoleDepartmentController {

    @Autowired
    IScRoleDepartmentService iScRoleDepartmentService;

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

        ScRoleDepartment entity = iScRoleDepartmentService.getById(id);
        return new ResultUtil<ScRoleDepartment>().setData(entity);
    }

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

        List<ScRoleDepartment> list = iScRoleDepartmentService.list();
        return new ResultUtil<List<ScRoleDepartment>>().setData(list);
    }

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

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

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

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

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

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

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