using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Repository.MySql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{
    public class DicMapDeptDoctorRepository : BaseRepositoryToASHermed_SinoInterface<Dic_Map_Dept_Doctor>, IDicMapDeptDoctorRepository
    {
        public List<DoctorBelongTo> GetDocHosIds(int doctorId)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@DocId", doctorId);
            var selectSql = @"select APT_HOS_ID,APT_DEPT_ID from dic_map_dept_doctor where APT_DOCTOR_ID = @DocId and is_deleted=0 group by APT_HOS_ID,APT_DEPT_ID";
            var result = Query<DoctorBelongTo>(selectSql, parms);
            return result?.ToList();

        }

        public List<int> GetDocIdsForDeptIds(List<int> deptids, int apttype)
        {
            var sql = string.Empty;
            if (apttype == 1)
            {
                sql = @"SELECT
	                            a.DocId 
                            FROM
	                            dic_video_dept_docs a
	                            INNER JOIN dic_dept b ON a.DeptId = b.ID 
                            WHERE
	                            a.DeptId IN @Ids";
            }
            else if (apttype == 2)
            {
                sql = @"SELECT
	                            a.DocId 
                            FROM
	                            dic_revisit_dept_docs a
	                            INNER JOIN dic_dept b ON a.DeptId = b.ID 
                            WHERE
	                            a.DeptId IN @Ids";
            }
            return Query<int>(sql, new { Ids = deptids }).ToList();
        }

        public Dic_Map_Dept_Doctor FindByHisHosDeptDoctorCode(string hisHosCode, string hisDeptCode, string hisDoctorCode, int useOccastion)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@hisDeptCode", hisDeptCode);
            parms.Add("@hisHosCode", hisHosCode);
            parms.Add("@hisDoctorCode", hisDoctorCode);
            parms.Add("@useOccasion", useOccastion);
            var selectSql = @"select a.*
                                        from dic_map_dept_doctor a,
                                             dic_hos b,
                                             dic_map_his_dept c,
                                             dic_map_his_doctor d
                                        where a.IS_DELETED = false
                                          and b.IS_DELETED = false
                                          and c.IS_DELETED = false
                                          and d.IS_DELETED = false
                                          and a.APT_DEPT_ID = c.APT_DEPT_ID
                                          and c.HIS_DEPT_CODE = @hisDeptCode
                                          and a.APT_HOS_ID = b.ID
                                          and b.HIS_HOS_CODE = @hisHosCode
                                          and a.APT_DOCTOR_ID = d.APT_DOCTOR_ID
                                          and d.HIS_DOCTOR_CODE = @hisDoctorCode
                                          and a.USE_OCCASION = @useOccasion
                                        limit 0,1";
            var result = Query<Dic_Map_Dept_Doctor>(selectSql, parms);
            return result?.FirstOrDefault();
        }


    }
}
