package com.kxs.system.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.system.api.model.KxsSchoolStudy;
import com.kxs.system.api.vo.kxsapp.school.SchoolFirstMenuVO;
import com.kxs.system.biz.mapper.KxsSchoolMenuMapper;
import com.kxs.system.api.model.KxsSchoolMenu;
import com.kxs.system.biz.mapper.KxsSchoolStudyMapper;
import com.kxs.system.biz.service.KxsSchoolMenuService;
import com.kxs.system.biz.service.KxsSchoolStudyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 商学院创客学堂分类(KxsSchoolMenu)表服务实现类
 *
 * @author 系统
 * @since 2023-12-15 12:16:31
 */
@Service("kxsSchoolMenuService")
@RequiredArgsConstructor
public class KxsSchoolMenuServiceImpl extends ServiceImpl<KxsSchoolMenuMapper, KxsSchoolMenu> implements KxsSchoolMenuService {
    private final KxsSchoolMenuMapper kxsSchoolMenuMapper;
    private final KxsSchoolStudyMapper kxsSchoolStudyMapper;
    @Override
    public Object firstMenu() {
        List<KxsSchoolMenu> list = list(Wrappers.<KxsSchoolMenu>query().lambda().eq(KxsSchoolMenu::getPid,0));
        ArrayList<SchoolFirstMenuVO> schoolFirstMenuVOList = new ArrayList<>();
        for (KxsSchoolMenu kxsSchoolMenu : list) {
            SchoolFirstMenuVO schoolFirstMenuVO = new SchoolFirstMenuVO();
            BeanUtil.copyProperties(kxsSchoolMenu,schoolFirstMenuVO);
            //获取pid为当前类型id的类型
            long count = count(Wrappers.<KxsSchoolMenu>query().lambda().eq(KxsSchoolMenu::getPid, kxsSchoolMenu.getId()));
            schoolFirstMenuVO.setChildCount(count);//下级数量
            //获取当前时间
            LocalDateTime now = LocalDateTime.now();
            //获取当前时间之前三天的时间
            LocalDateTime threeDaysBefore = now.minusDays(3);
            List<KxsSchoolStudy> kxsSchoolStudies = kxsSchoolStudyMapper.selectList(Wrappers.<KxsSchoolStudy>query().lambda().eq(KxsSchoolStudy::getMenuId,kxsSchoolMenu.getId()).between(KxsSchoolStudy::getCreateTime, threeDaysBefore, now));
            //三天内存在数据则返回有更新
            if (kxsSchoolStudies != null && !kxsSchoolStudies.isEmpty()){
                schoolFirstMenuVO.setIsUpdate(1);//是否更新
            }else {
                schoolFirstMenuVO.setIsUpdate(0);//是否更新
            }
            schoolFirstMenuVOList.add(schoolFirstMenuVO);
        }
        return schoolFirstMenuVOList;
    }
}

