package com.schoolinout.api.service.impl;

import com.schoolinout.api.mapper.ApiParentMapper;
import com.schoolinout.api.mapper.ApiParentStudentRelationMapper;
import com.schoolinout.api.service.IApiParentService;
import com.schoolinout.system.domain.Parent;
import com.schoolinout.system.domain.ParentStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author sakura
 * @date 2024/1/9 17:17:18 Tue
 */
@Service
public class ApiParentServiceImpl implements IApiParentService {

    @Autowired
    private ApiParentMapper parentMapper;
    @Autowired
    private ApiParentStudentRelationMapper relationMapper;

    @Override
    public Parent listParentSimple(Parent selectCondition) {
        return parentMapper.selectOnlyOne(selectCondition);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveParent(Parent parent) {
        // 先保存 家长
        parentMapper.insert(parent);
        // 两种情况
        //  一种是他绑定原来系统中就已经绑定的
        //  第二他第一次绑定
        ParentStudent parentStudent = new ParentStudent();
        parentStudent.setParentId(parent.getId());
        // 默认先只能绑定一个
        parentStudent.setStudentId(Long.valueOf(parent.getStudentIds()));
        relationMapper.insertOrUpdate(parentStudent);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveOrUpdateRelation(Long parentId, Long studentId) {
        ParentStudent parentStudent = new ParentStudent();
        parentStudent.setStudentId(studentId);
        parentStudent.setParentId(parentId);
        relationMapper.insertOrUpdate(parentStudent);
    }
}
