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

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.home.EduHomeFunction;
import com.yida.data.common.core.entity.notice.EduNoticeSchool;
import com.yida.data.common.core.entity.school.EduDeptHomeApp;
import com.yida.data.common.core.entity.school.EduSchoolHomeAnnouncement;
import com.yida.data.common.core.entity.school.EduSchoolHomeCover;
import com.yida.data.common.core.entity.school.EduSchoolHomeMicro;
import com.yida.data.common.core.entity.school.EduSchoolHomeNews;
import com.yida.data.common.core.entity.school.EduSchoolManuscript;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.EduYidaApp;
import com.yida.data.common.core.entity.system.enums.DeptTypeEnum;
import com.yida.data.common.core.enums.EnableStatusEnum;
import com.yida.data.common.core.utils.AppUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.school.index.mapper.EduSchoolHomeAppMapper;
import com.yida.data.school.index.mapper.EduSchoolHomeCoverMapper;
import com.yida.data.school.index.mapper.EduSchoolHomeNewsMapper;
import com.yida.data.school.index.service.EduDeptHomeAppService;
import com.yida.data.school.index.service.EduHomeFunctionService;
import com.yida.data.school.index.service.EduSchoolHomeAnnouncementService;
import com.yida.data.school.index.service.EduSchoolHomeCoverService;
import com.yida.data.school.index.service.EduSchoolHomeMicroService;
import com.yida.data.school.news.mapper.EduNoticeSchoolMapper;
import com.yida.data.school.news.mapper.EduSchoolManuscriptMapper;
import com.yida.data.school.news.service.EduSchoolMatterService;
import com.yida.data.school.vo.index.HomeInfoVO;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service实现
 *
 * @author zhanghaijun
 * @date 2021-07-19 17:21:58
 */
