using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IService;
using System;
using ASHermed.SinoInterface.Tools;
using System.Collections.Generic;
using System.Linq;
using ASHermed.SinoInterface.Api.ContractModel.Coupon.Entity;

namespace ASHermed.SinoInterface.Api.Service
{
    /// <summary>
	/// 券流水
	/// </summary>
	public class PubCouponFlowService : BaseService, IPubCouponFlowService
    {
        #region 初始化
        IPubCouponFlowRepository pubCouponFlowRepository;
        ICouponService _couponService;
        IPubPatInfoRepository _pubPatInfoRepository;
        public PubCouponFlowService(IPubCouponFlowRepository _pubCouponFlowRepository, ICouponService couponService, IPubPatInfoRepository pubPatInfoRepository)
        {
            pubCouponFlowRepository = _pubCouponFlowRepository;
            _couponService = couponService;
            _pubPatInfoRepository = pubPatInfoRepository;
        }
        #endregion

        /// <summary>
        /// 添加流水信息
        /// </summary>
        /// <param name="pubCouponFlow"></param>
        /// <param name="coupon"></param>
        /// <param name="flowId"></param>
        public void AddCouponFlow(Pub_Coupon_Flow pubCouponFlow, Dic_Coupon coupon, int? flowId)
        {
            // 添加优惠券流水
            pubCouponFlow.Create_Time = DateTime.Now;
            pubCouponFlow.Validity_Start_Date = DateTime.Now;

            //判断优惠券有效期限单位并计算到期时间
            var endDate = DateTime.Now;
            if (coupon.Period_Validity_Unit == 1)
            {
                endDate.AddYears(coupon.Period_Of_Validity);
                //periodValidityUnit = DateTime.Now.Year;
            }
            else if (coupon.Period_Validity_Unit == 2)
            {
                endDate.AddMonths(coupon.Period_Of_Validity);
                //periodValidityUnit = DateTime.Now.Month;
            }
            else if (coupon.Period_Validity_Unit == 3)
            {
                endDate.AddDays(coupon.Period_Of_Validity);
                //periodValidityUnit = DateTime.Now.DayOfYear;
            }

            //var endDate = CalendarUtil.add(date, periodValidityUnit, coupon.Period_Of_Validity);
            pubCouponFlow.Validity_End_Date = coupon.End_Validity_Date != null && endDate > coupon.End_Validity_Date ? coupon.End_Validity_Date : endDate;
            pubCouponFlow.Status = 2;
            pubCouponFlow.Integral_Flow_Id = flowId;
            //添加优惠券剩余次数
            pubCouponFlow.Times = coupon.Times_Limit;
            pubCouponFlowRepository.InsertPubCouponFlow(pubCouponFlow);
        }


        public List<ReceivedCoupon> FindReceivedByPhone(int id, int language)
        {
            var pats = _pubPatInfoRepository.SelectByUserWithRelationByApp(GetBaseUserInfo().ID);

            foreach (var item in pats)
            {
                //如果患者没有MRN的话，就不同步发券，因为现在写死的券都是根据mrn查TC就诊记录的
                if (!string.IsNullOrEmpty(item.PatMrn))
                {
                    //获取写死的活动
                    List<Online_Coupon> couponNames = _couponService.GetCoupons(item.Id);

                    //给用户发送优惠券，用于写死的活动
                    _couponService.IssueCoupons(item.Id, couponNames);

                    //同步已过期优惠券状态
                    _couponService.SynchronizationCouponStatus(item.Id);
                }
            }


            //线下优惠券
            var coupon = pubCouponFlowRepository.SelectReceivedCouponByPhone(GetBaseUserInfo().Phone, id);
            //线上优惠券
            var onlinecoupon = _couponService.GetPatConponForPatId(pats.Select(w => w.Id).ToList(), language);
            foreach (var item in onlinecoupon)
            {
                if (!(item.ConponDetail.Name == "老客户专享优惠" || item.ConponDetail.Name == "Exclusive discount for old customers"))
                {
                    coupon.Add(new ReceivedCoupon { IsOutCoupon = false, OnlineCoupon = item });
                }
            }
            return coupon;
        }

    }
}
