package shop.alien.store.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import shop.alien.entity.store.LifeClassBanner;
import shop.alien.entity.store.LifeClassManage;
import shop.alien.mapper.LifeClassBannerMapper;
import shop.alien.mapper.LifeClassManageMapper;

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

/**
 * 课程管理
 */
@Service
@RequiredArgsConstructor
public class LifeClassManageService {

    private final LifeClassManageMapper lifeClassManageMapper;
    private final LifeClassBannerMapper lifeClassBannerMapper;

    public IPage<LifeClassManage> getStores(int page, int size, String className, String classTeacher, String classBigType, String classSmallType) {
        IPage<LifeClassManage> storePage = new Page<>(page, size);
        QueryWrapper<LifeClassManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(null != className && !className.isEmpty(), "class_name", className);
        queryWrapper.like(null != classTeacher && !classTeacher.isEmpty(), "class_teacher", classTeacher);
        queryWrapper.like(null != classBigType && !classBigType.isEmpty(), "class_big_type", classBigType);
        queryWrapper.like(null != classSmallType && !classSmallType.isEmpty(), "class_small_type", classSmallType);
        queryWrapper.orderByDesc("created_time");
        return lifeClassManageMapper.selectPage(storePage, queryWrapper);
    }

    public int addOrUpdateStore(LifeClassManage store) {
        if (StringUtils.isEmpty(store.getId())) {
            store.setClassState("正常");
            Date nowDate = new Date(System.currentTimeMillis());
            store.setPlayCount(0);
            store.setCreatedTime(nowDate);
            return lifeClassManageMapper.insert(store);
        } else {
            return lifeClassManageMapper.updateById(store);
        }
    }

    public int setStoreState(String id, String classState) {
        LambdaUpdateWrapper<LifeClassManage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(LifeClassManage::getId, id)
                .set(LifeClassManage::getClassState, classState);
        return lifeClassManageMapper.update(null, updateWrapper);
    }

    public int delete(String id) {
        LambdaUpdateWrapper<LifeClassManage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(LifeClassManage::getId, id);
        return lifeClassManageMapper.delete(updateWrapper);
    }

    public int setIsRead(String id) {
        LambdaUpdateWrapper<LifeClassManage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LifeClassManage::getId, id)
                .setSql("play_count = play_count + 1"); // play_count + 1
        return lifeClassManageMapper.update(null, updateWrapper);
    }

    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>>> resultMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(manages)) {
            resultMap = manages.stream()
                    .collect(Collectors.groupingBy(
                            LifeClassManage::getClassBigType,
                            Collectors.groupingBy(LifeClassManage::getClassSmallType)
                    ));
        }
        return resultMap;
    }

    public List<LifeClassManage> getAllList() {
        return lifeClassManageMapper.selectList(new LambdaQueryWrapper<>());
    }

    public List<LifeClassManage> getCLassVideo(String classBigType, String classSmallType) {
        LambdaUpdateWrapper<LifeClassManage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.like(LifeClassManage::getClassBigType, classBigType)
                .like(LifeClassManage::getClassSmallType, classSmallType)
                .orderByDesc(LifeClassManage::getCreatedTime);
        return lifeClassManageMapper.selectList(wrapper);
    }

    public IPage<LifeClassBanner> getClassBannerPages(int page, int size, String bannerTitle, String playFlag) {
        IPage<LifeClassBanner> ipage = new Page<>(page, size);
        return lifeClassBannerMapper.selectPage(ipage, new LambdaQueryWrapper<LifeClassBanner>()
                .like(null != bannerTitle && !bannerTitle.isEmpty(), LifeClassBanner::getBannerTitle, bannerTitle)
                .eq(ObjectUtils.isNotEmpty(playFlag), LifeClassBanner::getPlayFlag, playFlag)
                .eq(LifeClassBanner::getDeleteFlag, 0).orderByDesc(LifeClassBanner::getPlayFlag)
                .orderByAsc(LifeClassBanner::getBannerSort));
    }
}
