using Mall.Common;
using Mall.Common.Plugin;
using Mall.Model.Extend.TradePavilion;
using Mall.Module.User;
using Mall.Repository.TradePavilion;
using Mall.Repository.User;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using VT.FW.DB;

namespace Mall.Module.TradePavilion
{
    /// <summary>
    /// 榜单处理类
    /// </summary>
    public class FirstShopListModule
    {
        /// <summary>
        /// 榜单
        /// </summary>
        private readonly RB_FirstShop_ListRepository firstShop_ListRepository = new RB_FirstShop_ListRepository();
        /// <summary>
        /// 报名
        /// </summary>
        private readonly RB_FirstShop_ListEnrollRepository firstShop_ListEnrollRepository = new RB_FirstShop_ListEnrollRepository();
        /// <summary>
        /// 品牌
        /// </summary>
        private readonly RB_BrandRepository brandRepository = new RB_BrandRepository();
        /// <summary>
        /// 载体
        /// </summary>
        private readonly RB_CarrierRepository carrierRepository = new RB_CarrierRepository();
        /// <summary>
        /// 企业信息
        /// </summary>
        private readonly RB_CompanyRepository companyRepository = new RB_CompanyRepository();
        /// <summary>
        /// 用户
        /// </summary>
        private readonly RB_Member_UserRepository member_UserRepository = new RB_Member_UserRepository();

