using PoJun.Dapper;
using ASHermed.SinoInterface.Api.IRepository.Test;
using ASHermed.SinoInterface.Entity.Test;
using ASHermed.SinoInterface.Repository.MySql;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{

    public class PubCouponFlowRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Coupon_Flow>, IPubCouponFlowRepository
    {
        public int InsertPubCouponFlow(Pub_Coupon_Flow pubCouponFlow)
        {
            StringBuilder str = new StringBuilder();
            str.Append("insert into Pub_Coupon_Flow ");
            str.Append(@"(RECEIPTOR_PHONE,
                            COUPON_ID,
                            REDEEM_CODE,
                            SHARING_PHONE,
                            VALIDITY_START_DATE,
                            VALIDITY_END_DATE,
                            INTEGRAL_FLOW_ID,
                            TIMES,
                            STATUS,
                            CREATE_USER,
                            CREATE_TIME,
                            UPDATE_USER,
                            UPDATE_TIME) ");
            str.Append(" values ");
            str.Append(@"(@RECEIPTOR_PHONE,
                            @COUPON_ID,
                            @REDEEM_CODE,
                            @SHARING_PHONE,
                            @VALIDITY_START_DATE,
                            @VALIDITY_END_DATE,
                            @INTEGRAL_FLOW_ID,
                            @TIMES,
                            @STATUS,
                            @CREATE_USER,
                            @CREATE_TIME,
                            @UPDATE_USER,
                            @UPDATE_TIME); ");
            str.Append("select @@identity; ");
            var parms = new Dictionary<string, object>();
            parms.Add("@RECEIPTOR_PHONE", pubCouponFlow.Receiptor_Phone);
            parms.Add("@COUPON_ID", pubCouponFlow.Coupon_Id);
            parms.Add("@REDEEM_CODE", pubCouponFlow.Redeem_Code);
            parms.Add("@SHARING_PHONE", pubCouponFlow.Sharing_Phone);
            parms.Add("@VALIDITY_START_DATE", pubCouponFlow.Validity_Start_Date);
            parms.Add("@VALIDITY_END_DATE", pubCouponFlow.Validity_End_Date);
            parms.Add("@INTEGRAL_FLOW_ID", pubCouponFlow.Integral_Flow_Id);
            parms.Add("@TIMES", pubCouponFlow.Times);
            parms.Add("@STATUS", pubCouponFlow.Status);
            parms.Add("@CREATE_USER", pubCouponFlow.Create_User);
            parms.Add("@CREATE_TIME", pubCouponFlow.Create_Time);
            parms.Add("@UPDATE_USER", pubCouponFlow.Update_User);
            parms.Add("@UPDATE_TIME", pubCouponFlow.Update_Time);
            var result = Execute(str.ToString(), parms);
            return result;
        }

        public List<ReceivedCoupon> SelectReceivedCouponByPhone(string phone, int id)
        {
            StringBuilder str = new StringBuilder();
            var parms = new Dictionary<string, object>();
            str.Append(@" select a.ID, a.CREATE_TIME as CreateTime, a.VALIDITY_START_DATE as ValidityStartDate, a.RECEIPTOR_PHONE as ReceiptorPhone, d.NICK_NAME ReceiptorName,
                                a.REDEEM_CODE RedeemCode,a.TIMES,a.`STATUS`,
                                a.VALIDITY_END_DATE as ValidityEndDate, b.COUPON_TYPE as CouponType, b.CN_COUPON_NAME as CNCouponName, b.EN_COUPON_NAME as ENCouponName, b.PRICE, b.PERIOD_OF_VALIDITY as PeriodOfValidity,
                                b.CN_IMAGE as CNImage, b.EN_IMAGE as ENImage, a.SHARING_PHONE as SharingPhone, c.NICK_NAME SharingUserName, b.ID AS CouponId,
                                a.VALIDITY_END_DATE < DATE_FORMAT(NOW(),'%Y-%m-%d') as IsExpired,
                                b.CN_TEXT_ST as CNTextSt,
                                b.EN_TEXT_ST as ENTextSt,
                                b.CN_TEXT_ND as CNTextNd,
                                b.EN_TEXT_ND as ENTextNd,
                                b.CN_DESCRIPTION as CNDescription,
                                b.EN_DESCRIPTION as ENDescription,
								'true' as IsOutCoupon
                                FROM pub_coupon_flow a
                                INNER JOIN dic_coupon b ON a.COUPON_ID = b.ID AND b.`STATUS` != 1
                                LEFT JOIN pub_user_info c ON c.PHONE = a.SHARING_PHONE AND c.IS_DELETED = FALSE 
                                INNER JOIN pub_user_info d ON d.PHONE = a.RECEIPTOR_PHONE AND d.IS_DELETED = FALSE 
                                JOIN ( SELECT max( id ) AS id FROM pub_coupon_flow WHERE `STATUS` != 1 GROUP BY REDEEM_CODE ) e ON a.id = e.id
                                where a.`STATUS` != 1
                                and a.RECEIPTOR_PHONE = @phone");
            
            if (id > 0)
            {
                str.Append(" and a.id = @id");
                parms.Add("@id", id);
            }
            parms.Add("@phone", phone);
            var result = Query<ReceivedCoupon>(str.ToString(), parms);
            return result.ToList();
        }




    }


}
