using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mall.Model.Extend.TradePavilion;
using Mall.Repository.TradePavilion;
using Newtonsoft.Json;
using VT.FW.DB;
using Mall.Common.Plugin;
using Mall.Common;
using Mall.Common.Enum;
using VT.FW.DB.Dapper;
using RabbitMQ.Client.Framing.Impl;
using Mall.Model.Extend.Product;
using RabbitMQ.Client;
using Mall.Common.Enum.TradePavilion;
using Mall.Model.Query;

namespace Mall.Module.TradePavilion
{
    /// <summary>
    /// 载体处理类
    /// </summary>
    public class CarrierModule
    {
        /// <summary>
        /// 载体信息仓储层对象
        /// </summary>
        private readonly RB_CarrierRepository carrierRepository = new RB_CarrierRepository();
        /// <summary>
        /// 载体地铁信息仓储层对象
        /// </summary>
        private readonly RB_CarrierMetroRepository carrierMetroRepository = new RB_CarrierMetroRepository();
        /// <summary>
        /// 载体信息仓储层对象
        /// </summary>
        private readonly RB_CompanyBrandRepository companyBrandRepository = new RB_CompanyBrandRepository();
        /// <summary>
        /// 公司信息仓储层对象
        /// </summary>
        private readonly RB_CompanyRepository companyRepository = new RB_CompanyRepository();
        /// <summary>
        /// 品牌申请
        /// </summary>
        private readonly RB_BrandApplyForRepository brandApplyForRepository = new RB_BrandApplyForRepository();
        /// <summary>
        /// 载体申请
        /// </summary>
        private readonly RB_CarrierApplyForRepository carrierApplyForRepository = new RB_CarrierApplyForRepository();
        /// <summary>
        /// 载体地铁信息
        /// </summary>
        private readonly RB_CarrierMetroApplyForRepository carrierMetroApplyForRepository = new RB_CarrierMetroApplyForRepository();
        /// <summary>
        /// 用户
        /// </summary>
        private readonly Repository.User.RB_Member_UserRepository member_UserRepository = new Repository.User.RB_Member_UserRepository();
        /// <summary>
        /// 品牌信息仓储层对象
        /// </summary>
        private readonly RB_BrandRepository brandRepository = new RB_BrandRepository();


        /// <summary>
        /// 品牌分类信息仓储层对象
        /// </summary>
        private readonly RB_BrandClassRepository brandClassRepository = new RB_BrandClassRepository();

        /// <summary>
        /// 收藏仓储层对象
        /// </summary>
        private readonly RB_CollectRepository collectRepository = new RB_CollectRepository();

        /// <summary>
        /// 奖项仓储层对象
        /// </summary>
        private readonly RB_PrizeRepository prizeRepository = new RB_PrizeRepository();

        /// <summary>
        /// 【商载通】楼宇、载体仓储层
        /// </summary>
        private readonly RB_Building_CarrierRepository building_CarrierRepository = new RB_Building_CarrierRepository();
        /// <summary>
        /// 【商载通】楼宇、载体地铁仓储层
        /// </summary>
        private readonly RB_Building_CarriermetroRepository building_CarriermetroRepository = new RB_Building_CarriermetroRepository();

