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.StrUtil;
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.school.EduSchoolHomeApp;
import com.yida.data.common.core.entity.school.EduSchoolHomeAppShow;
import com.yida.data.common.core.entity.user.EduStaffDept;
import com.yida.data.school.index.mapper.EduSchoolHomeAppMapper;
import com.yida.data.school.index.service.EduSchoolHomeAppService;
import com.yida.data.school.index.service.EduSchoolHomeAppShowService;
import com.yida.data.school.news.service.EduSchoolMatterService;
import com.yida.data.system.feign.RemoteDeptService;
import com.yida.data.user.feign.RemoteStaffService;
import com.yida.data.user.feign.RemoteUserDeptService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
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 EduSchoolHomeAppServiceImpl extends ServiceImpl
    <EduSchoolHomeAppMapper, EduSchoolHomeApp> implements EduSchoolHomeAppService {

    private final EduSchoolHomeAppShowService eduSchoolHomeAppShowService;
    private final RemoteStaffService remoteStaffService;
    private final RemoteDeptService remoteDeptService;
    private final RemoteUserDeptService remoteUserDeptService;
    private final EduSchoolMatterService eduSchoolMatterService;
    private final RedisService redisService;

    @Override
    @Transactional
    public void saveApp(List<EduSchoolHomeApp> app) {
        List<Long> newIds =
            app.stream().filter(x -> x.getId() != null).map(x -> x.getId()).collect(Collectors.toList());
        if (CollUtil.isEmpty(newIds)) {
            newIds = new ArrayList<>();
            newIds.add(-1L);
        }
        remove(Wrappers.lambdaQuery(new EduSchoolHomeApp())
            .eq(EduSchoolHomeApp::getSchoolId, app.get(0).getSchoolId())
            .notIn(EduSchoolHomeApp::getId, newIds));
        saveOrUpdateBatch(app);
        // 删除可见范围后再添加
        eduSchoolHomeAppShowService.remove(Wrappers.lambdaQuery(new EduSchoolHomeAppShow())
            .eq(EduSchoolHomeAppShow::getSchoolId, app.get(0).getSchoolId()));
        List<EduSchoolHomeAppShow> showSaveList = new ArrayList<>();
        List<String> urlList = new ArrayList<>();
        for (EduSchoolHomeApp homeApp : app) {
            if (StrUtil.isNotBlank(homeApp.getLogo())) {
                urlList.add(homeApp.getLogo());
            }
            if (CollUtil.isNotEmpty(homeApp.getShowList())) {
                for (EduSchoolHomeAppShow show : homeApp.getShowList()) {
                    show.setAppId(homeApp.getId());
                    show.setSchoolId(homeApp.getSchoolId());
                    showSaveList.add(show);
                }
            }
        }
        eduSchoolMatterService.saveMatterBatch(urlList, app.get(0).getSchoolId(), 0);
        if (showSaveList.size() > 0) {
            eduSchoolHomeAppShowService.saveBatch(showSaveList);
        }
    }

    @Override
    public List<EduSchoolHomeApp> listApp(Long schoolId, Integer type, Long identityId) {
        List<Long> deptIds = null;
        if (type == 1 && identityId != null) {
            List<EduStaffDept> staffDeptList = remoteStaffService.getStaff(identityId).getData().getStaffDept();
            if (CollUtil.isNotEmpty(staffDeptList)) {
                deptIds = remoteUserDeptService.getUpAndDownId(
                    staffDeptList.stream().map(EduStaffDept::getDeptId).toArray(Long[]::new)).getData();
            }
        }
        return baseMapper.listApp(schoolId, type, deptIds, identityId);
    }

    @Override
    @Transactional
    public void delAppBatch(List<Long> ids) {
        removeByIds(ids);
        eduSchoolHomeAppShowService.remove(Wrappers.lambdaQuery(new EduSchoolHomeAppShow())
            .in(EduSchoolHomeAppShow::getAppId, ids));
    }
}