        /// <summary>
        /// 获取榜单分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_FirstShop_List_Extend> GetFirstShopListPageList(int pageIndex, int pageSize, out long rowsCount, RB_FirstShop_List_Extend query)
        {
            var list = firstShop_ListRepository.GetPageList(pageIndex, pageSize, out rowsCount, query);
            if (list.Any())
            {
                foreach (var item in list)
                {
                    if (item.ListState == Common.Enum.TradePavilion.ListStateEnum.Enroll && item.StartDate > DateTime.Now)
                    {
                        item.ListState = Common.Enum.TradePavilion.ListStateEnum.NotStarted;
                    }
                    else if (item.ListState == Common.Enum.TradePavilion.ListStateEnum.Enroll && item.EndDate <= DateTime.Now)
                    {
                        item.ListState = Common.Enum.TradePavilion.ListStateEnum.Rating;
                    }
                    item.ObjectTypeList = new List<string>();
                    if (!string.IsNullOrEmpty(item.ObjectTypeIds))
                    {
                        List<int> OIdList = JsonHelper.DeserializeObject<List<int>>("[" + item.ObjectTypeIds + "]");
                        foreach (var qitem in OIdList)
                        {
                            item.ObjectTypeList.Add(((Common.Enum.TradePavilion.ListObjectTypeEnum)qitem).GetEnumName());
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 获取小程序 - 榜单分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_FirstShop_List_Extend> GetAppletFirstShopListPageList(int pageIndex, int pageSize, out long rowsCount, RB_FirstShop_List_Extend query)
        {
            var list = firstShop_ListRepository.GetPageList(pageIndex, pageSize, out rowsCount, query);
            if (list.Any())
            {
                foreach (var item in list)
                {
                    if (item.ListState == Common.Enum.TradePavilion.ListStateEnum.Enroll && item.StartDate <= DateTime.Now && item.EndDate >= DateTime.Now)
                    {
                        item.State = 1;//进行中
                    }
                    else if (item.ListState == Common.Enum.TradePavilion.ListStateEnum.Enroll && item.EndDate <= DateTime.Now)
                    {
                        item.State = 2;//报名已截止
                    }
                    else if (item.ListState == Common.Enum.TradePavilion.ListStateEnum.Confirmed)
                    {
                        item.State = 3;//已放榜
                    }

                    item.ObjectTypeList = new List<string>();
                    if (!string.IsNullOrEmpty(item.ObjectTypeIds))
                    {
                        List<int> OIdList = JsonHelper.DeserializeObject<List<int>>("[" + item.ObjectTypeIds + "]");
                        foreach (var qitem in OIdList)
                        {
                            item.ObjectTypeList.Add(((Common.Enum.TradePavilion.ListObjectTypeEnum)qitem).GetEnumName());
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 获取榜单详情
        /// </summary>
        /// <param name="wishId"></param>
        /// <returns></returns>
        public RB_FirstShop_List_Extend GetFirstShopListInfo(int listId)
        {
            return firstShop_ListRepository.GetEntity<RB_FirstShop_List_Extend>(listId);
        }

        /// <summary>
        /// 小程序获取榜单详情
        /// </summary>
        /// <param name="listId"></param>
        /// <returns></returns>
        public RB_FirstShop_List_Extend GetAppletFirstShopListInfo(int listId, Common.AppletUserInfo userInfo)
        {
            var model = firstShop_ListRepository.GetEntity<RB_FirstShop_List_Extend>(listId);
            if (model != null)
            {
                if (model.ListState == Common.Enum.TradePavilion.ListStateEnum.Confirmed)
                {
                    //已放榜  查询所有上榜的列表
                    var enrollList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = userInfo.MallBaseId, ListId = listId, RankListState = 1 });
                    //查询 品牌/载体  名称 图片
                    if (enrollList.Any())
                    {
                        var e1list = enrollList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Brand).ToList();
                        var e2list = enrollList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Carrier).ToList();
                        if (e1list.Any())
                        {
                            //查询品牌
                            string bids = string.Join(",", e1list.Select(x => x.ObjectId));
                            var blist = brandRepository.GetBrandList(new RB_Brand_Extend() { MallBaseId = userInfo.MallBaseId, BrandIds = bids });
                            foreach (var item in e1list)
                            {
                                var bmodel = blist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                                item.ObjectName = bmodel?.BrandName ?? "";
                                item.ObjectIcon = bmodel?.Logo ?? "";
                            }
                        }
                        if (e2list.Any())
                        {
                            //查询载体
                            string cids = string.Join(",", e2list.Select(x => x.ObjectId));
                            var clist = carrierRepository.GetCarrierList(new RB_Carrier_Extend() { MallBaseId = userInfo.MallBaseId, CarrierIds = cids });
                            foreach (var item in e2list)
                            {
                                var cmodel = clist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                                item.ObjectName = cmodel?.CarrierName ?? "";
                                item.ObjectIcon = cmodel?.Logo ?? "";
                            }
                        }
                    }
                    model.RankList = enrollList;
                }
                else
                {
                    //查询个人报名记录
                    var enrollList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = userInfo.MallBaseId, ListId = listId, UserId = userInfo.UserId });
                    //查询 品牌/载体  名称 图片
                    if (enrollList.Any())
                    {
                        var e1list = enrollList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Brand).ToList();
                        var e2list = enrollList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Carrier).ToList();
                        if (e1list.Any())
                        {
                            //查询品牌
                            string bids = string.Join(",", e1list.Select(x => x.ObjectId));
                            var blist = brandRepository.GetBrandList(new RB_Brand_Extend() { MallBaseId = userInfo.MallBaseId, BrandIds = bids });
                            foreach (var item in e1list)
                            {
                                var bmodel = blist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                                item.ObjectName = bmodel?.BrandName ?? "";
                                item.ObjectIcon = bmodel?.Logo ?? "";
                            }
                        }
                        if (e2list.Any())
                        {
                            //查询载体
                            string cids = string.Join(",", e2list.Select(x => x.ObjectId));
                            var clist = carrierRepository.GetCarrierList(new RB_Carrier_Extend() { MallBaseId = userInfo.MallBaseId, CarrierIds = cids });
                            foreach (var item in e2list)
                            {
                                var cmodel = clist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                                item.ObjectName = cmodel?.CarrierName ?? "";
                                item.ObjectIcon = cmodel?.Logo ?? "";
                            }
                        }
                    }
                    model.EnrollList = enrollList;
                }
            }
            return model;
        }

        /// <summary>
        /// 新增修改榜单
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public string SetFirstShopListInfo(RB_FirstShop_List_Extend demodel)
        {
            if (demodel.Id > 0)
            {
                var oldModel = firstShop_ListRepository.GetEntity(demodel.Id);
                if (oldModel == null || oldModel.Status == 1 || oldModel.ListState == Common.Enum.TradePavilion.ListStateEnum.Cancel || oldModel.ListState == Common.Enum.TradePavilion.ListStateEnum.Confirmed)
                {
                    return "绑单状态不正确";
                }

                //修改
                Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                    { nameof(RB_FirstShop_List_Extend.ListType),demodel.ListType },
                    { nameof(RB_FirstShop_List_Extend.ObjectTypeIds),demodel.ObjectTypeIds },
                    { nameof(RB_FirstShop_List_Extend.Sponsor),demodel.Sponsor },
                    { nameof(RB_FirstShop_List_Extend.ListName),demodel.ListName },
                    { nameof(RB_FirstShop_List_Extend.CoverImage),demodel.CoverImage },
                    { nameof(RB_FirstShop_List_Extend.FinishDate),demodel.FinishDate },
                    { nameof(RB_FirstShop_List_Extend.StartDate),demodel.StartDate },
                    { nameof(RB_FirstShop_List_Extend.EndDate),demodel.EndDate },
                    { nameof(RB_FirstShop_List_Extend.Content),demodel.Content },
                    { nameof(RB_FirstShop_List_Extend.EnableRank),demodel.EnableRank },
                    { nameof(RB_FirstShop_List_Extend.PrizeNum),demodel.PrizeNum },
                    { nameof(RB_FirstShop_List_Extend.UpdateBy),demodel.UpdateBy },
                    { nameof(RB_FirstShop_List_Extend.UpdateDate),demodel.UpdateDate }
                };
                List<WhereHelper> wheres = new List<WhereHelper>() {
                    new WhereHelper(){
                         FiledName=nameof(RB_FirstShop_List_Extend.Id),
                          FiledValue=demodel.Id,
                           OperatorEnum=OperatorEnum.Equal
                    }
                };
                bool flag = firstShop_ListRepository.Update(keyValues, wheres);
                return flag ? "" : "修改失败,请联系管理员";
            }
            else
            {
                //新增
                bool flag = firstShop_ListRepository.Insert(demodel) > 0;
                return flag ? "" : "新增失败，请联系管理员";
            }
        }

        /// <summary>
        /// 取消榜单
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public string DelFirstShopListInfo(int listId, int mallBaseId)
        {
            Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                { nameof(RB_FirstShop_List_Extend.ListState), Common.Enum.TradePavilion.ListStateEnum.Cancel}
            };
            List<WhereHelper> wheres = new List<WhereHelper>() {
                new WhereHelper(){
                     FiledName=nameof(RB_FirstShop_List_Extend.Id),
                      FiledValue=listId,
                       OperatorEnum=OperatorEnum.Equal
                },
                new WhereHelper(){
                     FiledName= nameof(RB_FirstShop_List_Extend.MallBaseId),
                      FiledValue= mallBaseId,
                       OperatorEnum=OperatorEnum.Equal
                }
            };
            bool flag = firstShop_ListRepository.Update(keyValues, wheres);
            return flag ? "" : "取消失败";
        }

