package com.sqx.modules.resumes.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.firstLogin.service.HrService;
import com.sqx.modules.record.service.RecordService;
import com.sqx.modules.resumes.entity.PostPush;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.PostPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

    @Autowired
    private PostPushService postPushService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private HrService hrService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;

    @Login
    @PostMapping("/savePostPush")
    @ApiOperation("添加或修改招聘信息")
    public Result savePostPush(@RequestBody PostPush postPush, @RequestAttribute("userId") Long userId) {
        postPush.setUserId(String.valueOf(userId));
        postPush.setOrderId(1);
        return postPushService.savePostPush(postPush, userId);
    }

    @GetMapping("/getPostPushList")
    @ApiOperation("获取发布的招聘岗位列表")
    public Result getPostPushList(Integer page, Integer limit, Long userId, PostPush postPush, Integer screen,Integer isDue) {
        if (screen == 3&&postPush.getLat()==null&&postPush.getLng()==null){
            return  Result.error("请打开你的定位");
        }
        return postPushService.getPostPushList(page, limit, null, null, userId, postPush, screen,isDue);
    }
    @Login
    @GetMapping("/selectPostPushDetails")
    @ApiOperation("查询招聘信息详情(通用)")
    public Result selectPostPushDetails(@RequestAttribute("userId")Long userId, Long postPushId,Double lng,Double lat) {
        return Result.success().put("data", postPushService.selectPostPushDetails(userId, postPushId,lng,lat));
    }

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

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

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

    /**
     * -----------------------------------------上面是企业接口,下面是用户接口------------------------------------------------
     */
    @Login
    @GetMapping("/userGetPostPushList")
    @ApiOperation("用户获取招聘岗位列表")
    public Result userGetPostPushList(Integer page, Integer limit,Double lng,Double lat, @RequestAttribute("userId") Long userId, PostPush postPush, Integer screen,Integer isDue) {
        if (StringUtils.isNotBlank(postPush.getRuleClassifyName()) && userId != null) {
            recordService.saveRecord(userId, postPush.getRuleClassifyName());
        }
        postPush.setStatus(2);
        return postPushService.getPostPushList(page, limit, lng, lat,  userId, postPush, screen,isDue);
    }

    @ApiOperation("获取所有有职位的省")
    @GetMapping("getCityList")
    public Result getCityList(String city) {
        return Result.success().put("data", postPushService.getCityList(city));
    }

    @ApiOperation("根据省获取市")
    @GetMapping("getCountyByCity")
    public Result getCountyByCity(String city) {
        return Result.success().put("data", postPushService.getCountyByCity(city));
    }

    @ApiOperation("获取热门城市")
    @GetMapping("getHotCity")
    public Result getHotCity(Integer limitCount) {
        return Result.success().put("data", postPushService.getHotCity(limitCount));
    }

    @ApiOperation("获取市县列表")
    @GetMapping("getCityCounty")
    public Result getCityCounty() {
        return Result.success().put("data", postPushService.getCityCounty());
    }

    @ApiOperation("根据公司id查询已发布岗位的市")
    @GetMapping("getCityCompanyId")
    public Result getCityCompanyId(Long companyId) {
        return Result.success().put("data", postPushService.getCityCompanyId(companyId));
    }

    @ApiOperation("根据公司查询发布的岗位")
    @GetMapping("getCompanyClassify")
    public Result getCompanyClassify(Long companyId) {
        return Result.success().put("data", postPushService.getCompanyClassify(companyId));
    }

    @Login
    @ApiOperation(value = "岗位刷新")
    @PostMapping("refreshPostPush")
    public Result refreshPostPush(@RequestAttribute("userId") Long userId) {
        return postPushService.refreshPostPush(userId);
    }

    @ApiOperation(value = "根据公司获取职位")
    @GetMapping("listPostPushByCompanyId")
    public Result listPostPushByCompanyId(Long companyId) {
        return Result.success().put("data", postPushService.listPostPushByCompanyId(companyId));
    }

    @Login
    @GetMapping("getHotList")
    @ApiOperation("获取热门列表")
    public  Result getHotList(){
        return Result.success().put("data",postPushService.getHotPost());
    }
    @Login
    @GetMapping("getMyPostPush")
    @ApiOperation("获取我发布的岗位")
    public Result getMyPostPush(@RequestAttribute("userId")Long userId,Integer limit,Integer page){
        Page<PostPush> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<PostPush> selectPage =postPushService.getBaseMapper().selectPage(pages,new QueryWrapper<PostPush>().eq("user_id",userId));
        for(PostPush postPush : pages.getRecords()){
            postPush.setHr(hrService.getByUserId(String.valueOf(userId)));
            postPush.setCompany(companyService.getById(postPush.getCompanyId()));
            postPush.setUser(userService.getById(userId));
        }
        return  Result.success().put("data",selectPage);
    }
}
