using PoJun.Dapper;
using ASHermed.SinoInterface.Api.IRepository.Test;
using ASHermed.SinoInterface.Entity.Test;
using ASHermed.SinoInterface.Repository.MySql;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{

    public class PubMapUserPatRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Map_User_Pat>, IPubMapUserPatRepository
    {

        public int InsertPubMapUserPat(Pub_Map_User_Pat pubMapUserPat)
        {
            StringBuilder str = new StringBuilder();
            str.Append("insert into Pub_Map_User_Pat ");
            str.Append(@"(USER_ID,
                          PAT_ID,
                          IS_DELETED,
                          RELATION_SHIP,
                          Ties,
                          CREATE_TIME) ");
            str.Append(" values ");
            str.Append(@"(@USER_ID,
                            @PAT_ID,
                            @IS_DELETED,
                            @RELATION_SHIP,
                            @Ties,
                            @CREATE_TIME); ");

            var parms = new Dictionary<string, object>();
            parms.Add("@USER_ID", pubMapUserPat.User_ID);
            parms.Add("@PAT_ID", pubMapUserPat.Pat_ID);
            parms.Add("@IS_DELETED", pubMapUserPat.Is_Deleted);
            parms.Add("@RELATION_SHIP", pubMapUserPat.Relation_Ship);
            parms.Add("@Ties", pubMapUserPat.Ties);
            parms.Add("@CREATE_TIME", pubMapUserPat.Create_Time);
            //parms.Add("@UPDATE_TIME", pubMapUserPat.Update_Time);
            var result = Execute(str.ToString(), parms);
            return result;
        }

        public int UpdatePubMapUserPat(Pub_Map_User_Pat pubMapUserPat)
        {
            StringBuilder str = new StringBuilder();
            str.Append("update Pub_Map_User_Pat set ");
            str.Append(@"USER_ID=@USER_ID,
                            PAT_ID=@PAT_ID,
                            IS_DELETED=@IS_DELETED,
                            RELATION_SHIP=@RELATION_SHIP,
                            Ties=@Ties,
                            UPDATE_TIME=@UPDATE_TIME ");
            str.Append(@" where ID=@ID");
            var parms = new Dictionary<string, object>();
            parms.Add("@USER_ID", pubMapUserPat.User_ID);
            parms.Add("@PAT_ID", pubMapUserPat.Pat_ID);
            parms.Add("@IS_DELETED", pubMapUserPat.Is_Deleted);
            parms.Add("@RELATION_SHIP", pubMapUserPat.Relation_Ship);
            parms.Add("@Ties",pubMapUserPat.Ties);
            //parms.Add("@CREATE_TIME", pubMapUserPat.Create_Time);
            parms.Add("@UPDATE_TIME", DateTime.Now);
            parms.Add("@ID", pubMapUserPat.Id);
            var result = Execute(str.ToString(), parms);
            return result;
        }

        public Pub_Map_User_Pat GetPubUserPat(int userId, int patId, string type = "sino")
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"select up.* from Pub_Map_User_Pat up left join pub_user_info u on up.USER_ID=u.ID
                            where up.PAT_ID = @PatId and up.IS_DELETED = false ");
            if (userId != 0)
            {
                str.Append(@" and up.USER_ID = @UserId");
                parms.Add("@UserId", userId);
            }
            parms.Add("@PatId", patId);
            if (type == null || type == "sino")
            {
                str.Append(@" and u.REGISTRATION_SOURCE < @SOURCE");
                parms.Add("@SOURCE", 4);
            }
            if (type == "cigna") 
            {
                str.Append(@" and u.REGISTRATION_SOURCE = @SOURCE");
                parms.Add("@SOURCE", 4);
            }
            var result = Query<Pub_Map_User_Pat>(str.ToString(), parms);
            return result?.FirstOrDefault();
        }
        public Pub_Map_User_Pat GetUserPat(int userId, int patId)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"select up.* from Pub_Map_User_Pat up left join pub_user_info u on up.USER_ID=u.ID
                            where up.PAT_ID = @PatId and up.IS_DELETED = false ");
            if (userId != 0)
            {
                str.Append(@" and up.USER_ID = @UserId");
                parms.Add("@UserId", userId);
            }
            parms.Add("@PatId", patId);
            
            var result = Query<Pub_Map_User_Pat>(str.ToString(), parms);
            return result?.FirstOrDefault();
        }
        public List<Pub_Map_User_Pat> GetUserPats(int userId)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"select * from Pub_Map_User_Pat
                            where and USER_ID = @UserId and IS_DELETED = false");
            parms.Add("@UserId", userId);
            var result = Query<Pub_Map_User_Pat>(str.ToString(), parms);
            return result?.ToList();
        }

        public List<Pub_Map_User_Pat> SelectByPatId(int id, string type = "sino")
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"select up.*
                            from pub_map_user_pat up left join pub_user_info u on up.USER_ID=u.ID
                            where up.PAT_ID = @PAT_ID
                            and up.IS_DELETED = false");
            parms.Add("@PAT_ID", id);
            if (type == null || type == "sino")
            {
                str.Append(@" and u.REGISTRATION_SOURCE < @SOURCE");
                parms.Add("@SOURCE", 4);
            }
            if (type == "cigna")
            {
                str.Append(@" and u.REGISTRATION_SOURCE = @SOURCE");
                parms.Add("@SOURCE", 4);
            }
            var result = Query<Pub_Map_User_Pat>(str.ToString(), parms);
            return result?.ToList();
        }

        /// <summary>
        /// 根据患者Id查询
        /// </summary>
        /// <param name="patId"></param>
        /// <returns></returns>
        public Pub_Map_User_Pat GetByPatId(int patId)
        {
            const string sql = "select * from pub_map_user_pat where IS_DELETED=false and PAT_ID=@patId and (Relation_Ship=1 or Relation_Ship=2) ";

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

            var result = Query<Pub_Map_User_Pat>(sql, parms); 

            return result?.FirstOrDefault();
        }

        /// <summary>
        /// 查询患者的所有绑定关系
        /// </summary>
        /// <param name="patId"></param>
        /// <returns></returns>
        public List<Pub_Map_User_Pat> GetBind(int patId)
        {
            const string sql = "select * from pub_map_user_pat where IS_DELETED=false and PAT_ID=@patId ";

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

            var result = Query<Pub_Map_User_Pat>(sql, parms);

            return result?.ToList();
        }

        public async Task<Pub_Map_User_Pat> GetByUserIdAndPatIdAsync(int userId,int patId)
        {
            const string sql = "select * from pub_map_user_pat where IS_DELETED=false and USER_ID=@userId and PAT_ID=@patId ";
            var parms = new Dictionary<string, object>
            {
                { "@userId", userId },
                { "@patId", patId }
            };
            var result = await QueryAsync<Pub_Map_User_Pat>(sql, parms);
            return result.FirstOrDefault();
        }

        public Pub_Map_User_Pat GetByUserIdOneself(int userId)
        {
            var sql = @" select * from pub_map_user_pat where IS_DELETED=false and USER_ID=@UserId And Ties = 1 ";
            return Query<Pub_Map_User_Pat>(sql, new { UserId = userId }).FirstOrDefault();
        }




    }


}