@Service
@RequiredArgsConstructor
@Transactional
public class EduSchoolHomeCoverServiceImpl extends ServiceImpl
    <EduSchoolHomeCoverMapper, EduSchoolHomeCover> implements EduSchoolHomeCoverService {

    private final EduHomeFunctionService eduHomeFunctionService;
    private final EduSchoolMatterService eduSchoolMatterService;
    private final EduSchoolHomeMicroService eduSchoolHomeMicroService;
    private final EduSchoolHomeAnnouncementService eduSchoolHomeAnnouncementService;
    private final EduDeptHomeAppService eduDeptHomeAppService;
    private final EduNoticeSchoolMapper eduNoticeSchoolMapper;
    private final EduSchoolHomeAppMapper eduSchoolHomeAppMapper;
    private final EduSchoolHomeNewsMapper eduSchoolHomeNewsMapper;
    private final EduSchoolManuscriptMapper eduSchoolManuscriptMapper;

    private final CommonService commonService;
    private final RedisService redisService;

    @Override
    public HomeInfoVO getHomeInfo(Long schoolId, Integer userType) {
        HomeInfoVO homeInfoVO = new HomeInfoVO();
        //缓存主页信息
        if (redisService.hHasKey(CachePrefixConstant.SCHOOL_HOME_INFO, schoolId + "" + userType)) {
            homeInfoVO = (HomeInfoVO) redisService
                .hget(CachePrefixConstant.SCHOOL_HOME_INFO, schoolId + "" + userType);
        } else {
            Dept dept = commonService.getDept(schoolId);
            homeInfoVO.setDeptType(dept.getDeptType());
            homeInfoVO.setCoverList(list(Wrappers.lambdaQuery(new EduSchoolHomeCover())
                .eq(EduSchoolHomeCover::getSchoolId, schoolId)));
            if (dept.getDeptType().equals(DeptTypeEnum.SCHOOL_TYPE.getValue())) {
                homeInfoVO.setParentAppList(eduSchoolHomeAppMapper.listApp(schoolId, 0, null, null));
                homeInfoVO.setTeacherAppList(eduSchoolHomeAppMapper.listApp(schoolId, 1, null, null));
                // 查询公告区
                EduSchoolHomeAnnouncement announcement =
                    eduSchoolHomeAnnouncementService.getOne(Wrappers.lambdaQuery(new EduSchoolHomeAnnouncement())
                        .eq(EduSchoolHomeAnnouncement::getSchoolId, schoolId));
                // 无公告进行保存操作
                if (announcement == null) {
                    announcement = new EduSchoolHomeAnnouncement();
                    announcement.setSchoolId(schoolId);
                    announcement.setName("公示公告");
                    eduSchoolHomeAnnouncementService.saveAnnouncement(announcement);
                }
                // 设置了置顶公告则进行查询
                if (announcement.getTopAnnouncementId() != null) {
                    EduNoticeSchool topNotice = eduNoticeSchoolMapper.selectById(announcement.getTopAnnouncementId());
                    if (topNotice != null) {
                        announcement.setTopAnnouncementPubDate(topNotice.getSendDate());
                        announcement.setTopAnnouncement(topNotice);
                    }
                }
                // 查询公告区公告
                LambdaQueryWrapper<EduNoticeSchool> condition = Wrappers.lambdaQuery(new EduNoticeSchool())
                    .eq(EduNoticeSchool::getSchoolId, schoolId)
                    .eq(EduNoticeSchool::getIsPublic, 1)
                    .orderByDesc(EduNoticeSchool::getSendDate)
                    .last(announcement.getTopAnnouncementId() == null ? "limit 3" : "limit 2");
                if (announcement.getTopAnnouncementId() != null) {
                    condition.ne(EduNoticeSchool::getId, announcement.getTopAnnouncementId());
                }
                announcement.setManuscriptList(eduNoticeSchoolMapper.selectList(condition));
                homeInfoVO.setAnnouncement(announcement);
                // 微官网区
                EduSchoolHomeMicro microInfo = eduSchoolHomeMicroService.getInfo(schoolId);
                if (microInfo == null) {
                    eduSchoolHomeMicroService.initMicro(schoolId);
                    microInfo = eduSchoolHomeMicroService.getInfo(schoolId);
                }
                homeInfoVO.setMicro(microInfo);
                // 查询新闻区
                EduSchoolHomeNews news = eduSchoolHomeNewsMapper.selectOne(Wrappers.lambdaQuery(new EduSchoolHomeNews())
                    .eq(EduSchoolHomeNews::getSchoolId, schoolId));
                if (news == null) {
                    news = new EduSchoolHomeNews();
                    news.setSchoolId(schoolId);
                    news.setName("学校新闻");
                    eduSchoolHomeNewsMapper.insert(news);
                }
                homeInfoVO.setNews(news);
                if (homeInfoVO.getNews().getTopNewsId() != null) {
                    EduSchoolManuscript topNews = eduSchoolManuscriptMapper.getInfo(homeInfoVO.getNews().getTopNewsId());
                    homeInfoVO.getNews().setTopNews(topNews);
                }
            }
            // 查询功能区
            EduHomeFunction function = eduHomeFunctionService
                .getOne(Wrappers.<EduHomeFunction>lambdaQuery().eq(EduHomeFunction::getDeptId, schoolId));
            if (function == null) {
                function = new EduHomeFunction();
                function.setDeptId(schoolId);
                function.setSearchSchool(0);
                function.setMatchSchool(0);
                function.setEnrolSign(0);
                function.setEnrolQa(0);
                eduHomeFunctionService.save(function);
            }

            if (ObjectUtil.isNotNull(userType)) {
                List<EduDeptHomeApp> list = eduDeptHomeAppService.list(Wrappers.<EduDeptHomeApp>lambdaQuery()
                    .eq(EduDeptHomeApp::getDeptId, schoolId)
                    .eq(EduDeptHomeApp::getUserType, userType)
                    .eq(EduDeptHomeApp::getEnableStatus, EnableStatusEnum.ENABLE_STATUS.getStatus())
                    .orderByAsc(EduDeptHomeApp::getSort));
                if (CollUtil.isNotEmpty(list)) {
                    homeInfoVO.setEduDeptHomeAppList(list);
                }

            }
            homeInfoVO.setFunction(function);

            if (ObjectUtil.isNotNull(homeInfoVO)) {
                redisService.hset(CachePrefixConstant.SCHOOL_HOME_INFO, schoolId + "" + userType, homeInfoVO);
            }
        }
        return homeInfoVO;
    }

    @Override
    public void updateCover(List<EduSchoolHomeCover> coverList, Long schoolId) {
        remove(Wrappers.lambdaQuery(new EduSchoolHomeCover())
            .eq(EduSchoolHomeCover::getSchoolId, schoolId));
        List<String> urlList = new ArrayList<>();
        List<Integer> sortList = new ArrayList<>();
        for (EduSchoolHomeCover cover : coverList) {
            cover.setSchoolId(schoolId);
            cover.setId(null);
            cover.setCreateId(null);
            cover.setCreateDate(null);
            cover.setUpdateId(null);
            cover.setUpdateDate(null);
            urlList.add(cover.getPicUrl());
            sortList.add(cover.getSort());
        }
        saveBatch(coverList);
        Dept school = commonService.getDept(schoolId);
        eduSchoolMatterService.saveMatterBatch(urlList, schoolId, 0);
        if (school.getDeptType().equals(DeptTypeEnum.SCHOOL_TYPE.getValue()) && school.getSchoolType().contains("1")) {
            EduYidaApp yidaApp = commonService.getYidaApp(schoolId);
            AppUtil.bannerUpdate(school.getYidaAppColId(), urlList, sortList, yidaApp);
        }

        redisService.del(CachePrefixConstant.SCHOOL_HOME_INFO, schoolId + "" + 0);
        redisService.del(CachePrefixConstant.SCHOOL_HOME_INFO, schoolId + "" + 1);
    }
}
