package com.xunwang.education.service.impl;

import com.xunwang.education.entity.CoursePrice;
import com.xunwang.education.entity.vo.CoursePriceVo;
import com.xunwang.education.entity.vo.CourseTreeVo;
import com.xunwang.education.mapper.CoursePriceMapper;
import com.xunwang.education.service.CoursePriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Long
 * @since 2022-11-16
 */
@Service
public class CoursePriceServiceImpl extends ServiceImpl<CoursePriceMapper, CoursePrice> implements CoursePriceService {

    /**
     * 获取课程树形结构
     * 获取课程价格对应关系
     *
     * @return
     */
    @Override
    public Map<String, Object> getTreeAndPrice() {
        List<CoursePrice> coursePrices = this.baseMapper.selectList(null);
        // 获取课程树形结构
        List<CourseTreeVo> treeCourse = getTreeCourse(coursePrices);
        // 获取课程价目表
        List<CoursePriceVo> coursePrice = getCoursePrice(coursePrices);

        HashMap<String, Object> hashMap = new HashMap<>();
        // 1.放入树形结构课程表
        hashMap.put("treeCourse", treeCourse);
        // 2.放入课程对应价格表
        hashMap.put("coursePrice", coursePrice);

        return hashMap;
    }

    /**
     * 获取课程树形结构
     *
     * @return
     */
    private List<CourseTreeVo> getTreeCourse(List<CoursePrice> courses) {
        List<CourseTreeVo> treeCourse = courses.stream().filter(course ->
                // 获取课程大类
                "0".equals(course.getParentId())
        ).map(course ->
                new CourseTreeVo().setText(course.getCName()).setValue(course.getCName()).setChildren(getTreeCourseLevel2(courses, course))
        ).collect(Collectors.toList());

        return treeCourse;
    }

    /**
     * 获取具体课程
     *
     * @param courses
     * @param course
     * @return
     */
    private List<CourseTreeVo> getTreeCourseLevel2(List<CoursePrice> courses, CoursePrice course) {
        return courses.stream().filter(courseLevel2 ->
                // 获取具体课程
                !"0".equals(courseLevel2.getParentId()) && courseLevel2.getParentId().equals(course.getCNum())
        ).map(courseLevel2 ->
                new CourseTreeVo().setText(courseLevel2.getCName()).setValue(courseLevel2.getCName())
        ).collect(Collectors.toList());
    }

    /**
     * 获取课程价格关系表
     *
     * @param courses
     * @return
     */
    private List<CoursePriceVo> getCoursePrice(List<CoursePrice> courses) {
        return courses.stream().filter(course ->
                !"0".equals(course.getParentId())
        ).map(course -> {
            CoursePriceVo coursePriceVo = new CoursePriceVo();
            coursePriceVo.setCourseName(course.getCName());

            HashMap<String, BigDecimal> hashMap = new HashMap<>();
            hashMap.put("stu_price_1_l", course.getStuPrice1L());
            hashMap.put("stu_price_1_h", course.getStuPrice1H());
            hashMap.put("stu_price_2_l", course.getStuPrice2L());
            hashMap.put("stu_price_2_h", course.getStuPrice2H());
            hashMap.put("fulltime_price_1_l", course.getFulltimePrice1L());
            hashMap.put("fulltime_price_1_h", course.getFulltimePrice1H());
            hashMap.put("fulltime_price_2_l", course.getFulltimePrice2L());
            hashMap.put("fulltime_price_2_h", course.getFulltimePrice2H());

            coursePriceVo.setPriceMapping(hashMap);
            return coursePriceVo;

        }).collect(Collectors.toList());
    }
}
