package com.yihu.jw.hospital.team.service;

import com.tencentcloudapi.tci.v20190318.models.Person;
import com.yihu.jw.area.dao.BaseTownDao;
import com.yihu.jw.entity.base.area.BaseTownDO;
import com.yihu.jw.entity.hospital.DmHospitalDO;
import com.yihu.jw.entity.hospital.team.WlyyAdminTeamDO;
import com.yihu.jw.entity.hospital.team.WlyyAdminTeamMemberDO;
import com.yihu.jw.hospital.HospitalDao;
import com.yihu.jw.hospital.team.dao.WlyyAdminTeamDao;
import com.yihu.jw.hospital.team.dao.WlyyAdminTeamMemberDao;
import com.yihu.jw.hospital.team.dao.WlyySignFamilyDao;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WlyyAdminTeamService {
    @Autowired
    private WlyyAdminTeamDao wlyyAdminTeamDao;
    @Autowired
    private WlyyAdminTeamMemberDao wlyyAdminTeamMemberDao;
    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private WlyySignFamilyDao wlyySignFamilyDao;
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private BaseTownDao baseTownDao;
    //查询机构
    public MixEnvelop findHospitalByTown(String townCode,String name,Integer page,Integer pageSize){
        PageRequest pageRequest = new PageRequest(page-1,pageSize);
        MixEnvelop mixEnvelop = new MixEnvelop();
        List<DmHospitalDO> list =null;
        if (StringUtils.isNoneBlank(name)){
            list=hospitalDao.findByTownCodeAndName(townCode,name,pageRequest);
        }else {
            list = hospitalDao.findByTownCode(townCode,pageRequest);
        }
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setTotalCount(list==null?0:list.size());
        return mixEnvelop;
    }
    //查询团队
    public MixEnvelop findTeamByOrg(String cityCode,String townCode,String orgCode,Integer page,Integer pageSize){
        String sql ="select t.id as \"id\"," +
                "t.name as \"name\"," +
                "t.create_time as \"createTime\"," +
                "t.leader_code as \"leaderCode\"," +
                "t.leader_name as \"leaderName\"," +
                "t.org_code as \"orgCode\"," +
                "t.member_count as \"memberCount\"," +
                "b.name as \"orgName\"," +
                "t.accept_type as \"acceptType\"," +
                "t.special_code as \"specialCode\"," +
                "t.special_name as \"specialName\"," +
                "t.available as \"available\"" +
                " from wlyy_admin_team t inner join dm_hospital b on t.org_code = b.code where t.available=1";
        PageRequest pageRequest = new PageRequest(page-1,pageSize);
        MixEnvelop mixEnvelop = new MixEnvelop();
        if (StringUtils.isNoneBlank(townCode)){
           sql+=" and b.town ='"+townCode+"'";
        }else {
            sql+=" and b.city ='"+cityCode+"'";
        }
        if (StringUtils.isNoneBlank(orgCode)){
            sql+=" and t.org_code ='"+orgCode+"'";
        }
        List<Map<String ,Object>>list= hibenateUtils.createSQLQuery(sql,page,pageSize);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setTotalCount(list==null?0:list.size());
        return mixEnvelop;
    }

    //查询团队下的医生
    public MixEnvelop findDoctorByTeam(String teamCode,Integer page,Integer pageSize){
        PageRequest pageRequest = new PageRequest(page-1,pageSize);
        MixEnvelop mixEnvelop = new MixEnvelop();
        String orgName="";
        List<WlyyAdminTeamMemberDO> list = wlyyAdminTeamMemberDao.findByTeamId(Long.parseLong(teamCode),pageRequest);
        WlyyAdminTeamDO wlyyAdminTeamDO1 =wlyyAdminTeamDao.findOne(Integer.parseInt(teamCode));
        if (wlyyAdminTeamDO1!=null){
            DmHospitalDO dmHospitalDO = hospitalDao.findByCode(wlyyAdminTeamDO1.getOrgCode());
            if (dmHospitalDO!=null){
                orgName=dmHospitalDO.getName();
            }
        }

        for (WlyyAdminTeamMemberDO wlyyAdminTeamMemberDO:list){
            /*Integer count = wlyySignFamilyDao.getCountByDoctorCode(wlyyAdminTeamMemberDO.getDoctorCode());
            wlyyAdminTeamMemberDO.setSignNum(count==null?0:count);*/
            WlyyAdminTeamDO wlyyAdminTeamDO = wlyyAdminTeamDao.findOne(wlyyAdminTeamMemberDO.getTeamId().intValue());
            if (wlyyAdminTeamDO != null) {
                if (wlyyAdminTeamDO.getLeaderCode().equalsIgnoreCase(wlyyAdminTeamMemberDO.getDoctorCode())) {
                    wlyyAdminTeamMemberDO.setTeamLeader(true);
                    wlyyAdminTeamMemberDO.setTeamLeaderOrder(1);
                }else {
                    wlyyAdminTeamMemberDO.setTeamLeaderOrder(0);
                }
            }else {
                wlyyAdminTeamMemberDO.setTeamLeaderOrder(0);
            }
            wlyyAdminTeamMemberDO.setOrgName(orgName);
        }
        list=list.stream().sorted(Comparator.comparing(WlyyAdminTeamMemberDO::getTeamLeaderOrder).reversed()).collect(Collectors.toList());
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setTotalCount(list==null?0:list.size());
        return mixEnvelop;
    }
    public List<BaseTownDO> findTownByCity(String city){
       return baseTownDao.findByCityCode(city);
    }

    public WlyyAdminTeamMemberDO findOneTeamMemberById(Integer id){
        return wlyyAdminTeamMemberDao.findOne(id);
    }
}
