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 System.Web;
using MySystem.Models.Main1;
using LitJson;
using Library;
using MySystem.Service.Main;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/v1/[controller]/[action]")]
    public class PosMachinesController : BaseController
    {
        public PosMachinesController(IHttpContextAccessor accessor) : base(accessor)
        {
        }



        #region 商户-二维码关联音箱
        // [Authorize]
        public JsonResult List(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            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 BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].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>>();
            Models.Main.MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(BindMerchantId) ?? new Models.Main.MerchantInfo();
            List<Dictionary<string, object>> query = PosMachinesService.List(new List<RelationData>(), " and BindMerchantId=" + BindMerchantId, PageNum, PageSize);
            foreach (var subdata in query)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("BindingTime", subdata["BindingTime"].ToString()); //绑定时间
                curData.Add("PosSn", subdata["PosSn"].ToString()); //SN编号
                curData.Add("Id", subdata["Id"].ToString()); //Id
                curData.Add("MerchantName", merchant.Name); //商户名称
                string CheckKey = "_" + subdata["Id"].ToString();
                int BindCount = MachineForQrCodeDbconn.Instance.GetList(BindMerchantId).Count(m => m.DataId.EndsWith(CheckKey));
                curData.Add("BindCount", BindCount); //关联数量
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 商户-已绑音箱
        // [Authorize]
        public JsonResult BindedMachines(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = BindedMachinesDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> BindedMachinesDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].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>>();
            Models.Main.MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(BindMerchantId) ?? new Models.Main.MerchantInfo();
            List<int> query = PosMachinesDbconn.Instance.GetList(BindMerchantId, PageNum, PageSize);
            foreach (int MachineId in query)
            {
                PosMachines subdata = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines();
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("BindingTime", subdata.BindingTime == null ? "" : subdata.BindingTime.Value.ToString("yyyy-MM-dd")); //绑定时间
                curData.Add("PosSn", subdata.PosSn); //SN编号
                curData.Add("Id", subdata.Id); //Id
                curData.Add("MachineNo", subdata.Detail);
                curData.Add("MerchantName", merchant.Name); //商户名称
                string CheckKey = "_" + subdata.Id;
                List<Models.Main.MachineForQrCode> QrCodes = MachineForQrCodeDbconn.Instance.GetList(BindMerchantId);
                QrCodes = QrCodes.Where(m => m.DataId.EndsWith(CheckKey)).ToList();
                curData.Add("BindCount", QrCodes.Count); //关联数量
                List<Dictionary<string, object>> QrCodeList = new List<Dictionary<string, object>>();
                foreach (Models.Main.MachineForQrCode QrCode in QrCodes)
                {
                    Dictionary<string, object> item = new Dictionary<string, object>();
                    string DataId = QrCode.DataId;
                    if (DataId.Contains("_"))
                    {
                        DataId = DataId.Split('_')[0];
                    }
                    item.Add("Id", DataId);
                    item.Add("Type", 1);
                    item.Add("BindDate", QrCode.BindDate == null ? "" : QrCode.BindDate.Value.ToString("yyyy-MM-dd"));
                    item.Add("Sn", QrCode.SnNo);
                    item.Add("MachineNo", QrCode.MachineSnNo);
                    QrCodeList.Add(item);
                }
                curData.Add("QrCodeList", QrCodeList); //二维码列表
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 商户-替换音箱码
        // [Authorize]
        public JsonResult ReplaceMachines(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = ReplaceMachinesDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> ReplaceMachinesDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            string CheckKey = "_" + BindMerchantId;
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            List<Dictionary<string, object>> query = MerchantQrCodeService.List(new List<RelationData>(), " and MerchantId=" + BindMerchantId, 1, 1000);
            foreach (Dictionary<string, object> subdata in query)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("BindingTime", subdata["UpdateDate"].ToString()); //绑定时间
                curData.Add("PosSn", subdata["SnNo"].ToString()); //SN编号
                curData.Add("Id", subdata["Id"].ToString()); //Id
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 商户-确认关联音箱
        // [Authorize]
        public JsonResult Add(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = AddDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson AddDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
            // string QrCodeId = data["QrCodeId"].ToString(); //二维码Id
            int QrCodeId = int.Parse(function.CheckInt(data["QrCodeId"].ToString())); //二维码Id
            string MachineId = data["MachineId"].ToString(); //音箱Id
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            Models.Main.MerchantQrCode qrcode = MerchantQrCodeService.Query(QrCodeId);
            PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines();
            Models.Main.MachineForQrCode query = new Models.Main.MachineForQrCode()
            {
                DataId = QrCodeId + "_" + MachineId,
                MerchantId = BindMerchantId,
                BindDate = DateTime.Now,
                SnNo = qrcode.SnNo,
                MachineSnNo = machine.PosSn,
            };
            machine.BindingState = 1;
            qrcode.MachineId = int.Parse(MachineId);
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion

        #region 商户-绑定列表
        // [Authorize]
        public JsonResult BindList(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = BindListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> BindListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].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>>();
            string condition = "";
            if (!string.IsNullOrEmpty(data["BindMerchantId"].ToString()))
            {
                condition += " and BindMerchantId=" + BindMerchantId;
            }
            List<Dictionary<string, object>> query = PosMachinesService.List(new List<RelationData>(), condition, PageNum, PageSize);
            foreach (var subdata in query)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("BindingTime", subdata["BindingTime"].ToString()); //绑定时间
                curData.Add("PosSn", subdata["PosSn"].ToString()); //SN编号
                curData.Add("MerchantName", ""); //商户名称
                curData.Add("MerchantLogo", ""); //商户Logo
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 商户-绑定机具
        // [Authorize]
        public JsonResult Bind(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = BindDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson BindDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string PosSn = data["PosSn"].ToString(); //SN编号
            int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
            Models.Main.MerchantInfo merchant = MerchantInfoService.Query(BindMerchantId);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            PosMachines query = new PosMachines();
            query = new PosMachines()
            {
                CreateDate = DateTime.Now, //创建时间
                UpdateDate = DateTime.Now, //修改时间
                BuyUserId = merchant.UserId, //绑定所属创客Id
                UserId = merchant.UserId, //绑定所属创客Id
                PosSn = PosSn, //SN编号
                BindMerchantId = BindMerchantId, //绑定商户
            };
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion

        #region 商户-已绑音箱解除关联
        // [Authorize]
        public JsonResult Remove(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = RemoveDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson RemoveDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string CheckKey = "_" + Id;
            var list = main1db.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == BindMerchantId && m.DataId.EndsWith(CheckKey)).ToList();
            foreach (var sub in list)
            {
                Models.Main.MachineForQrCode edit = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId);
                if (edit != null)
                {
                    maindb.MachineForQrCode.Remove(edit);
                    int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1]));
                    PosMachines machine = main1db.PosMachines.FirstOrDefault(m => m.BindMerchantId == MachineId);
                    if (machine != null)
                    {
                        machine.BindMerchantId = 0;
                        machine.BuyUserId = 0;
                        machine.UserId = 0;
                    }
                }
            }
            main1db.SaveChanges();

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



        #region 首页-客小爽产品-机具管理-已绑定机具列表
        // [Authorize]
        public JsonResult MyMachinesForBind(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = MyMachinesForBindDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        public List<Dictionary<string, object>> MyMachinesForBindDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客
            int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
            string SnNo = data["SnNo"].ToString(); //SN号
            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>>();
            if (!string.IsNullOrEmpty(SnNo))
            {
                PageSize = 100000;
            }
            IQueryable<PosMachinesTwo> query = main1db.PosMachinesTwo.Where(m => m.Status > -1 && m.BuyUserId == UserId && m.BindingState == 1 && m.OpId == 2);
            if (!string.IsNullOrEmpty(SnNo))
            {
                query = query.Where(m => m.PosSn == SnNo);
            }
            Other = new Dictionary<string, object>();
            if (PageNum == 1)
            {
                Other.Add("count", query.Count());
                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 IsLeader = 0;//是否为大盟主机(0-否 1-是)
                if (subdata.LeaderUserId > 0)
                {
                    IsLeader = 1;
                }
                else
                {
                    IsLeader = 0;
                }
                curData.Add("ActivationState", subdata.ActivationState == 1 ? "已激活" : "未激活"); //激活状态
                curData.Add("PosSn", subdata.PosSn); //SN编号
                curData.Add("PosSnType", RelationClass.GetPosSnTypeInfo(subdata.PosSnType)); //机具类型
                curData.Add("Id", subdata.Id); //Id
                decimal Deposit = 299;
                if (BrandId == 6)
                {
                    Deposit = 249;
                }
                if (!string.IsNullOrEmpty(subdata.PrizeParams))
                {
                    Deposit = decimal.Parse(function.CheckNum(subdata.PrizeParams));
                }
                curData.Add("Deposit", Deposit);
                Models.Main.MerchantAddInfo merchant = maindb.MerchantAddInfo.FirstOrDefault(m => m.Id == subdata.BindMerchantId) ?? new Models.Main.MerchantAddInfo();
                curData.Add("BindedMerchant", merchant.CertMerchantName);
                curData.Add("IsLeader", IsLeader);
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 首页-客小爽产品-机具管理-总机具列表
        // [Authorize]
        public JsonResult MyMachines(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = MyMachinesDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        public List<Dictionary<string, object>> MyMachinesDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客
            int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
            string SnNo = data["SnNo"].ToString(); //SN号
            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>>();
            if (!string.IsNullOrEmpty(SnNo))
            {
                PageSize = 100000;
            }
            IQueryable<PosMachinesTwo> query = main1db.PosMachinesTwo.Where(m => m.Status > -1 && m.BuyUserId == UserId && m.BrandId == BrandId);
            if (!string.IsNullOrEmpty(SnNo))
            {
                query = query.Where(m => m.PosSn == SnNo);
            }
            Other = new Dictionary<string, object>();
            if (PageNum == 1)
            {
                Other.Add("count", query.Count());
                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 IsLeader = 0;//是否为大盟主机(0-否 1-是)
                if (subdata.LeaderUserId > 0)
                {
                    IsLeader = 1;
                }
                else
                {
                    IsLeader = 0;
                }
                curData.Add("PosSn", subdata.PosSn); //SN编号
                curData.Add("PosSnType", RelationClass.GetPosSnTypeInfo(subdata.PosSnType)); //机具类型
                curData.Add("Id", subdata.Id); //Id
                curData.Add("CreateDate", subdata.TransferTime == null ? "" : subdata.TransferTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
                StoreHouse store = StoreHouseDbconn.Instance.Get(subdata.StoreId) ?? new StoreHouse();
                curData.Add("FromStore", store.StoreName); //来自仓库
                decimal Deposit = 299;
                if (BrandId == 6)
                {
                    Deposit = 249;
                }
                if (!string.IsNullOrEmpty(subdata.PrizeParams))
                {
                    Deposit = decimal.Parse(function.CheckNum(subdata.PrizeParams));
                }
                curData.Add("Deposit", Deposit);
                curData.Add("IsLeader", IsLeader);
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 首页-客小爽产品-机具管理-未绑定机具列表
        // [Authorize]
        public JsonResult MyMachinesForUnBind(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = MyMachinesForUnBindDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        public List<Dictionary<string, object>> MyMachinesForUnBindDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客
            int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
            string SnNo = data["SnNo"].ToString(); //SN号
            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>>();
            if (!string.IsNullOrEmpty(SnNo))
            {
                PageSize = 100000;
            }
            IQueryable<PosMachinesTwo> query = main1db.PosMachinesTwo.Where(m => m.Status > -1 && m.BuyUserId == UserId && m.BindingState == 0);
            if (!string.IsNullOrEmpty(SnNo))
            {
                query = query.Where(m => m.PosSn == SnNo && m.PosSnType == 0);
            }
            Other = new Dictionary<string, object>();
            if (PageNum == 1)
            {
                Other.Add("count", query.Count());
                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 IsLeader = 0;//是否为大盟主机(0-否 1-是)
                if (subdata.LeaderUserId > 0)
                {
                    IsLeader = 1;
                }
                else
                {
                    IsLeader = 0;
                }
                curData.Add("PosSn", subdata.PosSn); //SN编号
                curData.Add("PosSnType", RelationClass.GetPosSnTypeInfo(subdata.PosSnType)); //机具类型
                curData.Add("Id", subdata.Id); //Id
                if (subdata.RecycEndDate != null)
                {
                    TimeSpan ts = subdata.RecycEndDate.Value - DateTime.Now;
                    curData.Add("ActDays", ts.TotalDays.ToString("f0")); //活动剩余天数
                }
                else
                {
                    curData.Add("ActDays", 0); //活动剩余天数
                }
                curData.Add("CreateDate", subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //机具初始日期
                curData.Add("EndDate", subdata.RecycEndDate == null ? "" : subdata.RecycEndDate.Value.ToString("yyyy-MM-dd")); //截止日期
                decimal Deposit = 299;
                if (BrandId == 6)
                {
                    Deposit = 249;
                }
                if (!string.IsNullOrEmpty(subdata.PrizeParams))
                {
                    Deposit = decimal.Parse(function.CheckNum(subdata.PrizeParams));
                }
                curData.Add("Deposit", Deposit);
                curData.Add("IsLeader", IsLeader);
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion



        
        #region 首页-客小爽产品-机具管理-已绑定机具列表
        // [Authorize]
        public JsonResult MyMachinesForBind2(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = MyMachinesForBind2Do(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        public List<Dictionary<string, object>> MyMachinesForBind2Do(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客
            int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
            string SnNo = data["SnNo"].ToString(); //SN号
            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>>();
            if (!string.IsNullOrEmpty(SnNo))
            {
                PageSize = 100000;
            }
            IQueryable<PosMachines> query = main1db.PosMachines.Where(m => m.Status > -1 && m.BuyUserId == UserId && m.BindingState == 1 && m.DeviceKind == "2");
            if (!string.IsNullOrEmpty(SnNo))
            {
                query = query.Where(m => m.PosSn == SnNo);
            }
            Other = new Dictionary<string, object>();
            if (PageNum == 1)
            {
                Other.Add("count", query.Count());
                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("ActivationState", subdata.ActivationState == 1 ? "已激活" : "未激活"); //激活状态
                curData.Add("PosSn", subdata.PosSn); //SN编号
                curData.Add("PosSnType", RelationClass.GetPosSnTypeInfo(subdata.PosSnType)); //机具类型
                curData.Add("Id", subdata.Id); //Id
                decimal Deposit = 299;
                if (BrandId == 6)
                {
                    Deposit = 249;
                }
                if (!string.IsNullOrEmpty(subdata.PrizeParams))
                {
                    Deposit = decimal.Parse(function.CheckNum(subdata.PrizeParams));
                }
                curData.Add("Deposit", Deposit);
                var merchant = maindb.MerchantAddInfo.FirstOrDefault(m => m.Id == subdata.BindMerchantId) ?? new Models.Main.MerchantAddInfo();
                curData.Add("BindedMerchant", merchant.CertMerchantName);
                curData.Add("IsLeader", 0);
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 首页-客小爽产品-机具管理-总机具列表
        // [Authorize]
        public JsonResult MyMachines2(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = MyMachines2Do(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        public List<Dictionary<string, object>> MyMachines2Do(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客
            int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
            string SnNo = data["SnNo"].ToString(); //SN号
            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>>();
            if (!string.IsNullOrEmpty(SnNo))
            {
                PageSize = 100000;
            }
            IQueryable<PosMachines> query = main1db.PosMachines.Where(m => m.Status > -1 && m.BuyUserId == UserId && m.BrandId == BrandId);
            if (!string.IsNullOrEmpty(SnNo))
            {
                query = query.Where(m => m.PosSn == SnNo);
            }
            Other = new Dictionary<string, object>();
            if (PageNum == 1)
            {
                Other.Add("count", query.Count());
                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 IsLeader = 0;//是否为大盟主机(0-否 1-是)
                curData.Add("PosSn", subdata.PosSn); //SN编号
                curData.Add("PosSnType", RelationClass.GetPosSnTypeInfo(subdata.PosSnType)); //机具类型
                curData.Add("Id", subdata.Id); //Id
                curData.Add("CreateDate", subdata.TransferTime == null ? "" : subdata.TransferTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
                StoreHouse store = StoreHouseDbconn.Instance.Get(subdata.StoreId) ?? new StoreHouse();
                curData.Add("FromStore", store.StoreName); //来自仓库
                decimal Deposit = 299;
                if (BrandId == 6)
                {
                    Deposit = 249;
                }
                if (!string.IsNullOrEmpty(subdata.PrizeParams))
                {
                    Deposit = decimal.Parse(function.CheckNum(subdata.PrizeParams));
                }
                curData.Add("Deposit", Deposit);
                curData.Add("IsLeader", IsLeader);
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 首页-客小爽产品-机具管理-未绑定机具列表
        // [Authorize]
        public JsonResult MyMachinesForUnBind2(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = MyMachinesForUnBind2Do(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        public List<Dictionary<string, object>> MyMachinesForUnBind2Do(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客
            int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
            string SnNo = data["SnNo"].ToString(); //SN号
            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>>();
            if (!string.IsNullOrEmpty(SnNo))
            {
                PageSize = 100000;
            }
            IQueryable<PosMachines> query = main1db.PosMachines.Where(m => m.Status > -1 && m.BuyUserId == UserId && m.BindingState == 0);
            if (!string.IsNullOrEmpty(SnNo))
            {
                query = query.Where(m => m.PosSn == SnNo && m.PosSnType == 0);
            }
            Other = new Dictionary<string, object>();
            if (PageNum == 1)
            {
                Other.Add("count", query.Count());
                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 IsLeader = 0;
                curData.Add("PosSn", subdata.PosSn); //SN编号
                curData.Add("PosSnType", RelationClass.GetPosSnTypeInfo(subdata.PosSnType)); //机具类型
                curData.Add("Id", subdata.Id); //Id
                if (subdata.RecycEndDate != null)
                {
                    TimeSpan ts = subdata.RecycEndDate.Value - DateTime.Now;
                    curData.Add("ActDays", ts.TotalDays.ToString("f0")); //活动剩余天数
                }
                else
                {
                    curData.Add("ActDays", 0); //活动剩余天数
                }
                curData.Add("CreateDate", subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //机具初始日期
                curData.Add("EndDate", subdata.RecycEndDate == null ? "" : subdata.RecycEndDate.Value.ToString("yyyy-MM-dd")); //截止日期
                decimal Deposit = 299;
                if (BrandId == 6)
                {
                    Deposit = 249;
                }
                if (!string.IsNullOrEmpty(subdata.PrizeParams))
                {
                    Deposit = decimal.Parse(function.CheckNum(subdata.PrizeParams));
                }
                curData.Add("Deposit", Deposit);
                curData.Add("IsLeader", IsLeader);
                dataList.Add(curData);
            }
            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

    }
}