        /// <summary>
        /// 获取用户可报表的品牌/载体列表
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public object GetAppletMyBrandCarrierList(int listId, int enrollId, AppletUserInfo userInfo)
        {
            List<object> RList = new List<object>();
            //查询该榜单  
            var listModel = firstShop_ListRepository.GetEntity(listId);
            if (listModel == null) { return null; }
            var enrollList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = userInfo.MallBaseId, ListId = listId, UserId = userInfo.UserId });
            if (enrollId > 0) { enrollList = enrollList.Where(x => x.Id != enrollId).ToList(); }//排除当前修改的
            //查询用户下所有的品牌
            if (listModel.ObjectTypeIds.Contains("1"))
            {
                //品牌
                var blist = brandRepository.GetBrandList(new RB_Brand_Extend() { MallBaseId = userInfo.MallBaseId, UserId = userInfo.UserId });
                blist = blist.Where(x => !enrollList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Brand).Select(y => y.ObjectId).Contains(x.ID)).ToList();
                if (blist.Any())
                {
                    foreach (var item in blist)
                    {
                        RList.Add(new
                        {
                            Id = item.ID,
                            Name = item.BrandName,
                            ObjectType = 1
                        });
                    }
                }
            }
            //查询用户下所有的载体
            if (listModel.ObjectTypeIds.Contains("2"))
            {
                var clist = carrierRepository.GetCarrierList(new RB_Carrier_Extend() { MallBaseId = userInfo.MallBaseId, UserId = userInfo.UserId });
                clist = clist.Where(x => !enrollList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Carrier).Select(y => y.ObjectId).Contains(x.ID)).ToList();
                if (clist.Any())
                {
                    foreach (var item in clist)
                    {
                        RList.Add(new
                        {
                            Id = item.ID,
                            Name = item.CarrierName,
                            ObjectType = 2
                        });
                    }
                }
            }

            return RList;
        }

        /// <summary>
        /// 新增修改报名
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public string SetAppletFirstShopListInfo(RB_FirstShop_ListEnroll_Extend demodel)
        {
            #region 验证用户是否已认证
            var companyModel = companyRepository.GetCompanyRepository(new RB_Company_Extend() { MallBaseId = demodel.MallBaseId, CreateBy = demodel.UserId });
            if (companyModel == null || companyModel.CompanyStatus != 1) { return "未认证，无法进行报名"; }
            #endregion
            var listModel = firstShop_ListRepository.GetEntity(demodel.ListId);
            if (listModel == null || listModel.Status == 1 || listModel.ListState == Common.Enum.TradePavilion.ListStateEnum.Cancel)
            {
                return "榜单不存在，请核实后再试";
            }
            if (listModel.StartDate > DateTime.Now) { return "榜单还未开始报名，无法报名"; }
            if (listModel.EndDate < DateTime.Now) { return "榜单已截止报名"; }

            if (demodel.Id > 0)
            {
                #region 验证
                var valistList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = demodel.MallBaseId, ListId = demodel.ListId, UserId = demodel.UserId, ObjectType = demodel.ObjectType, ObjectId = demodel.ObjectId });
                if (valistList.Where(x => x.Id != demodel.Id).Any())
                {
                    return "该品牌/载体已报名，无法重复报名";
                }
                #endregion
                Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                    { nameof(RB_FirstShop_ListEnroll_Extend.ObjectType),demodel.ObjectType},
                    { nameof(RB_FirstShop_ListEnroll_Extend.ObjectId),demodel.ObjectId},
                    { nameof(RB_FirstShop_ListEnroll_Extend.Content),demodel.Content},
                };
                List<WhereHelper> wheres = new List<WhereHelper>() {
                    new WhereHelper(){
                         FiledName=nameof(RB_FirstShop_ListEnroll_Extend.Id),
                          FiledValue= demodel.Id,
                           OperatorEnum=OperatorEnum.Equal
                    }
                };
                bool flag = firstShop_ListEnrollRepository.Update(keyValues, wheres);
                return flag ? "" : "修改失败，请联系管理人员";
            }
            else
            {
                #region 验证
                var valistList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = demodel.MallBaseId, ListId = demodel.ListId, UserId = demodel.UserId, ObjectType = demodel.ObjectType, ObjectId = demodel.ObjectId });
                if (valistList.Any())
                {
                    return "该品牌/载体已报名，无法重复报名";
                }
                #endregion
                bool flag = firstShop_ListEnrollRepository.Insert(demodel) > 0;
                return flag ? "" : "报名失败，请联系管理人员";
            }
        }

        /// <summary>
        /// 取消报名
        /// </summary>
        /// <param name="enrollId"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public string CancelAppletFirstShopListInfo(int enrollId, AppletUserInfo userInfo)
        {
            Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                { nameof(RB_FirstShop_ListEnroll_Extend.Status),1},
                { nameof(RB_FirstShop_ListEnroll_Extend.UpdateDate),DateTime.Now},
            };
            List<WhereHelper> wheres = new List<WhereHelper>() {
                new WhereHelper(){
                        FiledName=nameof(RB_FirstShop_ListEnroll_Extend.Id),
                        FiledValue= enrollId,
                        OperatorEnum=OperatorEnum.Equal
                }
            };
            bool flag = firstShop_ListEnrollRepository.Update(keyValues, wheres);
            return flag ? "" : "取消失败，请联系管理员";
        }

        /// <summary>
        /// 获取小程序  我的报名榜单列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_FirstShop_List_Extend> GetAppletMyEnrollFistShopListPageList(int pageIndex, int pageSize, out long count, RB_FirstShop_List_Extend demodel)
        {
            var list = firstShop_ListRepository.GetAppletMyEnrollFistShopListPageList(pageIndex, pageSize, out count, demodel);
            return list;
        }

        /// <summary>
        /// 获取报名分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_FirstShop_ListEnroll_Extend> GetFirstShopEnrollPageList(int pageIndex, int pageSize, out long count, RB_FirstShop_ListEnroll_Extend demodel)
        {
            var list = firstShop_ListEnrollRepository.GetPageList(pageIndex, pageSize, out count, demodel);
            if (list.Any())
            {
                string userIds = string.Join(",", list.Select(x => x.UserId).Distinct());
                //企业名称
                var companylist = companyRepository.GetCompanyListRepository(new RB_Company_Extend() { MallBaseId = demodel.MallBaseId, UserIds = userIds });
                foreach (var item in list)
                {
                    var companyModel = companylist.Where(x => x.CreateBy == item.UserId).FirstOrDefault();
                    item.CompanyName = companyModel.CompanyName;
                    item.Principal = companyModel.Principal;
                    item.Mobile = companyModel.Mobile;
                }

                //查询 品牌/载体  名称 图片
                var e1list = list.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Brand).ToList();
                var e2list = list.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Carrier).ToList();
                if (e1list.Any())
                {
                    //查询品牌
                    string bids = string.Join(",", e1list.Select(x => x.ObjectId));
                    var blist = brandRepository.GetBrandList(new RB_Brand_Extend() { MallBaseId = demodel.MallBaseId, BrandIds = bids });
                    foreach (var item in e1list)
                    {
                        var bmodel = blist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                        item.ObjectName = bmodel?.BrandName ?? "";
                        item.ObjectIcon = bmodel?.Logo ?? "";
                    }
                }
                if (e2list.Any())
                {
                    //查询载体
                    string cids = string.Join(",", e2list.Select(x => x.ObjectId));
                    var clist = carrierRepository.GetCarrierList(new RB_Carrier_Extend() { MallBaseId = demodel.MallBaseId, CarrierIds = cids });
                    foreach (var item in e2list)
                    {
                        var cmodel = clist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                        item.ObjectName = cmodel?.CarrierName ?? "";
                        item.ObjectIcon = cmodel?.Logo ?? "";
                    }
                }
            }
            return list;
        }

        #region 报名excel导出
        /// <summary>
        /// 单榜单excel导出
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public List<ExcelDataSource> GetFirstShopListEnrollToExcel(int listId, int mallBaseId)
        {
            List<ExcelDataSource> RList = new List<ExcelDataSource>();
            var enrollList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = mallBaseId, ListId = listId });

            if (enrollList.Any())
            {
                string userIds = string.Join(",", enrollList.Select(x => x.UserId).Distinct());
                var clist = companyRepository.GetCompanyListRepository(new RB_Company_Extend() { MallBaseId = mallBaseId, UserIds = userIds });
                //查询 品牌/载体  名称 图片
                var e1list = enrollList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Brand).ToList();
                var e2list = enrollList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Carrier).ToList();
                if (e1list.Any())
                {
                    //查询品牌
                    string bids = string.Join(",", e1list.Select(x => x.ObjectId));
                    var blist = brandRepository.GetBrandList(new RB_Brand_Extend() { MallBaseId = mallBaseId, BrandIds = bids });
                    foreach (var item in e1list)
                    {
                        var bmodel = blist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                        item.ObjectName = bmodel?.BrandName ?? "";
                        item.ObjectIcon = bmodel?.Logo ?? "";
                    }
                }
                if (e2list.Any())
                {
                    //查询载体
                    string cids = string.Join(",", e2list.Select(x => x.ObjectId));
                    var carrlist = carrierRepository.GetCarrierList(new RB_Carrier_Extend() { MallBaseId = mallBaseId, CarrierIds = cids });
                    foreach (var item in e2list)
                    {
                        var cmodel = carrlist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                        item.ObjectName = cmodel?.CarrierName ?? "";
                        item.ObjectIcon = cmodel?.Logo ?? "";
                    }
                }

                #region 组装头部
                var enrollModel = enrollList.FirstOrDefault();
                if (string.IsNullOrEmpty(enrollModel.Content))
                {
                    return new List<ExcelDataSource>();
                }
                ExcelDataSource header = new ExcelDataSource()
                {
                    ExcelRows = new List<ExcelColumn>(30) {
                        new ExcelColumn(value: "企业名称") { CellWidth = 25, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                        new ExcelColumn(value: "类型") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                        new ExcelColumn(value: "对象名称") { CellWidth = 20, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER }
                    },
                    ColumnHight = 30
                };
                List<FormDataItem> elist = JsonHelper.DeserializeObject<List<FormDataItem>>(enrollModel.Content);
                if (elist.Any())
                {
                    foreach (var item in elist)
                    {
                        var obj = JsonHelper.DeserializeObject<UploadItem>(item.CompData.ToString());
                        header.ExcelRows.Add(new ExcelColumn(value: obj.Name) { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER });
                    }
                }
                header.ExcelRows.Add(new ExcelColumn(value: "状态") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER });
                header.ExcelRows.Add(new ExcelColumn(value: "评语") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER });
                header.ExcelRows.Add(new ExcelColumn(value: "报名时间") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER });
                RList.Add(header);
                #endregion

                #region 组装数据
                foreach (var item in enrollList)
                {
                    var cmodel = clist.Where(x => x.CreateBy == item.UserId).FirstOrDefault();
                    ExcelDataSource row = new ExcelDataSource()
                    {
                        ExcelRows = new List<ExcelColumn>(30) {
                            new ExcelColumn(value: cmodel?.CompanyName??"") { },
                            new ExcelColumn(value: item.ObjectType.GetEnumName()) { },
                            new ExcelColumn(value: item.ObjectName) { },
                        },
                        ColumnHight = 30
                    };
                    elist = JsonHelper.DeserializeObject<List<FormDataItem>>(item.Content);
                    if (elist.Any())
                    {
                        foreach (var qitem in elist)
                        {
                            string value = "";
                            if (qitem.CompKey == "SingleLineText" || qitem.CompKey == "MultiLineText")
                            {
                                var objv = JsonHelper.DeserializeObject<TextItem>(qitem.CompData.ToString());
                                value = objv.TextValue;
                            }
                            else if (qitem.CompKey == "DorpDownList")
                            {
                                var objv = JsonHelper.DeserializeObject<WordSelectItem>(qitem.CompData.ToString());
                                var olist = objv.OptionList.Where(x => ("," + objv.OptionValue + ",").Contains("," + x.Id + ",")).ToList();
                                value = string.Join(",", olist.Select(x => x.Name));
                            }
                            else
                            {
                                var objv = JsonHelper.DeserializeObject<UploadItem>(qitem.CompData.ToString());
                                value = string.Join(",", objv.FileList);
                            }
                            row.ExcelRows.Add(new ExcelColumn(value: value) { });
                        }
                    }
                    row.ExcelRows.Add(new ExcelColumn(value: item.RankListState == 1 ? "上榜" : "未上榜") { });
                    row.ExcelRows.Add(new ExcelColumn(value: item.Reviews) { });
                    row.ExcelRows.Add(new ExcelColumn(value: item.CreateDate.ToString("yyyy-MM-dd HH:mm:ss")) { });
                    RList.Add(row);
                }
                #endregion
            }
            return RList;
        }

        /// <summary>
        /// 多榜单excel导出
        /// </summary>
        /// <param name="listIds"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public List<ExcelDataSource> GetFirstShopMoreListEnrollToExcel(string listIds, int mallBaseId)
        {
            List<ExcelDataSource> RList = new List<ExcelDataSource>();
            var list = firstShop_ListRepository.GetList(new RB_FirstShop_List_Extend() { MallBaseId = mallBaseId, ListIds = listIds });
            if (!list.Any()) { return new List<ExcelDataSource>(); }
            //查询报名记录
            var enrollList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = mallBaseId, ListIds = listIds });

            #region 组装头部
            ExcelDataSource header = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                        new ExcelColumn(value: "企业名称") { CellWidth = 25, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER }
                    },
                ColumnHight = 30
            };
            foreach (var item in list)
            {
                header.ExcelRows.Add(new ExcelColumn(value: item.ListName) { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER });
            }
            RList.Add(header);
            #endregion

            #region 组装数据
            if (enrollList.Any())
            {
                string userIds = string.Join(",", enrollList.Select(x => x.UserId).Distinct());
                var clist = companyRepository.GetCompanyListRepository(new RB_Company_Extend() { MallBaseId = mallBaseId, UserIds = userIds });
                foreach (var item in clist)
                {
                    ExcelDataSource rowData = new ExcelDataSource()
                    {
                        ExcelRows = new List<ExcelColumn>(30) {
                            new ExcelColumn(value: item.CompanyName) { }
                        },
                        ColumnHight = 30
                    };
                    foreach (var qitem in list)
                    {
                        bool IsOk = enrollList.Where(x => x.ListId == qitem.Id && x.UserId == item.CreateBy).Any();
                        rowData.ExcelRows.Add(new ExcelColumn(value: IsOk ? "√" : "") { });
                    }
                    RList.Add(rowData);
                }
            }
            #endregion
            return RList;
        }
        #endregion

        /// <summary>
        /// 设置报名状态
        /// </summary>
        /// <param name="enrollId"></param>
        /// <param name="type"></param>
        /// <param name="mallBaseId"></param>
        /// <param name="empId"></param>
        /// <returns></returns>
        public string SetFirstShopEnrollState(int enrollId, int type, int mallBaseId, int empId)
        {
            Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                { nameof(RB_FirstShop_ListEnroll_Extend.UpdateBy),empId},
                { nameof(RB_FirstShop_ListEnroll_Extend.UpdateDate),DateTime.Now}
            };
            if (type == 1)
            {
                //淘汰
                keyValues.Add(nameof(RB_FirstShop_ListEnroll_Extend.CandidateState), 3);
            }
            else if (type == 2)
            {
                //候选
                keyValues.Add(nameof(RB_FirstShop_ListEnroll_Extend.CandidateState), 2);
            }
            else if (type == 3)
            {
                //重置
                keyValues.Add(nameof(RB_FirstShop_ListEnroll_Extend.CandidateState), 1);
            }
            else
            {
                return "状态有误，请核实后再试";
            }
            List<WhereHelper> wheres = new List<WhereHelper>() {
                new WhereHelper(){
                     FiledName=nameof(RB_FirstShop_ListEnroll_Extend.Id),
                      FiledValue=enrollId,
                       OperatorEnum=OperatorEnum.Equal
                },
                new WhereHelper(){
                     FiledName = nameof(RB_FirstShop_ListEnroll_Extend.MallBaseId),
                      FiledValue=mallBaseId,
                       OperatorEnum=OperatorEnum.Equal
                }
            };
            bool flag = firstShop_ListEnrollRepository.Update(keyValues, wheres);
            return flag ? "" : "修改失败，请联系管理员";
        }

        /// <summary>
        /// 获取候选的报名列表
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public List<RB_FirstShop_ListEnroll_Extend> GetFirstShopEnrollCandidateList(int listId, int mallBaseId)
        {
            var list = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = mallBaseId, ListId = listId, IsSelectCandidate = 1 });
            if (list.Any())
            {
                string userIds = string.Join(",", list.Select(x => x.UserId).Distinct());
                //企业名称
                var companylist = companyRepository.GetCompanyListRepository(new RB_Company_Extend() { MallBaseId = mallBaseId, UserIds = userIds });
                foreach (var item in list)
                {
                    var companyModel = companylist.Where(x => x.CreateBy == item.UserId).FirstOrDefault();
                    item.CompanyName = companyModel.CompanyName;
                    item.Principal = companyModel.Principal;
                    item.Mobile = companyModel.Mobile;
                }

                //查询 品牌/载体  名称 图片
                var e1list = list.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Brand).ToList();
                var e2list = list.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Carrier).ToList();
                if (e1list.Any())
                {
                    //查询品牌
                    string bids = string.Join(",", e1list.Select(x => x.ObjectId));
                    var blist = brandRepository.GetBrandList(new RB_Brand_Extend() { MallBaseId = mallBaseId, BrandIds = bids });
                    foreach (var item in e1list)
                    {
                        var bmodel = blist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                        item.ObjectName = bmodel?.BrandName ?? "";
                        item.ObjectIcon = bmodel?.Logo ?? "";
                    }
                }
                if (e2list.Any())
                {
                    //查询载体
                    string cids = string.Join(",", e2list.Select(x => x.ObjectId));
                    var clist = carrierRepository.GetCarrierList(new RB_Carrier_Extend() { MallBaseId = mallBaseId, CarrierIds = cids });
                    foreach (var item in e2list)
                    {
                        var cmodel = clist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                        item.ObjectName = cmodel?.CarrierName ?? "";
                        item.ObjectIcon = cmodel?.Logo ?? "";
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 设置放榜
        /// </summary>
        /// <param name="type"></param>
        /// <param name="listId"></param>
        /// <param name="deList"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public string SetFirstShopConfirmListInfo(int type, int listId, List<RB_FirstShop_ListEnroll_Extend> deList, int mallBaseId, int empId)
        {
            var listModel = firstShop_ListRepository.GetEntity(listId);
            if (listModel == null || listModel.MallBaseId != mallBaseId) { return "榜单不存在"; }
            if (listModel.ListState == Common.Enum.TradePavilion.ListStateEnum.Confirmed) { return "已放榜，无法再次修改"; }
            if (listModel.PrizeNum != deList.Count()) { return "名单数量不正确"; }
            //首先清除历史评比
            firstShop_ListEnrollRepository.ClearHostoryAppraisal(listId, mallBaseId);
            int RankListState = type == 1 ? 3 : 2;
            if (type == 1)
            {
                //设置榜单为已放榜
                Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                    { nameof(RB_FirstShop_List_Extend.ListState),Common.Enum.TradePavilion.ListStateEnum.Confirmed},
                    { nameof(RB_FirstShop_List_Extend.UpdateBy),empId},
                    { nameof(RB_FirstShop_List_Extend.UpdateDate),DateTime.Now},
                };
                List<WhereHelper> wheres = new List<WhereHelper>() {
                    new WhereHelper(){
                         FiledName=nameof(RB_FirstShop_List_Extend.Id),
                          FiledValue=listModel.Id,
                           OperatorEnum=OperatorEnum.Equal
                    }
                };
                firstShop_ListRepository.Update(keyValues, wheres);
            }
            foreach (var item in deList)
            {
                Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                    { nameof(RB_FirstShop_ListEnroll_Extend.Reviews),item.Reviews },
                    { nameof(RB_FirstShop_ListEnroll_Extend.Rank),item.Rank },
                    { nameof(RB_FirstShop_ListEnroll_Extend.MedalImage),item.MedalImage },
                    { nameof(RB_FirstShop_ListEnroll_Extend.MedalImageBig),item.MedalImageBig },
                    { nameof(RB_FirstShop_ListEnroll_Extend.RankListState),RankListState },
                    { nameof(RB_FirstShop_ListEnroll_Extend.UpdateBy),empId },
                    { nameof(RB_FirstShop_ListEnroll_Extend.UpdateDate),item.Reviews }
                };
                List<WhereHelper> wheres = new List<WhereHelper>() {
                    new WhereHelper(){
                         FiledName=nameof(RB_FirstShop_ListEnroll_Extend.Id),
                          FiledValue=item.Id,
                           OperatorEnum=OperatorEnum.Equal
                    }
                };
                firstShop_ListEnrollRepository.Update(keyValues, wheres);
            }

            if (type == 2)
            {
                //推送小程序消息
                System.Threading.Tasks.Task.Run(() =>
                {
                    //查询所有的报名记录
                    var SendList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = mallBaseId, ListId = listId });
                    if (SendList.Any())
                    {
                        //查询 品牌/载体  名称 图片
                        var e1list = SendList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Brand).ToList();
                        var e2list = SendList.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Carrier).ToList();
                        if (e1list.Any())
                        {
                            //查询品牌
                            string bids = string.Join(",", e1list.Select(x => x.ObjectId));
                            var blist = brandRepository.GetBrandList(new RB_Brand_Extend() { MallBaseId = mallBaseId, BrandIds = bids });
                            foreach (var item in e1list)
                            {
                                var bmodel = blist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                                item.ObjectName = bmodel?.BrandName ?? "";
                                item.ObjectIcon = bmodel?.Logo ?? "";
                            }
                        }
                        if (e2list.Any())
                        {
                            //查询载体
                            string cids = string.Join(",", e2list.Select(x => x.ObjectId));
                            var clist = carrierRepository.GetCarrierList(new RB_Carrier_Extend() { MallBaseId = mallBaseId, CarrierIds = cids });
                            foreach (var item in e2list)
                            {
                                var cmodel = clist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                                item.ObjectName = cmodel?.CarrierName ?? "";
                                item.ObjectIcon = cmodel?.Logo ?? "";
                            }
                        }

                        foreach (var item in SendList)
                        {
                            var umodel = member_UserRepository.GetEntity(item.UserId);
                            string ListName = listModel.ListName;
                            string ObjectName = item.ObjectName;
                            string Result = "未上榜";
                            string Remark = "很遗憾通知您未上榜，感谢您的参与";
                            if (deList.Where(x => x.Id == item.Id).Any())
                            {
                                Result = "上榜";
                                Remark = item.Reviews;
                            }
                            new MiniProgramMsgModule().SendFirstShopPrizeMsg(umodel.TenantId, umodel.MallBaseId, umodel.OpenId, ListName, ObjectName, Result, Remark);
                        }
                    }
                });
            }
            return "";
        }

        /// <summary>
        /// 获取放榜详情列表
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public List<RB_FirstShop_ListEnroll_Extend> GetFirstShopConfirmListList(int listId, int mallBaseId)
        {
            var list = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend() { MallBaseId = mallBaseId, ListId = listId, IsSelectPrize = 1 });
            if (list.Any())
            {
                string userIds = string.Join(",", list.Select(x => x.UserId).Distinct());
                //企业名称
                var companylist = companyRepository.GetCompanyListRepository(new RB_Company_Extend() { MallBaseId = mallBaseId, UserIds = userIds });
                foreach (var item in list)
                {
                    var companyModel = companylist.Where(x => x.CreateBy == item.UserId).FirstOrDefault();
                    item.CompanyName = companyModel.CompanyName;
                    item.Principal = companyModel.Principal;
                    item.Mobile = companyModel.Mobile;
                }

                //查询 品牌/载体  名称 图片
                var e1list = list.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Brand).ToList();
                var e2list = list.Where(x => x.ObjectType == Common.Enum.TradePavilion.ListObjectTypeEnum.Carrier).ToList();
                if (e1list.Any())
                {
                    //查询品牌
                    string bids = string.Join(",", e1list.Select(x => x.ObjectId));
                    var blist = brandRepository.GetBrandList(new RB_Brand_Extend() { MallBaseId = mallBaseId, BrandIds = bids });
                    foreach (var item in e1list)
                    {
                        var bmodel = blist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                        item.ObjectName = bmodel?.BrandName ?? "";
                        item.ObjectIcon = bmodel?.Logo ?? "";
                    }
                }
                if (e2list.Any())
                {
                    //查询载体
                    string cids = string.Join(",", e2list.Select(x => x.ObjectId));
                    var clist = carrierRepository.GetCarrierList(new RB_Carrier_Extend() { MallBaseId = mallBaseId, CarrierIds = cids });
                    foreach (var item in e2list)
                    {
                        var cmodel = clist.Where(x => x.ID == item.ObjectId).FirstOrDefault();
                        item.ObjectName = cmodel?.CarrierName ?? "";
                        item.ObjectIcon = cmodel?.Logo ?? "";
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_FirstShop_ListEnroll_Extend> GetFirstShopListEnrollList(RB_FirstShop_ListEnroll_Extend dmodel)
        {
            return firstShop_ListEnrollRepository.GetList(dmodel);
        }

        /// <summary>
        /// 榜单报名下载word 单次
        /// </summary>
        /// <param name="enrollId"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public string GetFirstShopEnrollWordExport(int enrollId, int mallBaseId, out string errorMsg)
        {
            errorMsg = "";
            //存储的临时文件地址
            string rootBook = AppDomain.CurrentDomain.BaseDirectory;
            string tempPath = rootBook + "/upfile/temporary/listenroll";
            var enrollModel = firstShop_ListEnrollRepository.GetEntity(enrollId);
            if (enrollModel == null) { errorMsg = "报名信息不存在"; return ""; }
            //获取榜单信息
            var listModel = firstShop_ListRepository.GetEntity(enrollModel.ListId);
            if (listModel == null) { errorMsg = "榜单不存在"; return ""; }

            //创建一个临时文件夹
            string tempFoldr = "";

            #region 解析表单内容

            if (string.IsNullOrEmpty(enrollModel.Content))
            {
                errorMsg = "报名内容不存在，请核实后再试";
                return "";
            }
            List<FormDataItem> elist = JsonHelper.DeserializeObject<List<FormDataItem>>(enrollModel.Content);
            elist = elist.Where(x => x.CompKey == "ImageUploadComp" || x.CompKey == "VideoUploadComp").ToList();
            if (elist.Any())
            {
                //开始下载图片视频至临时文件
                foreach (var item in elist)
                {
                    var obj = JsonHelper.DeserializeObject<UploadItem>(item.CompData.ToString());
                    if (item.CompKey == "ImageUploadComp")
                    {
                        //图片处理
                        string Name = obj.Name;
                        foreach (var qitem in obj.FileList)
                        {
                            //FileHelper.CreateImage();  下载腾讯云至本地
                        }
                    }
                    else if (item.CompKey == "VideoUploadComp")
                    {
                        //视频处理

                    }
                }
            }

            //ZipHelper.Zip(); 打包方法
            #endregion

            return "";
        }

        /// <summary>
        /// 榜单报名下载word
        /// </summary>
        /// <param name="enrollIdList"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public string GetFirstShopEnrollWordExport(List<int> enrollIdList, int mallBaseId)
        {
            return "";
        }



        /// <summary>
        /// 榜单报名下载word 单次
        /// </summary>
        /// <param name="enrollId"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public string GetFirstShopEnrollZipExport(string listIds, int mallBaseId, out string errorMsg)
        {
            errorMsg = "";
            //存储的临时文件地址
            string rootBook = AppDomain.CurrentDomain.BaseDirectory;
            string timeStr = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");
            string endUrl = "upfile\\temporary\\firstshopenrollzip\\";
            string tempPath = Path.Combine(rootBook + endUrl);

            //获取榜单信息
            var listModel = firstShop_ListRepository.GetList(new RB_FirstShop_List_Extend { MallBaseId = mallBaseId, QIds = listIds });
            if (listModel == null || !listModel.Any()) { errorMsg = "榜单不存在"; return ""; }


            var enrollList = firstShop_ListEnrollRepository.GetList(new RB_FirstShop_ListEnroll_Extend { MallBaseId = mallBaseId, ListIds = listIds });
            if (enrollList == null || !enrollList.Any()) { errorMsg = "报名信息不存在"; return ""; }
            string userIds = string.Join(",", enrollList.Select(x => x.UserId).Distinct());
            var clist = companyRepository.GetCompanyListRepository(new RB_Company_Extend() { MallBaseId = mallBaseId, UserIds = userIds });

            //创建一个临时文件夹
            string tempFoldr = "";

            #region 解析表单内容
            foreach (var listItem in listModel)
            {
                foreach (var enrollModel in enrollList.Where(x => x.ListId == listItem.Id))//榜单下面的公司
                {
                    if (string.IsNullOrEmpty(enrollModel.Content))
                    {
                        errorMsg = "报名内容不存在，请核实后再试";
                        return "";
                    }
                    List<FormDataItem> elist = JsonHelper.DeserializeObject<List<FormDataItem>>(enrollModel.Content);
                    elist = elist.Where(x => x.CompKey == "ImageUploadComp" || x.CompKey == "VideoUploadComp").ToList();
                    if (elist.Any())
                    {
                        var companyName = clist.Where(x => x.CreateBy == enrollModel.UserId).FirstOrDefault()?.CompanyName ?? System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        var templistPath = Path.Combine(tempPath + "file\\" + timeStr + "\\" + listItem.ListName + "\\" + companyName + "\\");//路径
                        if (!Directory.Exists(templistPath))
                        {
                            Directory.CreateDirectory(templistPath);
                        }
                        //开始下载图片视频至临时文件

                        foreach (var itemFile in elist)
                        {
                            var obj = JsonHelper.DeserializeObject<UploadItem>(itemFile.CompData.ToString());
                            if (itemFile.CompKey == "ImageUploadComp")
                            {
                                //图片处理
                                string Name = obj.Name;
                                foreach (var qitem in obj.FileList)
                                {

                                    try
                                    {
                                        Uri uri = new Uri(qitem); //imgPath :网络图片地址
                                        WebRequest webRequest = WebRequest.Create(uri);
                                        //  byte[] bytes;
                                        using (WebResponse webResponse = webRequest.GetResponse())
                                        {
                                            Bitmap bitmap = new Bitmap(webResponse.GetResponseStream());

                                            bitmap.Save(templistPath + Path.GetFileName(qitem), System.Drawing.Imaging.ImageFormat.Jpeg);
                                        }
                                    }
                                    catch (Exception ex)
                                    {

                                    }
                                }
                            }
                            else if (itemFile.CompKey == "VideoUploadComp")
                            {
                                //视频处理
                                foreach (var qitem in obj.FileList)
                                {

                                    try
                                    {
                                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(qitem);
                                        request.Method = "GET";
                                        byte[] fileBytes;
                                        using (WebResponse webRes = request.GetResponse())
                                        {
                                            int length = (int)webRes.ContentLength;
                                            HttpWebResponse response = webRes as HttpWebResponse;
                                            Stream stream = response.GetResponseStream();
                                            var contentdisposition = response.Headers["Content-Disposition"];
                                            var filename = Path.GetFileName(qitem);
                                            //读取到内存
                                            MemoryStream stmMemory = new MemoryStream();
                                            byte[] buffer = new byte[length];
                                            int i;
                                            //将字节逐个放入到Byte中
                                            while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
                                            {
                                                stmMemory.Write(buffer, 0, i);
                                            }
                                            fileBytes = stmMemory.ToArray();//文件流Byte
                                            FileStream fs = new FileStream(templistPath + Path.GetFileName(qitem), FileMode.OpenOrCreate);
                                            stmMemory.WriteTo(fs);
                                            stmMemory.Close();
                                            fs.Close();
                                        }
                                    }
                                    catch (Exception ex)
                                    {

                                    }
                                }
                            }
                        }
                    }
                }
            }
            var templistZipPath = Path.Combine(tempPath + "zip\\" + timeStr + "\\");//路径
            if (!Directory.Exists(templistZipPath))
            {
                Directory.CreateDirectory(templistZipPath);
            }
            bool result = Common.Plugin.ZipHelper.Zip(Path.Combine(tempPath + "file\\" + timeStr), Path.Combine(templistZipPath, "榜单导出文件夹.zip"), ref tempFoldr);
            if (result)
            {
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(Path.Combine(tempPath + "file\\"));
                    DirectoryInfo[] dirArr = dir.GetDirectories();
                    foreach (DirectoryInfo item in dirArr)
                    {
                        if (item.Name == timeStr)
                        {
                            foreach (FileInfo fi in item.GetFiles())
                            {
                                fi.Delete();
                            }
                            item.Delete(true);
                        }
                    }

                }
                catch
                {
                    LogHelper.Write("清理临时文件失败");
                }
                return Path.Combine("/upfile/temporary/firstshopenrollzip/" + "zip/" + timeStr + "/榜单导出文件夹.zip");
            }
            else
            {
                return "";
            }
            #endregion


        }



        /// <summary>
        /// 删除生成的zip文件
        /// </summary>
        /// <param name="enrollId"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public bool DelFirstShopEnrollZipExport(string Url)
        {
            //存储的临时文件地址
            string rootBook = AppDomain.CurrentDomain.BaseDirectory;
            string tempPath = Path.Combine(rootBook + Url.TrimStart('/'));
            #region 解析表单内容

            string timeStr = Url.TrimStart('/').Split("/")[4];
            if (!File.Exists(tempPath))
            {
                return true;
            }
            else
            {
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(Path.Combine(rootBook + "upfile\\temporary\\firstshopenrollzip\\" + "zip\\"));
                    DirectoryInfo[] dirArr = dir.GetDirectories();
                    foreach (DirectoryInfo item in dirArr)
                    {
                        if (item.Name == timeStr)
                        {
                            foreach (FileInfo fi in item.GetFiles())
                            {
                                fi.Delete();
                            }
                            item.Delete(true);
                        }
                    }
                    return true;
                }
                catch (Exception ex)
                {

                    return false;
                }

            }

            #endregion


        }
    }
}
