package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.RegisterParentsStudent;
import com.ruoyi.system.domain.RegisterSchool;
import com.ruoyi.system.domain.RegisterTeacherClass;
import com.ruoyi.system.mapper.FormalParentsStudentMapper;
import com.ruoyi.system.mapper.RegisterParentsStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RegisterParentsMapper;
import com.ruoyi.system.domain.RegisterParents;
import com.ruoyi.system.service.IRegisterParentsService;

/**
 * 注册-家长Service业务层处理
 * 
 * @author boman
 * @date 2023-05-25
 */
@Service
public class RegisterParentsServiceImpl implements IRegisterParentsService 
{
    @Autowired
    private RegisterParentsMapper registerParentsMapper;

    @Autowired
    private RegisterParentsStudentMapper registerParentsStudentMapper;

    /**
     * 查询注册-家长
     * 
     * @param id 注册-家长主键
     * @return 注册-家长
     */
    @Override
    public RegisterParents selectRegisterParentsById(Long id)
    {
        return registerParentsMapper.selectRegisterParentsById(id);
    }

    /**
     * 查询注册-家长列表
     * 
     * @param registerParents 注册-家长
     * @return 注册-家长
     */
    @Override
    public List<RegisterParents> selectRegisterParentsList(RegisterParents registerParents)
    {
        return registerParentsMapper.selectRegisterParentsList(registerParents);
    }

    /**
     * 新增注册-家长
     * 
     * @param registerParents 注册-家长
     * @return 结果
     */
    @Override
    public AjaxResult insertRegisterParents(RegisterParents registerParents)
    {

        //查询是否已经注册过
        RegisterParents registerParentsOld = registerParentsMapper.selectRegisterParentsByOppenId(registerParents.getOpenId());
        if(registerParentsOld!=null){
            if(registerParentsOld.getIsPass().equals("1")){
                return AjaxResult.error("该微信已注册当前身份，信息正在审核中。",registerParentsOld);
            }else if(registerParentsOld.getIsPass().equals("2")){
                return AjaxResult.error("该微信已注册当前身份，审核已通过，请登录。",registerParentsOld);
            }else{
                return AjaxResult.error("该微信已注册当前身份，审核未通过。",registerParentsOld);
            }
        }
        registerParents.setCreateTime(DateUtils.getNowDate());
        registerParents.setUpdateTime(DateUtils.getNowDate());
        int count = registerParentsMapper.insertRegisterParents(registerParents);

        List<RegisterParentsStudent> registerParentsStudentList = registerParents.getRegisterParentsStudentList();
        if(registerParentsStudentList!=null && registerParentsStudentList.size()>0){
            for (RegisterParentsStudent registerParentsStudent : registerParentsStudentList) {
                registerParentsStudent.setParentsId(registerParents.getId());
                registerParentsStudent.setParentsName(registerParents.getUserName());
                registerParentsStudent.setParentsPhone(registerParents.getUserPhone());
                registerParentsStudent.setCreateTime(DateUtils.getNowDate());
                registerParentsStudent.setUpdateTime(DateUtils.getNowDate());
                registerParentsStudentMapper.insertRegisterParentsStudent(registerParentsStudent);
            }
        }


        return count > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改注册-家长
     * 
     * @param registerParents 注册-家长
     * @return 结果
     */
    @Override
    public int updateRegisterParents(RegisterParents registerParents)
    {
        //先删除审核未通过的数据
        List<RegisterParentsStudent> registerParentsStudentList = registerParents.getRegisterParentsStudentList();
        if(registerParentsStudentList!=null && registerParentsStudentList.size()>0){
            registerParentsStudentMapper.deleteRegisterParentsStudentByParentsId(registerParents.getId());
            for (RegisterParentsStudent registerParentsStudent : registerParentsStudentList) {
                if("3".equals(registerParentsStudent.getIsPass()) || StringUtils.isEmpty(registerParentsStudent.getIsPass())){
                    registerParentsStudent.setParentsId(registerParents.getId());
                    registerParentsStudent.setParentsName(registerParents.getUserName());
                    registerParentsStudent.setParentsPhone(registerParents.getUserPhone());
                    registerParentsStudent.setIsPass("1");
                    registerParentsStudent.setCreateTime(DateUtils.getNowDate());
                    registerParentsStudent.setUpdateTime(DateUtils.getNowDate());
                    registerParentsStudentMapper.insertRegisterParentsStudent(registerParentsStudent);
                }
            }
        }
        registerParents.setIsPass("1");
        registerParents.setUpdateTime(DateUtils.getNowDate());
        return registerParentsMapper.updateRegisterParents(registerParents);
    }

    /**
     * 批量删除注册-家长
     * 
     * @param ids 需要删除的注册-家长主键
     * @return 结果
     */
    @Override
    public int deleteRegisterParentsByIds(Long[] ids)
    {
        return registerParentsMapper.deleteRegisterParentsByIds(ids);
    }

    /**
     * 删除注册-家长信息
     * 
     * @param id 注册-家长主键
     * @return 结果
     */
    @Override
    public int deleteRegisterParentsById(Long id)
    {
        return registerParentsMapper.deleteRegisterParentsById(id);
    }
}
