package com.mosty.common.core.business.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.exception.Asserts;
import com.mosty.common.base.util.IpUtil;
import com.mosty.common.config.entity.vo.AddPositionVO;
import com.mosty.common.config.entity.vo.DeleteVO;
import com.mosty.common.config.entity.vo.EditPositionVO;
import com.mosty.common.core.business.entity.SysPosition;
import com.mosty.common.core.business.entity.vo.PositionPage;
import com.mosty.common.core.business.service.SysPositionService;
import com.mosty.common.token.JwtSysUser;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 岗位表 前端控制器
 * </p>
 *
 * @author kevin
 * @since 2022-02-12
 */
@Slf4j
@RestController
@AllArgsConstructor
@SuppressWarnings("unused")
@RequestMapping("/sysPosition")
@Api(tags = "岗位信息接口")
public class SysPositionController {

    private final SysPositionService sysPositionService;

    /**
     * 添加岗位
     * @param addPositionVO 添加岗位请求
     * @return 添加结果
     */
    @JwtSysUser
    @PostMapping("addSysPosition")
    @Log(title = "岗位功能",businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加岗位", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> addSysPosition(@RequestBody @Valid AddPositionVO addPositionVO) {

        SysPosition position = new SysPosition();

        position.setPostDesc(addPositionVO.getPostDesc());
        position.setPostCode(addPositionVO.getPostCode());
        position.setPostName(addPositionVO.getPostName());
        position.setBz(addPositionVO.getBz());

        UserInfo userInfo = UserInfoManager.get();
        position.setXtZxbz(DeletedEnum.NATURE.code);

        position.setXtCjsj(new Date());
        position.setXtZhxgsj(new Date());
        position.setXtZhxgrid(userInfo.getUserId().toString());
        position.setXtZhxgrxm(userInfo.getUserName());
        position.setXtZhxgrbmid(userInfo.getDeptId().toString());
        position.setXtZhxgrbm(userInfo.getDeptName());

        position.setXtLrsj(new Date());
        position.setXtLrrid(userInfo.getUserId().toString());
        position.setXtLrrxm(userInfo.getUserName());
        position.setXtLrrbmid(userInfo.getDeptId().toString());
        position.setXtLrrbm(userInfo.getDeptName());

        // 获取ip地址
        String ipAddress = IpUtil.getIpAddress();
        position.setXtLrip(ipAddress);
        position.setXtZhxgip(ipAddress);

        sysPositionService.getBaseMapper().insert(position);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 修改岗位
     * @param editPositionVO 修改岗位请求
     * @return 修改结果
     */
    @JwtSysUser
    @PostMapping("updateSysPosition")
    @Log(title = "岗位功能",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改岗位", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> updateSysPosition(@RequestBody @Valid EditPositionVO editPositionVO) {
        Asserts.check(editPositionVO.getId() == null, "主键id不能为空！");
        SysPosition oldSysPosition = sysPositionService.getBaseMapper().selectById(editPositionVO.getId());
        Asserts.check(oldSysPosition == null, "主键为 %s 的岗位信息不存在", editPositionVO.getId());

        // 获取ip地址
        String ipAddress = IpUtil.getIpAddress();
        oldSysPosition.setXtZhxgip(ipAddress);

        sysPositionService.updateSysPost(oldSysPosition, editPositionVO);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 删除岗位信息
     * @param deleteVO 岗位id
     * @return 删除结果
     */
    @PostMapping("deleteSysPosition")
    @JwtSysUser
    @Log(title = "岗位功能",businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除岗位信息", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> deleteSysPosition(@RequestBody DeleteVO deleteVO) {
        SysPosition oldSysPosition = sysPositionService.getBaseMapper().selectById(deleteVO.getId());
        Asserts.check(oldSysPosition == null, "主键为 %s 的岗位信息不存在", deleteVO.getId());

        oldSysPosition.setXtZxbz(DeletedEnum.DELETED.code);
        oldSysPosition.setXtZxyy(deleteVO.getZxyy());

        // 获取ip地址
        String ipAddress = IpUtil.getIpAddress();

        UserInfo userInfo = UserInfoManager.get();
        Asserts.checkAuth(userInfo == null, "用户信息为空，请登陆");
        oldSysPosition.setXtZhxgsj(new Date());
        oldSysPosition.setXtZhxgrid(userInfo.getUserId().toString());
        oldSysPosition.setXtZhxgrxm(userInfo.getUserName());
        oldSysPosition.setXtZhxgrbmid(userInfo.getDeptId().toString());
        oldSysPosition.setXtZhxgrbm(userInfo.getDeptName());
        oldSysPosition.setXtZhxgip(ipAddress);

        sysPositionService.getBaseMapper().updateById(oldSysPosition);
        return ResponseResult.success(Boolean.TRUE);
    }

    @GetMapping("selectPage")
    @ApiOperation(value = "分页查询岗位信息", httpMethod = "GET", response = SysPosition.class)
    public ResponseResult<IPage<SysPosition>> selectPage(PositionPage positionPage) {
        IPage<SysPosition> positionIPage = sysPositionService.selectByPage(positionPage);
        return ResponseResult.success(positionIPage);
    }

}

