package org.jeecg.modules.system.app.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.IdCardUtil;
import org.jeecg.modules.system.app.entity.*;
import org.jeecg.modules.system.app.form.ClassGroupingCasePageForm;
import org.jeecg.modules.system.app.form.ClassGroupingPageForm;
import org.jeecg.modules.system.app.form.StudentInfoForm;
import org.jeecg.modules.system.app.mapper.*;
import org.jeecg.modules.system.app.service.IAppClassGroupingService;
import org.jeecg.modules.system.app.vo.ClassGroupingCaseVO;
import org.jeecg.modules.system.app.vo.ClassGroupingVO;
import org.jeecg.modules.system.app.vo.StudentInfoVO;
import org.jeecg.modules.system.app.vo.staff.StaffResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppClassGroupingServiceImpl extends ServiceImpl<AppClassGroupingMapper, AppClassGrouping> implements IAppClassGroupingService {

    @Resource
    private AppClassUserMapper appClassUserMapper;
    @Resource
    private AppOrderProInfoMapper appOrderProInfoMapper;
    @Resource
    private FamilyMembersMapper familyMembersMapper;
    @Resource
    private AppCoursesMapper appCoursesMapper;
    @Resource
    private AppSiteMapper appSiteMapper;
    @Autowired
    private AppClassGroupingMapper appClassGroupingMapper;

    @Override
    public IPage<ClassGroupingVO> queryPage(ClassGroupingPageForm classGroupingPageForm) {
        Page<ClassGroupingVO> page = new Page<ClassGroupingVO>(classGroupingPageForm.getPageNo(), classGroupingPageForm.getPageSize());
        IPage<ClassGroupingVO> queryPage = baseMapper.queryPage(page, classGroupingPageForm);
        queryPage.getRecords().forEach(record -> {

            //排除临时约课
            long AlreadyClassGroupingNum =
                    appClassUserMapper.selectCount(Wrappers.lambdaQuery(AppClassUser.class).eq(AppClassUser::getCourseId, record.getId()).eq(AppClassUser::getOrTemp, 0));

            long UnClassGroupingNum = record.getApplyNum() - AlreadyClassGroupingNum;

            record.setAlreadyClassGroupingNum((int) AlreadyClassGroupingNum).setUnClassGroupingNum((int) UnClassGroupingNum);
        });
        return queryPage;
    }

    @Override
    public IPage<ClassGroupingCaseVO> queryCasePage(ClassGroupingCasePageForm classGroupingCasePageForm) {

        Page<ClassGroupingCaseVO> page = new Page<ClassGroupingCaseVO>(classGroupingCasePageForm.getPageNo(), classGroupingCasePageForm.getPageSize());
        return baseMapper.queryCasePage(page, classGroupingCasePageForm);
    }

    @Override
    public List<StudentInfoVO> getStudentInfoList(StudentInfoForm studentInfoForm) {
        List<StudentInfoVO> studentInfoList = new ArrayList<>();
        AppCourses appCourses = appCoursesMapper.selectById(studentInfoForm.getCoursesId());
        if (ObjectUtil.isNotEmpty(appCourses)) {

            String siteId = appCourses.getAddressSiteId();
            AppSite appSite = appSiteMapper.selectById(siteId);

            //查询当前课程下已分班的学员
            List<AppClassUser> appClassUserList =
                    appClassUserMapper.selectList(Wrappers.lambdaQuery(AppClassUser.class).eq(AppClassUser::getCourseId, studentInfoForm.getCoursesId()));

            //根据当前课程查询订单
            List<AppOrderProInfo> orderProInfoList =
                    appOrderProInfoMapper.selectList(Wrappers.lambdaQuery(AppOrderProInfo.class).between(AppOrderProInfo::getOrderStatus, 1, 3).eq(AppOrderProInfo::getProductId, studentInfoForm.getCoursesId()));
            //所有学员
            for (AppOrderProInfo appOrderProInfo : orderProInfoList) {
                StudentInfoVO studentInfoVO = new StudentInfoVO();
                FamilyMembers familyMembers = familyMembersMapper.selectById(appOrderProInfo.getFamilyUserId());

                if (studentInfoForm.getClassGroupingId() != null) {
                    AppClassUser appClassUser = appClassUserMapper.selectOne(Wrappers.lambdaQuery(AppClassUser.class).eq(AppClassUser::getClassGroupingId,
                            studentInfoForm.getClassGroupingId()).eq(AppClassUser::getFamilyMemberId, appOrderProInfo.getFamilyUserId()));
                    if (ObjectUtil.isNotEmpty(appClassUser)) {
                        studentInfoVO.setId(familyMembers.getId());
                        studentInfoVO.setName(familyMembers.getFullName());
                        studentInfoVO.setGender(IdCardUtil.getGender(familyMembers.getIdentityCard()));
                        studentInfoVO.setAge(IdCardUtil.getAge(familyMembers.getIdentityCard()));
                        studentInfoVO.setSchoolName("无");
                        studentInfoVO.setLevelInfo("无");
                        studentInfoVO.setOrChoose(true);
                        if (appSite.getType() == 0) {
                            if (ObjectUtil.isNotEmpty(appOrderProInfo.getLevelInfo())) {
                                studentInfoVO.setSchoolName(appSite.getName());
                                studentInfoVO.setLevelInfo(appOrderProInfo.getLevelInfo());
                            }
                        }
                        studentInfoList.add(studentInfoVO);
                    }
                }
                long count = appClassUserList.stream().filter(appClassUser -> appClassUser.getFamilyMemberId().equals(appOrderProInfo.getFamilyUserId())).count();
                if (count == 0) {
                    studentInfoVO.setId(familyMembers.getId());
                    studentInfoVO.setName(familyMembers.getFullName());
                    studentInfoVO.setGender(IdCardUtil.getGender(familyMembers.getIdentityCard()));
                    studentInfoVO.setAge(IdCardUtil.getAge(familyMembers.getIdentityCard()));
                    studentInfoVO.setSchoolName("无");
                    studentInfoVO.setLevelInfo("无");
                    if (appSite.getType() == 0) {
                        if (ObjectUtil.isNotEmpty(appOrderProInfo.getLevelInfo())) {
                            studentInfoVO.setSchoolName(appSite.getName());
                            studentInfoVO.setLevelInfo(appOrderProInfo.getLevelInfo());
                        }
                    }
                    studentInfoList.add(studentInfoVO);
                }
            }
        }
        return studentInfoList;
    }
}
