using System;
using System.Collections.Generic;
using Edu.Common.Plugin;
using Edu.Model.Entity.Advertising;
using Edu.Model.ViewModel.Advertising;
using Edu.Repository.Advertising;
using VT.FW.DB;

namespace Edu.Module.Advertising
{
    public class AdvertisingModule
    {
        private Rb_AdvertisingRepository repository = new Rb_AdvertisingRepository();

        /// <summary>
        /// 广告基础信息处理类
        /// </summary>
        private Rb_Advertising_MakeRepository advertisingMakeRepository = new Rb_Advertising_MakeRepository();


        #region 广告制作
        /// <summary>
        /// 获取创建的广告信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Advertising_Make_Extend> GetPageList(int pageIndex, int pageSize, Rb_Advertising_Make_Extend model, out long count)
        {
            return advertisingMakeRepository.GetPage(pageIndex, pageSize, model, out count);
        }

        /// <summary>
        /// 获取广告信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<Rb_Advertising_Make_Extend> GetList(Rb_Advertising_Make_Extend model)
        {
            return advertisingMakeRepository.Get(model);
        }

        /// <summary>
        /// 添加修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="fileName"></param>
        /// <param name="fileSize"></param>
        /// <returns></returns>
        public int Set(Rb_Advertising_Make_Extend model, string fileName, long fileSize)
        {
            int Id = 0;
            if (model.ID > 0)
            {
                Id = model.ID;
                IDictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(Rb_Advertising_Make.SecretKey),model.SecretKey},
                    { nameof(Rb_Advertising_Make.AdvertisingPicUrl),model.AdvertisingPicUrl},
                    { nameof(Rb_Advertising_Make.Content),model.Content},
                };
                IList<WhereHelper> whereHelpers = new List<WhereHelper>()
                {
                    new WhereHelper (){ FiledName=nameof(Rb_Advertising_Make.ID),FiledValue=model.ID,OperatorEnum=OperatorEnum.Equal}
                };
                advertisingMakeRepository.Update(fileds, whereHelpers);
            }
            else
            {
                Id = advertisingMakeRepository.Insert(model);
            }
            return Id;
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="EmployeeId"></param>
        /// <returns></returns>
        public bool Remove(int ID, int EmployeeId)
        {
            IDictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(Rb_Advertising_Make_Extend.Status),(int) Common.Enum.DateStateEnum.Delete},
            };
            IList<WhereHelper> whereHelpers = new List<WhereHelper>()
            {
                new WhereHelper (){ FiledName=nameof(Rb_Advertising_Make_Extend.ID),FiledValue=ID,OperatorEnum=OperatorEnum.Equal}
            };
            bool flag = advertisingMakeRepository.Update(fileds, whereHelpers);
            return flag;
        }


        #endregion


        #region 广告管理
        /// <summary>
        /// 新增修改广告基础信息
        /// </summary>
        /// <param name="dmodel"></param>
        public bool SetAdvertising(Rb_Advertising_Extend dmodel)
        {
            bool flag = false;
            int ID = 0;
            try
            {
                if (dmodel.ID > 0)
                {
                    ID = dmodel.ID;
                    var bamodel = repository.GetEntity(dmodel.ID);
                    bamodel.ChargeAmount = dmodel.ChargeAmount;
                    bamodel.IsFee = dmodel.IsFee;
                    bamodel.Title = dmodel.Title;
                    bamodel.Lable = dmodel.Lable;
                  //  bamodel.PictureUrl = dmodel.PictureUrl;
                   // bamodel.SamplePicturesUrl = dmodel.SamplePicturesUrl;
                    flag = repository.Update(bamodel);
                }
                else
                {
                    Rb_Advertising model = dmodel.RefMapperTo<Rb_Advertising>();
                    model.Status = 0;
                    ID = repository.Insert(model);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "SetAdvertising");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 获取广告列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="lineId"></param>
        /// <param name="seriesId"></param>
        /// <param name="countryId"></param>
        /// <param name="advName"></param>
        /// <param name="IsFee"></param>
        /// <param name="RB_Group_id"></param>
        /// <returns></returns>
        public List<Rb_Advertising_Extend> GetAdvertisingList(int pageIndex, int pageSize, out long count, string advName, int IsFee, int RB_Group_id)
        {
            var list = repository.GetAdvertisingList(pageIndex, pageSize, out count, advName, IsFee, RB_Group_id);
            return list;
        }

        /// <summary>
        /// 根据where 查询列表
        /// </summary>
        /// <param name="lineId"></param>
        /// <param name="seriesId"></param>
        /// <param name="countryId"></param>
        /// <param name="advName"></param>
        /// <param name="RB_Group_id"></param>
        /// <returns></returns>
        public List<Rb_Advertising_Extend> GetAdvertsingListForComboBox( string advName, int RB_Group_id)
        {
            var list = repository.GetAdvertsingListForComboBox( advName, RB_Group_id);
            return list;
        }

        /// <summary>
        /// 删除广告
        /// </summary>
        /// <param name="bAID"></param>
        public bool DelAdvertising(int bAID)
        {
            try
            {
                var fileds = new Dictionary<string, object>
                {
                    { nameof(Rb_Advertising.Status), 1},
                };
                var whereHelpers = new List<WhereHelper>
                {
                    new WhereHelper()
                    {
                        FiledName = nameof(Rb_Advertising.ID),
                        FiledValue = bAID,
                        OperatorEnum = OperatorEnum.Equal
                    }
                };
                var flag = repository.Update(fileds, whereHelpers);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "DelAdvertising");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取实体详情
        /// </summary>
        /// <param name="bAID"></param>
        /// <returns></returns>
        public Rb_Advertising GetAdvertsingInfo(int bAID)
        {
            return repository.GetEntity(bAID);
        }
        #endregion
    }
}