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.BaseStudentBehaviorClassRelation;
import com.xjrsoft.module.student.entity.BaseStudentBehaviorManage;
import com.xjrsoft.module.student.mapper.BaseStudentBehaviorClassRelationMapper;
import com.xjrsoft.module.student.service.IBaseStudentBehaviorClassRelationService;
import com.xjrsoft.module.student.service.IBaseStudentBehaviorManageService;
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 BaseStudentBehaviorClassRelationServiceImpl extends ServiceImpl<BaseStudentBehaviorClassRelationMapper, BaseStudentBehaviorClassRelation> implements IBaseStudentBehaviorClassRelationService {

    private final BaseStudentBehaviorClassRelationMapper baseStudentBehaviorClassRelationMapper;
    private final IBaseStudentBehaviorManageService baseStudentBehaviorManageService;

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

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

            baseStudentBehaviorClassRelationMapper.insert(classRelation);
        }

        return true;
    }
}
