package com.xinyilu.system.service.appletstudentmanage.Impl;

import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.core.domain.entity.SysDictData;
import com.xinyilu.common.exception.ServiceException;
import com.xinyilu.common.utils.DateUtils;
import com.xinyilu.system.domain.classinfo.ClassInfo;
import com.xinyilu.system.domain.dto.StudentInfoDTO;
import com.xinyilu.system.domain.parentInfo.ParentInfo;
import com.xinyilu.system.domain.parentStudentInfo.ParentStudentInfo;
import com.xinyilu.system.domain.vo.StudentInfoVO;
import com.xinyilu.system.mapper.classinfo.ClassInfoMapper;
import com.xinyilu.system.mapper.kindergarteninfo.KindergartenInfoMapper;
import com.xinyilu.system.mapper.parentStudentInfo.ParentStudentInfoMapper;
import com.xinyilu.system.mapper.studentInfo.StudentInfoMapper;
import com.xinyilu.system.service.ISysDictTypeService;
import com.xinyilu.system.service.appletstudentmanage.IAppletStudentManageService;
import com.xinyilu.system.service.studentInfo.IStudentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * 家长小程序学生管理
 *
 * @author xinyilu
 * @date 2023-07-31
 */
@Slf4j
@Service
public class AppletStudentManageServiceImpl implements IAppletStudentManageService {
    @Resource
    private IStudentInfoService studentInfoService;
    @Resource
    private StudentInfoMapper studentInfoMapper;
    @Resource
    private ParentStudentInfoMapper parentStudentInfoMapper;
    @Resource
    private ClassInfoMapper classInfoMapper;
    @Resource
    private KindergartenInfoMapper kindergartenInfoMapper;
    @Resource
    private ISysDictTypeService sysDictTypeService;

    /**
     * 查询学生信息
     *
     * @param studentId 学生id
     * @return AjaxResult
     */
    @Override
    public AjaxResult getStudentInformation(Long studentId) {
        StudentInfoVO studentInfoVO = studentInfoService.selectStudentInfoById(studentId);
        if (Objects.nonNull(studentInfoVO) && CollectionUtils.isNotEmpty(studentInfoVO.getParentStudentInfoList())) {
            StringBuilder parentName = new StringBuilder();
            for (ParentInfo parentInfo : studentInfoVO.getParentStudentInfoList()) {
                parentName.append(parentInfo.getParentName()).append(" ");
            }
            studentInfoVO.setParentName(parentName.toString());
        }
        return AjaxResult.success(studentInfoVO);
    }

    /**
     * 修改学生信息
     *
     * @param studentInfo 学生信息
     * @return 结果
     */
    @Override
    public AjaxResult updateStudentInformation(StudentInfoDTO studentInfo) {
        if (Objects.isNull(studentInfo.getId()) && CollectionUtils.isEmpty(studentInfo.getParentIdList())) {
            return AjaxResult.error("家长信息不能为空！");
        }
        if (Objects.nonNull(studentInfo.getBirthDate()) && studentInfo.getBirthDate().isAfter(LocalDate.now())) {
            return AjaxResult.error("出生日期不能大于现在日期！");
        }
        StudentInfoVO studentInfoVO = studentInfoMapper.selectStudentInfoById(studentInfo.getId());
        boolean bool = (Objects.nonNull(studentInfoVO)) && (StringUtils.isNotBlank(studentInfo.getCardNumber())) && !studentInfo.getCardNumber().equals(studentInfoVO.getCardNumber());
        boolean boole = Objects.isNull(studentInfoVO) && StringUtils.isNotBlank(studentInfo.getCardNumber());
        if (BooleanUtils.isTrue(boole) || BooleanUtils.isTrue(bool)) {
            int studentCardNumberInfo = studentInfoMapper.getStudentCardNumberInfo(studentInfo.getCardNumber());
            if (studentCardNumberInfo > 0) {
                return AjaxResult.error("证件号：" + studentInfo.getCardNumber() + "与某个学生的证件号重复，请重新填写！");
            }
        }
        //查询幼儿表id
        ClassInfo classInfo = classInfoMapper.selectClassInfoById(studentInfo.getClassId());
        if (Objects.nonNull(classInfo)) {
            studentInfo.setKindergartenId(Objects.isNull(classInfo.getKindergartenId()) ? 0L : classInfo.getKindergartenId());
        }
        //修改学生信息
        if (Objects.isNull(studentInfo.getId())) {
            studentInfo.setCreateTime(DateUtils.getNowDate());
            studentInfo.setDelFlag(0);
            int insertCount = studentInfoMapper.insertStudentInfo(studentInfo);
            if (insertCount <= 0) {
                throw new ServiceException("新增学生信息失败");
            }
            //新增家长学生关系表信息
            for (Long parentId : studentInfo.getParentIdList()) {
                ParentStudentInfo parentStudentInfo = new ParentStudentInfo();
                parentStudentInfo.setParentId(parentId);
                parentStudentInfo.setStudentId(studentInfo.getId());
                parentStudentInfo.setDelFlag(0);
                parentStudentInfo.setCreateTime(DateUtils.getNowDate());
                int parentStudentCount = parentStudentInfoMapper.insertParentStudentInfo(parentStudentInfo);
                if (parentStudentCount <= 0) {
                    log.info("新增学生家长信息失败");
                    throw new ServiceException();
                }
            }
        } else {
            studentInfo.setUpdateTime(DateUtils.getNowDate());
            int updateCount = studentInfoMapper.updateStudentInfo(studentInfo);
            if (updateCount <= 0) {
                throw new ServiceException("修改学生信息失败");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 查询幼儿园信息
     *
     * @param kindergartenName 幼儿园信息
     * @return AjaxResult
     */
    @Override
    public AjaxResult getKindergartenList(String kindergartenName) {
        return AjaxResult.success(kindergartenInfoMapper.getKindergartenNameList(kindergartenName));
    }

    /**
     * 根据幼儿园id查询班级名称
     *
     * @param kindergartenId 幼儿园id
     * @param className      班级名
     * @return AjaxResult
     */
    @Override
    public AjaxResult getClassInfo(Long kindergartenId, String className) {
        return AjaxResult.success(classInfoMapper.getClassList(kindergartenId, className));
    }

    /**
     * 字典查询民族类型
     *
     * @param dictType 字典类型
     * @return SysDictData
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return sysDictTypeService.selectDictDataByType(dictType);
    }
}