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

namespace ASHermed.SinoInterface.Api.MySqlRepository.App
{
    public class PubClubYouZanOrderRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Club_YouZanOrder>, IPubClubYouZanOrderRepository
    {
        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<Pub_Club_YouZanOrder> GetByUserIdAsync(int userId)
        {
            const string sql = "select * from pub_club_youzanorder where UserId=@userId ";
            var parms = new Dictionary<string, object>
            {
                {"@userId", userId}
            };
             
            var result = await QueryAsync<Pub_Club_YouZanOrder>(sql, parms);

            return result?.FirstOrDefault();
        }

        /// <summary>
        /// 根据有赞订单号查询
        /// </summary>
        /// <param name="youZanOrderId"></param>
        /// <returns></returns>
        public async Task<Pub_Club_YouZanOrder> GetByYouZanOrderId(string youZanOrderId)
        {
            const string sql = "select * from pub_club_youzanorder where YzOrderId=@youZanOrderId ";
            var parms = new Dictionary<string, object>
            {
                {"@youZanOrderId", youZanOrderId}
            };

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

            return result?.FirstOrDefault();
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<Pub_Club_YouZanOrder>> GetAllListAsync()
        {
            const string sql = "select * from pub_club_youzanorder ";

            var result = await QueryAsync<Pub_Club_YouZanOrder>(sql);

            return result?.ToList();
        }

        /// <summary>
        /// 插入返回id
        /// </summary>
        /// <param name="youZanOrder"></param>
        /// <returns></returns>
        public async Task<int> InsertAndReturnIdAsync(Pub_Club_YouZanOrder youZanOrder)
        {
            const string sql = @"insert into pub_club_youzanorder (UserId,Phone,YzOpenId,YzOrderId,CreateTime) values(@UserId,@Phone,@YzOpenId,@YzOrderId,@CreateTime) ";

            return await ExecuteAsync(sql, youZanOrder);
        }
    }
}