package com.template.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.template.annotation.PassToken;
import com.template.api.AlumniOrgControllerAPI;
import com.template.common.utils.HttpsClient;
import com.template.common.utils.WeiXiaoUtil;
import com.template.config.WxOpenidConfig;
import com.template.model.pojo.AlumniOrg;
import com.template.model.result.CommonResult;
import com.template.model.vo.DepartmentTreeVo;
import com.template.services.AlumniOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-11-19
 */
@RestController
public class AlumniOrgController implements AlumniOrgControllerAPI {

    @Autowired
    private WeiXiaoUtil weiXiaoUtil;
    @Autowired
    private AlumniOrgService alumniOrgService;

    @Override
    @PassToken
    public CommonResult queryWeixiaoOrgs() {
         CommonResult result = weiXiaoUtil.queryOrgs();
        return result;
    }

    @Override
    public CommonResult queryColleges() {
        List<AlumniOrg> result = alumniOrgService.queryColleges();
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryPeriods(int collegeId) {
        List<AlumniOrg> result = alumniOrgService.queryPeriods(collegeId);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryMajors(int periodId) {
        List<AlumniOrg> result = alumniOrgService.queryMajors(periodId);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryClasses(int majorId) {
        List<AlumniOrg> result = alumniOrgService.queryClasses(majorId);
        return CommonResult.ok(result);
    }

    @Override
    @PassToken
    public CommonResult queryOrgTree() {
        List<AlumniOrg> result = alumniOrgService.queryAlumniOrg();

        List<AlumniOrg> zeroResult = result.stream().filter(e -> e.getParentId().intValue() == 0).collect(Collectors.toList());
        List<DepartmentTreeVo> departments = new ArrayList<>();
        for (AlumniOrg data : zeroResult) {
            DepartmentTreeVo newData = new DepartmentTreeVo().builder()
                    .id(data.getId())
                    .name(data.getName())
                    .parentId(data.getParentId())
                    .build();

            List<DepartmentTreeVo> departmentTrees = QueryDepartmentTreeRecords(newData.getId(), result);
            newData.setChildren(departmentTrees);
            departments.add(newData);
        }

        return CommonResult.ok(departments);
    }

    /**
     * 根据父级ID获取树形数据
     *
     * @param parentID 父级ID
     * @param lists    数据集合
     * @return
     */
    private List<DepartmentTreeVo> QueryDepartmentTreeRecords(Integer parentID, List<AlumniOrg> lists) {
        List<DepartmentTreeVo> newTrees = new ArrayList<>();

        List<AlumniOrg> datas = lists.stream().filter(e -> e.getParentId().equals(parentID)).collect(Collectors.toList());

        for (AlumniOrg data : datas) {
            DepartmentTreeVo item = DepartmentTreeVo.builder()
                    .id(data.getId())
                    .parentId(parentID)
                    .name(data.getName())
                    .build();
            List<DepartmentTreeVo> news = QueryDepartmentTreeRecords(item.getId(), lists);
            if (news == null || news.size() == 0) {
                newTrees.add(item);
                continue;
            } else {
                item.setChildren(news);
                newTrees.add(item);
            }
        }

        return newTrees;
    }
}

