using ASHermed.SinoInterface.Api.ContractModel;
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;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{
    public class SanFangRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Message_Qimo>, ISanFangRepository
    {
        public int InsertPubMessageQimo(Pub_Message_Qimo ent)
        {
            var parms = new Dictionary<string, object>();
            var selectSql = @"insert into pub_message_qimo (UserId,IsRead,CreateTime,Content) values (@UserId ,@IsRead,@CreateTime,@Content)";
            parms.Add("@UserId", ent.UserId);
            parms.Add("@IsRead", ent.IsRead);
            parms.Add("@CreateTime", ent.CreateTime);
            parms.Add("@Content", ent.Content);
            var result = Insert(selectSql, parms);
            return result;
        }

        public int ReadQimoMessage(int userId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" UPDATE pub_message_qimo
                                    SET IsRead= TRUE
                                    WHERE UserId = @UserId");
            var parms = new Dictionary<string, object>();
            parms.Add("@UserId", userId);
            var result = Execute(selectSql.ToString(), parms);
            return result;
        }

        public int GetQimoMessage(int userId)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select count(1) from pub_message_qimo where UserId = @UserId and IsRead=false");
            parms.Add("@UserId", userId);
            var result = Query<int>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }

        public Lock_Usrn GetLockUsrn(int patId, string aptUsrn)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select * from Lock_Usrn where AptUsrn =@AptUsrn and IsUse = 0 and patId =@patId ");
            parms.Add("@AptUsrn", aptUsrn);
            parms.Add("@patId", patId);
            var result = Query<Lock_Usrn>(selectSql.ToString(), parms);
            return result?.LastOrDefault();
        }

        public List<Lock_Usrn> GetPatLockUsrn(int patId)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select * from Lock_Usrn where IsUse = 0 and patId =@patId ");
            parms.Add("@patId", patId);
            var result = Query<Lock_Usrn>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public int InsertLockUsrn(Lock_Usrn ent)
        {
            var parms = new Dictionary<string, object>();
            var selectSql = @"insert into Lock_Usrn (UserId,PatId,AptUsrn,CreateTime,UsrnStatus,Record,IsUse,Remark) values (@UserId ,@PatId,@AptUsrn,@CreateTime,@UsrnStatus,@Record,@IsUse,@Remark)";
            parms.Add("@UserId", ent.UserId);
            parms.Add("@PatId", ent.PatId);
            parms.Add("@AptUsrn", ent.AptUsrn);
            parms.Add("@CreateTime", ent.CreateTime);
            parms.Add("@UsrnStatus", ent.UsrnStatus);
            parms.Add("@Record", ent.Record);
            parms.Add("@IsUse", ent.IsUse);
            parms.Add("@Remark", ent.Remark);
            var result = Insert(selectSql, parms);
            return result;
        }

        public int UpdateLockUsrn(Lock_Usrn ent)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" UPDATE Lock_Usrn
                                    SET UserId= @UserId,
                                        PatId=@PatId,
                                        AptUsrn=@AptUsrn,
                                        Record=@Record,
                                        IsUse=@IsUse,
                                        Remark=@Remark,
                                        UsrnStatus=@UsrnStatus
                                    WHERE Id = @Id");
            var parms = new Dictionary<string, object>();
            parms.Add("@UserId", ent.UserId);
            parms.Add("@PatId", ent.PatId);
            parms.Add("@AptUsrn", ent.AptUsrn);
            parms.Add("@Record", ent.Record);
            parms.Add("@UsrnStatus", ent.UsrnStatus);
            parms.Add("@IsUse", ent.IsUse);
            parms.Add("@Remark", ent.Remark);
            parms.Add("@Id", ent.Id);
            var result = Execute(selectSql.ToString(), parms);
            return result;
        }

        public async Task<Lock_Usrn> GetLockRecord(int userId)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select * from Lock_Usrn where userId =@userId and IsUse = 0 and UsrnStatus='L'");
            parms.Add("@userId", userId);
            var result = QueryAsync<Lock_Usrn>(selectSql.ToString(), parms);
            return result.Result?.LastOrDefault();
        }

        public async Task<bool> UserAbandonApt(int userId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" UPDATE Lock_Usrn
                                    SET UsrnStatus='U'
                                    WHERE UserId= @UserId  and IsUse = 0 and UsrnStatus='L'");
            var parms = new Dictionary<string, object>();
            parms.Add("@UserId", userId);
            var result = ExecuteAsync(selectSql.ToString(), parms);
            return result.Result > 0;
        }

        public List<Lock_Usrn> GetAbandonApt(int userId)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select * from Lock_Usrn where UserId= @UserId  and IsUse = 0 and UsrnStatus='L'");
            parms.Add("@userId", userId);
            var result = Query<Lock_Usrn>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public int UpdateLockNoUseUsrnStatus(string remark)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@"update Lock_Usrn set UsrnStatus = 'U',Remark = @Remark where  IsUse = 0 and UsrnStatus='L' and now() > date_add(CreateTime,interval 5 minute)");
            var parms = new Dictionary<string, object>();
            parms.Add("@Remark", remark);
            var result = Execute(selectSql.ToString(), parms);
            return result;
        }
        public int UpdateLockNoUseUsrnStatusById(int id, string remark)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@"update Lock_Usrn set UsrnStatus = 'U',Remark = @Remark where ID=@ID");
            var parms = new Dictionary<string, object>();
            parms.Add("@Remark", remark);
            parms.Add("@ID", id);
            var result = Execute(selectSql.ToString(), parms);
            return result;
        }

        public int UnLockUsrn(int patId, string usrn, string remark)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@"update Lock_Usrn set UsrnStatus = 'U',Remark = @Remark where  IsUse = 0 and patId=@patId and AptUsrn=@AptUsrn");
            var parms = new Dictionary<string, object>();
            parms.Add("@Remark", remark);
            parms.Add("@patId", patId);
            parms.Add("@AptUsrn", usrn);
            var result = Execute(selectSql.ToString(), parms);
            return result;
        }

        public List<Lock_Usrn> GetLockNoUseUsrnStatus()
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select * from Lock_Usrn where  IsUse = 0 and UsrnStatus='L' and now() > date_add(CreateTime,interval 5 minute) ");
            var result = Query<Lock_Usrn>(selectSql.ToString(), parms);
            return result?.ToList();
        }


        public async Task<int> InsertUserFeedback(User_Feedback ent)
        {
            var parms = new Dictionary<string, object>();
            var selectSql = @"insert into user_feedback (Content,UserId,ContactWay,CreateTime) values (@Content ,@UserId,@ContactWay,@CreateTime)";
            parms.Add("@Content", ent.Content);
            parms.Add("@UserId", ent.UserId);
            parms.Add("@ContactWay", ent.ContactWay);
            parms.Add("@CreateTime", ent.CreateTime);
            var result = await InsertAsync(selectSql, parms);
            return result;
        }
    }
}
