package com.ruoyi.web.controller.front;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
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.enums.BoolValues;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.syzy.domain.BBusiWishUniversitySubmitRecruitPlan;
import com.ruoyi.syzy.domain.BusiWishUniversitySubmitPreEnroll;
import com.ruoyi.syzy.domain.BusiWishUniversitySubmitPreRecruitPlan;
import com.ruoyi.syzy.service.IBBusiWishUniversitySubmitRecruitPlanService;
import com.ruoyi.syzy.service.IBusiWishUniversitySubmitPreEnrollService;
import com.ruoyi.syzy.service.IBusiWishUniversitySubmitPreRecruitPlanService;
import com.ruoyi.web.util.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
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.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "院校提前批")
@RequestMapping("front/syzy/pre/")
public class FrontSyzyPreController extends BaseController {

    @Autowired
    private IBusiWishUniversitySubmitPreRecruitPlanService preRecruitPlanService;
    @Autowired
    private IBusiWishUniversitySubmitPreEnrollService preEnrollService;

    @GetMapping("years")
    @ApiOperation("01.报考年份")
    public TableDataInfo years() {
        String location = SecurityUtils.getLoginUser().getUser().getProvinceName();
        List<String> arr = preRecruitPlanService.years(location, null, null);
        return getDataTable(arr);
    }

    @GetMapping("types")
    @ApiOperation("02.报考科类")
//    public TableDataInfo types(@ApiParam(required = false) Integer year) {
    public TableDataInfo types(@RequestParam Integer year) {
        String location = SecurityUtils.getLoginUser().getUser().getProvinceName();
        List<String> arr = preRecruitPlanService.types(location,year);
        return getDataTable(arr);
    }

    @GetMapping("getPreTree")
    @ApiOperation("03.获取提前批树形结构")
    public AjaxResult getPreTree(@ApiParam("年份") @RequestParam Integer year,@ApiParam("类别") @RequestParam Integer category) {
        JSONArray result = new JSONArray();
        for (Object ll : JsonUtils.readJsonArray("pre_tree.json")) {
            JSONObject item = (JSONObject) ll;
            if (category.equals(item.getInteger("category"))) {
                result.add(item);
            }

        }
        return AjaxResult.success(result);
    }

    @GetMapping("getPreRecruitPlan")
    @ApiOperation("04.提前批招生计划")
    public TableDataInfo getPreRecruitPlan(BusiWishUniversitySubmitPreRecruitPlan criteria
//            , @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
//                                @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize
    ) {
        criteria.setLocation(SecurityUtils.getLoginUser().getUser().getProvinceName());
        if(StringUtils.isNotEmpty(criteria.getType())){
            /**对应搜索course0，物理/历史或文科/理科**/
            criteria.setCourse0(criteria.getType().trim());
        }
        //如果是数字，就是学校代码
        if(StringUtils.isNumber(criteria.getKeyword())){
            criteria.setUniversityCode(criteria.getKeyword());
            criteria.setKeyword(null);
        }
        if (BoolValues.isFalse(SecurityUtils.getLoginUser().getUser().getIsBind())) {
            PageHelper.startPage(1, 1);
        } else {
            startPage();
        }
        List<BusiWishUniversitySubmitPreRecruitPlan> arr = preRecruitPlanService.selectBusiWishUniversitySubmitPreRecruitPlanList(criteria);
        return getDataTable(arr);
    }

    @GetMapping("getPreRecruitPlanV2")
    @ApiOperation("04-2.提前批招生计划")
    public TableDataInfo getPreRecruitPlanV2(BusiWishUniversitySubmitPreRecruitPlan criteria
//            , @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
//                                @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize
    ) {
        criteria.setLocation(SecurityUtils.getLoginUser().getUser().getProvinceName());
        if(StringUtils.isNotEmpty(criteria.getType())){
            /**对应搜索course0，物理/历史或文科/理科**/
            criteria.setCourse0(criteria.getType().trim());
        }
        //如果是数字，就是学校代码
        if(StringUtils.isNumber(criteria.getKeyword())){
            criteria.setUniversityCode(criteria.getKeyword());
            criteria.setKeyword(null);
        }
        if (BoolValues.isFalse(SecurityUtils.getLoginUser().getUser().getIsBind())) {
            PageHelper.startPage(1, 1);
        } else {
            startPage();
        }
        criteria.setDataType("U");
        List<BusiWishUniversitySubmitPreRecruitPlan> arr = preRecruitPlanService.selectBusiWishUniversitySubmitPreRecruitPlanList(criteria);
        List<String> pudidList=new ArrayList<>();
        // 通过stream将arr的pudid转换为list
        arr.stream().forEach(item -> {
            if(StringUtils.isNotEmpty(item.getUdid())){
                pudidList.add(item.getUdid());
            }
        });

        Map<String,List<BusiWishUniversitySubmitPreRecruitPlan>> map =new HashMap<>();
        if(CollectionUtils.isNotEmpty(pudidList)){
            BusiWishUniversitySubmitPreRecruitPlan query=new BusiWishUniversitySubmitPreRecruitPlan();
            query.setPudids(pudidList);
            List<BusiWishUniversitySubmitPreRecruitPlan> list=preRecruitPlanService.selectBusiWishUniversitySubmitPreRecruitPlanList(query);
            list.stream().forEach(item -> {
                String pudid = item.getPudid();
                if(!map.containsKey(pudid)){
                    map.put(pudid,new ArrayList<>());
                }
                List<BusiWishUniversitySubmitPreRecruitPlan> subMajorList = map.get(pudid);
                subMajorList.add(item);
                map.put(pudid,subMajorList);
            });
        }

        //填充子级专业数据
        arr.forEach(item -> {
            if(map.containsKey(item.getUdid())){
                item.setSubMajors(map.get(item.getUdid()));
            }
        });

        return getDataTable(arr);
    }

