package com.ruoyi.web.controller.front;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
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.StringUtils;
import com.ruoyi.sy.domain.*;
import com.ruoyi.sy.service.*;
import com.ruoyi.web.service.SyTestMajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@Api(tags = "前端-三库-职业")
@RestController
@RequestMapping("front/vocational/")
public class FrontSyVocationalController extends BaseController {
    @Autowired
    private ISyVocationalService vocationalService;
    @Autowired
    private ISyVocationalOverviewService vocationalOverviewService;
    @Autowired
    private ISyVocationalPostService vocationalPostService;
    @Autowired
    private ISyVocationalPostDetailService vocationalPostDetailService;
    @Autowired
    private ISyVocationalHitsLackService vocationalHitsLackService;
    @Autowired
    private SyTestMajorService syTestMajorService;

    @GetMapping("getVocationalPostDetailByPostName")
    @ApiOperation("05 根据就业岗位名称获取岗位详情")
    @Anonymous
    public AjaxResult getVocationalPostDetailByPostName(@RequestParam(required = true) String postName) {
        SyVocationalPostDetail vocationalPostDetail = vocationalPostDetailService.selectSyVocationalPostDetailByName(postName);
        return AjaxResult.success(vocationalPostDetail);
    }

    @GetMapping("getVocationalPosts")
    @ApiOperation("04 获取就业岗位")
    @Anonymous
    public AjaxResult getVocationalPosts(@RequestParam(required = true) String code) {
        List<SyVocationalPost> vocationalPost = vocationalPostService.selectSyVocationalPostByVocationalCode(code);
        return AjaxResult.success(vocationalPost);
    }

    @GetMapping("getVocationalOverview")
    @ApiOperation("03 获取职业概况")
    @Anonymous
    public AjaxResult getVocationalOverview(@RequestParam String code) {
        SyVocationalOverview vocationalOverview = vocationalOverviewService.selectSyVocationalOverviewByCode(code);
        JSONArray marjorList = new JSONArray();
        Set<String> marjorCodeSet = syTestMajorService.loadMarjorCodes();
        Set<String> marjorNameSet = syTestMajorService.loadMarjorNames();
        vocationalOverview.getPostMajors().forEach(t -> {
//            if (marjorCodeSet.contains(((JSONObject) t).getString("code"))) {
            if (marjorNameSet.contains(((JSONObject) t).getString("name"))&&marjorCodeSet.contains(((JSONObject) t).getString("code"))) {
                marjorList.add(t);
            }
        });
        vocationalOverview.setPostMajors(marjorList.toString());
        return AjaxResult.success(vocationalOverview);
    }

    @GetMapping("getAllVocation")
    @ApiOperation("02 获取所有职业（三级树形结构）")
    @Anonymous
    public AjaxResult getAllVocation(String name,@ApiParam("层级")  Integer level) {
        SyVocational query = new SyVocational();
        if(StringUtils.isNotBlank(name)){
            query.setName(name).setLevel(3);//只能搜索三级职业名称
        }
        List<SyVocational> vocationalList = vocationalService.selectSyVocationalList(query);
        return AjaxResult.success(vocationalService.buildVocationalTreeSelect(vocationalList));
    }

    @GetMapping("getVocationByParentCode")
    @ApiOperation("02-2 根据父级编码获取子职业")
    public AjaxResult getVocationByParentCode(@RequestParam String parentCode) {
        List<SyVocational> vocationalList = vocationalService.selectSyVocationalList(new SyVocational().setParentCode(parentCode));
        return AjaxResult.success(vocationalList);
    }

    @GetMapping("getVocationHot")
    @ApiOperation("01 热门/紧缺职业排名")
    public TableDataInfo getVocationalHot(@ApiParam(value = "职业类型（1热门职业，2紧缺职业） ", example = "1") @RequestParam Integer type,
        @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
        @ApiParam(value = "页大小", example = "5") @RequestParam Integer pageSize ) {
        SyVocationalHitsLack query = new SyVocationalHitsLack();
        query.setType(type);
        startPage();
        List<SyVocationalHitsLack > list = vocationalHitsLackService.selectSyVocationalHitsLackList(query);

        return getDataTable(list);
    }
}
