package com.app.service.guardian;

import com.app.controller.api.GuardianChildRequest;
import com.app.entity.BaseEntity;
import com.app.entity.guardian.GuardianChild;
import com.app.mapper.guardian.GuardianChildMapper;
import com.app.service.basics.SchoolService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class GuardianChildService {

    @Autowired
    private GuardianChildMapper mapper;

    @Autowired
    private SchoolService schoolService;

    public Optional<GuardianChild> findById(String id) {
        return Optional.ofNullable(mapper.selectById(id));
    }

    public List<GuardianChild> findByGuardianId(String guardianId) {
        return mapper.selectList(new QueryWrapper<GuardianChild>().lambda()
                .eq(GuardianChild::getGuardianId, guardianId)
                .eq(BaseEntity::getIsDel, Boolean.FALSE)
        );
    }

    public void relation(GuardianChildRequest request, String guardianId) {
        GuardianChild _child;
        if (StringUtils.isEmpty(request.getId())) {
            _child = new GuardianChild();
            _child.setGuardianId(guardianId);
            _child.setPhoneNumber(request.getPhoneNumber());
            _child.setChildName(request.getChildName());
            _child.setSchoolId(request.getSchoolId());
            _child.setSchoolName(schoolService.getName(request.getSchoolId())); // 翻译[学校名称]字段
            _child.setGradeName(request.getGradeName());
            _child.setClassName(request.getClassName());
            _child.setGoHome(request.getGoHome());
            _child.setIsGraduate(false);
            mapper.insert(_child);
        } else {
            _child = mapper.selectById(request.getId());
            _child.setPhoneNumber(request.getPhoneNumber());
            _child.setChildName(request.getChildName());
            _child.setSchoolId(request.getSchoolId());
            _child.setSchoolName(schoolService.getName(request.getSchoolId())); // 翻译[学校名称]字段
            _child.setGradeName(request.getGradeName());
            _child.setClassName(request.getClassName());
            _child.setGoHome(request.getGoHome());
            mapper.updateById(_child);
        }
    }

    public void delete(String guardianId, String id) {
        Optional<GuardianChild> optional = findById(id);
        if (optional.isPresent()) {
            GuardianChild accountChild = optional.get();
            accountChild.setIsDel(Boolean.TRUE);
            mapper.updateById(accountChild);
        }
    }

    /**
     * 功能描述：查询学校的学生
     *
     * @param schoolId  学校标识
     * @param gradeName 年级名称
     * @return
     */
    public List<GuardianChild> findBySchoolIdAndGradeName(String schoolId, String gradeName) {
        return mapper.selectList(new QueryWrapper<GuardianChild>().lambda()
                .eq(GuardianChild::getSchoolId, schoolId)
                .eq(GuardianChild::getGradeName, gradeName)
        );
    }

}
