package com.shulead.datasync.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shulead.datasync.bo.ClassBO;
import com.shulead.datasync.entity.*;
import com.shulead.datasync.mapper.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Classname ClassManager
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/5/8 14:10
 * @Created by liuxiangshun
 */
@Component
public class ClassManager {
    @Resource
    private MClassMapper mClassMapper;

    @Resource
    private MClassDetailMapper mClassDetailMapper;

    @Resource
    private MClassUnitMapper mClassUnitMapper;

    @Resource
    private MPriceListDetailMapper mPriceListDetailMapper;

    @Resource
    private MPriceListMapper mPriceListMapper;

    @Resource
    private SDictionaryMapper sDictionaryMapper;

    public List<ClassBO> pageClass(Long startId, Integer limit) {
        // 查询班级信息
        LambdaQueryWrapper<MClass> classWrapper = Wrappers.lambdaQuery(MClass.class)
                .ge(MClass::getId, startId)
                .orderByAsc(MClass::getId)
                .last("limit " + limit);
        List<MClass> classList = mClassMapper.selectList(classWrapper);
        // 构建 classBO
        List<ClassBO> classBOList = new ArrayList<>();
        for (MClass mClass : classList) {
            ClassBO classBO = new ClassBO();
            classBO.setId(mClass.getId());
            classBO.setClassName(mClass.getClassName());
            classBO.setLessonFee(getFee(mClass.getId(), "课时费"));
            classBO.setTotalLessonFee(getFee(mClass.getId(), "总课时费"));
            classBO.setTotalInstalments(mClass.getInstallmentNums());
            LambdaQueryWrapper<MPriceList> priceListWrapper = Wrappers.lambdaQuery(MPriceList.class)
                    .eq(MPriceList::getClassId, mClass.getId());
            List<MPriceList> priceLists = mPriceListMapper.selectList(priceListWrapper);
            if (CollectionUtils.isNotEmpty(priceLists)) {
                double sum = priceLists.stream().mapToDouble(MPriceList::getPrice).sum();
                classBO.setTotalPrice(sum);
            }
            LambdaQueryWrapper<SDictionary> sDictionaryWrapper = Wrappers.lambdaQuery(SDictionary.class)
                    .eq(SDictionary::getType, "classtype")
                    .eq(SDictionary::getValue, mClass.getClassType());
            SDictionary sDictionary = sDictionaryMapper.selectOne(sDictionaryWrapper);
            if (sDictionary != null) {
                classBO.setType(sDictionary.getValueName());
            }
            classBOList.add(classBO);
        }
        return classBOList;
    }

    public List<ClassBO> getClassByUpdateDate(Date beginDate, Date endDate) {
        // 查询班级信息
        LambdaQueryWrapper<MClass> classWrapper = Wrappers.lambdaQuery(MClass.class)
                .ge(MClass::getUpdatedDate, beginDate)
                .le(MClass::getUpdatedDate, endDate);
        List<MClass> classList = mClassMapper.selectList(classWrapper);
        // 构建 classBO
        List<ClassBO> classBOList = new ArrayList<>();
        for (MClass mClass : classList) {
            ClassBO classBO = new ClassBO();
            classBO.setId(mClass.getId());
            classBO.setClassName(mClass.getClassName());
            classBO.setLessonFee(getFee(mClass.getId(), "课时费"));
            classBO.setTotalLessonFee(getFee(mClass.getId(), "总课时费"));
            classBO.setTotalInstalments(mClass.getInstallmentNums());
            LambdaQueryWrapper<MPriceList> priceListWrapper = Wrappers.lambdaQuery(MPriceList.class)
                    .eq(MPriceList::getClassId, mClass.getId());
            List<MPriceList> priceLists = mPriceListMapper.selectList(priceListWrapper);
            if (CollectionUtils.isNotEmpty(priceLists)) {
                double sum = priceLists.stream().mapToDouble(MPriceList::getPrice).sum();
                classBO.setTotalPrice(sum);
            }
            LambdaQueryWrapper<SDictionary> sDictionaryWrapper = Wrappers.lambdaQuery(SDictionary.class)
                    .eq(SDictionary::getType, "classtype")
                    .eq(SDictionary::getValue, mClass.getClassType());
            SDictionary sDictionary = sDictionaryMapper.selectOne(sDictionaryWrapper);
            if (sDictionary != null) {
                classBO.setType(sDictionary.getValueName());
            }
            classBOList.add(classBO);
        }
        return classBOList;
    }

    public Double getFee(Long classId, String unitName) {
        // 查询 class_detail
        LambdaQueryWrapper<MClassDetail> classDetailWrapper = Wrappers.lambdaQuery(MClassDetail.class)
                .eq(MClassDetail::getClassId, classId);
        List<MClassDetail> classDetailList = mClassDetailMapper.selectList(classDetailWrapper);
        List<Long> classUnitIdList = classDetailList.stream().map(MClassDetail::getClassUnitId).toList();
        if (CollectionUtils.isNotEmpty(classUnitIdList)) {
            // 查询课时费
            LambdaQueryWrapper<MClassUnit> classUnitWrapper1 = Wrappers.lambdaQuery(MClassUnit.class)
                    .in(MClassUnit::getId, classUnitIdList)
                    .eq(MClassUnit::getClassUnitName, unitName);
            List<MClassUnit> classUnitList1 = mClassUnitMapper.selectList(classUnitWrapper1);
            if (CollectionUtils.isNotEmpty(classUnitList1)) {
                Set<Long> lessonFeeIds = classUnitList1.stream().map(MClassUnit::getId).collect(Collectors.toSet());
                List<Long> lessonFeeClassDetailIds = classDetailList.stream().filter(mClassDetail -> lessonFeeIds.contains(mClassDetail.getClassUnitId())).map(MClassDetail::getId).toList();
                if (CollectionUtils.isNotEmpty(lessonFeeClassDetailIds)) {
                    LambdaQueryWrapper<MPriceListDetail> priceListDetailWrapper1 = Wrappers.lambdaQuery(MPriceListDetail.class)
                            .in(MPriceListDetail::getClassDetailId, lessonFeeClassDetailIds);
                    List<MPriceListDetail> priceListDetailList = mPriceListDetailMapper.selectList(priceListDetailWrapper1);
                    if (CollectionUtils.isNotEmpty(priceListDetailList)) {
                        return priceListDetailList.stream().mapToDouble(MPriceListDetail::getPrice).sum();
                    }
                }
            }
        }
        return null;
    }

    public MClass getById(Long id) {
        return mClassMapper.selectById(id);
    }
}
