package com.sqx.modules.resumes.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.resumes.entity.PostPush;
import com.sqx.modules.resumes.service.PostPushService;
import com.sqx.modules.utils.EasyPoi.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Api(value = "招聘信息", tags = {"招聘信息"})
@RequestMapping(value = "/postPush")
public class PostPushController {

    @Autowired
    private PostPushService postPushService;
    @Login
    @PostMapping("/savePostPush")
    @ApiOperation("保存招聘信息")
    public Result savePostPush(@RequestBody PostPush postPush) {
        if (postPush.getPostPushId() == null) {
            return postPushService.savePostPush(postPush, null);
        } else {
            return postPushService.updateById(postPush) ? Result.success() : Result.error();
        }

    }

    @GetMapping("/getPostPushList")
    @ApiOperation("获取发布的招聘岗位列表")
    public Result getPostPushList(Integer page, Integer limit, Long userId, PostPush postPush, Integer screen,Integer isDue) {
        return postPushService.getPostPushList(page, limit, null, null, userId, postPush, screen,isDue);
    }

    @PostMapping("/deletePostPush")
    @ApiOperation("删除招聘信息")
    public Result deletePostPush(Long userId, Long postPushId) {
        return postPushService.deletePostPush(userId, postPushId);
    }

    @PostMapping("/auditPostPush")
    @ApiOperation("审核招聘信息")
    public Result auditPostPush(String ids, String content, Integer status) {
        return postPushService.auditPostPush(ids, content, status);
    }

    @PostMapping("/updatePostPushIsRecommend")
    @ApiOperation("修改是否推荐")
    public Result updatePostPushIsRecommend(Long postPushId) {
        PostPush postPush = postPushService.getById(postPushId);
        if (postPush.getIsRecommend() == null || postPush.getIsRecommend() == 1) {
            postPush.setIsRecommend(2);
        } else {
            postPush.setIsRecommend(1);
        }
        postPushService.updateById(postPush);
        return Result.success();
    }

    @PostMapping("/cancelPostPush")
    @ApiOperation("取消发布招聘")
    public Result cancelPostPush(Long userId, Long postPushId) {
        return postPushService.cancelPostPush(userId, postPushId, 1);
    }

    @PostMapping("/updatePostPushIsTop")
    @ApiOperation("修改是否置顶")
    public Result updatePostPushIsTop(Long postPushId) {
        PostPush postPush = postPushService.getById(postPushId);
        if (postPush.getIsTop() == null || postPush.getIsTop() == 1) {
            postPush.setIsTop(2);
        } else {
            postPush.setIsTop(1);
        }
        postPushService.updateById(postPush);
        return Result.success();
    }

   /* @GetMapping("/selectPostPushDetails")
    @ApiOperation("查询招聘信息详情")
    public Result selectPostPushDetails(Long postPushId) {
        return Result.success().put("data", postPushService.selectPostPushDetails(null, postPushId, null, null));
    }*/
    @Login
    @GetMapping("/selectPostPushDetails")
    @ApiOperation("查询招聘信息详情")
    public Result selectPostPushDetails(@RequestAttribute("userId")Long userId, Long postPushId) {
         return Result.success().put("data", postPushService.selectPostPushDetails(userId, postPushId, null, null));
     }

    @GetMapping("/closeOrOpen")
    @ApiOperation("关闭或打开招聘")
    public Result closeOrOpen(Long userId, Long postPushId) {
        return postPushService.closeOrOpen(userId, postPushId);
    }


    /**
     * 招聘有效期检测
     */
    @Scheduled(cron = "0/2 * * * * ?", zone = "Asia/Shanghai")
    public void cancelPostPush() {
        List<PostPush> pushList = postPushService.list(new QueryWrapper<PostPush>().eq("status", 2));
        for (PostPush postPush : pushList) {
            if (postPush.getOverdueTime() != null && LocalDateTime.now().isAfter(postPush.getOverdueTime())) {
                postPush.setStatus(5);
                postPush.setIsOverdue(1);
                postPushService.updateById(postPush);
            }
        }

    }



    @GetMapping("/getAdminPostPushList")
    @ApiOperation("获取发布的招聘岗位列表")
    public Result getAdminPostPushList(Integer page, Integer limit,PostPush postPush,String startTime,String  endTime) {
        return Result.success().put("data", postPushService.getAdminPostPushList(page, limit, postPush,startTime,endTime));
    }


    @ApiOperation("招聘信息列表--导出")
    @GetMapping("/postPushExcelOut")
    public void postPushExcelOut(PostPush postPush, String startTime, String endTime, HttpServletResponse response) throws IOException {
        List<PostPush> list = postPushService.getAdminPostPushList(null,null,postPush,startTime, endTime).getRecords();
        ExcelUtils.exportExcel(list, "招聘信息表", "招聘信息Sheet", PostPush.class, "招聘信息表", response);
    }
}
