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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.common.constant.SqlConstant;
import com.sunyard.ssp.common.constant.ValidatorConstant;
import com.sunyard.ssp.modules.user.entity.ScPosition;
import com.sunyard.ssp.modules.user.entity.ScUser;
import com.sunyard.ssp.modules.user.service.IScPositionService;
import com.sunyard.ssp.modules.user.service.IScUserService;
import com.sunyard.ssp.modules.user.utils.validator.PositionValidator;
import com.sunyard.ssp.utils.PageUtil;
import com.sunyard.ssp.utils.ResultUtil;
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.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.DataBinder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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 zs
 * @since 2020-03-17
 */
@RestController
@Slf4j
@Api(description = "职位管理接口",tags ="")
@RequestMapping("/position")
@Transactional
public class ScPositionController {

    @Autowired
    PositionValidator positionValidator;

    @Autowired
    IScPositionService iPositionService;

    @Autowired
    IScUserService userService;

    @InitBinder(ValidatorConstant.POSTION_BASE_NAME)
    public void initBainder(DataBinder binder){
        binder.replaceValidators(positionValidator);
    }

    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "通过id获取")
    public Result<ScPosition> get(@RequestParam String id){

        ScPosition entity = iPositionService.getById(id);
        return new ResultUtil<ScPosition>().setData(entity);
    }

    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部数据")
    public Result<List<ScPosition>> getAll(@RequestParam(required = false,defaultValue = "") String name){

        List<ScPosition> list = iPositionService.list(new QueryWrapper<ScPosition>().lambda()
                .like(ScPosition::getName, SqlConstant.SQL_LIKE_WILDCARD + name + SqlConstant.SQL_LIKE_WILDCARD));
        return new ResultUtil<List<ScPosition>>().setData(list);
    }

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页获取")
    public Result<IPage<ScPosition>> getByPage(PageVo page){
        Pageable pageable = PageUtil.initMybatisPage(page );
        Page<ScPosition> data = new Page<>( pageable.getPageNumber(),pageable.getPageSize());
        IPage<ScPosition> iPage =  iPositionService.page(data);
        return new ResultUtil<IPage<ScPosition>>().setData(iPage);
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存数据")
    public Result<ScPosition> save(@ModelAttribute(ValidatorConstant.POSTION_BASE_NAME) @Validated ScPosition entity){

        if(CollectionUtils.isNotEmpty(iPositionService.list(new QueryWrapper<ScPosition>().lambda().eq(ScPosition::getName, entity.getName())))){
            return new ResultUtil<ScPosition>().setErrorMsg("添加失败,职位名已存在");
        }

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

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "更新数据")
    public Result<UpdateChainWrapper<ScPosition>> update(@ModelAttribute(ValidatorConstant.POSTION_BASE_NAME) @Validated ScPosition entity){

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

    @RequestMapping(value = "/deleteByIds/{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id:ids){
            List<ScUser> users = userService.list(new QueryWrapper<ScUser>().lambda().eq(ScUser::getPositionId, id));
            ScPosition position = iPositionService.getById(id);
            if(CollectionUtils.isNotEmpty(users)){
                return new ResultUtil<Object>().setErrorMsg("职位"+position.getName()+"有用户使用,禁止删除");
            }
            iPositionService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
