package com.ruoyi.cms.controller.app;

import com.ruoyi.cms.domain.ESJobDocument;
import com.ruoyi.cms.domain.Job;
import com.ruoyi.cms.domain.JobApply;
import com.ruoyi.cms.domain.query.ESJobSearch;
import com.ruoyi.cms.service.*;
import com.ruoyi.cms.util.RoleUtils;
import com.ruoyi.cms.util.sensitiveWord.SensitiveWordChecker;
import com.ruoyi.common.annotation.BussinessLog;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SiteSecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ArrayUtils;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 岗位Controller
 * 
 * @author lishundong
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/app/job")
@Api(tags = "移动端:岗位相关接口")
public class AppJobController extends BaseController
{
    @Autowired
    private IJobService jobService;
    @Autowired
    private IJobCollectionService jobCollectionService;
    @Autowired
    private IESJobSearchService esJobSearchService;
    @Autowired
    private SensitiveWordChecker sensitiveWordChecker;
    @Autowired
    private IJobApplyService jobApplyService;

    /**
     * 查询岗位列表
     */
    @ApiOperation("查询岗位列表")
    @GetMapping("/list")
    public TableDataInfo list(ESJobSearch job)
    {
        EsPageInfo<ESJobDocument> list = jobService.appList(job);
        return getTableDataInfo2(list,job);
    }

    private TableDataInfo getTableDataInfo2(EsPageInfo<ESJobDocument> list, ESJobSearch job) {
        long total = list.getTotal();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setRows(list.getList());
        rspData.setTotal(total > 200 ? 200 : total);
        job.setCompany(null);
        rspData.setData(job);
        return rspData;
    }

    /**
     * 获取5条推荐岗位
     */
    @ApiOperation("获取推荐岗位")
    @GetMapping("/recommend")
    public AjaxResult recommend(ESJobSearch esJobSearch)
    {
        if (RoleUtils.getAppCurrentUseridCard() != null) {
            esJobSearch.setCode(RoleUtils.getAppCurrentUseridCard());
            esJobSearch.setUserType(RoleUtils.getAppIscompanyUser());
        }
        List<ESJobDocument> jobList = jobService.recommend(esJobSearch);
        return success(jobList);
    }
    @ApiOperation("获取短视频")
    @GetMapping("/littleVideo")
    public AjaxResult littleVideo(ESJobSearch esJobSearch)
    {
        List<ESJobDocument> jobList = jobService.littleVideo(esJobSearch);
        return success(jobList);
    }

    @ApiOperation("随机获取短视频")
    @GetMapping("/littleVideo/random")
    public AjaxResult littleVideo(@RequestParam(required = true) String uuid,@RequestParam(required = false) Integer count,@RequestParam(required = false) String jobTitle)
    {
        List<ESJobDocument> jobList = jobService.littleVideoRandom(uuid,count,jobTitle);
        return success(jobList);
    }
    /**
     * 附件工作
     */
    @ApiOperation("附近工作")
    @PostMapping(value = "/nearJob")
    public TableDataInfo nearJob(@RequestBody ESJobSearch jobQuery)
    {
        EsPageInfo<ESJobDocument> list = esJobSearchService.nearJob(jobQuery);
        List<ESJobDocument> jobList = list.getList();
        list.setList(jobList);
        return getTableDataInfo(list);
    }
    /**
     * 附件工作
     */
    @ApiOperation("区县工作")
    @PostMapping(value = "/countyJob")
    public TableDataInfo countyJob(@RequestBody ESJobSearch job)
    {
        EsPageInfo<ESJobDocument> list = jobService.countyJobList(job);
        List<ESJobDocument> jobList = list.getList();
        list.setList(jobList);
        return getTableDataInfo(list);
    }
    /**
     * 地铁周边
     */
    @ApiOperation("地铁周边")
    @PostMapping(value = "/subway")
    public TableDataInfo subway(@RequestBody ESJobSearch jobQuery)
    {
        EsPageInfo<ESJobDocument> list = jobService.subway(jobQuery);
        List<ESJobDocument> jobList = list.getList();
        list.setList(jobList);
        return getTableDataInfo(list);
    }

    /**
     * 商圈周边
     */
    @ApiOperation("商圈周边")
    @PostMapping(value = "/commercialArea")
    public TableDataInfo commercialArea(@RequestBody ESJobSearch jobQuery)
    {
        EsPageInfo<ESJobDocument> list = jobService.commercialArea(jobQuery);
        List<ESJobDocument> jobList = list.getList();
        list.setList(jobList);
        return getTableDataInfo(list);
    }

    /**
     * ai对话模型查询岗位数据(es查询)
     * @param job
     * @return
     */
    @ApiOperation("添加模型查询岗位")
    @GetMapping("/searchModelJobsList")
    public TableDataInfo searchModelJobsList(ESJobSearch job)
    {
        EsPageInfo<ESJobDocument> list = esJobSearchService.nearJob(job);
        List<ESJobDocument> jobList = list.getList();
        jobList.forEach(it->it.setAppJobUrl(String.valueOf(it.getJobId())));
        return getDataTable(jobList);
    }

