package com.ho.user.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.entity.PageVO;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageUtilsOld;
import com.ho.user.entity.SysPosition;
import com.ho.user.service.PositionService;
import com.ho.user.vo.req.PositionAddReqVO;
import com.ho.user.vo.req.PositionDeleteReqVO;
import com.ho.user.vo.req.PositionQueryReqVO;
import com.ho.user.vo.req.PositionUpdateReqVO;
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.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description 岗位Controller
 * Author yule
 * Date 2022/12/1 15:56
 */
@RequestMapping(ContextConstant.SYS + "position")
@RestController
@Api(tags = "系统模块-岗位管理")
@Slf4j
public class PositionController {

    @Autowired
    PositionService positionService;


    @GetMapping("selectOne/{id}")
    @ApiOperation(value = "根据id查询岗位对象")
    //@LogAnnotation(title = "岗位管理", action = "根据id查询岗位对象")
    public DataResult<SysPosition> selectList(@PathVariable("id") @NotNull(message = "岗位id不能为空") Integer id) {
        SysPosition sysPosition = positionService.selectById(id);
        return DataResult.success(sysPosition);
    }

    @PostMapping("selectList")
    @ApiOperation(value = "查询岗位列表")
    public DataResult<Page<SysPosition>> selectList(@RequestBody PositionQueryReqVO vo) {
        PageVO<SysPosition> pageVO = null;
        if (vo == null) {
            //如果没有条件,查全部岗位
            PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
            List<SysPosition> positions = positionService.selectAll();
            pageVO = PageUtilsOld.getPageVO(positions);
        } else {
            //条件存在,进行条件的模糊查询
            PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
            List<SysPosition> positions = positionService.selectObscure(vo);
            pageVO = PageUtilsOld.getPageVO(positions);
        }
        return DataResult.success(pageVO);
    }

    @PostMapping("selectAll")
    @ApiOperation(value = "查询所有岗位 不分页")
    public DataResult<List<SysPosition>> selectAll() {
        //如果没有条件,查全部岗位
        List<SysPosition> positions = positionService.selectAvailable();

        return DataResult.success(positions);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增岗位")
    @LogAnnotation(title = "岗位管理", action = "新增岗位")
    public DataResult addPosition(@RequestBody @Valid PositionAddReqVO vo) {
        //先判断是否有相同的type的值
        SysPosition sysPosition = new SysPosition();
        BeanUtils.copyProperties(vo, sysPosition);
        List<SysPosition> positions = positionService.selectAccuratePositionByInfo(sysPosition);
        if (!positions.isEmpty()) {
            throw new BusinessException(BaseResponseCode.POSITION_ALREADY_EXISTS);
        }
        int count = positionService.add(vo);
        if (count != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
        return DataResult.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改岗位")
    @LogAnnotation(title = "岗位管理", action = "修改岗位")
    public DataResult updatePosition(@RequestBody @Valid PositionUpdateReqVO vo) {
        //先判断是否有相同的type的值
        SysPosition sysPosition =new SysPosition();
        BeanUtils.copyProperties(vo, sysPosition);
        List<SysPosition> positions = positionService.selectAccuratePositionByInfo(sysPosition);
        if (!positions.isEmpty()) {
            throw new BusinessException(BaseResponseCode.POSITION_ALREADY_EXISTS);
        }
        positionService.updatePosition(vo);
        return DataResult.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除岗位")
    @LogAnnotation(title = "岗位管理", action = "删除岗位")
    public DataResult deletePosition(@RequestBody @Valid PositionDeleteReqVO vo) {
        positionService.deleted(vo.getId());
        return DataResult.success();
    }


}
