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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.school.EduSchoolHomeAnnouncement;
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.utils.AppUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.school.index.mapper.EduSchoolHomeAnnouncementMapper;
import com.yida.data.school.index.service.EduSchoolHomeAnnouncementService;
import com.yida.data.school.news.mapper.EduSchoolManuscriptMapper;
import com.yida.data.system.feign.RemoteYidaAppService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service实现
 *
 * @author zhanghaijun
 * @date 2021-07-19 17:21:58
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduSchoolHomeAnnouncementServiceImpl extends ServiceImpl
    <EduSchoolHomeAnnouncementMapper, EduSchoolHomeAnnouncement> implements EduSchoolHomeAnnouncementService {

    private final RemoteYidaAppService remoteYidaAppService;
    private final CommonService commonService;
    private final EduSchoolManuscriptMapper eduSchoolManuscriptMapper;

    @Override
    public void saveAnnouncement(EduSchoolHomeAnnouncement announcement) {
        EduSchoolHomeAnnouncement exist = getOne(Wrappers.lambdaQuery(new EduSchoolHomeAnnouncement())
            .eq(EduSchoolHomeAnnouncement::getSchoolId, announcement.getSchoolId()));
        if (exist != null) {
            announcement.setId(exist.getId());
            announcement.setYidaAppRegionId(exist.getYidaAppRegionId());
        }
        Dept school = commonService.getDept(announcement.getSchoolId());

        // 在app中将稿件放入通知区域位中
        if (school.getDeptType().equals(DeptTypeEnum.SCHOOL_TYPE.getValue()) && school.getSchoolType().contains("1")) {
            EduYidaApp yidaApp = remoteYidaAppService.getAppBySchool(announcement.getSchoolId()).getData();
            JSONArray content = new JSONArray();
            // 查询所有从公告同步的稿件
            List<EduSchoolManuscript> list =
                eduSchoolManuscriptMapper.selectList(Wrappers.lambdaQuery(new EduSchoolManuscript())
                    .eq(EduSchoolManuscript::getSchoolId, school.getDeptId())
                    .eq(EduSchoolManuscript::getStatus, 2)
                    .isNotNull(EduSchoolManuscript::getNoticeSchoolId)
//                    .like(EduSchoolManuscript::getPublishPath, EduSchoolManuscript.SECOND_CLASS_PATH)
                    .ne(EduSchoolManuscript::getId, announcement.getTopAnnouncementId()));
            for (EduSchoolManuscript manuscriptNotice : list) {
                if (StrUtil.isNotBlank(manuscriptNotice.getYidaAppMunascriptId())) {
                    JSONObject oneNotice = new JSONObject() {{
                        set("id", manuscriptNotice.getYidaAppMunascriptId());
                        set("sort", 1);
                    }};
                    if (manuscriptNotice.getCoverType() != 0) {
                        oneNotice.set("img", manuscriptNotice.getCoverUrl().split(",")[0]);
                    }
                    content.add(oneNotice);
                }
            }
            // 添加置顶稿件
            if (announcement.getTopAnnouncementId() != null) {
                EduSchoolManuscript top =
                    eduSchoolManuscriptMapper.selectById(announcement.getTopAnnouncementId());
                if (exist != null && !announcement.getTopAnnouncementId().equals(exist.getTopAnnouncementId())) {
                    EduSchoolManuscript oldTop =
                        eduSchoolManuscriptMapper.selectById(announcement.getTopAnnouncementId());
                    if (top != null) {
                        content.add(new JSONObject() {{
                            set("id", top.getYidaAppMunascriptId());
                            set("sort", 0);
                            set("img", " ");
                        }});
                        AppUtil.topManuscript(top.getYidaAppMunascriptId(), yidaApp, 1);
                    }
                    if (ObjectUtil.isNotNull(oldTop)) {
                        AppUtil.topManuscript(oldTop.getYidaAppMunascriptId(), yidaApp, 0);
                    }
                }
            }
            announcement.setYidaAppRegionId(AppUtil.regionUpdate(announcement.getYidaAppRegionId(), announcement.getName(), 0,
                school.getYidaAppColId(), AppUtil.REGION_TYPE_NOTICE, content, yidaApp));
        }

        saveOrUpdate(announcement);
        if (announcement.getTopAnnouncementId() == null) {
            update(Wrappers.<EduSchoolHomeAnnouncement>lambdaUpdate()
                .set(EduSchoolHomeAnnouncement::getTopAnnouncementId, null)
                .set(EduSchoolHomeAnnouncement::getTopAnnouncementName, null)
                .eq(EduSchoolHomeAnnouncement::getId, announcement.getId()));
        }
    }
}
