using ASHermed.SinoInterface.Api.ContractModel.Coupon.Entity;
using ASHermed.SinoInterface.Api.ContractModel.Coupon.Model;
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 OnlineCouponPubUserRepository : BaseRepositoryToASHermed_SinoInterface<Online_CouponPubUser>, IOnlineCouponPubUserRepository
    {
        public List<Online_CouponPubUser> GetOnlineCouponPubUserList(int userid, int status)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT * from online_couponpubuser
                          WHERE PubUserId = @PubUserId AND Status = @Status ");
            return Query<Online_CouponPubUser>(sql.ToString(), new { PubUserId = userid, Status = status }).ToList();
        }
        /// <summary>
        /// status=999为拉取全部优惠券
        /// </summary>
        /// <param name="patId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<Online_CouponPubUser> GetOnlineCouponPubUserListForPatId(List<int> patid, int status)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT * from online_couponpubuser
                          WHERE PatId IN @PatId ");
            if (status != 999)
            {
                sql.Append(@" AND Status = @Status ");
            }
            return Query<Online_CouponPubUser>(sql.ToString(), new { PatId = patid, Status = status }).ToList();
        }
        public int InsertCouponPubUser(Online_CouponPubUser model)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" INSERT INTO `online_couponpubuser` (`PubUserId`, `CouponId`, `Money`, `CreateTime`, `FullMoney`, `Status`, `PatId`, `GetType` )
                          VALUES
	                                                         (@PubUserId, @CouponId, @Money, @CreateTime, @FullMoney, @Status, @PatId, @GetType ); ");
            return Execute(sql.ToString(), model);
        }

        public int UpdateCouponPubUserStatus(List<int> ids, int status)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" Update online_couponpubuser Set Status=@Status Where Id In @Ids ");
            return Execute(sql.ToString(), new { Ids = ids, Status = status });
        }

        public List<UserCouponView> GetUserCoupons(int patId)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT
	                            a.Id,
	                            a.PubUserId,
	                            a.PatId,
	                            a.CouponId,
	                            a.Money,
	                            a.FullMoney,
	                            a.`Status`,
                                a.CreateTime,
	                            b.`Name`,
	                            b.Type,
	                            b.StartTime,
	                            b.EndTime,
	                            b.DocIds,
                                b.AptType,
                                b.EffectiveTime,
                                b.DeptId,
                                b.IsDelete,
                                b.EffectiveDateTime
                            FROM
	                            online_couponpubuser a
	                            INNER JOIN online_coupon b ON a.CouponId = b.Id 
                            WHERE
	                            a.STATUS = 0 
	                            AND b.STATUS = 1 
	                            AND a.PatId=@PatId
                                AND b.IsDelete=0 ");
            return Query<UserCouponView>(sql.ToString(), new { PatId = patId }).ToList();
        }
        public List<UserCouponView> GetUserCouponsNoStatus(int patId)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT
	                            a.Id,
	                            a.PubUserId,
	                            a.PatId,
	                            a.CouponId,
	                            a.Money,
	                            a.FullMoney,
	                            a.`Status`,
                                a.CreateTime,
	                            b.`Name`,
	                            b.Type,
	                            b.StartTime,
	                            b.EndTime,
	                            b.DocIds,
                                b.AptType,
                                b.EffectiveTime,
                                b.DeptId,
                                b.IsDelete 
                            FROM
	                            online_couponpubuser a
	                            INNER JOIN online_coupon b ON a.CouponId = b.Id 
                            WHERE b.STATUS = 1 
	                            AND a.PatId=@PatId
                                AND b.IsDelete=0 ");
            return Query<UserCouponView>(sql.ToString(), new { PatId = patId }).ToList();
        }

        //public List<string> GetCouponOverPatPhone()
        //{ 
        //var sql = @" select * from online_couponpubuser a inner join online_coupon b on a.CouponId=b.Id inner join pub_pat_info c on c.Id=a.PatId "
        //}
    }
}
