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.Text;
using System.Linq;
using ASHermed.SinoInterface.Api.ContractModel.Coupon.Model;

namespace ASHermed.SinoInterface.Api.MySqlRepository.App
{
    public class OnlineCouponRepository : BaseRepositoryToASHermed_SinoInterface<Online_Coupon>, IOnlineCouponRepository
    {
        public Online_Coupon GetOnlineCoupon(int id, int language)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT * from online_coupon
                          WHERE Id = @Id AND Status = 1 ");
            var res = Query<Online_Coupon>(sql.ToString(), new { Id = id }).FirstOrDefault();
            if (language != 1)
            {
                res.Name = res.NameEN;
            }
            return res;
        }

        public List<Online_Coupon> GetOnlineCouponList(List<int> ids, int language)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT * from online_coupon
                          WHERE Id in @Id AND Status = 1 ");
            var res = Query<Online_Coupon>(sql.ToString(), new { Id = ids }).ToList();
            if (language != 1)
            {
                res.ForEach(w =>
                {
                    w.Name = w.NameEN;
                    w.Remarks = w.RemarksEN;
                    w.MoneyRemark = w.MoneyRemarkEN;
                    w.TermOfValidity = w.TermOfValidityEN;
                    w.Content = w.ContentEN;
                });
            }
            return res;
        }

        public List<Online_Coupon> GetOnlineCouponForName(List<string> name)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" SELECT * from online_coupon
                          WHERE Name IN @Name ");
            var res = Query<Online_Coupon>(sql.ToString(), new { Name = name }).ToList();
            return res;
        }

        public int InsertCoupon(Online_Coupon model)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" INSERT INTO `online_coupon` ( `Type`, `Name`, `Img`, `StartTime`, `EndTime`, `Money`, `Status`, `Remarks`, `CreateTime`, `FullMoney`, `DocIds`, `Quota`, `NameEN`, `AptType`, `EffectiveTime`, `DeptId`, `IsDelete`,`Content`,`RemarksEN`,`MoneyRemark`,`MoneyRemarkEN`,`TermOfValidity`,`TermOfValidityEN`,`ContentEN`,`HerfUrl`,`CreateUser`,`UpdateUser`,`UpdateTime` ) VALUES ( @Type, @Name, @Img, @StartTime, @EndTime, @Money, @Status,@Remarks, @CreateTime, @FullMoney, @DocIds, @Quota, @NameEN, @AptType,@EffectiveTime,@DeptId,@IsDelete,@Content,@RemarksEN,@MoneyRemark,@MoneyRemarkEN,@TermOfValidity,@TermOfValidityEN,@ContentEN, @HerfUrl, @CreateUser, @UpdateUser, @UpdateTime ) ");
            return this.Insert(sql.ToString(), model);

        }

        public int UpdateCouponQuota(int couponId, int quota)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" UPDATE `online_coupon` Set Quota=@Quota Where Id=@Id ");
            return Execute(sql.ToString(), new { Id = couponId, Quota = quota });
        }

        

    }
}
