package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.entity.*;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.BasDoctorInfoServiceImpl;
import com.diagbot.util.ListUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.data.BasDoctorInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Description:
 * @author: cy
 * @time: 2020/12/24 14:38
 */
@Component
public class BasDoctorInfoFacade extends BasDoctorInfoServiceImpl {
    @Autowired
    SysUserDeptFacade sysUserDeptFacade;
    @Autowired
    BasDeptInfoFacade basDeptInfoFacade;
    @Autowired
    BasDoctorInfoFacade basDoctorInfoFacade;
    @Autowired
    BehospitalInfoFacade behospitalInfoFacade;


    /**
     * 获取医院医生下拉列表信息
     *
     * @param basDoctorInfoVO
     * @return 医院医生下拉列表信息
     */
    public List<BasDoctorInfo> getDoctorByDept(BasDoctorInfoVO basDoctorInfoVO) {
            basDoctorInfoVO.setHospitalId(Long.valueOf(SysUserUtils.getCurrentHospitalID()));
            basDoctorInfoVO.setUserId(Long.valueOf( SysUserUtils.getCurrentPrincipleID()));
            QueryWrapper<SysUserDept> userDeptWrapper = new QueryWrapper<>();
            List<BehospitalInfo> list = new ArrayList<>();
            List<BasDoctorInfo> doctorData = new ArrayList<>();
                    userDeptWrapper.eq("hospital_id",basDoctorInfoVO.getHospitalId());
            userDeptWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
            userDeptWrapper.eq("user_id",basDoctorInfoVO.getUserId());
            List<SysUserDept> UserDeptlist = sysUserDeptFacade.list(userDeptWrapper);
            Set<String> deptSet = new HashSet<String>();

        if(ListUtil.isNotEmpty(UserDeptlist)){
                UserDeptlist.forEach(sysUserDept->{
                    deptSet.add(sysUserDept.getDeptId());
                });
                QueryWrapper<BasDeptInfo> deptInfo = new QueryWrapper<>();
                deptInfo.eq("hospital_id",basDoctorInfoVO.getHospitalId());
                deptInfo.eq("is_deleted", IsDeleteEnum.N.getKey());
                deptInfo.eq("dept_name",basDoctorInfoVO.getDeptName());
                deptInfo.in("dept_id",deptSet);
                List<BasDeptInfo> listInfo = basDeptInfoFacade.list(deptInfo);
                Set<String> deptSetInfo = new HashSet<String>();
                if(ListUtil.isNotEmpty(listInfo)){
                    listInfo.forEach(basDeptInfo->{
                        deptSetInfo.add(basDeptInfo.getDeptId());
                    });
                QueryWrapper<BehospitalInfo> QueryWrapper = new QueryWrapper<>();
                QueryWrapper.eq("hospital_id",basDoctorInfoVO.getHospitalId());
                QueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
                //QueryWrapper.eq("is_placefile", basDoctorInfoVO.getIsPlacefile());
                QueryWrapper.in("beh_dept_id",deptSetInfo);

                if(StringUtils.isNotEmpty(basDoctorInfoVO.getInputStr())){
                    QueryWrapper.like("doctor_name",basDoctorInfoVO.getInputStr());
                }
                QueryWrapper.groupBy("doctor_name");
                QueryWrapper.orderByAsc("doctor_name");
                list = behospitalInfoFacade.list(QueryWrapper);
                if(ListUtil.isNotEmpty(list)){
                    list.forEach(behospitalInfo -> {
                                if(behospitalInfo!= null && StringUtils.isNotEmpty(behospitalInfo.getDoctorName())){
                                    BasDoctorInfo basDoctorInfo = new BasDoctorInfo();
                                    basDoctorInfo.setHospitalId(behospitalInfo.getHospitalId());
                                    basDoctorInfo.setDoctorId(behospitalInfo.getDoctorId());
                                    basDoctorInfo.setName(behospitalInfo.getDoctorName());
                                    doctorData.add(basDoctorInfo);
                                }
                            }
                    );
                }

            }
        }

            return doctorData;
    }
}