        #region 载体
        /// <summary>
        /// 载体信息列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Carrier_Extend> GetCarrierPageList(int pageIndex, int pageSize, out long rowCount, RB_Carrier_Extend query, bool IsSelectUser = false)
        {
            var list = carrierRepository.GetCarrierPageList(pageIndex, pageSize, out rowCount, query);
            if (list != null && list.Any())
            {
                string carrierIds = string.Join(",", list.Select(x => x.ID));
                if (!string.IsNullOrWhiteSpace(carrierIds))
                {
                    var metroList = carrierMetroRepository.GetCarrierMetroList(new RB_CarrierMetro_Extend { TenantId = query.TenantId, MallBaseId = query.MallBaseId, CarrierIds = carrierIds });
                    if (metroList != null && metroList.Any())
                    {
                        foreach (var item in list)
                        {
                            item.CarrierMetroList = new List<RB_CarrierMetro_Extend>();
                            item.CarrierMetroList = metroList.Where(x => x.CarrierId == item.ID).ToList();
                        }
                    }
                }
                if (IsSelectUser)
                {
                    if (list.Where(x => x.UserId > 0).Any())
                    {
                        //查询用户列表
                        string userIds = string.Join(",", list.Where(x => x.UserId > 0).Select(x => x.UserId));
                        var ulist = member_UserRepository.GetList(new Model.Extend.User.RB_Member_User_Extend() { MallBaseId = query.MallBaseId, UserIds = userIds });
                        foreach (var item in list.Where(x => x.UserId > 0))
                        {
                            var umodel = ulist.Where(x => x.Id == item.UserId).FirstOrDefault();
                            item.UserName = umodel.Name;
                            item.UserIcon = umodel.Photo;
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 载体信息列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Carrier_Extend> GetCarrierList(RB_Carrier_Extend query)
        {
            var list = carrierRepository.GetCarrierList(query);
            if (list != null && list.Any())
            {
                string carrierIds = string.Join(",", list.Select(x => x.ID));
                if (!string.IsNullOrWhiteSpace(carrierIds))
                {
                    var metroList = carrierMetroRepository.GetCarrierMetroList(new RB_CarrierMetro_Extend { TenantId = query.TenantId, MallBaseId = query.MallBaseId, CarrierIds = carrierIds });
                    if (metroList != null && metroList.Any())
                    {
                        foreach (var item in list)
                        {
                            if (!string.IsNullOrWhiteSpace(item.Banner))
                            {
                                item.BannerList = JsonConvert.DeserializeObject<List<string>>(item.Banner);
                            }
                            if (!string.IsNullOrWhiteSpace(item.Honor))
                            {
                                item.HonorList = JsonConvert.DeserializeObject<List<string>>(item.Honor);
                            }
                            if (!string.IsNullOrWhiteSpace(item.FirstShopNum))
                            {
                                item.FirstShopNumList = JsonConvert.DeserializeObject<List<FirstShopNumModel>>(item.FirstShopNum);
                            }
                            item.CarrierMetroList = new List<RB_CarrierMetro_Extend>();
                            item.CarrierMetroList = metroList.Where(x => x.CarrierId == item.ID).ToList();
                        }
                    }
                }
            }
            return list;
        }



        /// <summary>
        /// 载体信息列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public RB_Carrier_Extend GetCarrierModel(RB_Carrier_Extend query)
        {
            var model = carrierRepository.GetCarrierList(query).FirstOrDefault();
            if (model != null && model.ID > 0)
            {
                var metroList = carrierMetroRepository.GetCarrierMetroList(new RB_CarrierMetro_Extend { TenantId = query.TenantId, MallBaseId = query.MallBaseId, CarrierId = model.ID });
                if (metroList != null && metroList.Any())
                {

                    model.CarrierMetroList = new List<RB_CarrierMetro_Extend>();
                    model.CarrierMetroList = metroList;
                }
                if (!string.IsNullOrWhiteSpace(model.Banner))
                {
                    model.BannerList = JsonConvert.DeserializeObject<List<string>>(model.Banner);
                }
                else
                {
                    model.BannerList = new List<string>();
                }
                if (!string.IsNullOrWhiteSpace(model.Honor))
                {
                    model.HonorList = JsonConvert.DeserializeObject<List<string>>(model.Honor);
                }
                else
                {
                    model.HonorList = new List<string>();
                }
                if (!string.IsNullOrWhiteSpace(model.FirstShopNum))
                {
                    model.FirstShopNumList = JsonConvert.DeserializeObject<List<FirstShopNumModel>>(model.FirstShopNum);
                }
                else
                {
                    model.FirstShopNumList = new List<FirstShopNumModel>();
                }
            }
            return model;
        }

        /// <summary>
        /// 新增/修改载体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetCarrier(RB_Carrier_Extend model)
        {
            bool flag = false;
            int Id = 0;
            if (model.ID > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Carrier_Extend.CarrierName),model.CarrierName },
                    {nameof(RB_Carrier_Extend.Logo),model.Logo },
                    {nameof(RB_Carrier_Extend.Banner),model.Banner },
                    {nameof(RB_Carrier_Extend.VideoUrl),model.VideoUrl },
                    {nameof(RB_Carrier_Extend.Address),model.Address },
                    {nameof(RB_Carrier_Extend.LatAndLon),model.LatAndLon },
                    {nameof(RB_Carrier_Extend.Developers),model.Developers },
                    {nameof(RB_Carrier_Extend.OpenTime),model.OpenTime },
                    {nameof(RB_Carrier_Extend.CarrierSize),model.CarrierSize },
                    {nameof(RB_Carrier_Extend.ProjectType),(int)model.ProjectType },
                    {nameof(RB_Carrier_Extend.LayersNum),model.LayersNum },
                    {nameof(RB_Carrier_Extend.CarNum),model.CarNum },
                    {nameof(RB_Carrier_Extend.CarrierPlan),model.CarrierPlan },
                    {nameof(RB_Carrier_Extend.CarrierTarget),model.CarrierTarget },
                    {nameof(RB_Carrier_Extend.Location),model.Location },
                    {nameof(RB_Carrier_Extend.Crowd),model.Crowd },
                     {nameof(RB_Carrier_Extend.FirstStoreTest),model.FirstStoreTest },
                     {nameof(RB_Carrier_Extend.OpeningStatus),model.OpeningStatus },
                      {nameof(RB_Carrier_Extend.Discount),model.Discount },
                    {nameof(RB_Carrier_Extend.Honor),model.Honor },
                    {nameof(RB_Carrier_Extend.YeJi),model.YeJi },
                    {nameof(RB_Carrier_Extend.ShopNum),model.ShopNum },
                    {nameof(RB_Carrier_Extend.AreaRequirement ),model.AreaRequirement  },
                    {nameof(RB_Carrier_Extend.EndAreaRequirement),model.EndAreaRequirement },
                    {nameof(RB_Carrier_Extend.BuiltUpArea),model.BuiltUpArea },
                    {nameof(RB_Carrier_Extend.EndBuiltUpArea),model.EndBuiltUpArea },
                    {nameof(RB_Carrier_Extend.FirstShopNum),model.FirstShopNum },
                    {nameof(RB_Carrier_Extend.StartingInfo),model.StartingInfo },
                     {nameof(RB_Carrier_Extend.ContactPhone),model.ContactPhone },
                    {nameof(RB_Carrier_Extend.ContactName),model.ContactName }

                };
                flag = carrierRepository.Update(fileds, new WhereHelper(nameof(RB_Carrier_Extend.ID), model.ID));
                if (flag)
                {
                    var oldCarrierMetroList = carrierMetroRepository.GetCarrierMetroList(new RB_CarrierMetro_Extend { TenantId = model.TenantId, MallBaseId = model.MallBaseId, CarrierId = model.ID });
                    carrierMetroRepository.DeleteBatch(oldCarrierMetroList);
                    model.CarrierMetroList.ForEach(x => x.CarrierId = model.ID);
                    model.CarrierMetroList.ForEach(x => x.Status = 0);
                    model.CarrierMetroList.ForEach(x => x.TenantId = model.TenantId);
                    model.CarrierMetroList.ForEach(x => x.MallBaseId = model.MallBaseId);
                    carrierMetroRepository.InsertBatch(model.CarrierMetroList);
                }
            }
            else
            {
                Id = carrierRepository.Insert(model);
                if (Id > 0)
                {
                    model.CarrierMetroList.ForEach(x => x.CarrierId = Id);
                    model.CarrierMetroList.ForEach(x => x.Status = 0);
                    model.CarrierMetroList.ForEach(x => x.TenantId = model.TenantId);
                    model.CarrierMetroList.ForEach(x => x.MallBaseId = model.MallBaseId);
                    carrierMetroRepository.InsertBatch(model.CarrierMetroList);
                }
                flag = Id > 0;
            }

            return flag;
        }

        /// <summary>
        /// 验证载体名称
        /// </summary>
        /// <param name="carrierName"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public bool ValidateCarrierName(string carrierName, int mallBaseId)
        {
            List<WhereHelper> wheres = new List<WhereHelper>() {
                new WhereHelper(){
                     FiledName=nameof(RB_Carrier_Extend.CarrierName),
                      FiledValue=carrierName,
                       OperatorEnum=OperatorEnum.Equal
                },
                new WhereHelper(){
                     FiledName=nameof(RB_Carrier_Extend.MallBaseId),
                      FiledValue=mallBaseId,
                       OperatorEnum=OperatorEnum.Equal
                }
            };
            return carrierRepository.Exists(wheres);
        }

        /// <summary>
        /// 新增修改载体申请
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetCarrierApplyFor(RB_CarrierApplyFor_Extend model)
        {
            var cmodel = companyRepository.GetEntity(model.CompanyId);
            if (cmodel == null) { return false; }
            if (cmodel.FirstShopType != 2) { return false; }
            bool flag = false;
            if (model.ID > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_CarrierApplyFor_Extend.CarrierName),model.CarrierName },
                    {nameof(RB_CarrierApplyFor_Extend.Logo),model.Logo },
                    {nameof(RB_CarrierApplyFor_Extend.Banner),model.Banner },
                    {nameof(RB_CarrierApplyFor_Extend.VideoUrl),model.VideoUrl },
                    {nameof(RB_CarrierApplyFor_Extend.Address),model.Address },
                    {nameof(RB_CarrierApplyFor_Extend.LatAndLon),model.LatAndLon },
                    {nameof(RB_CarrierApplyFor_Extend.Developers),model.Developers },
                    {nameof(RB_CarrierApplyFor_Extend.OpenTime),model.OpenTime },
                    {nameof(RB_CarrierApplyFor_Extend.CarrierSize),model.CarrierSize },
                    {nameof(RB_CarrierApplyFor_Extend.ProjectType),(int)model.ProjectType },
                    {nameof(RB_CarrierApplyFor_Extend.LayersNum),model.LayersNum },
                    {nameof(RB_CarrierApplyFor_Extend.CarNum),model.CarNum },
                    {nameof(RB_CarrierApplyFor_Extend.CarrierPlan),model.CarrierPlan },
                    {nameof(RB_CarrierApplyFor_Extend.CarrierTarget),model.CarrierTarget },
                    {nameof(RB_CarrierApplyFor_Extend.Location),model.Location },
                    {nameof(RB_CarrierApplyFor_Extend.Crowd),model.Crowd },
                    {nameof(RB_CarrierApplyFor_Extend.ExistCarrierId),model.ExistCarrierId },
                    {nameof(RB_CarrierApplyFor_Extend.Discount),model.Discount },
                    {nameof(RB_CarrierApplyFor_Extend.Honor),model.Honor },
                    {nameof(RB_CarrierApplyFor_Extend.YeJi),model.YeJi },
                    {nameof(RB_CarrierApplyFor_Extend.ShopNum),model.ShopNum },
                    {nameof(RB_CarrierApplyFor_Extend.AreaRequirement ),model.AreaRequirement  },
                    {nameof(RB_CarrierApplyFor_Extend.EndAreaRequirement),model.EndAreaRequirement },
                    {nameof(RB_CarrierApplyFor_Extend.BuiltUpArea),model.BuiltUpArea },
                    {nameof(RB_CarrierApplyFor_Extend.EndBuiltUpArea),model.EndBuiltUpArea },
                    {nameof(RB_CarrierApplyFor_Extend.FirstShopNum),model.FirstShopNum },
                    {nameof(RB_CarrierApplyFor_Extend.StartingInfo),model.StartingInfo }
                };
                flag = carrierApplyForRepository.Update(fileds, new WhereHelper(nameof(RB_CarrierApplyFor_Extend.ID), model.ID));
                if (flag)
                {
                    var oldCarrierMetroList = carrierMetroApplyForRepository.GetCarrierMetroList(new RB_CarrierMetroApplyFor_Extend { TenantId = model.TenantId, MallBaseId = model.MallBaseId, CarrierId = model.ID });
                    carrierMetroApplyForRepository.DeleteBatch(oldCarrierMetroList);
                    model.CarrierMetroList.ForEach(x =>
                    {
                        x.CarrierId = model.ID;
                        x.Status = 0;
                        x.TenantId = model.TenantId;
                        x.MallBaseId = model.MallBaseId;
                    });
                    carrierMetroApplyForRepository.InsertBatch(model.CarrierMetroList);

                    //更新认证表  资料状态为完成
                    Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                        { nameof(RB_Company_Extend.IsComplete),1}
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>() {
                        new WhereHelper(){
                                FiledName=nameof(RB_Company_Extend.CompanyId),
                                FiledValue= model.CompanyId,
                                OperatorEnum=OperatorEnum.Equal
                        }
                    };
                    companyRepository.Update(keyValues, wheres);
                }
            }
            else
            {
                #region 验证载体申请是否已存在
                var oldList = carrierApplyForRepository.GetCarrierList(new RB_CarrierApplyFor_Extend() { MallBaseId = model.MallBaseId, CompanyId = model.CompanyId });
                if (oldList.Any())
                {
                    return false;
                }
                #endregion
                int Id = carrierApplyForRepository.Insert(model);
                if (Id > 0)
                {
                    model.CarrierMetroList.ForEach(x =>
                    {
                        x.ID = 0;
                        x.CarrierId = Id;
                        x.Status = 0;
                        x.TenantId = model.TenantId;
                        x.MallBaseId = model.MallBaseId;
                    });
                    carrierMetroApplyForRepository.InsertBatch(model.CarrierMetroList);

                    //更新认证表  资料状态为完成
                    Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                        { nameof(RB_Company_Extend.IsComplete),1}
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>() {
                        new WhereHelper(){
                                FiledName=nameof(RB_Company_Extend.CompanyId),
                                FiledValue= model.CompanyId,
                                OperatorEnum=OperatorEnum.Equal
                        }
                    };
                    companyRepository.Update(keyValues, wheres);
                }
                flag = Id > 0;
            }
            if (flag)
            {
                if (Config.IsFirtsShopAutoExamine == "1")//自动审核
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Company_Extend.CompanyStatus),1 },
                    };

                    List<WhereHelper> wheres = new List<WhereHelper>() {
                     new WhereHelper(){
                         FiledName=nameof(RB_Company_Extend.CompanyId),
                          FiledValue= model.CompanyId,
                           OperatorEnum=OperatorEnum.Equal
                     }
                };
                    companyRepository.Update(fileds, wheres);
                    //载体
                    var carrierList = carrierApplyForRepository.GetCarrierList(new RB_CarrierApplyFor_Extend() { MallBaseId = cmodel.MallBaseId, CompanyId = cmodel.CompanyId });
                    if (carrierList != null && carrierList.Any())
                    {
                        var CarrierModel = carrierList.FirstOrDefault();
                        var MetroList = carrierMetroApplyForRepository.GetCarrierMetroList(new RB_CarrierMetroApplyFor_Extend { MallBaseId = cmodel.MallBaseId, CarrierId = CarrierModel.ID });

                        if (CarrierModel.ExistCarrierId > 0)
                        {
                            var carrmodel = carrierRepository.GetEntity(CarrierModel.ExistCarrierId);

                            #region 修改载体
                            int FirstStoreTest = 0;
                            if (!string.IsNullOrWhiteSpace(CarrierModel.StartingInfo))
                            {
                                FirstStoreTest = 1;
                            }
                            int OpeningStatus = 0;
                            if (CarrierModel.OpenTime.HasValue)
                            {
                                if (CarrierModel.OpenTime.Value <= System.DateTime.Now)
                                {
                                    OpeningStatus = 1;
                                }
                            }
                            Dictionary<string, object> filedsCarrier = new Dictionary<string, object>()
                                    {
                                        {nameof(RB_Carrier_Extend.Logo),CarrierModel.Logo },
                                        {nameof(RB_Carrier_Extend.Banner),CarrierModel.Banner },
                                        {nameof(RB_Carrier_Extend.VideoUrl),CarrierModel.VideoUrl },
                                        {nameof(RB_Carrier_Extend.Address),CarrierModel.Address },
                                        {nameof(RB_Carrier_Extend.LatAndLon),CarrierModel.LatAndLon },
                                        {nameof(RB_Carrier_Extend.Developers),CarrierModel.Developers },
                                        {nameof(RB_Carrier_Extend.OpenTime),CarrierModel.OpenTime },
                                        {nameof(RB_Carrier_Extend.CarrierSize),CarrierModel.CarrierSize },
                                        {nameof(RB_Carrier_Extend.ProjectType),(int)CarrierModel.ProjectType },
                                        {nameof(RB_Carrier_Extend.LayersNum),CarrierModel.LayersNum },
                                        {nameof(RB_Carrier_Extend.CarNum),CarrierModel.CarNum },
                                        {nameof(RB_Carrier_Extend.CarrierPlan),CarrierModel.CarrierPlan },
                                        {nameof(RB_Carrier_Extend.CarrierTarget),CarrierModel.CarrierTarget },
                                        {nameof(RB_Carrier_Extend.Location),CarrierModel.Location },
                                        {nameof(RB_Carrier_Extend.Crowd),CarrierModel.Crowd },
                                        {nameof(RB_Carrier_Extend.UserId),cmodel.CreateBy },
                                        {nameof(RB_Carrier_Extend.Discount),CarrierModel.Discount },
                                        {nameof(RB_Carrier_Extend.Honor),CarrierModel.Honor },
                                        {nameof(RB_Carrier_Extend.YeJi),CarrierModel.YeJi },
                                        {nameof(RB_Carrier_Extend.ShopNum),CarrierModel.ShopNum },
                                        {nameof(RB_Carrier_Extend.AreaRequirement ),CarrierModel.AreaRequirement  },
                                        {nameof(RB_Carrier_Extend.EndAreaRequirement),CarrierModel.EndAreaRequirement },
                                        {nameof(RB_Carrier_Extend.BuiltUpArea),CarrierModel.BuiltUpArea },
                                        {nameof(RB_Carrier_Extend.EndBuiltUpArea),CarrierModel.EndBuiltUpArea },
                                        {nameof(RB_Carrier_Extend.FirstShopNum),CarrierModel.FirstShopNum },
                                        {nameof(RB_Carrier_Extend.StartingInfo),CarrierModel.StartingInfo },
                                          {nameof(RB_Carrier_Extend.FirstStoreTest),FirstStoreTest },
                                          {nameof(RB_Carrier_Extend.OpeningStatus),OpeningStatus }
                                    };
                            flag = carrierRepository.Update(filedsCarrier, new WhereHelper(nameof(RB_Carrier_Extend.ID), carrmodel.ID));
                            if (flag)
                            {
                                var oldCarrierMetroList = carrierMetroRepository.GetCarrierMetroList(new RB_CarrierMetro_Extend { MallBaseId = CarrierModel.MallBaseId, CarrierId = carrmodel.ID });
                                carrierMetroRepository.DeleteBatch(oldCarrierMetroList);
                                var mlist = MetroList.RefMapperToList<RB_CarrierMetro_Extend>();
                                mlist.ForEach(x =>
                                {
                                    x.ID = 0;
                                    x.CarrierId = carrmodel.ID;
                                    x.Status = 0;
                                    x.TenantId = CarrierModel.TenantId;
                                    x.MallBaseId = CarrierModel.MallBaseId;
                                });
                                carrierMetroRepository.InsertBatch(mlist);
                            }
                            #endregion
                        }
                        else
                        {

                            #region 新增载体
                            var inserCModel = CarrierModel.RefMapperTo<RB_Carrier_Extend>();
                            inserCModel.ID = 0;
                            inserCModel.UserId = cmodel.CreateBy;
                            if (CarrierModel.OpenTime.HasValue)
                            {
                                if (CarrierModel.OpenTime.Value <= System.DateTime.Now)
                                {
                                    inserCModel.OpeningStatus = 1;
                                }
                            }
                            if (!string.IsNullOrWhiteSpace(CarrierModel.StartingInfo))
                            {
                                inserCModel.FirstStoreTest = 1;
                            }
                            int Id = carrierRepository.Insert(inserCModel);
                            if (Id > 0)
                            {
                                var mlist = MetroList.RefMapperToList<RB_CarrierMetro_Extend>();
                                mlist.ForEach(x =>
                                {
                                    x.ID = 0;
                                    x.CarrierId = Id;
                                    x.Status = 0;
                                    x.TenantId = CarrierModel.TenantId;
                                    x.MallBaseId = CarrierModel.MallBaseId;
                                });
                                carrierMetroRepository.InsertBatch(mlist);
                            }
                            #endregion
                        }
                    }
                }
            }
            return flag;
        }


        /// <summary>
        ///删除载体信息状态
        /// </summary>
        /// <param name="CompanyId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool RemoveCarrierModule(int CarrierId, int TenantId, int MallBaseId, int Status)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Carrier_Extend.Status),Status },
            };
            bool flag = carrierRepository.Update(fileds, new WhereHelper(nameof(RB_Carrier_Extend.ID), CarrierId));
            if (flag)
            {
                var oldCarrierMetroList = carrierMetroRepository.GetCarrierMetroList(new RB_CarrierMetro_Extend { TenantId = TenantId, MallBaseId = MallBaseId, CarrierId = CarrierId });
                if (oldCarrierMetroList != null && oldCarrierMetroList.Any())
                {
                    oldCarrierMetroList.ForEach(x => x.Status = Common.Enum.DateStateEnum.Delete);
                    carrierMetroRepository.UpdateBatch(oldCarrierMetroList);
                }
            }
            return flag;
        }



        #endregion


        #region 公司品牌

        /// <summary>
        /// 获取品牌信息分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_CompanyBrand_Extend> GetBrandPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_CompanyBrand_Extend query)
        {
            return companyBrandRepository.GetBrandPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 根据查询条件获取品牌信息实体
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RB_CompanyBrand_Extend GetBrandRepository(RB_CompanyBrand_Extend query)
        {
            return companyBrandRepository.GetBrandRepository(query);
        }

        /// <summary>
        /// 新增修改品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetCompanyBrand(RB_CompanyBrand_Extend model)
        {
            if (model.ID == 0)
            {
                return companyBrandRepository.Insert(model) > 0;
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_CompanyBrand_Extend.BrandName),model.BrandName },
                    {nameof(RB_CompanyBrand_Extend.ShopNum),model.ShopNum },
                    {nameof(RB_CompanyBrand_Extend.BuiltUpArea),model.BuiltUpArea },
                    {nameof(RB_CompanyBrand_Extend.EndBuiltUpArea),model.EndBuiltUpArea },
                    {nameof(RB_CompanyBrand_Extend.FullBrandName),model.FullBrandName },
                    {nameof(RB_CompanyBrand_Extend.CustomerType),model.CustomerType },
                    {nameof(RB_CompanyBrand_Extend.CustomerOtherType),model.CustomerOtherType },
                    {nameof(RB_CompanyBrand_Extend.StoreExpansion),model.StoreExpansion },
                    {nameof(RB_CompanyBrand_Extend.ProjectType),(int)model.ProjectType },
                    {nameof(RB_CompanyBrand_Extend.AreaRequirement),model.AreaRequirement },
                    {nameof(RB_CompanyBrand_Extend.EndAreaRequirement),model.EndAreaRequirement },
                };
                return companyBrandRepository.Update(fileds, new WhereHelper(nameof(RB_CompanyBrand_Extend.ID), model.ID));
            }
        }


        #endregion


        #region 品牌
        /// <summary>
        /// 获取品牌信息分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Brand_Extend> GetBrandPageList(int pageIndex, int pageSize, out long rowsCount, RB_Brand_Extend query, bool IsSelectUser = false)
        {
            var list = brandRepository.GetBrandPageList(pageIndex, pageSize, out rowsCount, query);
            if (IsSelectUser)
            {
                if (list.Where(x => x.UserId > 0).Any())
                {
                    //查询用户列表
                    string userIds = string.Join(",", list.Where(x => x.UserId > 0).Select(x => x.UserId));
                    var ulist = member_UserRepository.GetList(new Model.Extend.User.RB_Member_User_Extend() { MallBaseId = query.MallBaseId, UserIds = userIds });


                    foreach (var item in list.Where(x => x.UserId > 0))
                    {
                        var umodel = ulist.Where(x => x.Id == item.UserId).FirstOrDefault();
                        item.UserName = umodel.Name;
                        item.UserIcon = umodel.Photo;
                    }
                }
            }
            if (list != null && list.Any())//绑定奖章
            {
                string sourceIds = string.Join(",", list.Where(x => !string.IsNullOrWhiteSpace(x.SourceIds)).Select(x => x.SourceIds));
                List<RB_Prize_Extend> prizeList = new List<RB_Prize_Extend>();
                if (!string.IsNullOrWhiteSpace(sourceIds))
                {
                    prizeList = prizeRepository.GetList(new RB_Prize_Extend { MallBaseId = query.MallBaseId, TenantId = query.TenantId, QPrizeIds = sourceIds });
                }
                if (prizeList != null && prizeList.Any())
                {
                    foreach (var item in list.Where(x => !string.IsNullOrWhiteSpace(x.SourceIds)))
                    {
                        item.PrizeList = new List<RB_Prize_Extend>();
                        foreach (var itemSourceId in item.SourceIds.Split(","))
                        {
                            var nowPrizeModel = prizeList.Where(x => x.Id.ToString() == itemSourceId).FirstOrDefault();
                            if (nowPrizeModel != null && nowPrizeModel.Id > 0)
                            {
                                item.PrizeList.Add(nowPrizeModel);
                            }
                        }
                    }
                }



            }
            return list;
        }

        /// <summary>
        /// 根据查询条件获取品牌信息实体
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RB_Brand_Extend GetBrand(RB_Brand_Extend query)
        {
            return brandRepository.GetBrand(query);
        }

        /// <summary>
        /// 验证品牌名称
        /// </summary>
        /// <param name="brandName"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public bool ValidateBrandName(string brandName, int mallBaseId)
        {
            List<WhereHelper> wheres = new List<WhereHelper>() {
                new WhereHelper(){
                     FiledName=nameof(RB_Brand_Extend.BrandName),
                      FiledValue=brandName,
                       OperatorEnum=OperatorEnum.Equal
                },
                new WhereHelper(){
                     FiledName=nameof(RB_Brand_Extend.MallBaseId),
                      FiledValue=mallBaseId,
                       OperatorEnum=OperatorEnum.Equal
                }
            };
            return brandRepository.Exists(wheres);
        }

        /// <summary>
        /// 验证品牌全称
        /// </summary>
        /// <param name="brandName"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public bool ValidateFullBrandName(string brandName, int mallBaseId)
        {
            List<WhereHelper> wheres = new List<WhereHelper>() {
                new WhereHelper(){
                     FiledName=nameof(RB_Brand_Extend.FullBrandName),
                      FiledValue=brandName,
                       OperatorEnum=OperatorEnum.Equal
                },
                new WhereHelper(){
                     FiledName=nameof(RB_Brand_Extend.MallBaseId),
                      FiledValue=mallBaseId,
                       OperatorEnum=OperatorEnum.Equal
                }
            };
            return brandRepository.Exists(wheres);
        }

        /// <summary>
        /// 根据查询条件获取品牌信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Brand_Extend> GetBrandListByWhere(RB_Brand_Extend query)
        {
            return brandRepository.GetBrandListByWhere(query);
        }

        /// <summary>
        /// 获取品牌列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Brand_Extend> GetBrandList_V2(RB_Brand_Extend query)
        {
            return brandRepository.GetBrandList(query);
        }

        /// <summary>
        /// 新增/修改品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetBrand(RB_Brand_Extend model)
        {
            if (model.ID == 0)
            {
                return brandRepository.Insert(model) > 0;
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Brand_Extend.UpdateDate),model.UpdateDate },
                    {nameof(RB_Brand_Extend.Logo),model.Logo },
                    {nameof(RB_Brand_Extend.Status),model.Status },
                    {nameof(RB_Brand_Extend.Banner),model.Banner },
                    {nameof(RB_Brand_Extend.VideoUrl),model.VideoUrl },
                    {nameof(RB_Brand_Extend.FullBrandName),model.FullBrandName },
                    {nameof(RB_Brand_Extend.BrandName),model.BrandName },
                    {nameof(RB_Brand_Extend.ShopNum),model.ShopNum },
                    {nameof(RB_Brand_Extend.BuiltUpArea),model.BuiltUpArea },
                    {nameof(RB_Brand_Extend.EndBuiltUpArea),model.EndBuiltUpArea },
                    {nameof(RB_Brand_Extend.BrandType),model.BrandType },
                    {nameof(RB_Brand_Extend.CustomerType),model.CustomerType },
                    {nameof(RB_Brand_Extend.StoreExpansion),model.StoreExpansion },
                    {nameof(RB_Brand_Extend.ProjectType),model.ProjectType },
                    {nameof(RB_Brand_Extend.AreaRequirement),model.AreaRequirement },
                    {nameof(RB_Brand_Extend.EndAreaRequirement),model.EndAreaRequirement },
                    {nameof(RB_Brand_Extend.Advertising),model.Advertising },
                    {nameof(RB_Brand_Extend.Other),model.Other },
                    {nameof(RB_Brand_Extend.ShopSize),model.ShopSize },
                    {nameof(RB_Brand_Extend.Plumbing),model.Plumbing },
                    {nameof(RB_Brand_Extend.Caliber),model.Caliber },
                    {nameof(RB_Brand_Extend.Sewage),model.Sewage },
                    {nameof(RB_Brand_Extend.RanQi),model.RanQi },
                    {nameof(RB_Brand_Extend.DianLiang),model.DianLiang },
                    {nameof(RB_Brand_Extend.PaiYan),model.PaiYan },
                    {nameof(RB_Brand_Extend.KongTiao),model.KongTiao },
                    {nameof(RB_Brand_Extend.XinFeng),model.XinFeng },
                    {nameof(RB_Brand_Extend.BrandClassId),model.BrandClassId },
                    {nameof(RB_Brand_Extend.RanQiBZ),model.RanQiBZ },
                    {nameof(RB_Brand_Extend.IsDianLiang),model.IsDianLiang },
                    {nameof(RB_Brand_Extend. PaiYanBZ),model. PaiYanBZ },
                    {nameof(RB_Brand_Extend.IsShopSize),model.IsShopSize },
                    {nameof(RB_Brand_Extend.OpenShopWish),model.OpenShopWish },
                    {nameof(RB_Brand_Extend.IsInChengdu),model.IsInChengdu },
                    {nameof(RB_Brand_Extend.ContactName),model.ContactName },
                    {nameof(RB_Brand_Extend.ContactPhone),model.ContactPhone }
                };
                return brandRepository.Update(fileds, new WhereHelper(nameof(RB_Brand_Extend.ID), model.ID));
            }
        }

        /// <summary>
        /// 品牌申请
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetBrandApplyFor(RB_BrandApplyFor_Extend model)
        {
            var cmodel = companyRepository.GetEntity(model.CompanyId);
            if (cmodel == null) { return false; }
            if (cmodel.FirstShopType != 1) { return false; }
            if (model.ID > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_BrandApplyFor_Extend.UpdateDate),model.UpdateDate },
                    {nameof(RB_BrandApplyFor_Extend.Logo),model.Logo },
                    {nameof(RB_BrandApplyFor_Extend.Banner),model.Banner },
                    {nameof(RB_BrandApplyFor_Extend.VideoUrl),model.VideoUrl },
                    {nameof(RB_BrandApplyFor_Extend.FullBrandName),model.FullBrandName },
                    {nameof(RB_BrandApplyFor_Extend.BrandName),model.BrandName },
                    {nameof(RB_BrandApplyFor_Extend.ShopNum),model.ShopNum },
                    {nameof(RB_BrandApplyFor_Extend.BuiltUpArea),model.BuiltUpArea },
                    {nameof(RB_BrandApplyFor_Extend.EndBuiltUpArea),model.EndBuiltUpArea },
                    {nameof(RB_BrandApplyFor_Extend.BrandType),model.BrandType },
                    {nameof(RB_BrandApplyFor_Extend.CustomerType),model.CustomerType },
                    {nameof(RB_BrandApplyFor_Extend.StoreExpansion),model.StoreExpansion },
                    {nameof(RB_BrandApplyFor_Extend.ProjectType),model.ProjectType },
                    {nameof(RB_BrandApplyFor_Extend.AreaRequirement),model.AreaRequirement },
                    {nameof(RB_BrandApplyFor_Extend.EndAreaRequirement),model.EndAreaRequirement },
                    {nameof(RB_BrandApplyFor_Extend.Advertising),model.Advertising },
                    {nameof(RB_BrandApplyFor_Extend.Other),model.Other },
                    {nameof(RB_BrandApplyFor_Extend.ShopSize),model.ShopSize },
                    {nameof(RB_BrandApplyFor_Extend.Plumbing),model.Plumbing },
                    {nameof(RB_BrandApplyFor_Extend.Caliber),model.Caliber },
                    {nameof(RB_BrandApplyFor_Extend.Sewage),model.Sewage },
                    {nameof(RB_BrandApplyFor_Extend.RanQi),model.RanQi },
                    {nameof(RB_BrandApplyFor_Extend.DianLiang),model.DianLiang },
                    {nameof(RB_BrandApplyFor_Extend.PaiYan),model.PaiYan },
                    {nameof(RB_BrandApplyFor_Extend.KongTiao),model.KongTiao },
                    {nameof(RB_BrandApplyFor_Extend.XinFeng),model.XinFeng },
                    {nameof(RB_BrandApplyFor_Extend.BrandClassId),model.BrandClassId },
                    {nameof(RB_BrandApplyFor_Extend.RanQiBZ),model.RanQiBZ },
                    {nameof(RB_BrandApplyFor_Extend.IsDianLiang),model.IsDianLiang },
                    {nameof(RB_BrandApplyFor_Extend.PaiYanBZ),model. PaiYanBZ },
                    {nameof(RB_BrandApplyFor_Extend.IsShopSize),model.IsShopSize },
                    {nameof(RB_BrandApplyFor_Extend.OpenShopWish),model.OpenShopWish },
                    {nameof(RB_BrandApplyFor_Extend.IsInChengdu),model.IsInChengdu },
                    {nameof(RB_BrandApplyFor_Extend.ExistBrandId),model.ExistBrandId },
                };
                bool flag = brandApplyForRepository.Update(fileds, new WhereHelper(nameof(RB_BrandApplyFor_Extend.ID), model.ID));
                if (flag)
                {
                    //更新认证表  资料状态为完成
                    Dictionary<string, object> keyValues = new Dictionary<string, object>()
                    {
                        { nameof(RB_Company_Extend.IsComplete),1}
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>()
                    {
                        new WhereHelper()
                        {
                             FiledName=nameof(RB_Company_Extend.CompanyId),
                             FiledValue= model.CompanyId,
                             OperatorEnum=OperatorEnum.Equal
                        }
                    };
                    companyRepository.Update(keyValues, wheres);
                }
                return flag;
            }
            else
            {
                #region 验证品牌数据是否已存在
                var oldList = brandApplyForRepository.GetBrandListByWhere(new RB_BrandApplyFor_Extend() { MallBaseId = model.MallBaseId, CompanyId = model.CompanyId });
                if (oldList.Any())
                {
                    return false;
                }
                #endregion
                bool flag = brandApplyForRepository.Insert(model) > 0;
                if (flag)
                {
                    if (Config.IsFirtsShopAutoExamine == "1")//自动审核
                    {
                        //更新认证表  资料状态为完成
                        Dictionary<string, object> keyValues = new Dictionary<string, object>()
                        {
                            { nameof(RB_Company_Extend.IsComplete),1},
                            { nameof(RB_Company_Extend.CompanyStatus),1}
                         };
                        List<WhereHelper> wheres = new List<WhereHelper>()
                        {
                            new WhereHelper()
                            {
                                 FiledName=nameof(RB_Company_Extend.CompanyId),
                                 FiledValue= model.CompanyId,
                                 OperatorEnum=OperatorEnum.Equal
                            }
                         };
                        companyRepository.Update(keyValues, wheres);

                        try
                        {
                            var brandList = brandApplyForRepository.GetBrandListByWhere(new RB_BrandApplyFor_Extend { MallBaseId = cmodel.MallBaseId, CompanyId = cmodel.CompanyId });
                            if (brandList != null && brandList.Any())
                            {
                                var BrandModel = brandList.FirstOrDefault();
                                if (BrandModel.ExistBrandId > 0)
                                {
                                    Dictionary<string, object> filedsBrand = new Dictionary<string, object>()
                                    {
                                        {nameof(RB_Brand_Extend.UpdateDate),DateTime.Now },
                                        {nameof(RB_Brand_Extend.Logo),BrandModel.Logo },
                                        {nameof(RB_Brand_Extend.Banner),BrandModel.Banner },
                                        {nameof(RB_Brand_Extend.VideoUrl),BrandModel.VideoUrl },
                                        {nameof(RB_Brand_Extend.ShopNum),BrandModel.ShopNum },
                                        {nameof(RB_Brand_Extend.BuiltUpArea),BrandModel.BuiltUpArea },
                                        {nameof(RB_Brand_Extend.EndBuiltUpArea),BrandModel.EndBuiltUpArea },
                                        {nameof(RB_Brand_Extend.BrandType),BrandModel.BrandType },
                                        {nameof(RB_Brand_Extend.CustomerType),BrandModel.CustomerType },
                                        {nameof(RB_Brand_Extend.StoreExpansion),BrandModel.StoreExpansion },
                                        {nameof(RB_Brand_Extend.ProjectType),BrandModel.ProjectType },
                                        {nameof(RB_Brand_Extend.AreaRequirement),BrandModel.AreaRequirement },
                                        {nameof(RB_Brand_Extend.EndAreaRequirement),BrandModel.EndAreaRequirement },
                                        {nameof(RB_Brand_Extend.ShopSize),BrandModel.ShopSize },
                                        {nameof(RB_Brand_Extend.Plumbing),BrandModel.Plumbing },
                                        {nameof(RB_Brand_Extend.Caliber),BrandModel.Caliber },
                                        {nameof(RB_Brand_Extend.Sewage),BrandModel.Sewage },
                                        {nameof(RB_Brand_Extend.RanQi),BrandModel.RanQi },
                                        {nameof(RB_Brand_Extend.DianLiang),BrandModel.DianLiang },
                                        {nameof(RB_Brand_Extend.PaiYan),BrandModel.PaiYan },
                                        {nameof(RB_Brand_Extend.KongTiao),BrandModel.KongTiao },
                                        {nameof(RB_Brand_Extend.XinFeng),BrandModel.XinFeng },
                                        {nameof(RB_Brand_Extend.BrandClassId),BrandModel.BrandClassId },
                                        {nameof(RB_Brand_Extend.RanQiBZ),BrandModel.RanQiBZ },
                                        {nameof(RB_Brand_Extend.IsDianLiang),BrandModel.IsDianLiang },
                                        {nameof(RB_Brand_Extend.PaiYanBZ),BrandModel.PaiYanBZ },
                                        {nameof(RB_Brand_Extend.IsShopSize),BrandModel.IsShopSize },
                                        {nameof(RB_Brand_Extend.OpenShopWish),BrandModel.OpenShopWish },
                                        {nameof(RB_Brand_Extend.IsInChengdu),BrandModel.IsInChengdu },
                                        {nameof(RB_Brand_Extend.UserId),cmodel.CreateBy },
                                    };
                                    brandRepository.Update(filedsBrand, new WhereHelper(nameof(RB_Brand_Extend.ID), BrandModel.ExistBrandId));
                                }
                                else
                                {
                                    #region 新增品牌
                                    var inserBModel = BrandModel.RefMapperTo<RB_Brand_Extend>();
                                    inserBModel.ID = 0;
                                    inserBModel.UserId = cmodel.CreateBy;
                                    brandRepository.Insert(inserBModel);
                                    #endregion
                                }
                            }
                        }
                        catch
                        {


                        }
                    }
                    else
                    {
                        //更新认证表  资料状态为完成
                        Dictionary<string, object> keyValues = new Dictionary<string, object>()
                        {
                            { nameof(RB_Company_Extend.IsComplete),1}
                        };
                        List<WhereHelper> wheres = new List<WhereHelper>()
                        {
                            new WhereHelper()
                                {
                                    FiledName=nameof(RB_Company_Extend.CompanyId),
                                    FiledValue= model.CompanyId,
                                    OperatorEnum=OperatorEnum.Equal
                                }
                        };
                        companyRepository.Update(keyValues, wheres);
                    }

                }
                return flag;
            }
        }

        /// <summary>
        ///删除品牌状态
        /// </summary>
        /// <param name="CompanyId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool RemoveBrandModule(int CarrierId)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Brand_Extend.Status),1 },
            };
            bool flag = brandRepository.Update(fileds, new WhereHelper(nameof(RB_Brand_Extend.ID), CarrierId));
            return flag;
        }

        /// <summary>
        /// 获取品牌信息分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_BrandClass_Extend> GetBrandClassPageModule(int pageIndex, int pageSize, out long rowsCount, RB_BrandClass_Extend query)
        {
            return brandClassRepository.GetBrandClassPageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取品牌分类实体
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_BrandClass_Extend GetBrandClassModule(int Id)
        {
            var extModel = brandClassRepository.GetEntity<RB_BrandClass_Extend>(Id);
            return extModel;
        }

        /// <summary>
        /// 根据查询条件获取品牌信息实体
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_BrandClass_Extend> GetBrandClassListModule(RB_BrandClass_Extend query)
        {
            return brandClassRepository.GetBrandClassListRepository(query);
        }

        /// <summary>
        /// 获取商载通链接
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<object> GetBrandClassListLinkModule(RB_BrandClass_Extend query)
        {
            List<object> resultList = new List<object>();
            var rootList = Common.Plugin.EnumHelper.EnumToList(typeof(BrandCategoryEnum));
            List<RB_BrandClass_Extend> categoryList = GetBrandClassListModule(new RB_BrandClass_Extend() { TenantId = query.TenantId, MallBaseId = query.MallBaseId });

            if (rootList != null && rootList.Count > 0)
            {
                foreach (var firstItem in rootList)
                {
                    var subList = categoryList.Where(qitem => qitem.BrandCategory.ToInt() == firstItem.Id).ToList();
                    List<object> secondResultList = new List<object>();
                    foreach (var secondItem in subList)
                    {
                        string secondUrl ="";
                        //载体
                        if (firstItem.Id == 1)
                        {
                            secondUrl = "/pages/kotra/carrier/list?id=" + secondItem.ID;
                        }
                        //楼宇
                        else if (firstItem.Id == 2)
                        {
                            secondUrl = "/pages/kotra/carrier/list?id=" + secondItem.ID;
                        }
                        //消费品牌
                        else if (firstItem.Id == 3)
                        {
                            secondUrl = "/pages/kotra/brand/list?id=" + secondItem.ID;
                        }
                        //企业服务
                        else
                        {
                            secondUrl = "/pages/kotra/brand/list?id=" + secondItem.ID;
                        }
                        secondResultList.Add(new
                        {
                            PageId = secondItem.ID + 300000,
                            PageName = secondItem.ClassName,
                            PageUrl = secondUrl,
                            PageType = Common.Enum.MallBase.PageTypeEnum.DiyPage,
                            IsParameter = 0,
                            TipText = "",
                            SelfPageName = secondItem.ClassName,
                            ParameterValue = "",
                            SubMenuList = new List<int>(),
                        });
                    }

                    string rootUrl = "";
                    //载体
                    if (firstItem.Id == 1)
                    {
                        rootUrl = "/pages/kotra/carrier/list";
                    }
                    //楼宇
                    else if (firstItem.Id == 2)
                    {
                        rootUrl = "/pages/kotra/carrier/list";
                    }
                    //消费品牌
                    else if (firstItem.Id == 3)
                    {
                        rootUrl = "/pages/kotra/brand/list";
                    }
                    //企业服务
                    else
                    {
                        rootUrl = "/pages/kotra/brand/list";
                    }
                    resultList.Add(new
                    {
                        PageId = firstItem.Id + 200000,
                        PageName = firstItem.Name,
                        PageUrl = rootUrl,
                        PageType = Common.Enum.MallBase.PageTypeEnum.DiyPage,
                        IsParameter = 0,
                        TipText = "",
                        SelfPageName = firstItem.Name,
                        ParameterValue = "",
                        SubMenuList = secondResultList,
                    });
                }
            }
            return resultList;
        }

        /// <summary>
        /// 新增/修改品牌分类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetBrandClassModule(RB_BrandClass_Extend model)
        {
            bool flag = false;
            if (model.ID > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_BrandClass_Extend.ClassName),model.ClassName },
                    {nameof(RB_BrandClass_Extend.Logo),model.Logo },
                    {nameof(RB_BrandClass_Extend.BrandCategory),model.BrandCategory},
                    {nameof(RB_BrandClass_Extend.Status),model.Status },
                    {nameof(RB_BrandClass_Extend.UpdateDate),model.UpdateDate },
                };
                flag = brandClassRepository.Update(fileds, new WhereHelper(nameof(RB_BrandClass_Extend.ID), model.ID));
            }
            else
            {
                var newId = brandClassRepository.Insert(model);
                model.ID = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        ///删除品牌状态
        /// </summary>
        /// <param name="CompanyId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool RemoveBrandClassModule(int CarrierId)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_BrandClass_Extend.Status),1 },
            };
            bool flag = brandClassRepository.Update(fileds, new WhereHelper(nameof(RB_BrandClass_Extend.ID), CarrierId));

            return flag;
        }

        #endregion

        #region 收藏
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Collect_Extend> GetPageList(int pageIndex, int pageSize, out long rowCount, RB_Collect_Extend query)
        {
            var list = collectRepository.GetPageList(pageIndex, pageSize, out rowCount, query);
            string sourceIds = "";
            if (list != null && list.Any())
            {
                sourceIds = string.Join(",", list.Select(x => x.SourceId));
            }
            if (query.Type == 1)//1-载体
            {
                if (!string.IsNullOrEmpty(sourceIds))
                {
                    var sourceList = GetCarrierList(new RB_Carrier_Extend { TenantId = query.TenantId, MallBaseId = query.MallBaseId, QCarrierIds = sourceIds });
                    foreach (var item in list)
                    {
                        RB_Carrier_Extend carrierModel = sourceList.Where(x => x.ID == item.SourceId).FirstOrDefault();
                        if (carrierModel != null && carrierModel.ID > 0)
                        {
                            item.SourceName = carrierModel.CarrierName;
                            item.Address = carrierModel.Address;
                            item.ProjectType = carrierModel.ProjectType;
                            item.BannerList = carrierModel.BannerList;
                        }
                    }
                }
            }
            else if (query.Type == 2)//2-品牌
            {
                if (!string.IsNullOrEmpty(sourceIds))
                {
                    var sourceList = GetBrandListByWhere(new RB_Brand_Extend
                    {
                        TenantId = query.TenantId,
                        MallBaseId = query.MallBaseId,
                        QBrandIds = sourceIds,
                        RanQi = -1,
                        KongTiao = -1,
                        XinFeng = -1,
                        Plumbing = -1,
                        Caliber = -1,
                        Sewage = -1,
                        PaiYan = -1,
                    });
                    foreach (var item in list)
                    {
                        RB_Brand_Extend carrierModel = sourceList.Where(x => x.ID == item.SourceId).FirstOrDefault();
                        if (carrierModel != null && carrierModel.ID > 0)
                        {
                            if (!string.IsNullOrWhiteSpace(carrierModel.Banner))
                            {
                                carrierModel.BannerList = JsonConvert.DeserializeObject<List<string>>(carrierModel.Banner);
                            }
                            item.SourceName = carrierModel.BrandName;
                            item.SourceFullName = carrierModel.FullBrandName;
                            item.ProjectType = carrierModel.ProjectType;
                            item.BannerList = carrierModel.BannerList;
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 收藏列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Collect_Extend> GetList(RB_Collect_Extend query)
        {
            return collectRepository.GetList(query);
        }


        /// <summary>
        /// 新增/修改我关注的会员配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddOrUpdateCollect(RB_Collect_Extend model)
        {
            bool flag;
            try
            {
                int Id = 0;
                if (model.Id == 0)
                {
                    Id = collectRepository.Insert(model);
                    flag = Id > 0;
                }
                else
                {
                    Id = model.Id;
                    Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                        { nameof(RB_Collect_Extend.Status),model.Status}
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>() {
                        new WhereHelper(){
                             FiledName=nameof(RB_Collect_Extend.Id),
                              FiledValue=model.Id,
                               OperatorEnum=OperatorEnum.Equal
                        }
                    };

                    flag = collectRepository.Update(keyValues, wheres);
                }
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "AddOrUpdateCollect");
                return false;
            }
            return flag;
        }





        #endregion


        #region 奖项
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Prize_Extend> GetPrizePageList(int pageIndex, int pageSize, out long rowCount, RB_Prize_Extend query)
        {
            return prizeRepository.GetPageList(pageIndex, pageSize, out rowCount, query);
        }


        /// <summary>
        /// 奖项列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Prize_Extend> GetPrizeList(RB_Prize_Extend query)
        {
            return prizeRepository.GetList(query);
        }

        /// <summary>
        /// 新增/修改奖项
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetPrize(RB_Prize_Extend model)
        {
            if (model.Id == 0)
            {
                return prizeRepository.Insert(model) > 0;
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Prize_Extend.Name),model.Name },
                     {nameof(RB_Prize_Extend.YearInfo),model.YearInfo },
                    {nameof(RB_Prize_Extend.BigIco),model.BigIco },
                    {nameof(RB_Prize_Extend.SmallIco),model.SmallIco }
                };
                return prizeRepository.Update(fileds, new WhereHelper(nameof(RB_Prize_Extend.Id), model.Id));
            }
        }


        /// <summary>
        ///删除奖项状态
        /// </summary>
        /// <param name="CompanyId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool RemovePrizeModule(int prizeId)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Prize_Extend.Status),1 },
            };
            bool flag = prizeRepository.Update(fileds, new WhereHelper(nameof(RB_Prize_Extend.Id), prizeId));
            return flag;
        }
        #endregion

        /// <summary>
        /// 获取配置项
        /// </summary>
        /// <returns></returns>
        public object GetYBConfigModule()
        {
            List<YBConfigItem> zhuangXiuList = new List<YBConfigItem>();
            zhuangXiuList.Add(new YBConfigItem() { Name = "清水交付" });
            zhuangXiuList.Add(new YBConfigItem() { Name = "精装交付" });

            List<YBConfigItem> fukuanList = new List<YBConfigItem>();
            fukuanList.Add(new YBConfigItem() { Name = "年付" });
            fukuanList.Add(new YBConfigItem() { Name = "押一付三" });
            fukuanList.Add(new YBConfigItem() { Name = "押一付一" });
            fukuanList.Add(new YBConfigItem() { Name = "半年付" });

            List<YBConfigItem> zichanList = new List<YBConfigItem>();
            zichanList.Add(new YBConfigItem() { Name = "国有资产" });
            zichanList.Add(new YBConfigItem() { Name = "私企" });
            zichanList.Add(new YBConfigItem() { Name = "个人" });
            zichanList.Add(new YBConfigItem() { Name = "其它" });

            List<YBConfigItem> wuyeList = new List<YBConfigItem>();
            wuyeList.Add(new YBConfigItem() { Name = "开口尺寸" });
            wuyeList.Add(new YBConfigItem() { Name = "上下水" });
            wuyeList.Add(new YBConfigItem() { Name = "管径" });
            wuyeList.Add(new YBConfigItem() { Name = "排污" });
            wuyeList.Add(new YBConfigItem() { Name = "燃气" });
            wuyeList.Add(new YBConfigItem() { Name = "电量负荷" });
            wuyeList.Add(new YBConfigItem() { Name = "排烟量" });
            wuyeList.Add(new YBConfigItem() { Name = "空调" });
            wuyeList.Add(new YBConfigItem() { Name = "新风" });
            List<Model.Entity.TradePavilion.RB_Building_Carrier> MallBaselList = new List<Model.Entity.TradePavilion.RB_Building_Carrier>();
            MallBaselList.Add(new Model.Entity.TradePavilion.RB_Building_Carrier { MallBaseId = 0, TenantId = 0, CarrierName = "全部" });
            MallBaselList.Add(new Model.Entity.TradePavilion.RB_Building_Carrier { MallBaseId = 8, TenantId = 18, CarrierName = "成都" });
            MallBaselList.Add(new Model.Entity.TradePavilion.RB_Building_Carrier { MallBaseId = 20, TenantId = 30, CarrierName = "宜宾" });
            object obj = new
            {
                zhuangXiuList,
                fukuanList,
                zichanList,
                wuyeList,
                mallBaselList = MallBaselList.Select(x => new { x.TenantId, x.MallBaseId, Name = x.CarrierName }),
            };
            return obj;
        }
    }
}
