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


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.classCall.EduClassCallPushConfig;
import com.yida.data.common.core.entity.classCall.EduClassCallPushConfigProcessStaff;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.classCall.mapper.EduClassCallPushConfigMapper;
import com.yida.data.school.classCall.service.EduClassCallPushConfigProcessStaffService;
import com.yida.data.school.classCall.service.EduClassCallPushConfigService;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
@Transactional
public class EduClassCallPushConfigServiceImpl extends ServiceImpl
    <EduClassCallPushConfigMapper, EduClassCallPushConfig> implements EduClassCallPushConfigService {

    private final EduClassCallPushConfigProcessStaffService eduClassCallPushConfigProcessStaffService;

    @Override
    public EduClassCallPushConfig getBySchoolId() {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        List<EduClassCallPushConfig> list = list(Wrappers.lambdaQuery(new EduClassCallPushConfig())
            .eq(EduClassCallPushConfig::getSchoolId, currentUser.getDeptId()));
        if (CollUtil.isNotEmpty(list)) {
            EduClassCallPushConfig eduClassCallPushConfig = list.get(0);
            List<EduClassCallPushConfigProcessStaff> staffList = eduClassCallPushConfigProcessStaffService
                .list(Wrappers.lambdaQuery(new EduClassCallPushConfigProcessStaff())
                    .eq(EduClassCallPushConfigProcessStaff::getConfigId, eduClassCallPushConfig.getId()));
            eduClassCallPushConfig.setStaffList(staffList);
            return eduClassCallPushConfig;
        } else {
            return new EduClassCallPushConfig();
        }
    }

    @Override
    public void addConfig(EduClassCallPushConfig eduClassCallPushConfig) {
        //保存主体
        saveOrUpdate(eduClassCallPushConfig);
        if (ObjectUtil.isNotNull(eduClassCallPushConfig.getId())) {
            eduClassCallPushConfigProcessStaffService.remove(Wrappers.lambdaQuery(new EduClassCallPushConfigProcessStaff())
                .eq(EduClassCallPushConfigProcessStaff::getConfigId, eduClassCallPushConfig.getId()));
        }
        List<EduClassCallPushConfigProcessStaff> staffList = new ArrayList<>();
        for (EduClassCallPushConfigProcessStaff eduClassCallPushConfigProcessStaff : eduClassCallPushConfig.getStaffList()) {
            eduClassCallPushConfigProcessStaff.setConfigId(eduClassCallPushConfig.getId());
            staffList.add(eduClassCallPushConfigProcessStaff);
        }
        eduClassCallPushConfigProcessStaffService.saveBatch(staffList);
    }
}
