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 PubMessageInfoRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Message_Info>, IPubMessageInfoRepository
    {
        public int InsertPubMessageInfo(Pub_Message_Info info)
        {
            var parms = new Dictionary<string, object>();
            var selectSql = @"insert into pub_message_info (USER_ID,
                                                            Pat_Id,
	                                                        SOURCE_TYPE,
	                                                        MESSAGE_TYPE,
	                                                        CONTENT,
	                                                        CREATE_TIME,
	                                                        STATUS,
	                                                        CONTENT_TYPE,
	                                                        SERVICE_ID,
	                                                        IS_HELLO,
	                                                        USER_READ,
	                                                        DOCTOR_READ,
	                                                        BUSINESS,
	                                                        BUSINESS_IMG,
	                                                        REMAKE,
	                                                        IS_CUSTOMIZE
                                                ) values (@USER_ID,
                                                            @Pat_Id,
	                                                        @SOURCE_TYPE,
	                                                        @MESSAGE_TYPE,
	                                                        @CONTENT,
	                                                        @CREATE_TIME,
	                                                        @STATUS,
	                                                        @CONTENT_TYPE,
	                                                        @SERVICE_ID,
	                                                        @IS_HELLO,
	                                                        @USER_READ,
	                                                        @DOCTOR_READ,
	                                                        @BUSINESS,
	                                                        @BUSINESS_IMG,
	                                                        @REMAKE,
	                                                        @IS_CUSTOMIZE);select @@identity; ";
            parms.Add("@USER_ID", info.User_Id);
            parms.Add("@Pat_Id", info.Pat_Id);
            parms.Add("@SOURCE_TYPE", info.Source_Type);
            parms.Add("@MESSAGE_TYPE", info.Message_Type);
            parms.Add("@CONTENT", info.Content);
            parms.Add("@CREATE_TIME", info.Create_Time);
            parms.Add("@STATUS", info.Status);
            parms.Add("@CONTENT_TYPE", info.Content_Type);
            parms.Add("@SERVICE_ID", info.Service_Id);
            parms.Add("@IS_HELLO", info.Is_Hello);
            parms.Add("@USER_READ", info.User_Read);
            parms.Add("@DOCTOR_READ", info.Doctor_Read);
            parms.Add("@BUSINESS", info.Business);
            parms.Add("@BUSINESS_IMG", info.Business_Img);
            parms.Add("@REMAKE", info.Remake);
            parms.Add("@IS_CUSTOMIZE", info.Is_Customize);
            var result = Execute(selectSql, parms);
            return result;
        }
        //小易用的
        public int SelectUserUnRead(int userId)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"SELECT COUNT(1)
                            FROM PUB_MESSAGE_INFO
                            WHERE USER_ID = @USER_ID
                              AND USER_READ = 0
                              AND `STATUS` != 1");
            parms.Add("@USER_ID", userId);

            var result = Query<int>(str.ToString(), parms);
            return result.FirstOrDefault();
        }

        public int SelectDoctorUnRead(int doctorId)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@" SELECT COUNT(1) FROM PUB_MESSAGE_INFO
                                WHERE SERVICE_ID = @SERVICE_ID
                                  AND DOCTOR_READ = 0
                                    AND `STATUS` != 1");
            parms.Add("@SERVICE_ID", doctorId);

            var result = Query<int>(str.ToString(), parms);
            return result.FirstOrDefault();
        }

        public List<AdvisoryMessage> SelecrMessageInfo(int userId, string doctorId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" SELECT *
        FROM (
                 SELECT M.*,
                        U.NICK_NAME AS USERNAME,
                        U.HEADIMG   AS USERHEADIMG,
                        D.NICKNAME AS DOCTORNAME,
                        D.UserImg   AS DOCTORHEADIMG
                 FROM pub_message_info M
                          LEFT JOIN pub_user_info U on M.USER_ID = U.ID
                          LEFT JOIN sys_user D ON M.SERVICE_ID = D.ID
                 WHERE USER_ID = @userId
                   and SERVICE_ID = @doctorId
                 ORDER BY CREATE_TIME DESC
             ) M");
            var parms = new Dictionary<string, object>();
            parms.Add("@userId", userId);
            parms.Add("@doctorId", doctorId);
            var result = Query<AdvisoryMessage>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public int UpdateRead(int userId, string doctorId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" UPDATE pub_message_info
                                    SET USER_READ= TRUE,
                                        DOCTOR_READ= TRUE
                                    WHERE USER_ID = @userId
                                      and SERVICE_ID = @doctorId");
            var parms = new Dictionary<string, object>();
            parms.Add("@userId", userId);
            parms.Add("@doctorId", doctorId);
            var result = Execute(selectSql.ToString(), parms);
            return result;
        }
        //这个是云诊室列表红点用的
        public List<Pub_Message_Info> GetPatUnReadMsg(int userId, int docId = 0)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"SELECT * FROM PUB_MESSAGE_INFO
                            WHERE USER_ID = @USER_ID
                              AND USER_READ = 0
                              AND `STATUS` != 1 and CONTENT_TYPE = 6 ");
            parms.Add("@USER_ID", userId);
            if (docId > 0)
            {
                str.Append(@" and SERVICE_ID = @SERVICE_ID");
                parms.Add("@SERVICE_ID", docId);
            }
            var result = Query<Pub_Message_Info>(str.ToString(), parms);
            return result.ToList();
        }

        public int UpdateMsgRead(int patId, int docId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" UPDATE pub_message_info
                                    SET USER_READ= TRUE,
                                        `STATUS`= 2
                                    WHERE PAT_ID = @PAT_ID
                                      and SERVICE_ID = @doctorId");
            var parms = new Dictionary<string, object>();
            parms.Add("@PAT_ID", patId);
            parms.Add("@doctorId", docId);
            var result = Execute(selectSql.ToString(), parms);
            return result;
        }







    }


}
