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

namespace ASHermed.SinoInterface.Api.MySqlRepository.App
{
    public class OnlineCouponlogsRepository : BaseRepositoryToASHermed_SinoInterface<Online_Couponlogs>, IOnlineCouponlogsRepository
    {
        public List<Online_Couponlogs> GetOnlineCouponLogList(int userid, int status)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT * from online_couponlogs
                          WHERE PubUserId = @PubUserId AND Status = @Status AND IsDelete=false ");
            return Query<Online_Couponlogs>(sql.ToString(), new { PubUserId = userid, Status = status }).ToList();
        }

        public List<Online_Couponlogs> GetOnlineCouponLogListForOrderNo(string orderNo)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT * from online_couponlogs
                          WHERE OrderNo=@OrderNo  AND IsDelete=false ");
            return Query<Online_Couponlogs>(sql.ToString(), new { OrderNo = orderNo }).ToList();
        }

        public int DeleteOnlineCouponLogListForOrderNo(string orderNo)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" Update online_couponlogs Set IsDelete=true
                          WHERE OrderNo=@OrderNo ");
            return Execute(sql.ToString(), new { OrderNo = orderNo });
        }

        public int InsertOnlineCouponLog(Online_Couponlogs model)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" INSERT INTO `online_couponlogs`(`PubUserId`, `CouponId`, `OrderNo`, `OrderOldMoney`, `CouponMoney`, `OrderNewMoney`, `CreateTime`, `Status`, `PatId`, `CouponPubUserId`, `IsDelete`) 
VALUES
															 (@PubUserId, @CouponId, @OrderNo, @OrderOldMoney, @CouponMoney, @OrderNewMoney, @CreateTime, @Status, @PatId, @CouponPubUserId, @IsDelete) ");
            return Execute(sql.ToString(), model);
        }

        public int UpdateOnlineCouponLogStatus(string orderNo, int status)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" Update `online_couponlogs` Set `Status`=@Status Where OrderNo=@OrderNo ");
            return Execute(sql.ToString(), new { Status = status, OrderNo = orderNo });
        }
    }
}
