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 PubVerifyCodeRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Verify_Code_Log>, IPubVerifyCodeRepository
    {
        public int InsertPubVerifyCode(Pub_Verify_Code_Log pubVerifyCodeLog)
        {
            StringBuilder str = new StringBuilder();
            str.Append("insert into Pub_Verify_Code_Log ");
            str.Append(@"(ACCOUNT_CODE,CODE,CREATE_TIME,IP_ADDRESS) ");
            str.Append(" values ");
            str.Append(@"(@ACCOUNT_CODE,@CODE,@CREATE_TIME,@IP_ADDRESS); ");
            str.Append("select @@identity; ");
            var parms = new Dictionary<string, object>();
            parms.Add("@ACCOUNT_CODE", pubVerifyCodeLog.Account_Code);
            parms.Add("@CODE", pubVerifyCodeLog.Code);
            parms.Add("@CREATE_TIME", pubVerifyCodeLog.Create_Time);
            parms.Add("@IP_ADDRESS", pubVerifyCodeLog.IP_Address);
            var result = Execute(str.ToString(), parms);
            return result;
        }

        public PubVerifyCodeLogModel CheckCode(string acc, string code)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@acc", acc);
            parms.Add("@code", code);
            var selectSql = @" select ID, ACCOUNT_CODE, `CODE`, CREATE_TIME, now() as CUR_TIME
                                from pub_verify_code_log
                                where ACCOUNT_CODE = @acc
                                  and CODE = @code
                                order by CREATE_TIME desc
                                limit 1";
            var result = Query<PubVerifyCodeLogModel>(selectSql, parms);

            return result?.FirstOrDefault();
        }

        public List<Pub_Verify_Code_Log> FindByIp(string ip)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@ip", ip);
            var selectSql = @" select ID, ACCOUNT_CODE, `CODE`, CREATE_TIME
                                    from pub_verify_code_log
                                    where TO_DAYS(CREATE_TIME) = TO_DAYS(NOW())
                                      and IP_ADDRESS = @ip
                                    order by CREATE_TIME desc";
            var result = Query<Pub_Verify_Code_Log>(selectSql, parms);
            return result?.ToList();
        }

        public List<Pub_Verify_Code_Log> FindByAcc(string acc)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@acc", acc);
            var selectSql = @"select ID, ACCOUNT_CODE, `CODE`, CREATE_TIME
                                from pub_verify_code_log
                                where ACCOUNT_CODE = @acc
                                order by CREATE_TIME desc";
            var result = Query<Pub_Verify_Code_Log>(selectSql, parms);
            return result?.ToList();
        }

    }
}