    @GetMapping("enroll/years")
    @ApiOperation("05.报考年份")
    public TableDataInfo enrollYears(String category) {
        String location = SecurityUtils.getLoginUser().getUser().getProvinceName();
        List<String> arr = preEnrollService.years(location,category);
        return getDataTable(arr);
    }

    @GetMapping("enroll/getTag")
    @ApiOperation("06.获取标签")
    public TableDataInfo getTag(@RequestParam("year") String year,String tag,String category) {
        String location = SecurityUtils.getLoginUser().getUser().getProvinceName();
        List<String> arr = preEnrollService.getTag(year,location,tag,category);
        //艺术类(平行组首选历史)>本科提前批 ,根据>分割获取第二个 for循环
        arr = arr.stream().map(s -> s.contains(">")?s.split(">")[1]:s).collect(Collectors.toList())
                .stream().distinct().collect(Collectors.toList());

        return getDataTable(arr);
    }

    @GetMapping("enroll/getCourse1")
    @ApiOperation("07.获取科目")
    public TableDataInfo getCourse1(@RequestParam("year") String year,String course0,String tag,@RequestParam("category") String category) {
        String location = SecurityUtils.getLoginUser().getUser().getProvinceName();
        List<String> arr = preEnrollService.getCourse1(year,location,course0,category,tag);

        return getDataTable(arr);
    }

    @GetMapping("enroll/getPreEnroll")
    @ApiOperation("08.提前批招生计划")
    public TableDataInfo getPreEnroll(BusiWishUniversitySubmitPreEnroll criteria) {
        criteria.setLocation(SecurityUtils.getLoginUser().getUser().getProvinceName());
//        if(StringUtils.isNotEmpty(criteria.getType())){
//            /**对应搜索course0，物理/历史或文科/理科**/
//            criteria.setCourse0(criteria.getType().trim());
//        }
        //如果是数字，就是学校代码
        if(StringUtils.isNumber(criteria.getKeyword())){
            criteria.setUniversityCode(criteria.getKeyword());
            criteria.setKeyword(null);
        }
        if(StringUtils.isNotEmpty(criteria.getType())){
            criteria.setCourse0(criteria.getType());
        }
        if (BoolValues.isFalse(SecurityUtils.getLoginUser().getUser().getIsBind())) {
            PageHelper.startPage(1, 1);
        } else {
            startPage();
        }
        List<BusiWishUniversitySubmitPreEnroll> arr = preEnrollService.selectBusiWishUniversitySubmitPreEnrollList(criteria);
        return getDataTable(arr);
    }

    @GetMapping("enroll/getPreEnrollV2")
    @ApiOperation("08-2.提前批招生计划")
    public TableDataInfo getPreEnrollV2(BusiWishUniversitySubmitPreEnroll criteria) {
        criteria.setLocation(SecurityUtils.getLoginUser().getUser().getProvinceName());
//        if(StringUtils.isNotEmpty(criteria.getType())){
//            /**对应搜索course0，物理/历史或文科/理科**/
//            criteria.setCourse0(criteria.getType().trim());
//        }
        //如果是数字，就是学校代码
        if(StringUtils.isNumber(criteria.getKeyword())){
            criteria.setUniversityCode(criteria.getKeyword());
            criteria.setKeyword(null);
        }
        if(StringUtils.isNotEmpty(criteria.getType())){
            criteria.setCourse0(criteria.getType());
        }

        criteria.setDataType("G");
        if (BoolValues.isFalse(SecurityUtils.getLoginUser().getUser().getIsBind())) {
            PageHelper.startPage(1, 1);
        } else {
            startPage();
        }
        List<BusiWishUniversitySubmitPreEnroll> arr = preEnrollService.selectBusiWishUniversitySubmitPreEnrollList(criteria);
        List<String> pudidList=new ArrayList<>();
        // 通过stream将arr的pudid转换为list
        arr.stream().forEach(item -> {
            if(StringUtils.isNotEmpty(item.getUdid())){
                pudidList.add(item.getUdid());
            }
        });

        Map<String,List<BusiWishUniversitySubmitPreEnroll>> map =new HashMap<>();
        if(CollectionUtils.isNotEmpty(pudidList)){
            BusiWishUniversitySubmitPreEnroll query=new BusiWishUniversitySubmitPreEnroll();
            query.setPudids(pudidList);
            List<BusiWishUniversitySubmitPreEnroll> list=preEnrollService.selectBusiWishUniversitySubmitPreEnrollList(query);
            list.stream().forEach(item -> {
                String pudid = item.getPudid();
                if(!map.containsKey(pudid)){
                    map.put(pudid,new ArrayList<>());
                }
                List<BusiWishUniversitySubmitPreEnroll> subMajorList = map.get(pudid);
                subMajorList.add(item);
                map.put(pudid,subMajorList);
            });
        }

        //填充子级专业数据
        arr.forEach(item -> {
            if(map.containsKey(item.getUdid())){
                item.setSubMajors(map.get(item.getUdid()));
            }
        });

        return getDataTable(arr);
    }

}
