using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ASHermed.SinoInterface.Api.ContractModel.Quest;
using ASHermed.SinoInterface.Api.IRepository.Quest;
using ASHermed.SinoInterface.Repository.MySql;

namespace ASHermed.SinoInterface.Api.MySqlRepository.Quest
{
    public class PubQuestDataRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Quest_Data>, IPubQuestDataRepository
    {
        /// <summary>
        /// 新增返回id
        /// </summary>
        /// <param name="questPatInfo"></param>
        /// <returns></returns>
        public async Task<int> InsertAndReturnIdAsync(Pub_Quest_Data questPatInfo)
        {
            const string insertSql = @"INSERT INTO pub_quest_data ( PatId, PID, PatBirthday, PatName, PatGender, PatAge, PatPhone, Language, CreateTime, InfoId, UpdateTime, UpdateUser, Version, CreateUser, UserId, AptId, FilePath, IsPass)
                                       VALUES
                                       	(
                                       		@PatId,
                                       		@PID,
                                       		@PatBirthday,
                                       		@PatName,
                                       		@PatGender,
                                       		@PatAge,
                                       		@PatPhone,
                                            @Language, @CreateTime, @InfoId, @UpdateTime, @UpdateUser, @Version, @CreateUser, @UserId, @AptId, @FilePath, @IsPass
                                       	)";

            var insertResult = await InsertAsync(insertSql, questPatInfo);

            return insertResult;
        }

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Pub_Quest_Data> GetByIdAsync(int id)
        {
            const string sql = "select * from pub_quest_data where Id=@id ";

            var parms = new Dictionary<string, object>
            {
                {"@id", id}
            };

            var result = await QueryAsync<Pub_Quest_Data>(sql, parms);

            return result.FirstOrDefault();
        }

        public async Task<List<Pub_Quest_Data>> GetByUserIdAsync(int? userid, int patId, int infoId, string pid)
        {
            string sql = "select * from pub_quest_data where  InfoId = @InfoId ";
            if (userid > 0)
            {
                sql += " And UserId=@UserId ";
            }
            if (patId > 0)
            {
                if (!string.IsNullOrEmpty(pid))
                {
                    sql += " And (PatId=@PatId OR  PID=@PID) ";
                }
                else
                {
                    sql += " And PatId=@PatId ";
                }
            }
            if (!string.IsNullOrEmpty(pid))
            {
                if (patId > 0)
                {
                    sql += " And (PatId=@PatId OR  PID=@PID) ";
                }
                else
                {
                    sql += " And PID=@PID ";
                }
            }
            var result = await QueryAsync<Pub_Quest_Data>(sql, new { UserId = userid, InfoId = infoId, PatId = patId, PID = pid });
            return result.ToList();
        }

        public async Task<List<Pub_Quest_Data>> GetByPatId3Async(int patId, int infoId)
        {
            string sql = "select * from pub_quest_data where  InfoId = @InfoId  And PatId=@PatId order by CreateTime desc ";
            var result = await QueryAsync<Pub_Quest_Data>(sql, new { InfoId = infoId, PatId = patId });
            return result.ToList();
        }

        public async Task<List<Pub_Quest_Data>> GetByAptId(List<int> aptIds)
        {
            var sql = @" select * from pub_quest_data where AptId IN @AptId ";
            var result = await QueryAsync<Pub_Quest_Data>(sql, new { AptId = aptIds });
            return result.ToList();
        }

        public List<Pub_Quest_Data> GetPatQuest(int patId)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" select * from pub_quest_data q INNER JOIN out_apt_info a on q.AptId = a.ID where a.APT_TIME > now() and PAT_ID = @patId ");
            return Query<Pub_Quest_Data>(sql.ToString(), new { patId = patId })?.ToList();
        }

        public List<Pub_Quest_Data> GetCurDayQuestData(int patId, int infoId)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" select * from pub_quest_data  where InfoId =@InfoId and DATE(createTime) = DATE(now()) and patId = @patId order by createTime desc");
            return Query<Pub_Quest_Data>(sql.ToString(), new { patId = patId, InfoId = infoId })?.ToList();
        }

        public List<Pub_Quest_Data> GetPhysicalQuestData(int aptId, int infoId)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" select * from pub_quest_data  where InfoId =@InfoId and aptId = @aptId order by createTime desc");
            return Query<Pub_Quest_Data>(sql.ToString(), new { aptId = aptId, InfoId = infoId })?.ToList();
        }

        public Pub_Quest_Data GetAptDayQuestData(int patId, int infoId, string aptCreateTime)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" select * from pub_quest_data  where InfoId =@InfoId and DATE(createTime) >= @aptCreateTime and patId = @patId order by createTime desc");
            return Query<Pub_Quest_Data>(sql.ToString(), new { patId = patId, InfoId = infoId, aptCreateTime = aptCreateTime })?.FirstOrDefault();
        }

        public List<Pub_Quest_Data> GetPatsQuest(List<int> patIds)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" select * from pub_quest_data q INNER JOIN out_apt_info a on q.AptId = a.ID where a.APT_TIME > now() and PAT_ID in @patId ");
            return Query<Pub_Quest_Data>(sql.ToString(), new { patId = patIds })?.ToList();
        }

        /// <summary>
        /// 获取患者流调
        /// </summary>
        /// <param name="patIds"></param>
        /// <returns></returns>
        public List<Pub_Quest_Data> GetPatsFluQuest(List<int> patIds)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" select * from pub_quest_data  where infoId=3 and PATID in @patId and DATE(createTime) = DATE(now())");
            return Query<Pub_Quest_Data>(sql.ToString(), new { patId = patIds })?.ToList();
        }

        public List<Pub_Quest_Data> GetPatQuestData(int patid, int infoId, DateTime aptTime)
        {
            var sql = @" select * from pub_quest_data  where infoId=@InfoId and PATID = @PatId Order By CreateTime Desc ";
            return Query<Pub_Quest_Data>(sql, new { InfoId = infoId, PatId = patid }).ToList();
        }
    }
}
