package com.yida.data.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.system.*;
import com.yida.data.system.mapper.EduSchoolInquireCommonQuestionMapper;
import com.yida.data.system.service.*;
import com.yida.data.system.vo.FindPhonePageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author ccl
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduInquireQuestionServiceImpl extends ServiceImpl<EduSchoolInquireCommonQuestionMapper,
        EduSchoolInquireCommonQuestion> implements EduInquireQuestionService {

    private final EduSchoolInquireCommonQuestionLabelService questionLabelService;
    private final EduSchoolInquirePhoneLabelService phoneLabelService;
    private final EduSchoolInquirePhoneService phoneService;

    @Override
    public IPage<EduSchoolInquireCommonQuestion> findQuestionPage(Long labelId, Long deptId, Page objectPage,String question) {

        return baseMapper.findQuestionPage(objectPage,labelId,deptId,question);
    }

    @Override
    public IPage<FindPhonePageVO> findPhonePage(Long labelId, Long deptId, Page page,String schoolName) {
        return baseMapper.findPhonePage(labelId,deptId,page,schoolName);
    }

    @Override
    public void removePhone(Long phoneId) {
        phoneService.removeById(phoneId);
        phoneLabelService.remove(Wrappers.query(new EduSchoolInquirePhoneLabel()).lambda().eq(EduSchoolInquirePhoneLabel::getPhoneId,phoneId));
    }

    @Override
    public void removeQuestion(Long questionId) {
        removeById(questionId);
        questionLabelService.remove(Wrappers.query(new EduSchoolInquireCommonQuestionLabel()).lambda().eq(EduSchoolInquireCommonQuestionLabel::getQuestionId,questionId));
    }

    @Override
    public void saveOrUpdatePhone(List<EduSchoolInquirePhone> phoneList) {

        phoneService.saveOrUpdateBatch(phoneList);
        List<Long> phoneIds = new ArrayList<>();
        List<EduSchoolInquirePhoneLabel> phoneLabelList = new ArrayList<>();
        for (EduSchoolInquirePhone phone : phoneList) {
            if (CollectionUtil.isNotEmpty(phone.getLabelId())){
                for (Long labelId : phone.getLabelId()) {
                    EduSchoolInquirePhoneLabel phoneLabel = new EduSchoolInquirePhoneLabel();
                    phoneLabel.setPhoneId(phone.getId());
                    phoneLabel.setLabelId(labelId);
                    phoneLabelList.add(phoneLabel);
                }
                phoneIds.add(phone.getId());
            }

        }
        if (CollectionUtil.isNotEmpty(phoneIds)){
            phoneLabelService.remove(Wrappers.query(new EduSchoolInquirePhoneLabel()).lambda().in(EduSchoolInquirePhoneLabel::getPhoneId,phoneIds));
        }

        phoneLabelService.saveBatch(phoneLabelList);
    }

    @Override
    public void saveOrUpdateQuestion(List<EduSchoolInquireCommonQuestion> questionList) {
        saveOrUpdateBatch(questionList);
        List<Long> questionIds = new ArrayList<>();
        List<EduSchoolInquireCommonQuestionLabel> questionLabelList = new ArrayList<>();
        for (EduSchoolInquireCommonQuestion question : questionList) {
            if (CollectionUtil.isNotEmpty(question.getLabelId())){
                for (Long labelId : question.getLabelId()) {
                    EduSchoolInquireCommonQuestionLabel questionLabel = new EduSchoolInquireCommonQuestionLabel();
                    questionLabel.setLabelId(labelId);
                    questionLabel.setQuestionId(question.getId());
                    questionLabelList.add(questionLabel);
                }
                questionIds.add(question.getId());
            }

        }
        if (CollectionUtil.isNotEmpty(questionIds)){
            questionLabelService.remove(Wrappers.query(new EduSchoolInquireCommonQuestionLabel()).lambda().in(EduSchoolInquireCommonQuestionLabel::getQuestionId,questionIds));
        }

        questionLabelService.saveBatch(questionLabelList);

    }

    @Override
    public EduSchoolInquirePhone findPhoneById(Long phoneId) {
        EduSchoolInquirePhone byId = phoneService.getById(phoneId);
        List<Long> labelId = new ArrayList<>();
        List<EduSchoolInquirePhoneLabel> phoneLabelList = phoneLabelService.list(Wrappers.query(new EduSchoolInquirePhoneLabel())
                .lambda().eq(EduSchoolInquirePhoneLabel::getPhoneId, phoneId));
        for (EduSchoolInquirePhoneLabel phoneLabel : phoneLabelList) {
            labelId.add(phoneLabel.getLabelId());
        }
        byId.setLabelId(labelId);
        return byId;
    }

    @Override
    public EduSchoolInquireCommonQuestion findQuestionById(Long questionId) {
        EduSchoolInquireCommonQuestion commonQuestion = getById(questionId);
        List<Long> labelId = new ArrayList<>();
        List<EduSchoolInquireCommonQuestionLabel> questionLabelList = questionLabelService.list(Wrappers.query(new EduSchoolInquireCommonQuestionLabel())
                .lambda().eq(EduSchoolInquireCommonQuestionLabel::getQuestionId, questionId));
        for (EduSchoolInquireCommonQuestionLabel questionLabel : questionLabelList) {
            labelId.add(questionLabel.getLabelId());
        }
        commonQuestion.setLabelId(labelId);
        return commonQuestion;
    }
}
