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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.utils.FebsUtil;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassConfig;
import com.yida.data.school.secondClass.mapper.EduSecondClassConfigMapper;
import com.yida.data.school.secondClass.service.EduSecondClassConfigService;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class EduSecondClassConfigServiceImpl extends ServiceImpl<EduSecondClassConfigMapper, EduSecondClassConfig> implements
    EduSecondClassConfigService {

    private static final String HOME_DEFAULT_NAME = "主页";
    private static final String VENUE_DEFAULT_NAME = "游览场馆";
    private static final String PLATFORM_DEFAULT_NAME = "智慧平台";
    private static final String COURSE_DEFAULT_NAME = "名师在线";

    @Override
    @Cacheable(value = CachePrefixConstant.SECOND_CLASS_CONFIG, key = "#deptId")
    public EduSecondClassConfig getConfig(Long deptId) {
        EduSecondClassConfig config = getOne(
            Wrappers.<EduSecondClassConfig>lambdaQuery().eq(EduSecondClassConfig::getDeptId, deptId));
        if (config == null) {
            config = new EduSecondClassConfig();
            config.setDeptId(deptId);
            config.setHomeTitle(HOME_DEFAULT_NAME);
            config.setVenueTitle(VENUE_DEFAULT_NAME);
            config.setPlatformTitle(PLATFORM_DEFAULT_NAME);
            config.setCourseTitle(COURSE_DEFAULT_NAME);
            save(config);
        }
        return config;
    }

    @Override
    @CacheEvict(value = CachePrefixConstant.SECOND_CLASS_CONFIG, key = "#deptId")
    public void updateConfig(Long deptId, Integer show, Integer type) {
        LambdaUpdateWrapper<EduSecondClassConfig> condition = Wrappers.<EduSecondClassConfig>lambdaUpdate()
            .eq(EduSecondClassConfig::getDeptId, FebsUtil.getTopDeptId());
        switch (type) {
            case 0:
                condition.set(EduSecondClassConfig::getHomeShow, show);
                break;
            case 1:
                condition.set(EduSecondClassConfig::getVenueShow, show);
                break;
            case 2:
                condition.set(EduSecondClassConfig::getPlatformShow, show);
                break;
            case 3:
                condition.set(EduSecondClassConfig::getCourseShow, show);
                break;
            default:
                break;
        }
        update(condition);
    }
}
