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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassConfig;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassConfig.Type;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassSubject;
import com.yida.data.school.dto.secondClass.SaveHomeInfoDTO;
import com.yida.data.school.secondClass.service.EduSecondClassBannerService;
import com.yida.data.school.secondClass.service.EduSecondClassConfigService;
import com.yida.data.school.secondClass.service.EduSecondClassContentService;
import com.yida.data.school.secondClass.service.EduSecondClassSubjectService;
import com.yida.data.school.vo.secondClass.HomeInfoVO;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class EduSecondClassContentServiceImpl implements EduSecondClassContentService {

    private final EduSecondClassSubjectService eduSecondClassSubjectService;
    private final EduSecondClassBannerService eduSecondClassBannerService;
    private final EduSecondClassConfigService eduSecondClassConfigService;

    @Override
    public HomeInfoVO getHomeInfo(Long deptId, Integer type) {
        HomeInfoVO res = new HomeInfoVO();
        res.setBanner(eduSecondClassBannerService.getBanner(deptId, type));
        res.setSubjectList(eduSecondClassSubjectService
            .list(Wrappers.<EduSecondClassSubject>lambdaQuery().eq(EduSecondClassSubject::getDeptId, deptId)
                .eq(EduSecondClassSubject::getTopicType, type)
                .select(EduSecondClassSubject::getId, EduSecondClassSubject::getName, EduSecondClassSubject::getIcon,
                    EduSecondClassSubject::getType, EduSecondClassSubject::getContentType, EduSecondClassSubject::getOrientation,
                    EduSecondClassSubject::getRowNum, EduSecondClassSubject::getTopicType, EduSecondClassSubject::getSort)));
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveHomeInfo(SaveHomeInfoDTO dto, Long deptId) {
        // 保存标题
        if (StrUtil.isNotBlank(dto.getTitle())) {
            eduSecondClassConfigService.update(
                Wrappers.<EduSecondClassConfig>lambdaUpdate().eq(EduSecondClassConfig::getDeptId, deptId)
                    .set(Type.HOME.getValue().equals(dto.getType()) ? EduSecondClassConfig::getHomeTitle
                            : Type.VENUE.getValue().equals(dto.getType()) ? EduSecondClassConfig::getVenueTitle
                                : Type.PLATFORM.getValue().equals(dto.getType()) ? EduSecondClassConfig::getPlatformTitle
                                    : EduSecondClassConfig::getCourseTitle
                        , dto.getTitle()));
        }
        // 保存banner
        if (dto.getBanner() != null) {
            dto.getBanner().setDeptId(deptId);
            dto.getBanner().setType(dto.getType());
            eduSecondClassBannerService.saveBanner(dto.getBanner());
        } else {
            eduSecondClassBannerService.removeBanner(deptId, dto.getType());
        }
        // 保存主题信息
        if (CollUtil.isNotEmpty(dto.getSubjectList())) {
            List<Long> existIds = new ArrayList<>();
            for (EduSecondClassSubject subject : dto.getSubjectList()) {
                subject.setDeptId(deptId);
                subject.setTopicType(dto.getType());
                if (subject.getId() != null) {
                    existIds.add(subject.getId());
                }
            }
            eduSecondClassSubjectService
                .remove(Wrappers.<EduSecondClassSubject>lambdaQuery().eq(EduSecondClassSubject::getDeptId, deptId)
                    .eq(EduSecondClassSubject::getTopicType, dto.getType())
                    .notIn(EduSecondClassSubject::getId, existIds));
            eduSecondClassSubjectService.saveOrUpdateBatch(dto.getSubjectList());
        } else {
            eduSecondClassSubjectService
                .remove(Wrappers.<EduSecondClassSubject>lambdaQuery().eq(EduSecondClassSubject::getDeptId, deptId)
                    .eq(EduSecondClassSubject::getTopicType, dto.getType()));
        }
    }

}
