package com.yihu.jw.base.util;

import com.yihu.jw.base.dao.course.CourseDao;
import com.yihu.jw.base.dao.course.CourseSalesOrderRecordDao;
import com.yihu.jw.base.dao.course.DoctorPatientTmpDao;
import com.yihu.jw.base.dao.course.RecruitStudentsRecordDao;
import com.yihu.jw.doctor.dao.BaseDoctorHospitalDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
import com.yihu.jw.entity.base.im.ConsultTeamDo;
import com.yihu.jw.entity.care.course.CourseDO;
import com.yihu.jw.entity.care.course.CourseSalesOrderRecordDO;
import com.yihu.jw.entity.care.course.DoctorPatientTmpDO;
import com.yihu.jw.entity.care.course.RecruitStudentsRecordDO;
import com.yihu.jw.entity.care.doorCoach.BaseDoorCoachOrderDO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 教师与新生儿 入学关系更新工具
 * Created by Bing on 2021/6/11.
 */
@Component
public class EntranceUtil {

    @Autowired
    private DoctorPatientTmpDao doctorPatientTmpDao;
    @Autowired
    private BaseDoctorHospitalDao doctorHospitalDao;

    /**
     *
     * @param patient
     * @param type
     * @param del
     * @param doctorCode
     * @param orgCode
     * @param relationCode
     */
    public void newRecord(String patient,Integer type,Integer del,String doctorCode,String orgCode,String relationCode){
        if (1==type||2==type){//在线咨询与上门辅导不重复保存
            if (doctorPatientTmpDao.findByPatientAndDoctorCodeAndType(patient,doctorCode,type)!=0){
                return ;
            }
        }
        DoctorPatientTmpDO tmpDO = new DoctorPatientTmpDO();
        tmpDO.setPatient(patient);
        tmpDO.setType(type);
        tmpDO.setDel(del);
        tmpDO.setStatus(0);
        if (StringUtils.isBlank(orgCode)){
            List<BaseDoctorHospitalDO> list = doctorHospitalDao.findByDoctorCode(doctorCode);
            if (list.size()>0){
                orgCode = list.get(0).getOrgCode();
            }
        }
        if (doctorPatientTmpDao.findByPatientAndOrgCodeAndTypeAndStatus(patient,orgCode,3,1)!=0){
            tmpDO.setDel(0);
        }
        tmpDO.setOrgCode(orgCode);
        tmpDO.setDoctorCode(doctorCode);
        tmpDO.setRelationCode(relationCode);
        doctorPatientTmpDao.save(tmpDO);
    }

    /**
     * 更新状态 状态为1 已入学，将该幼儿咨询、上门、课程数据失效
     * @param relationCode
     */
    public void updateStatus(Integer status,String relationCode){
        DoctorPatientTmpDO tmpDO = doctorPatientTmpDao.findByRelationCode(relationCode);
        if (tmpDO!=null){
            tmpDO.setStatus(status);
            doctorPatientTmpDao.save(tmpDO);
            if (status==1){//入学后 将该幼儿咨询、上门、课程数据失效
                List<DoctorPatientTmpDO> list = doctorPatientTmpDao.queryByPatientAndTypeIn(tmpDO.getPatient(),new Integer[]{1,2,4});
                List<String> relationCodes  = list.stream().map(DoctorPatientTmpDO::getRelationCode).collect(Collectors.toList());
                deleteRecord2List(relationCodes);
            }
        }

    }

    /**
     * 更新状态
     * @param relationCode
     */
    public void updateStatus2(Integer status,String[] relationCode) {
        List<DoctorPatientTmpDO> list = new ArrayList<>();
        List<String>relationCodes = new ArrayList<>();
        for (String id : relationCode) {
            DoctorPatientTmpDO tmpDO = doctorPatientTmpDao.findByRelationCode(id);
            if (tmpDO != null) {
                tmpDO.setStatus(status);
                list.add(tmpDO);
                if (status==1){//入学后 将该幼儿咨询、上门、课程数据失效
                    List<DoctorPatientTmpDO> listTmp = doctorPatientTmpDao.queryByPatientAndTypeIn(tmpDO.getPatient(),new Integer[]{1,2,4});
                    List<String> relationCodeTmp  = list.stream().map(DoctorPatientTmpDO::getRelationCode).collect(Collectors.toList());
                    relationCodes.addAll(relationCodeTmp);
                }
            }
        }
        doctorPatientTmpDao.save(list);
        deleteRecord2List(relationCodes);
    }

    /**
     * 删除记录 单条
     * @param relationCode
     */
    public void deleteRecord(String relationCode){
        DoctorPatientTmpDO tmpDO = doctorPatientTmpDao.findByRelationCode(relationCode);
        if (tmpDO!=null){
            tmpDO.setDel(0);
            doctorPatientTmpDao.save(tmpDO);
        }
        doctorPatientTmpDao.save(tmpDO);
    }

    /**
     * 删除记录
     * @param relationCode
     */
    public void deleteRecord2(String[] relationCode){
        List<DoctorPatientTmpDO> list = new ArrayList<>();
        for (String id : relationCode) {
            DoctorPatientTmpDO tmpDO = doctorPatientTmpDao.findByRelationCode(id);
            if (tmpDO!=null){
                tmpDO.setDel(0);
            }

        }
        doctorPatientTmpDao.save(list);
    }

    /**
     * 删除记录
     * @param relationCode
     */
    public void deleteRecord2List(List<String> relationCode){
        List<DoctorPatientTmpDO> list = new ArrayList<>();
        for (String id : relationCode) {
            DoctorPatientTmpDO tmpDO = doctorPatientTmpDao.findByRelationCode(id);
            if (tmpDO!=null){
                tmpDO.setDel(0);
            }

        }
        doctorPatientTmpDao.save(list);
    }

}
