package com.yida.data.school.smart.service.impl;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.smart.EduSmartWelcomeDepartment;
import com.yida.data.school.smart.mapper.EduSmartWelcomeDepartmentMapper;
import com.yida.data.school.smart.service.EduSmartWelcomeDepartmentService;
import java.util.List;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduSmartWelcomeDepartmentServiceImpl extends ServiceImpl
    <EduSmartWelcomeDepartmentMapper, EduSmartWelcomeDepartment> implements EduSmartWelcomeDepartmentService {

    private final RedisService redisService;

    @Override
    public List<EduSmartWelcomeDepartment> getDepartmentTree(Long deptId) {
//        String returnStr = (String) redisService.hget(CachePrefixConstant.WELCOME_DEPARTMENT_DATA, deptId.toString());
//
//        if (ObjectUtil.isNotNull(returnStr)) {
//            return JSONUtil.toList(returnStr, EduSmartWelcomeDepartment.class);
//        }
        List<EduSmartWelcomeDepartment> departmentList = list(Wrappers.lambdaQuery(new EduSmartWelcomeDepartment())
            .eq(EduSmartWelcomeDepartment::getDeptId, deptId)
            .isNull(EduSmartWelcomeDepartment::getParentId));
        for (EduSmartWelcomeDepartment department : departmentList) {
            List<EduSmartWelcomeDepartment> list = list(Wrappers.lambdaQuery(new EduSmartWelcomeDepartment())
                .eq(EduSmartWelcomeDepartment::getDeptId, deptId)
                .eq(EduSmartWelcomeDepartment::getParentId, department.getId()));
            department.setChildrenList(list);
        }
        log.info("缓存数据：{}", JSONUtil.toJsonStr(departmentList));
//        if (CollUtil.isNotEmpty(departmentList)) {
//            redisService.set(CachePrefixConstant.WELCOME_DEPARTMENT_DATA + deptId, JSONUtil.toJsonStr(departmentList));
//        }
        return departmentList;
    }
}