    /**
     * 获取岗位详细信息
     */
    @ApiOperation("获取岗位详细信息")
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId, HttpServletRequest request)
    {
        if (jobId == null) {
            return AjaxResult.error("jobId不能为空");
        }
        //Job job = jobService.selectJobByJobIdApp(jobId);
        Job job = jobService.selectHttpJobByJobIdApp(jobId,request);
        return success(job);
    }

    /**
     * 用户收藏岗位
     */
    @BussinessLog(title = "用户收藏岗位")
    @PostMapping("/collection/{jobId}")
    @ApiOperation("用户收藏")
    public AjaxResult jobCollection(@ApiParam("岗位id") @PathVariable Long jobId)
    {
        return toAjax(jobCollectionService.jobCollection(jobId));
    }

    /**
     * 用户取消收藏岗位
     */
    @BussinessLog(title = "用户取消收藏岗位")
    @DeleteMapping("/collection/{jobId}")
    @ApiOperation("用户取消收藏岗位")
    public AjaxResult cancel(@ApiParam("岗位id") @PathVariable Long jobId)
    {
        return toAjax(jobCollectionService.cancel(jobId));
    }
    /**
     * 用户申请岗位
     */
    @BussinessLog(title = "用户申请岗位")
    @GetMapping("/apply/{jobId}")
    @ApiOperation("用户申请岗位")
    public AjaxResult apply(@ApiParam("岗位id") @PathVariable Long jobId)
    {
        return toAjax(jobCollectionService.apply(jobId));
    }
    @GetMapping("/competitiveness/{jobId}")
    @ApiOperation("竞争力分析")
    public AjaxResult competitiveness(@ApiParam("岗位id") @PathVariable Long jobId) {
        return success(jobCollectionService.competitiveness(jobId));
    }
    private TableDataInfo getTableDataInfo(EsPageInfo<ESJobDocument> result){
        long total = result.getTotal();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setRows(result.getList());
        rspData.setTotal(total > 200 ? 200 : total);
        return rspData;
    }
    @BussinessLog(title = "移动端发布岗位")
    @PostMapping("/publishJob")
    public AjaxResult fix(@RequestBody Job job)
    {
        // 校验描述中的敏感词
        List<String> sensitiveWords = sensitiveWordChecker.checkSensitiveWords(job.getDescription());
        if (!sensitiveWords.isEmpty()) {
            String errorMsg = "描述中包含敏感词：" + String.join("、", sensitiveWords);
            return AjaxResult.error(errorMsg);
        }
        job.setJobStatus("0");//上架
        if(StringUtils.isBlank(job.getDownTime())){
            job.setDownTime(DateUtils.getMonthThree(DateUtils.YYYY_MM_DD_HH_MM_SS));
        }
        jobService.publishJob(job);
        return success();
    }

    @GetMapping("/jobCompare")
    @ApiOperation("根据多个岗位id查询岗位详情")
    public AjaxResult jobCompare(@ApiParam("岗位ID数组") @RequestParam("jobIds") Long[] jobIds) {
        if (ArrayUtils.isEmpty(jobIds)) {
            return AjaxResult.error("请传递岗位ID参数（jobIds），多个ID用&分隔");
        }
        if (jobIds.length > 5) {
            return AjaxResult.error("最多支持对比5个岗位，请减少参数数量");
        }
        return success(esJobSearchService.selectByIds(jobIds));
    }

    @GetMapping("/applyJobList")
    @ApiOperation("根据岗位id查询岗位申请人员")
    public AjaxResult applyJobList(@ApiParam("岗位id") @RequestParam("jobId") Long jobId) {
        if (jobId==null) {
            return AjaxResult.error("请传递岗位ID");
        }
        return success(jobService.selectApplyJobUserList(jobId));
    }

    @ApiOperation("删除岗位申请")
    @DeleteMapping("/applyJobCencal")
    public AjaxResult applyJobCencal(@RequestBody JobApply apply){
        if(apply==null){
            return AjaxResult.error("参数为空!");
        }
        if (apply.getJobId() == null) {
            return AjaxResult.error("岗位id为空!");
        }
        if(!SiteSecurityUtils.isLogin()){
            return AjaxResult.error("用户未登录!");
        }
        if (apply.getUserId() == null) {
            apply.setUserId(SiteSecurityUtils.getUserId());
        }
        return toAjax(jobApplyService.applyJobCencal(apply));
    }

    @ApiOperation("获取取消岗位岗位详情")
    @GetMapping("/selectCencalList")
    public TableDataInfo selectCencalList(){
        JobApply queryApply = new JobApply();
        if (queryApply.getUserId() == null) {
            queryApply.setUserId(SiteSecurityUtils.getUserId());
        }
        List<Job> list=jobApplyService.selectCencalList(queryApply);
        return getDataTable(list);
    }

    @ApiOperation("岗位下架")
    @PutMapping("/jobDown/{jobId}")
    public AjaxResult jobDown(@ApiParam("岗位id") @PathVariable Long jobId){
        if(jobId==null){
            return error("岗位id为空");
        }
        return toAjax(jobService.jobDown(jobId));
    }

    @ApiOperation("岗位上架")
    @PutMapping("/jobUp/{jobId}")
    public AjaxResult jobUp(@ApiParam("岗位id") @PathVariable Long jobId){
        if(jobId==null){
            return error("岗位id为空");
        }
        return toAjax(jobService.jobUp(jobId));
    }
}
