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.ScDepartmentHeader;
import com.sunyard.ssp.modules.user.service.IScDepartmentHeaderService;
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("/departmentHeader")
@Transactional
public class ScDepartmentHeaderController {

    @Autowired
    IScDepartmentHeaderService iScDepartmentHeaderService;

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

        ScDepartmentHeader entity = iScDepartmentHeaderService.getById(id);
        return new ResultUtil<ScDepartmentHeader>().setData(entity);
    }

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

        List<ScDepartmentHeader> list = iScDepartmentHeaderService.list();
        return new ResultUtil<List<ScDepartmentHeader>>().setData(list);
    }

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

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

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

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

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

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

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