package com.xunwang.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.xunwang.education.entity.TeacherCourses;
import com.xunwang.education.entity.vo.TeacherCoursesVo;
import com.xunwang.education.entity.vo.TeacherList;
import com.xunwang.education.entity.vo.TeacherListQuery;
import com.xunwang.education.feign.ImageFeign;
import com.xunwang.education.mapper.TeacherCoursesMapper;
import com.xunwang.education.service.TeacherCertificationsService;
import com.xunwang.education.service.TeacherCoursesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 老师课程发布 服务实现类
 * </p>
 *
 * @author Long
 * @since 2022-12-04
 */
@Service
public class TeacherCoursesServiceImpl extends ServiceImpl<TeacherCoursesMapper, TeacherCourses> implements TeacherCoursesService {

    @Autowired
    private TeacherCertificationsService certificationsService;

    @Autowired
    private ImageFeign imageFeign;

    /**
     * 添加课程发布信息
     * @param teacherCoursesVo
     * @param uid
     */
    @Override
    public int addTeacherCourses(TeacherCoursesVo teacherCoursesVo, String uid) {

        List<TeacherCourses> courses = new ArrayList<>();

        HashMap<String, String> subject = teacherCoursesVo.getSubject();
        Collection<String> values = subject.values();
        // 找到授课科目不为1的科目
        Object[] subjects = values.stream().filter(value -> !value.equals("1")).collect(Collectors.toSet()).toArray();

        for (int j = 0; j < subjects.length; j++) {
            TeacherCourses teacherCourses = new TeacherCourses();
            BeanUtils.copyProperties(teacherCoursesVo, teacherCourses);
            // uid
            teacherCourses.setUid(uid);
            // 授课时间
            String[] teachTime = teacherCoursesVo.getTeachTime();
            teacherCourses.setTeachTime(teachTime[0] + "-" + teachTime[1]);
            // 授课方式
            String[] modes = teacherCoursesVo.getMode();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < modes.length; i++) {
                if (i == modes.length - 1) {
                    sb.append(modes[i]);
                } else {
                    sb.append(modes[i] + ",");
                }
            }
            teacherCourses.setMode(sb.toString());
            // 老师类型
            String teacherType = certificationsService.getTeacherType(uid);
            teacherCourses.setTeacherType(teacherType);
            // 授课科目
            teacherCourses.setSubject((String) subjects[j]);

            courses.add(teacherCourses);
        }

        // 批量添加
        int i = baseMapper.batchInsert(courses);
        return i;
    }

    /**
     * 查询个人已发布的课程
     * @param uid
     * @return
     */
    @Override
    public List<TeacherCourses> showPersonCourse(String uid) {
        QueryWrapper<TeacherCourses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid).eq("deleted", "0").eq("verify_status", "已通过").orderByDesc("datetime").last("limit 9");
        List<TeacherCourses> teacherCourses = baseMapper.selectList(queryWrapper);
        return teacherCourses;
    }

    /**
     * 删除已发布的课程
     * @param courseId
     * @return
     */
    @Override
    public int deletePersonCourse(int courseId) {
        int i = baseMapper.deleteById(courseId);
        return i;
    }

    /**
     * 分页查询
     * 查询全部老师根据条件
     * @param queryObj
     */
    @Override
    public List<TeacherList> queryAllByConditions(TeacherListQuery queryObj) {
        queryObj.setPageNum((queryObj.getPageNum() - 1) * queryObj.getPageSize());
        List<TeacherList> teacherLists = baseMapper.selectTeachers(queryObj);

        // 利用feign:将头像地址转为base64字符串
        for (TeacherList teacherList : teacherLists) {
            String profilePhoto = teacherList.getProfilePhoto();
            String encode = imageFeign.download(profilePhoto);
            teacherList.setProfilePhoto(encode);
        }
        return teacherLists;
    }

    /**
     * 查询个人是否有正在审核的课程
     * @param uid
     * @return
     */
    @Override
    public boolean queryPersonVerifyCourse(String uid) {
        QueryWrapper<TeacherCourses> queryWrapper = new QueryWrapper();
        queryWrapper.eq("verify_status", "审核中");
        int count = count(queryWrapper);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询个人未成交的课程数
     * @param uid
     * @return
     */
    @Override
    public int queryPersonNoDeal(String uid) {
        return count(new QueryWrapper<TeacherCourses>().eq("deal", "未成交"));
    }

    /**
     * 查询老师详情总成交次数和最近成交时间
     * @param uid
     */
    @Override
    public Map queryCountAndLastTime(String uid) {
        // 1.查询总成交次数
        int count = count(new QueryWrapper<TeacherCourses>().eq("deal", "已成交").eq("uid", uid));
        // 2.最近成交
        String latest = baseMapper.selectLatest(uid);
        String time = "";
        if (latest == null) {
            time = "暂无";
        } else {
            String[] strings = latest.split(" ");
            time = strings[0];
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("count", count);
        hashMap.put("time", time);

        return hashMap;
    }
}
