using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using MySystem.MainModels;
using LitJson;
using Library;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/v1/[controller]/[action]")]
    public class OrdersController : BaseController
    {
        public OrdersController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }




        #region 创客-商城-确认支付
        [Authorize]
        public JsonResult Pay(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = PayDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson PayDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int PayMode = int.Parse(function.CheckInt(data["PayMode"].ToString())); //支付方式
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString()));
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            Orders query = maindb.Orders.FirstOrDefault(m => m.Id == Id);
            if (query != null)
            {
                query.PayMode = PayMode; //支付方式
                maindb.SaveChanges();
                string PayData = "";
                // int proId = OrderProductDbconn.Instance.GetList(Id).FirstOrDefault();
                OrderProduct pro = maindb.OrderProduct.FirstOrDefault(m => m.OrderId == Id) ?? new OrderProduct();
                Products product = maindb.Products.FirstOrDefault(m => m.Id == pro.ProductId) ?? new Products();
                PublicAccountSet set = new AlipayFunction(_accessor.HttpContext).SetData(UserId); //RedisDbconn.Instance.Get<PublicAccountSet>("PublicAccountSet") ?? new PublicAccountSet();
                if (PayMode == 1)
                {
                    string TotalPrice = query.TotalPrice.ToString();
                    if (UserId == 1)
                    {
                        TotalPrice = "0.01";
                    }
                    function.WriteLog(query.OrderNo, "支付宝支付日志");
                    function.WriteLog(TotalPrice.ToString(), "支付宝支付日志");
                    function.WriteLog(product.ProductName, "支付宝支付日志");
                    string ProductName = product.ProductName;
                    if(string.IsNullOrEmpty(ProductName) && query.OrderNo.StartsWith("CBJ"))
                    {
                        ProductName = "储备金";
                    }
                    if (query.ParentOrderId > 0)
                    {
                        OrderProduct ppro = maindb.OrderProduct.FirstOrDefault(m => m.OrderId == query.ParentOrderId) ?? new OrderProduct();
                        ProductName = ppro.ProductName;
                    }
                    PayData = new Alipay.AlipayPublicClass(_accessor.HttpContext).GetAlipayInfo(query.OrderNo, TotalPrice, ProductName, set.AlipayAppId, set.AlipayPrivateKey, SourceHost + "/Api/Alipay/NoticePay");
                    function.WriteLog(PayData, "支付宝支付日志");
                }
                else if (PayMode == 2)
                {
                    Dictionary<string, string> payinfo = new WeChat.WeChatPublicClass(_accessor.HttpContext).AppPay(set.WeChatOpenAppId, set.WeChatMchId, query.TotalPrice, query.OrderNo, product.ProductName, product.ProductName, set.ServerIp, set.WeChatPayKey, SourceHost + "/Api/WeChat/NoticePay");
                    string AppId = payinfo["appId"];
                    //string timeStamp = payinfo["timeStamp"];
                    string timeStamp = function.ConvertDateTimeInt(DateTime.Now).ToString();
                    string nonce_str = payinfo["nonce_str"];
                    string package = payinfo["package"];
                    string signStr = "appid=" + AppId;
                    signStr += "&noncestr=" + nonce_str;
                    signStr += "&package=Sign=WXPay";
                    signStr += "&partnerid=" + set.WeChatMchId;
                    signStr += "&prepayid=" + package.Replace("prepay_id=", "");
                    signStr += "&timestamp=" + timeStamp;
                    signStr += "&key=" + set.WeChatPayKey;
                    string paySign = function.MD532(signStr).ToUpper();
                    PayData = "{";
                    PayData += "\"partnerId\":\"" + set.WeChatMchId + "\",";
                    PayData += "\"prepayId\":\"" + package.Replace("prepay_id=", "") + "\",";
                    PayData += "\"nonceStr\":\"" + nonce_str + "\",";
                    PayData += "\"timeStamp\":\"" + timeStamp + "\",";
                    PayData += "\"sign\":\"" + paySign + "\"";
                    PayData += "}";
                }
                else if (PayMode == 3)//余额支付
                {
                    string TotalPrice = query.TotalPrice.ToString();
                    if (UserId == 1)
                    {
                        TotalPrice = "0.01";
                    }
                    else
                    {
                        if(decimal.Parse(TotalPrice) == 10000)
                        {
                            TotalPrice = "10800";
                        }
                        else if(decimal.Parse(TotalPrice) == 40000)
                        {
                            TotalPrice = "43200";
                        }
                        query.TotalPrice = decimal.Parse(TotalPrice);
                        maindb.SaveChanges();
                    }
                    var PayTotal = Convert.ToDecimal(TotalPrice);
                    UserAccount userAccount = maindb.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
                    if (userAccount.AccountStatus == 1)
                    {
                        return new AppResultJson() { Status = "-1", Info = "参数错误，请联系客服" };
                    }
                    else
                    {
                        if (userAccount.BalanceAmount >= PayTotal)
                        {
                            function.WriteLog(query.OrderNo, "余额支付日志");
                            function.WriteLog(TotalPrice.ToString(), "余额支付日志");
                            function.WriteLog(product.ProductName, "余额支付日志");
                            RedisDbconn.Instance.AddList("BalancePayQueue", Id.ToString());
                        }
                        else
                        {
                            return new AppResultJson() { Status = "-1", Info = "您的余额不足！", Data = Obj };
                        }
                    }
                }
                else if (PayMode == 4)//盟主储蓄金支付
                {
                    string TotalPrice = query.TotalPrice.ToString();
                    if (UserId == 1)
                    {
                        TotalPrice = "0.01";
                    }
                    var PayTotal = Convert.ToDecimal(TotalPrice);
                    UserAccount userAccount = maindb.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
                    if (userAccount.AccountStatus == 1)
                    {
                        return new AppResultJson() { Status = "-1", Info = "参数错误，请联系客服" };
                    }
                    else
                    {
                        if (userAccount.LeaderReserve >= PayTotal)
                        {
                            function.WriteLog(query.OrderNo, "盟主储蓄金支付日志");
                            function.WriteLog(TotalPrice.ToString(), "盟主储蓄金支付日志");
                            function.WriteLog(product.ProductName, "盟主储蓄金支付日志");
                            RedisDbconn.Instance.AddList("ReservePayQueue", Id.ToString());
                        }
                        else
                        {
                            return new AppResultJson() { Status = "-1", Info = "您的盟主储蓄金不足！", Data = Obj };
                        }
                    }
                }
                Obj.Add("PayData", PayData); //支付宝微信SDK所需数据
            }
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion

        #region 创客-商城-确认订单
        [Authorize]
        public JsonResult ConfirmOrder(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = ConfirmOrderDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson ConfirmOrderDo(string value)
        {
            function.WriteLog(value, "创客-商城-确认订单");
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
            int DeliveryType = int.Parse(function.CheckInt(data["DeliveryType"].ToString())); //提货类型
            string Remark = data["Remark"].ToString(); //订单备注
            int AddressId = int.Parse(function.CheckInt(data["AddressId"].ToString()));  //收货地址Id
            JsonData ProductJson = data["ProductJson"]; //商品信息
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string OrderNo = "BM" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
            UserAddress address = UserAddressDbconn.Instance.Get(AddressId) ?? new UserAddress();
            StoreHouse store = StoreHouseDbconn.Instance.Get(StoreId) ?? new StoreHouse();
            Users storeUser = UsersDbconn.Instance.Get(store.ManageUserId) ?? new Users();
            Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
            int TopUserId = PublicFunction.GetTopUserId(user.ParentNav);
            decimal TotalPrice = 0; //订单总额
            int TotalBuyCount = 0;
            for (int a = 0; a < ProductJson.Count; a++)
            {
                JsonData jsonData = ProductJson[a];
                int Id = int.Parse(function.CheckInt(jsonData["ProductId"].ToString()));
                int Count = int.Parse(function.CheckInt(jsonData["BuyCount"].ToString()));
                Products products = maindb.Products.FirstOrDefault(m => m.Id == Id);
                if (products.Stock - Count >= 0)
                {
                    Orders query = maindb.Orders.Add(new Orders()
                    {
                        OrderNo = OrderNo,
                        RealName = address.RealName,
                        Mobile = address.Mobile,
                        Areas = address.Areas,
                        Address = address.Address,
                        StoreContact = storeUser.RealName,
                        StoreContactMobile = store.ManageMobile,
                        StoreUserId = store.UserId,
                        StoreType = store.StoreType,
                        CreateDate = DateTime.Now, //创建时间
                        UserId = UserId, //创客
                        StoreId = StoreId, //仓库
                        TopUserId = TopUserId, //顶级创客
                        DeliveryType = DeliveryType, //提货类型
                        BuyCount = TotalBuyCount,
                        TotalPrice = TotalPrice, //订单总额
                        Remark = Remark, //订单备注
                    }).Entity;
                    maindb.SaveChanges();
                    OrderForNo orderFor = maindb.OrderForNo.Add(new OrderForNo()
                    {
                        OrderNo = OrderNo,
                        OrderIds = query.Id.ToString(),
                    }).Entity;
                    maindb.SaveChanges();

                    for (int i = 0; i < ProductJson.Count; i++)
                    {
                        JsonData item = ProductJson[i];
                        int ProductId = int.Parse(function.CheckInt(item["ProductId"].ToString()));
                        int NormId = int.Parse(function.CheckInt(item["NormId"].ToString()));
                        int BuyCount = int.Parse(function.CheckInt(item["BuyCount"].ToString()));
                        Products product = ProductsDbconn.Instance.Get(ProductId) ?? new Products();
                        ProductNorm norm = ProductNormDbconn.Instance.Get(NormId) ?? new ProductNorm();
                        OrderProduct pro = maindb.OrderProduct.Add(new OrderProduct()
                        {
                            CreateDate = DateTime.Now, //创建时间
                            OrderId = query.Id,
                            ProductId = ProductId,
                            ProductCount = BuyCount,
                            ProductName = product.ProductName,
                            ProductPhoto = product.ListPicPath,
                            NormId = NormId.ToString(),
                            NormDetail = norm.ColName,
                            ProductPrice = NormId == 0 ? product.Price : norm.Price,
                            TotalPrice = NormId == 0 ? product.Price * BuyCount : norm.Price * BuyCount,
                            ProductCode = product.ProductCode,
                            UserId = UserId,
                            StoreId = StoreId,
                        }).Entity;
                        maindb.SaveChanges();
                        if (NormId == 0)
                        {
                            TotalPrice += product.Price * BuyCount;
                        }
                        else
                        {
                            TotalPrice += norm.Price * BuyCount;
                        }
                        TotalBuyCount += BuyCount;
                    }
                    query.TotalPrice = TotalPrice;
                    query.BuyCount = TotalBuyCount;
                    maindb.SaveChanges();
                    Obj.Add("Id", query.Id); //Id
                }
                else
                {
                    return new AppResultJson() { Status = "-1", Info = "商品库存不足", Data = Obj };
                }
            }
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion

        #region 创客-商城-确认订单-机具券
        [Authorize]
        public JsonResult ConfirmOrder2(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = ConfirmOrder2Do(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson ConfirmOrder2Do(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            string Remark = data["Remark"].ToString(); //订单备注
            JsonData ProductJson = data["ProductJson"]; //商品信息
            DateTime start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
            for (int i = 0; i < ProductJson.Count; i++)
            {
                JsonData item = ProductJson[i];
                int ProductId = int.Parse(function.CheckInt(item["ProductId"].ToString()));
                if (ProductId == 15)
                {
                    bool checkPro = maindb.OrderProduct.Any(m => m.CreateDate >= start && m.UserId == UserId && m.ProductId == ProductId && m.Status > -1);
                    if (checkPro)
                    {
                        return new AppResultJson() { Status = "-1", Info = "请勿重复下单" };
                    }
                }

                if (ProductId == 29)
                {
                    bool checkPro = maindb.OrderProduct.Any(m => m.CreateDate >= start && m.UserId == UserId && m.ProductId == ProductId && m.Status > -1);
                    if (checkPro)
                    {
                        return new AppResultJson() { Status = "-1", Info = "请勿重复下单" };
                    }
                }
            }
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string OrderNo = "BM" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
            Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
            int TopUserId = PublicFunction.GetTopUserId(user.ParentNav);
            decimal TotalPrice = 0; //订单总额
            int TotalBuyCount = 0;
            for (int a = 0; a < ProductJson.Count; a++)
            {
                JsonData jsonData = ProductJson[a];
                int Id = int.Parse(function.CheckInt(jsonData["ProductId"].ToString()));
                int Count = int.Parse(function.CheckInt(jsonData["BuyCount"].ToString()));
                Products products = maindb.Products.FirstOrDefault(m => m.Id == Id);
                if (products != null)
                {
                    if (products.Stock - Count >= 0)
                    {
                        Orders query = maindb.Orders.Add(new Orders()
                        {
                            OrderNo = OrderNo,
                            CreateDate = DateTime.Now, //创建时间
                            UserId = UserId, //创客
                            TopUserId = TopUserId, //顶级创客
                            BuyCount = TotalBuyCount,
                            TotalPrice = TotalPrice, //订单总额
                            Remark = Remark, //订单备注
                        }).Entity;
                        maindb.SaveChanges();
                        for (int i = 0; i < ProductJson.Count; i++)
                        {
                            JsonData item = ProductJson[i];
                            int ProductId = int.Parse(function.CheckInt(item["ProductId"].ToString()));
                            int NormId = int.Parse(function.CheckInt(item["NormId"].ToString()));
                            int BuyCount = int.Parse(function.CheckInt(item["BuyCount"].ToString()));
                            Products product = ProductsDbconn.Instance.Get(ProductId) ?? new Products();
                            ProductNorm norm = ProductNormDbconn.Instance.Get(NormId) ?? new ProductNorm();
                            OrderProduct pro = maindb.OrderProduct.Add(new OrderProduct()
                            {
                                CreateDate = DateTime.Now, //创建时间
                                OrderId = query.Id,
                                ProductId = ProductId,
                                ProductCount = BuyCount,
                                ProductName = product.ProductName,
                                ProductPhoto = product.ListPicPath,
                                NormId = NormId.ToString(),
                                NormDetail = norm.ColName,
                                ProductPrice = NormId == 0 ? product.Price : norm.Price,
                                TotalPrice = NormId == 0 ? product.Price * BuyCount : norm.Price * BuyCount,
                                ProductCode = product.ProductCode,
                                UserId = UserId,
                            }).Entity;
                            maindb.SaveChanges();
                            if (NormId == 0)
                            {
                                TotalPrice += product.Price * BuyCount;
                            }
                            else
                            {
                                TotalPrice += norm.Price * BuyCount;
                            }
                            TotalBuyCount += BuyCount;
                        }
                        query.TotalPrice = TotalPrice;
                        query.BuyCount = TotalBuyCount;
                        OrderForNo orderFor = maindb.OrderForNo.Add(new OrderForNo()
                        {
                            OrderNo = OrderNo,
                            OrderIds = query.Id.ToString(),
                        }).Entity;
                        maindb.SaveChanges();
                        Obj.Add("Id", query.Id); //Id
                    }
                    else
                    {
                        return new AppResultJson() { Status = "-1", Info = "商品库存不足", Data = Obj };
                    }
                }
                else
                {
                    return new AppResultJson() { Status = "-1", Info = "无该商品", Data = Obj };
                }

            }
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion



        #region 创客-首页-机具管理-我的订单
        [Authorize]
        public JsonResult List(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = ListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> ListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            string ProductType = data["ProductType"].ToString(); //产品类型
            int Status = int.Parse(function.CheckInt(data["Status"].ToString())); //状态
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            IQueryable<Orders> query = maindb.Orders.Where(m => m.Status > -1 && m.UserId == UserId && m.ParentOrderId == 0 && m.QueryCount == 0 && m.TotalPrice > 0);
            if (Status > 0)
            {
                if (Status == 1) Status = 0;
                query = query.Where(m => m.Status == Status);
            }
            query = query.OrderByDescending(m => m.Id);
            if (PageNum == 1)
            {
                query = query.Take(PageSize);
            }
            else
            {
                int skipNum = PageSize * (PageNum - 1);
                query = query.Skip(skipNum).Take(PageSize);
            }
            foreach (var subdata in query.ToList())
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();

                int finshPay = -1;
                var order = maindb.Orders.Any(m => m.Id == subdata.Id && m.Status == 0 && m.ProductId <= 13 && m.ProductId >= 12 && m.ErpMode >= 0 && m.ErpMode <= 1);
                if (order)
                {
                    var totalOd = maindb.Orders.Where(m => m.ParentOrderId == subdata.Id).Count();
                    var paiedOd = maindb.Orders.Where(m => m.ParentOrderId == subdata.Id && m.PayStatus == 1).Count();
                    if (totalOd > paiedOd && paiedOd > 0)
                    {
                        finshPay = 0;
                        curData.Add("FinshStatus", finshPay); //部分支付
                    }

                }

                var orders = maindb.Orders.Any(m => m.Id == subdata.Id && m.Status == 0);
                if (orders)
                {
                    var totalOd = maindb.Orders.Where(m => m.ParentOrderId == subdata.Id).Count();
                    var paiedOd = maindb.Orders.Where(m => m.ParentOrderId == subdata.Id && m.PayStatus == 1).Count();
                    if (totalOd > paiedOd && paiedOd > 0)
                    {
                        finshPay = 0;
                        curData.Add("FinshStatus", finshPay); //部分支付
                    }
                }

                curData.Add("TotalPrice", subdata.TotalPrice); //总价
                curData.Add("BuyCount", subdata.BuyCount); //下单数量
                curData.Add("Id", subdata.Id); //Id
                curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
                List<Dictionary<string, object>> prolist = new List<Dictionary<string, object>>();
                List<int> pros = OrderProductDbconn.Instance.GetList(subdata.Id);
                int ProductId = 0;
                foreach (int proId in pros)
                {
                    ProductId = proId;
                    OrderProduct pro = OrderProductDbconn.Instance.Get(proId) ?? new OrderProduct();
                    Dictionary<string, object> row = new Dictionary<string, object>();
                    row.Add("ProductName", pro.ProductName);
                    row.Add("ProductPrice", pro.ProductPrice);
                    row.Add("ProductCount", pro.ProductCount);
                    row.Add("ProductPhoto", DefaultPic(pro.ProductPhoto));
                    prolist.Add(row);
                }
                string StatusName = RelationClass.GetOrderStatusInfo(subdata.Status);
                if (StatusName == "待发货" && (ProductId == 15 || ProductId == 29))
                {
                    StatusName = RelationClass.GetOrderStatusInfo(2);
                }
                curData.Add("Status", StatusName); //Status
                curData.Add("Products", prolist);
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 创客-首页-机具管理-我的订单-详情
        [Authorize]
        public JsonResult Detail(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = DetailDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> DetailDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            Orders query = maindb.Orders.FirstOrDefault(m => m.Id == Id && m.UserId == UserId) ?? new Orders();
            Obj.Add("OrderNo", query.OrderNo); //订单号
            Obj.Add("RealName", query.RealName); //姓名
            Obj.Add("Mobile", query.Mobile); //手机号
            Obj.Add("TotalPrice", query.TotalPrice); //总价
            Obj.Add("Areas", function.CheckNull(query.Areas).Replace(",", "")); //所在地区
            Obj.Add("Address", query.Address); //详细地址
            Obj.Add("PayDate", query.PayDate == null ? "" : query.PayDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //付款时间
            Obj.Add("DeliveryType", query.DeliveryType); //提货类型
            Obj.Add("SendStatus", query.SendStatus); //发货状态
            Obj.Add("StoreContact", query.StoreContact); //仓库联系人
            Obj.Add("BuyCount", query.BuyCount); //下单数量
            Obj.Add("StoreContactMobile", query.StoreContactMobile); //仓库联系人电话
            StoreHouse store = maindb.StoreHouse.FirstOrDefault(m => m.Id == query.StoreId) ?? new StoreHouse();
            Obj.Add("StoreName", store.StoreName); //提货仓库
            Obj.Add("Status", query.Status); //状态
            Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //创建时间
            List<Dictionary<string, object>> prolist = new List<Dictionary<string, object>>();
            List<int> pros = OrderProductDbconn.Instance.GetList(Id);
            int ProductId = 0;
            foreach (int proId in pros)
            {
                ProductId = proId;
                OrderProduct pro = OrderProductDbconn.Instance.Get(proId) ?? new OrderProduct();
                Dictionary<string, object> row = new Dictionary<string, object>();
                row.Add("ProductName", pro.ProductName);
                row.Add("ProductPrice", pro.ProductPrice);
                row.Add("ProductCount", pro.ProductCount);
                row.Add("ProductPhoto", DefaultPic(pro.ProductPhoto));
                prolist.Add(row);
            }
            string StatusName = RelationClass.GetOrderStatusInfo(query.Status);
            if (StatusName == "待发货" && (ProductId == 15 || ProductId == 29))
            {
                StatusName = RelationClass.GetOrderStatusInfo(2);
            }
            Obj.Add("StatusName", StatusName);
            Obj.Add("Products", prolist);
            return Obj;
        }
        #endregion

        #region 创客-首页-机具管理-我的订单-详情-V2
        [Authorize]
        public JsonResult Detail2(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = Detail2Do(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> Detail2Do(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            Orders query = OrdersDbconn.Instance.Get(Id) ?? new Orders();
            Obj.Add("OrderNo", query.OrderNo); //订单号
            Obj.Add("RealName", query.RealName); //姓名
            Obj.Add("Mobile", query.Mobile); //手机号
            Obj.Add("TotalPrice", query.TotalPrice); //总价
            Obj.Add("PayMode", query.PayMode);
            Obj.Add("Areas", function.CheckNull(query.Areas).Replace(",", "")); //所在地区
            Obj.Add("Address", query.Address); //详细地址
            Obj.Add("PayDate", query.PayDate == null ? "" : query.PayDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //付款时间
            Obj.Add("DeliveryType", query.DeliveryType); //提货类型
            Obj.Add("SendStatus", query.SendStatus); //发货状态
            Obj.Add("StoreContact", query.StoreContact); //仓库联系人
            Obj.Add("BuyCount", query.BuyCount); //下单数量
            Obj.Add("StoreContactMobile", query.StoreContactMobile); //仓库联系人电话
            StoreHouse store = maindb.StoreHouse.FirstOrDefault(m => m.Id == query.StoreId) ?? new StoreHouse();
            Obj.Add("StoreName", store.StoreName); //提货仓库
            Obj.Add("StatusName", RelationClass.GetOrderStatusInfo(query.Status));
            Obj.Add("Status", query.Status); //状态
            Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //创建时间
            List<Dictionary<string, object>> prolist = new List<Dictionary<string, object>>();
            List<int> pros = OrderProductDbconn.Instance.GetList(Id);
            foreach (int proId in pros)
            {
                OrderProduct pro = OrderProductDbconn.Instance.Get(proId) ?? new OrderProduct();
                Dictionary<string, object> row = new Dictionary<string, object>();
                row.Add("ProductName", pro.ProductName);
                row.Add("ProductPrice", pro.ProductPrice);
                row.Add("ProductCount", pro.ProductCount);
                row.Add("ProductPhoto", DefaultPic(pro.ProductPhoto));
                prolist.Add(row);
            }
            Obj.Add("Products", prolist);
            string Codes = query.SnNos;
            List<string> CodeList = new List<string>();
            if (!string.IsNullOrEmpty(Codes))
            {
                CodeList = Codes.Split(',').ToList();
            }
            Obj.Add("ExchangeCodeList", CodeList);
            return Obj;
        }
        #endregion



        #region 创客-首页-机具管理-我的订单-取消订单
        [Authorize]
        public JsonResult CancelOrder(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = CancelOrderDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> CancelOrderDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            Orders query = maindb.Orders.FirstOrDefault(m => m.Id == Id && m.UserId == UserId);
            if (query != null)
            {
                query.Status = -1;
                var orderpros = maindb.OrderProduct.Select(m => new { m.Id, m.OrderId }).Where(m => m.OrderId == Id).ToList();
                foreach (var orderpro in orderpros)
                {
                    OrderProduct edit = maindb.OrderProduct.FirstOrDefault(m => m.Id == orderpro.Id);
                    if (edit != null)
                    {
                        edit.Status = -1;
                    }
                }
                maindb.SaveChanges();
            }
            return Obj;
        }
        #endregion





        #region 创客-首页-仓库管理-订单列表
        [Authorize]
        public JsonResult StoreOrderList(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = StoreOrderListDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        public List<Dictionary<string, object>> StoreOrderListDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            string SearchKey = data["SearchKey"].ToString(); //手机号
            int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
            int Status = int.Parse(function.CheckInt(data["Status"].ToString())); //订单状态
            string StartDate = data["StartDate"].ToString(); //下单开始时间
            string EndDate = data["EndDate"].ToString(); //下单结束时间
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            WebCMSEntities db = new WebCMSEntities();
            IQueryable<Orders> query = db.Orders.Where(m => m.StoreId == StoreId && m.PayStatus == 1 && m.QueryCount < 2);
            if (!string.IsNullOrEmpty(SearchKey))
            {
                query = query.Where(m => m.RealName == SearchKey || m.Mobile == SearchKey);
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                DateTime time = DateTime.Parse(StartDate.Substring(0, 4) + "-" + StartDate.Substring(4, 2) + "-" + StartDate.Substring(6, 2) + " 00:00:00");
                query = query.Where(m => m.CreateDate >= time);
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                DateTime time = DateTime.Parse(EndDate.Substring(0, 4) + "-" + EndDate.Substring(4, 2) + "-" + EndDate.Substring(6, 2) + " 23:59:59");
                query = query.Where(m => m.CreateDate <= time);
            }
            if (Status > 0)
            {
                if (Status == 2) Status = 0;
                query = query.Where(m => m.SendStatus == Status);
            }
            query = query.OrderByDescending(m => m.Id);
            if (PageNum == 1)
            {
                query = query.Take(PageSize);
            }
            else
            {
                int skipNum = PageSize * (PageNum - 1);
                query = query.Skip(skipNum).Take(PageSize);
            }
            foreach (var item in query.ToList())
            {
                Orders subdata = OrdersDbconn.Instance.Get(item.Id) ?? new Orders();
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("OrderNo", subdata.OrderNo); //订单号
                curData.Add("RealName", subdata.RealName); //姓名
                curData.Add("Mobile", subdata.Mobile); //手机号
                curData.Add("Areas", subdata.Areas); //所在地区
                curData.Add("Address", subdata.Address); //详细地址
                curData.Add("DeliveryType", subdata.DeliveryType); //提货类型
                curData.Add("BuyCount", subdata.BuyCount); //下单数量
                curData.Add("SnNos", subdata.SnNos); //SN编号
                curData.Add("Id", subdata.Id); //Id
                curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
                List<int> pros = OrderProductDbconn.Instance.GetList(subdata.Id);
                int pid = pros.Count > 0 ? pros[0] : 0;
                OrderProduct orderProduct = OrderProductDbconn.Instance.Get(pid) ?? new OrderProduct();
                Products pro = ProductsDbconn.Instance.Get(orderProduct.ProductId) ?? new Products();
                curData.Add("ProductName", pro.ProductName); //商品名称
                Users user = UsersDbconn.Instance.Get(subdata.UserId) ?? new Users();
                curData.Add("OrderRealName", user.RealName); //下单人姓名
                curData.Add("OrderMakerCode", user.MakerCode); //下单人编号
                curData.Add("OrderKind", subdata.Sort > 0 ? "机具申请" : "商城购买"); //订单类型
                curData.Add("SendStatus", item.SendStatus);
                dataList.Add(curData);
            }
            Other = new Dictionary<string, object>();
            var count = query.Count();//数据条数
            Other.Add("count", count);
            return dataList;
        }
        #endregion

        #region 创客-首页-仓库管理-订单详情
        [Authorize]
        public JsonResult StoreOrderDetail(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = StoreOrderDetailDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> StoreOrderDetailDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            Orders query = OrdersDbconn.Instance.Get(Id) ?? new Orders();
            Obj.Add("UserId", query.UserId); //创客
            Obj.Add("RealName", query.RealName); //姓名
            Obj.Add("Mobile", query.Mobile); //手机号
            Obj.Add("Areas", function.CheckNull(query.Areas).Replace(",", "")); //所在地区
            Obj.Add("Address", query.Address); //详细地址
            int DeliveryType = query.DeliveryType;
            if (DeliveryType == 1)
            {
                Obj.Add("DeliveryType", "邮寄到付"); //提货类型
            }
            else if (DeliveryType == 2)
            {
                Obj.Add("DeliveryType", "上门自提"); //提货类型
            }
            Obj.Add("BuyCount", query.BuyCount); //下单数量
            Obj.Add("SnNos", query.SnNos); //SN编号
            Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
            List<int> OrderProducts = OrderProductDbconn.Instance.GetList(Id);
            if (OrderProducts.Count > 0)
            {
                OrderProduct pro = OrderProductDbconn.Instance.Get(OrderProducts[0]) ?? new OrderProduct();
                Obj.Add("ProductName", pro.ProductName); //商品名称
            }
            else
            {
                Obj.Add("ProductName", ""); //商品名称
            }
            return Obj;
        }
        #endregion




        #region 创客-我的-盟主中心-储备金购买/充值
        [Authorize]
        public JsonResult BuyReserve(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = BuyReserveDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson BuyReserveDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            int BuyType = int.Parse(function.CheckInt(data["BuyType"].ToString())); //购买类型(1 升级 2 购买)
            decimal TotalPrice = decimal.Parse(function.CheckNum(data["TotalPrice"].ToString())); //总价
            // int PayMode = int.Parse(function.CheckInt(data["PayMode"].ToString())); //支付方式(1-支付宝，3-余额支付)
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            Users Users = maindb.Users.FirstOrDefault(m => m.Id == UserId);
            var productId = 0;
            if(BuyType == 2 && TotalPrice == 10000)
            {
                productId = 40;
            }
            else if(BuyType == 2 && TotalPrice == 40000)
            {
                productId = 39;
            }
            else if (Users.LeaderLevel == 1)
            {
                productId = 27;
            }
            // else if (Users.LeaderLevel == 2)
            // {
            //     productId = 27;
            // }
            Products product = maindb.Products.FirstOrDefault(m => m.Id == productId) ?? new Products();
            string OrderNo = "CBJ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
            Orders query = maindb.Orders.Add(new Orders()
            {
                CreateDate = DateTime.Now, //创建时间
                PayStatus = 0,//支付状态(0 未支付 1 已支付)
                ErpMode = BuyType,//购买类型
                ProductId = productId,//27 大盟主 28 小盟主
                UserId = UserId, //创客
                BuyCount = 1,//购买数量
                TotalPrice = TotalPrice, //总价
                OrderNo = OrderNo,//订单号
                // PayMode = PayMode, //支付方式
            }).Entity;
            maindb.SaveChanges();
            maindb.OrderForNo.Add(new OrderForNo()
            {
                OrderNo = OrderNo,
                OrderIds = query.Id.ToString()
            });
            maindb.OrderProduct.Add(new OrderProduct()
            {
                CreateDate = DateTime.Now, //创建时间
                OrderId = query.Id,
                ProductId = productId,
                ProductCount = 1,
                ProductName = product.ProductName,
                ProductPhoto = product.ListPicPath,
                ProductPrice = TotalPrice,
                TotalPrice = TotalPrice,
                ProductCode = product.ProductCode,
                UserId = UserId,
            });
            maindb.SaveChanges();
            RedisDbconn.Instance.AddList("ReservePayQueue", query.Id.ToString());

            Obj.Add("Id", query.Id); //APP支付需要的数据
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion

        #region 创客-我的-盟主中心-储备金购买/充值支付
        [Authorize]
        public JsonResult PayReserve(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = PayReserveDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson PayReserveDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            // int PayMode = int.Parse(function.CheckInt(data["PayMode"].ToString())); //支付方式(1-支付宝，3-余额支付)
            string OpMode = data["OpMode"].ToString(); //操作方式(1-全款支付，2-拆单支付)
            int Times = int.Parse(function.CheckInt(data["Times"].ToString())); //分次数量
            int OrderId = int.Parse(function.CheckInt(data["OrderId"].ToString())); //订单Id
            string check = RedisDbconn.Instance.Get<string>(UserId + ":" + OrderId);
            if(check == "1")
            {
                return new AppResultJson() { Status = "1", Info = "" };
            }
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            Orders query = maindb.Orders.FirstOrDefault(m => m.Id == OrderId) ?? new Orders();

            var LeavePrice = query.TotalPrice;//剩余支付金额
            var TotalPrice = query.TotalPrice / Times;
            var min = (int)TotalPrice - 1000;
            var max = (int)TotalPrice + 1000;

            if (OpMode == "2")
            {
                for (int i = 0; i < Times - 1; i++)
                {
                    string OrderNo = "CBJ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
                    TotalPrice = function.get_Random(min, max);
                    LeavePrice -= TotalPrice;
                    Orders order = maindb.Orders.Add(new Orders()
                    {
                        CreateDate = DateTime.Now, //创建时间
                        PayStatus = 0,//支付状态(0 未支付 1 已支付)
                        ErpMode = query.ErpMode,//购买类型
                        UserId = UserId, //创客
                        TotalPrice = TotalPrice, //总价
                        OrderNo = OrderNo,//订单号
                        ParentOrderId = OrderId,//主订单Id
                    }).Entity;
                    maindb.SaveChanges();
                    maindb.OrderForNo.Add(new OrderForNo()
                    {
                        OrderNo = OrderNo,
                        OrderIds = order.Id.ToString()
                    });
                    maindb.SaveChanges();
                }
                string LastOrderNo = "CBJ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
                Orders orders = maindb.Orders.Add(new Orders()
                {
                    CreateDate = DateTime.Now, //创建时间
                    PayStatus = 0,//支付状态(0 未支付 1 已支付)
                    ErpMode = query.ErpMode,//购买类型
                    UserId = UserId, //创客
                    TotalPrice = LeavePrice, //总价
                    OrderNo = LastOrderNo,//订单号
                    ParentOrderId = OrderId,//主订单Id
                }).Entity;
                maindb.SaveChanges();
                maindb.OrderForNo.Add(new OrderForNo()
                {
                    OrderNo = LastOrderNo,
                    OrderIds = orders.Id.ToString()
                });
                maindb.SaveChanges();                
                RedisDbconn.Instance.Set(UserId + ":" + OrderId, "1");
                RedisDbconn.Instance.SetExpire(UserId + ":" + OrderId, 3600);
            }
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion

        #region 创客-首页-我的订单-详情-V2
        [Authorize]
        public JsonResult Detailv2(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = Detailv2Do(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> Detailv2Do(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            Orders query = OrdersDbconn.Instance.Get(Id) ?? new Orders();
            Obj.Add("OrderNo", query.OrderNo); //订单号
            Obj.Add("RealName", query.RealName); //姓名
            Obj.Add("Mobile", query.Mobile); //手机号
            Obj.Add("TotalPrice", query.TotalPrice); //总价
            Obj.Add("PayDate", query.PayDate == null ? "" : query.PayDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //付款时间
            Obj.Add("SendStatus", query.SendStatus); //发货状态
            Obj.Add("BuyCount", query.BuyCount); //下单数量
            Obj.Add("StatusName", RelationClass.GetOrderStatusInfo(query.Status));
            Obj.Add("Status", query.Status); //状态
            Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //创建时间

            var orders = maindb.Orders.Where(m => m.ParentOrderId == Id).ToList();
            List<Dictionary<string, object>> childOrder = new List<Dictionary<string, object>>();
            foreach (var item in orders)
            {
                Dictionary<string, object> row = new Dictionary<string, object>();
                row.Add("Id", item.Id);//订单Id
                row.Add("ProductPrice", item.TotalPrice);//价格
                row.Add("OrderNo", item.OrderNo);//订单号
                row.Add("PayStatus", item.PayStatus);//支付状态
                row.Add("CreateDate", item.CreateDate);//下单时间
                row.Add("PayDate", item.PayDate);//支付时间
                childOrder.Add(row);
            }
            Obj.Add("ChileOrders", childOrder);//子订单
            return Obj;
        }
        #endregion


        #region 创客-活动-推荐王-检查参与情况
        [Authorize]
        public JsonResult CheckJoinStatus(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = CheckJoinStatusDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> CheckJoinStatusDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            DateTime start = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
            DateTime end = DateTime.Now.AddDays(8 - DateTime.Now.Day).Date;
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            OrderProduct query = maindb.OrderProduct.FirstOrDefault(m => m.UserId == UserId && m.ProductId == 29 && m.CreateDate >= start && m.CreateDate <= end) ?? new OrderProduct();
            bool order = maindb.Orders.Any(m => m.Id == query.OrderId && m.Status > 0);
            if (order)
            {
                Obj.Add("Status", 1);
            }
            else
            {
                Obj.Add("Status", 0);
            }
            return Obj;
        }
        #endregion







        #region 首页-客小爽产品-机具管理-机具申请-要兑换的码牌
        [Authorize]
        public JsonResult ExchangeMachines(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = ExchangeMachinesDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        public List<Dictionary<string, object>> ExchangeMachinesDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            DateTime now = DateTime.Now;
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            List<KqProducts> KqProducts = maindb.KqProducts.ToList();
            IQueryable<PosMachines> query = maindb.PosMachines.Where(m => m.Status > -1 && m.BuyUserId == UserId && m.IsPurchase == 0);
            int ExpiredCount = query.Count(m => m.RecycEndDate < now);
            int ActCount = query.Count(m => m.ActivationState == 1);
            query = query.Where(m => m.RecycEndDate >= now && (m.ActivationState == 1 || (!string.IsNullOrEmpty(m.SeoKeyword) && m.SeoKeyword != "0")));
            if (PageNum == 1)
            {
                query = query.Take(PageSize);
            }
            else
            {
                int skipNum = PageSize * (PageNum - 1);
                query = query.Skip(skipNum).Take(PageSize);
            }
            foreach (var subdata in query.ToList())
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("SnId", subdata.Id); //机具Id
                curData.Add("SnNo", subdata.PosSn); //SN号
                curData.Add("MachineType", RelationClass.GetPosSnTypeInfo(subdata.PosSnType)); //机具类型
                int day = 0;
                if (subdata.RecycEndDate != null)
                {
                    TimeSpan ts = subdata.RecycEndDate.Value - DateTime.Now;
                    day = ts.Days;
                }
                curData.Add("RecycDays", day); //循环剩余天数
                curData.Add("EndDate", subdata.RecycEndDate == null ? "" : subdata.RecycEndDate.Value.ToString("yyyy-MM-dd")); //截止日期
                dataList.Add(curData);
            }
            Other = new Dictionary<string, object>();
            Other.Add("ActCount", ActCount); //激活数
            Other.Add("ExpiredCount", ExpiredCount); //过期数
            Other.Add("MinCount", 1); //最少领取数
            return dataList;
        }
        #endregion


        #region 检查签名是否合法，合法返回1，不合法返回提示信息

        /// <summary>
        /// 检查签名是否合法，合法返回1，不合法返回提示信息
        /// </summary>
        /// <param name="value">请求的参数(json字符串)</param>
        /// <param name="signField">要签名的字段</param>
        /// <returns></returns>
        private string CheckSign(string value, string[] signField)
        {
            JsonData json = JsonMapper.ToObject(value);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < signField.Length; i++)
            {
                dic.Add(signField[i], json[signField[i]].ToString());
            }
            string sign = json["sign"].ToString(); //客户端签名字符串
            return new Sign().sign(dic, sign);
        }

        #endregion

    }
}
