package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import shop.alien.entity.store.LifeClassManage;
import shop.alien.mapper.LifeClassManageMapper;
import shop.alien.store.service.StoreClassManageService;

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

@Service
@RequiredArgsConstructor
public class StoreClassManageServiceImpl implements StoreClassManageService {

    private final LifeClassManageMapper lifeClassManageMapper;

    @Override
    public Map<String, Map<String, List<LifeClassManage>>> getAllList(String className, String classTeacher, String classBigType, String classSmallType) {
        QueryWrapper<LifeClassManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!className.isEmpty() && !"null".equals(className), "class_name", className);
        queryWrapper.like(!classTeacher.isEmpty() && !"null".equals(classTeacher), "class_teacher", classTeacher);
        queryWrapper.like(!classBigType.isEmpty() && !"null".equals(classBigType), "class_big_type", classBigType);
        queryWrapper.like(!classSmallType.isEmpty() && !"null".equals(classSmallType), "class_small_type", classSmallType);
        queryWrapper.orderByDesc("created_time");
        List<LifeClassManage> manages = lifeClassManageMapper.selectList(queryWrapper);
        Map<String, Map<String, List<LifeClassManage>>> classMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(manages)) {
            classMap = manages.stream()
                    .collect(Collectors.groupingBy(
                            LifeClassManage::getClassBigType,
                            Collectors.groupingBy(LifeClassManage::getClassSmallType)
                    ));
        }
        return classMap;
    }

    @Override
    public List<LifeClassManage> getClassList() {
        return lifeClassManageMapper.selectList(new QueryWrapper<LifeClassManage>().orderByDesc("created_time"));
    }

    @Override
    public Map<String, List<LifeClassManage>> classesSelect(String keyword) {
        QueryWrapper<LifeClassManage> wrapper = new QueryWrapper<>();
        wrapper.like("class_name", keyword)
                .or()
                .like("class_teacher", keyword);
        List<LifeClassManage> manages = lifeClassManageMapper.selectList(wrapper);
        Map<String, List<LifeClassManage>> classMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(manages)) {
            classMap = manages.stream().collect(Collectors.groupingBy(LifeClassManage::getClassSmallType));
        }
        return classMap;
    }
}
