public IActionResult SetCommissionRate(CommissionViewModel commissionViewModel)
        {
            Guid             currentUserGuid = Guid.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "guid").Value);
            QrcodeSellerList guid            = sellerContext.QrcodeSellerList.First(s => s.SellerId == commissionViewModel.SellerID);

            //权限验证
            if (IsChild(guid.UserGuid, currentUserGuid) && (sellerContext.Commission.FirstOrDefault(x => x.UserGuid == currentUserGuid)?.Rate ?? 0.5) >= commissionViewModel.Rate)
            {
                Commission commission = new Commission()
                {
                    Mtime     = DateTime.Now,
                    Rate      = commissionViewModel.Rate,
                    SellerId  = commissionViewModel.SellerID,
                    UserGuid  = guid.UserGuid,
                    CreatedBy = currentUserGuid,
                };
                sellerContext.Commission.Add(commission);
                sellerContext.SaveChanges();
                return(Ok());
            }
            else
            {
                return(Forbid());
            }
        }
        public IActionResult Create([FromBody] QrcodeSellerList body, bool isMA = true)
        {
            IQueryable <QrcodeSellerList> exsit = from user in sellerContext.QrcodeSellerList
                                                  where user.PhoneNumber == body.PhoneNumber && user.Type == body.Type
                                                  select user;
            QrcodeSellerList exsitSeller = exsit.FirstOrDefault();

            if (exsitSeller != default)
            {
                if (exsitSeller.IsEnable)
                {
                    //每个用户同类分销码最多只可以有一个
                    return(Forbid());
                }
                else
                {
                    sellerContext.QrcodeSellerList.Remove(exsitSeller);
                    sellerContext.SaveChanges();
                    return(Create(body, isMA));
                }
            }

            using (HttpClient client = httpClient.CreateClient())
            {
                QrcodeSellerList currentSeller = GetCurrentSeller();

                //AutoCreate
                string        jupiterPath = configuration.GetSection("AppSetting").Get <JupiterKeys>().JupiterPath;
                StringContent content     = new StringContent(JsonConvert.SerializeObject(new { body.PhoneNumber, WeChatUnionID = default(string), Permission = isMA ? "QRCodeSellerMA" : "QRCodeSeller" }));
                content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                Task <HttpResponseMessage> x = client.PostAsync($"{jupiterPath}/api/v1/User/AutoCreate", content);
                body.SellerId   = AvailableSellerID(BitConverter.GetBytes(currentSeller.SellerId)[0] == 0 && isMA, isMA);
                body.CreateTime = DateTime.Now;
                body.ModifyTime = DateTime.Now;
                body.IsEnable   = true;
                body.CA         = currentSeller?.UserGuid ?? Guid.Empty;
                string guidStr = JsonConvert.DeserializeObject <dynamic>(x.Result.Content.ReadAsStringAsync().Result).data.guid;
                var    guid    = Guid.Parse(guidStr);
                //Guid guid = Guid.Parse(JToken.Parse(x.Result.Content.ReadAsStringAsync().Result.Trim()).Value<string>());
                IQueryable <QrcodeSellerList> belongOtherCA = from record in sellerContext.QrcodeSellerList
                                                              where record.UserGuid == guid
                                                              where record.CA != body.CA
                                                              where record.IsEnable == true
                                                              select record;

                if (belongOtherCA.FirstOrDefault() != default)
                {
                    return(Forbid());
                }

                body.UserGuid = guid;
                sellerContext.QrcodeSellerList.Add(body);
                sellerContext.SaveChanges();
            }
            return(Ok(body));
        }
        public IActionResult QRCodeSeller(int SellerId)
        {
            QrcodeSellerList currentSeller       = GetCurrentSeller();
            IQueryable <QrcodeSellerList> target = from seller in sellerContext.QrcodeSellerList
                                                   where seller.SellerId == SellerId
                                                   where seller.IsEnable == true
                                                   where seller.CA == currentSeller.CA
                                                   select seller;

            return(Ok(target.First()));
        }
        public IActionResult Update([FromBody] QrcodeSellerList body)
        {
            QrcodeSellerList currentSeller       = GetCurrentSeller();
            IQueryable <QrcodeSellerList> target = from seller in sellerContext.QrcodeSellerList
                                                   where seller.SellerId == body.SellerId
                                                   where seller.CA == currentSeller.CA || currentSeller.CA == Guid.Parse("FC45FBF4-1CE4-4422-96B6-FEFBAEBAFFC6")
                                                   select seller;
            QrcodeSellerList updateTarget = target.First();

            updateTarget.IdentityCardNumber = body.IdentityCardNumber;
            updateTarget.Name       = body.Name;
            updateTarget.IsEnable   = body.IsEnable;
            updateTarget.ModifyTime = DateTime.Now;
            sellerContext.Update(updateTarget);
            sellerContext.SaveChanges();
            return(Ok());
        }
        public IActionResult New([FromForm] int costumerID, [FromForm] int sellerID, [FromForm] string type = "Basic")
        {
            type = sellerContext.QrcodeSellerList.FirstOrDefault(x => x.SellerId == sellerID)?.Type;
            QrcodeSellerList message = sellerContext.QrcodeSellerList.FirstOrDefault(x => x.SellerId == sellerID);

            if (type == "Basic")
            {
                if ((from sum in sellerContext.SellerUserMapping
                     where sum.CostumerId == costumerID && sum.Type == type
                     select sum).FirstOrDefault() == default)
                {
                    sellerContext.SellerUserMapping.Add(new SellerUserMapping
                    {
                        CostumerId = costumerID,
                        SellerId   = sellerID,
                        CreateTime = DateTime.Now,
                        Type       = type
                    });
                    sellerContext.SaveChanges();
                    string x = AliyunSms.SendSms(message.PhoneNumber, message.Name, message.Type);
                    return(Ok());
                }
                else
                {
                    return(Forbid());
                }
            }
            else
            {
                sellerContext.SellerUserMapping.Add(new SellerUserMapping
                {
                    CostumerId = costumerID,
                    SellerId   = sellerID,
                    CreateTime = DateTime.Now,
                    Type       = type
                });
                sellerContext.SaveChanges();
                string x = AliyunSms.SendSms(message.PhoneNumber, message.Name, message.Type);
                return(Ok());
            }
        }
        public IActionResult Orders(int sellerID, string type = "Basic")
        {
            Guid             currentUserGuid = Guid.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "guid").Value);
            QrcodeSellerList sellerGuid      = sellerContext.QrcodeSellerList.FirstOrDefault(x => x.SellerId == sellerID);

            if (sellerGuid.UserGuid != Guid.Empty && !IsChild(sellerGuid.CA, currentUserGuid))
            {
                return(Ok(Array.Empty <object>()));
            }
            else
            {
                IQueryable <SellerUserMapping> relatedCostumers = from sum in sellerContext.SellerUserMapping
                                                                  where (sum.Type == type || type == "Any") && sum.SellerId == sellerID
                                                                  select sum;
                IEnumerable <LitemallOrder> relatedOrders = from sum in relatedCostumers.ToList()
                                                            from order in litemallContext.LitemallOrder
                                                            where order.UserId == sum.CostumerId
                                                            select order;
                LitemallOrder[] result = relatedOrders.ToArray();
                return(Ok(result));
            }
        }
        public IActionResult List(string type = "Basic", bool isEnable = true)
        {
            QrcodeSellerList currentSeller = GetCurrentSeller();
            IQueryable <IGrouping <Guid, QrcodeSellerList> > enableSeller = from seller in sellerContext.QrcodeSellerList
                                                                            where seller.IsEnable == isEnable
                                                                            where seller.Type == type || type == "Any"
                                                                            group seller by seller.CA;
            List <QrcodeSellerList> target = new List <QrcodeSellerList>();
            var rates = from com in sellerContext.Commission
                        orderby com.Mtime descending
                        select new { com.Rate, com.UserGuid };

            foreach (IGrouping <Guid, QrcodeSellerList> group in enableSeller)
            {
                if (IsChild(group.Key, currentSeller.UserGuid))
                {
                    target.AddRange(group);
                }
            }
            var result = from seller in target
                         select new { seller.IdentityCardNumber, seller.PhoneNumber, seller.IsEnable, seller.Name, SellerID = seller.SellerId, seller.Type, Rate = rates.FirstOrDefault(x => x.UserGuid == seller.UserGuid)?.Rate ?? 0.5 };

            return(Ok(result));
        }
 public IActionResult Create([FromBody] QrcodeSellerList body)
 {
     return(Create(body, false));
 }