package com.sqx.modules.post.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;

import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.post.entity.PostEntity;
import com.sqx.modules.post.service.PostService;
import com.sqx.modules.post.service.ViewPostService;
import io.swagger.annotations.ApiOperation;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/app/post")
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private ViewPostService viewPostService;


    @GetMapping("getPostListOfUser")
    @ApiOperation("获取岗位列表")
    public Result getPostListOfUser() {
        return Result.success().put("data", postService.getPostListOfUser());
    }
    @Login
    @GetMapping("getPostOnly")
    @ApiOperation("获取岗位详细信息")
    public Result getPostListOne(Integer postId,@RequestAttribute("userId") Long userId) {
        viewPostService.addViewPost(postId,userId);
        return Result.success().put("data", postService.getPostListOne(postId));
    }
    @Login
    @GetMapping("getPostOnlyHr")
    @ApiOperation("获取岗位详细信息")
    public Result getPostListOneByHr(Integer postId) {
        return Result.success().put("data", postService.getPostListOne(postId));
    }
    @Login
    @PostMapping("addPost")
    @ApiOperation("增加岗位")
    public void addPost(@RequestAttribute("userId") Long userId,@RequestBody PostEntity postEntity) {
        postEntity.setUserId(userId);
        postService.saveOrUpdate(postEntity);

    }

    @PostMapping("changePostStatus")
    @ApiOperation("更改岗位状态岗位列表")
    public void changePostStatus(Integer postId, Integer status) {
        PostEntity postEntity1 = postService.getById(postId);
        postEntity1.setStatus(status);
        postService.updateById(postEntity1);
    }

    @GetMapping("findPost")
    @ApiOperation("查找岗位列表")
    public Result findPost(String address, Integer wage, Integer edu, Integer companyPeople, String key) {
        return Result.success().put("data", postService.findPost(address, wage, edu, companyPeople, key));
    }
    @PostMapping("changePost")
    @ApiOperation("更改岗位状态岗位列表")
    public Result changePost(@RequestBody PostEntity postEntity) {
        postEntity.setUpdateTime(String.valueOf(LocalDateTime.now()));
        postService.saveOrUpdate(postEntity);
        return  Result.success();
    }
    @Login
    @GetMapping("getMyPost")
    @ApiOperation("获取我发布的岗位详细信息")
    public Result getPostListOne(@RequestAttribute("userId") Integer userId) {
        QueryWrapper<PostEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return Result.success().put("data", postService.getBaseMapper().selectList(queryWrapper));
    }
}
