package com.xjrsoft.module.student.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.student.entity.BaseStudentAssessmentClassRelation;
import com.xjrsoft.module.student.entity.BaseStudentAssessmentInspection;
import com.xjrsoft.module.student.mapper.BaseStudentAssessmentClassRelationMapper;
import com.xjrsoft.module.student.service.IBaseStudentAssessmentClassRelationService;
import com.xjrsoft.module.student.service.IBaseStudentAssessmentInspectionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @title: 学生班级巡查考核-关联班级
 * @Author fanxp
 * @Date: 2023-11-16
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class BaseStudentAssessmentClassRelationServiceImpl extends ServiceImpl<BaseStudentAssessmentClassRelationMapper, BaseStudentAssessmentClassRelation> implements IBaseStudentAssessmentClassRelationService {

    private final IBaseStudentAssessmentInspectionService baseStudentAssessmentInspectionService;

    private final BaseStudentAssessmentClassRelationMapper baseStudentAssessmentClassRelationMapper;

    @Override
    public Boolean dataCache(Long dataId) {
        BaseStudentAssessmentInspection baseStudentAssessmentInspection = baseStudentAssessmentInspectionService.getById(dataId);
        List<String> classIdList = StrUtil.split(baseStudentAssessmentInspection.getClassIds(), ",");

        for (String classId : classIdList) {
            List<BaseStudentAssessmentClassRelation> list = baseStudentAssessmentClassRelationMapper.selectList(
                    Wrappers.lambdaQuery(BaseStudentAssessmentClassRelation.class)
                            .eq(BaseStudentAssessmentClassRelation::getClassId, classId)
                            .eq(BaseStudentAssessmentClassRelation::getBaseStudentAssessmentInspectionId, dataId)
            );
            if(!list.isEmpty() && list.size() > 0){
                continue;
            }
            BaseStudentAssessmentClassRelation classRelation = new BaseStudentAssessmentClassRelation();
            Long classIdAsLong = Convert.toLong(classId);
            classRelation.setClassId(classIdAsLong);
            classRelation.setBaseStudentAssessmentInspectionId(dataId);

            baseStudentAssessmentClassRelationMapper.insert(classRelation);
        }

        return true;
    }
}
