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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.letter.EduLetterBoxConfig;
import com.yida.data.common.core.entity.letter.EduLetterBoxReplyPerson;
import com.yida.data.common.core.entity.user.EduStaff;
import com.yida.data.school.dto.letterbox.LetterBoxConfigDTO;
import com.yida.data.school.letter.mapper.EduLetterBoxConfigMapper;
import com.yida.data.school.letter.service.EduLetterBoxConfigService;
import com.yida.data.school.letter.service.EduLetterBoxReplyPersonService;
import com.yida.data.user.dto.ListStaffDTO;
import com.yida.data.user.feign.RemoteStaffService;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ccl
 */
@RefreshScope
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduLetterBoxConfigServiceImpl extends ServiceImpl
        <EduLetterBoxConfigMapper, EduLetterBoxConfig> implements EduLetterBoxConfigService {

    private final EduLetterBoxReplyPersonService personService;
    private final RemoteStaffService staffService;

    /**
     * 查询对应学校配置列表
     *
     * @param schoolId
     * @return
     */
    @Override
    public List<EduLetterBoxConfig> findBannerList(Long schoolId) {

        return list(Wrappers.query(new EduLetterBoxConfig()).lambda().eq(EduLetterBoxConfig::getSchoolId, schoolId));
    }

    /**
     * banner图保存
     *
     * @param configs 配置列表
     */
    @Override
    public void saveOrUpdateBanner(List<EduLetterBoxConfig> configs, Long schoolId) {
        //先删除历史数据
        remove(Wrappers.query(new EduLetterBoxConfig()).lambda().eq(EduLetterBoxConfig::getSchoolId, schoolId));
        //再新增新数据
        if (CollectionUtil.isNotEmpty(configs)) {
            for (int i = 0; i < configs.size(); i++) {
                configs.get(i).setSort(i);
                configs.get(i).setSchoolId(schoolId);
            }
        }
        saveBatch(configs);
    }

    @Override
    public LetterBoxConfigDTO findConfig(Long schoolId) {
        LetterBoxConfigDTO dto = new LetterBoxConfigDTO();
        List<EduLetterBoxConfig> list = list(Wrappers.query(new EduLetterBoxConfig()).lambda().eq(EduLetterBoxConfig::getSchoolId, schoolId));
        dto.setBannerList(list);
        if (CollectionUtil.isNotEmpty(list)) {
            dto.setTip(list.get(0).getTip());
        }
        List<EduLetterBoxReplyPerson> replyPerson = personService.findReplyPerson(schoolId);
        ListStaffDTO staffDTO = new ListStaffDTO();
        List<Long> ids = new ArrayList<>();
        for (EduLetterBoxReplyPerson person : replyPerson) {
            ids.add(person.getUserId());
        }
        staffDTO.setIds(ids);
        List<EduStaff> data = staffService.listStaff(staffDTO).getData();
        dto.setReplyPerson(data);
        return dto;
    }
}
