Добавьте файлы проекта.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Text.RegularExpressions;
|
||||
using SvetoforVKBot.Models.Commands.LK.Menu.Products;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using VkNet;
|
||||
using VkNet.Model.RequestParams;
|
||||
using Newtonsoft.Json;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using System.Linq;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu.Products
|
||||
{
|
||||
public class SelectProductCommand : Command
|
||||
{
|
||||
public override string Name => "{\"button\":\"selectProduct-";
|
||||
|
||||
public override void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
}
|
||||
|
||||
public override void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
var chatId = update.@object.message.from_id;
|
||||
string msg = "";
|
||||
string voronka = "";
|
||||
double sum = 0;
|
||||
string backPL = "startPL";
|
||||
Regex regex = new Regex("[^0-9]");
|
||||
ShowProductCaruselMain showProductCaruselMain = new ShowProductCaruselMain();
|
||||
ShowProductMain showProductMain = new ShowProductMain();
|
||||
CategoriesDostavka categories = new CategoriesDostavka();
|
||||
var keyboardBuilder = new KeyboardBuilder().Clear();
|
||||
|
||||
try
|
||||
{
|
||||
string[] payload = update.@object.message.payload.Split('-');
|
||||
int curCategory = Convert.ToInt32(regex.Replace(payload[1], ""));
|
||||
int curProduct = Convert.ToInt32(regex.Replace(payload[2], ""));
|
||||
int curWay = Convert.ToInt32(regex.Replace(payload[3], "")); //для ShowProductCaruselMain направление навигации, 1 по умолчанию
|
||||
|
||||
var cats = db.CategoriesDostavkas.Where(e => e.subMenu == curCategory);
|
||||
if(cats.Count() > 0)
|
||||
{
|
||||
ShowMenuDostavka showMenuDostavka = new ShowMenuDostavka();
|
||||
showMenuDostavka.Execute(update, client, db, msg, 2, categories.subMenu); //1
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (curCategory)
|
||||
{
|
||||
default:
|
||||
if (update.@object.client_info.carousel == true)
|
||||
showProductCaruselMain.Execute(update, client, db, curCategory, curProduct, curWay);
|
||||
else
|
||||
showProductMain.Execute(update, client, db, curCategory, curProduct, 0, 1);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в SelectProductCommand: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,269 @@
|
||||
using Newtonsoft.Json;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.SqlClient;
|
||||
using System.Security.Cryptography;
|
||||
using VkNet;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Model.RequestParams;
|
||||
using VkNet.Utils;
|
||||
using VkNet.Model.Template;
|
||||
using VkNet.Model.Template.Carousel;
|
||||
using System.Linq;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu.Products
|
||||
{
|
||||
public class ShowProductCaruselMain
|
||||
{
|
||||
private const string PHOTOIDNEXT = "-194717824_457239273_446146426236760985";
|
||||
//{"id": 457239273, "owner_id": -194717824, "access_key": "446146426236760985"}
|
||||
public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db, int curCategory, int curProduct, int curWay)
|
||||
//curCategory - id Categories, curProduct - порядковый номер в массиве продуктов, curWay - для showProductCarusel направление навигации
|
||||
{
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
var chatId = update.@object.message.from_id;
|
||||
int i = 0;
|
||||
int prevCatalog = 0, nextCatalog = 0;
|
||||
int naviState = 0;
|
||||
double sum = 0;
|
||||
string desc = "";
|
||||
//int curDay = (int)DateTime.Now.DayOfWeek;
|
||||
//if (curDay == 0) curDay = 7;
|
||||
|
||||
List<CategoriesDostavka> categories = new List<CategoriesDostavka>();
|
||||
List<ProductsDostavka> products = new List<ProductsDostavka>();
|
||||
var row = new List<MessageKeyboardButton>();
|
||||
var listButtons = new List<ReadOnlyCollection<MessageKeyboardButton>>();
|
||||
TemplateBuilder templateBuilder = new TemplateBuilder();
|
||||
CarouselElementAction carouselElementAction = new CarouselElementAction();
|
||||
carouselElementAction.Type = CarouselElementActionType.OpenPhoto;
|
||||
|
||||
try
|
||||
{
|
||||
var user = db.Users.Single(usr => usr.chatId == chatId);
|
||||
var cartObject = JsonConvert.DeserializeObject<CartObject>(user.cart);
|
||||
int curDay = user.day;
|
||||
|
||||
//SqlCommand getCart = new SqlCommand("SELECT cart, day, colorId FROM Users WHERE chatId = @chatId;", Con);
|
||||
//getCart.Parameters.AddWithValue("@chatId", chatId);
|
||||
//SqlDataReader rgetCart = getCart.ExecuteReader();
|
||||
//rgetCart.Read();
|
||||
//var cartObject = JsonConvert.DeserializeObject<CartObject>(rgetCart["cart"].ToString());
|
||||
//int curDay = Convert.ToInt32(rgetCart["day"]);
|
||||
//int colorId = Convert.ToInt32(rgetCart["colorId"]);
|
||||
//rgetCart.Close();
|
||||
|
||||
if (curDay == 8) curDay = (int)DateTime.Now.DayOfWeek;
|
||||
|
||||
foreach (var o in cartObject.orders)
|
||||
{
|
||||
sum += o.price * o.number;
|
||||
}
|
||||
|
||||
categories = db.CategoriesDostavkas.ToList().ConvertAll<CategoriesDostavka>(c =>
|
||||
new CategoriesDostavka()
|
||||
{
|
||||
categoryId = c.categoryId,
|
||||
name = c.name,
|
||||
btnName = c.btnName,
|
||||
kbColor = c.kbColor,
|
||||
});
|
||||
|
||||
|
||||
//SqlCommand getCategories = new SqlCommand("SELECT * FROM CategoriesDostavka;", Con);
|
||||
//getCategories.Parameters.AddWithValue("@chatId", chatId);
|
||||
//SqlDataReader rgetCategories = getCategories.ExecuteReader();
|
||||
//while (rgetCategories.Read())
|
||||
// categories.Add(new CategoriesDostavka()
|
||||
// {
|
||||
// categoryId = Convert.ToInt32(rgetCategories["categoryId"]),
|
||||
// name = rgetCategories["name"].ToString(),
|
||||
// btnName = rgetCategories["btnName"].ToString(),
|
||||
// kbColor = rgetCategories["kbColor"].ToString(),
|
||||
// });
|
||||
//rgetCategories.Close();
|
||||
|
||||
|
||||
products = db.ProductsDostavkas.Where(p => p.categoryId == curCategory &&
|
||||
JsonConvert.DeserializeObject<List<int>>(p.weekDays).Exists(day => day == curDay)).
|
||||
ToList().ConvertAll<ProductsDostavka>(e => new ProductsDostavka()
|
||||
{
|
||||
id = e.id,
|
||||
article = e.article,
|
||||
colorId = e.colorId,
|
||||
categoryId = e.categoryId,
|
||||
name = e.name,
|
||||
fullName = e.fullName,
|
||||
description = e.description,
|
||||
descriptionCarousel = e.descriptionCarousel,
|
||||
price = Convert.ToDouble(e.price),
|
||||
photo = e.photo,
|
||||
photoCarousel = e.photoCarousel,
|
||||
additions = JsonConvert.DeserializeObject<List<string>>(e.additions),
|
||||
recommends = JsonConvert.DeserializeObject<List<string>>(e.recommends),
|
||||
});
|
||||
|
||||
//SqlCommand getProducts = new SqlCommand("SELECT * FROM ProductsDostavka WHERE categoryId = @categoryId;", Con);
|
||||
//getProducts.Parameters.AddWithValue("@categoryId", curCategory);
|
||||
////getProducts.Parameters.AddWithValue("@colorId", colorId);
|
||||
//SqlDataReader rgetProducts = getProducts.ExecuteReader();
|
||||
//while (rgetProducts.Read())
|
||||
//{
|
||||
// var weekDays = JsonConvert.DeserializeObject<List<int>>(rgetProducts["weekDays"].ToString());
|
||||
// foreach (int weekDay in weekDays)
|
||||
// {
|
||||
// if (weekDay == curDay)
|
||||
// {
|
||||
// products.Add(new ProductsDostavka()
|
||||
// {
|
||||
// id = Convert.ToInt32(rgetProducts["id"]),
|
||||
// article = rgetProducts["article"].ToString(),
|
||||
// colorId = Convert.ToInt32(rgetProducts["colorId"]),
|
||||
// categoryId = Convert.ToInt32(rgetProducts["categoryId"]),
|
||||
// name = rgetProducts["name"].ToString(),
|
||||
// fullName = rgetProducts["fullName"].ToString(),
|
||||
// descriptionCarousel = rgetProducts["descriptionCarousel"].ToString(),
|
||||
// price = Convert.ToInt32(rgetProducts["price"]),
|
||||
// photo = rgetProducts["photo"].ToString(),
|
||||
// photoCarousel = rgetProducts["photoCarousel"].ToString(),
|
||||
// //weight = rgetProducts["weight"].ToString(),
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//rgetProducts.Close();
|
||||
|
||||
nextCatalog = curProduct + 9;
|
||||
prevCatalog = curProduct - 9;
|
||||
|
||||
if (curProduct > 0 && products.Count > 6)
|
||||
{
|
||||
if (nextCatalog > products.Count) nextCatalog = products.Count - (products.Count - curProduct);
|
||||
|
||||
if (prevCatalog < 0) prevCatalog = 0;
|
||||
}
|
||||
|
||||
for (i = curProduct; i < products.Count; i++)
|
||||
{
|
||||
var jsPhoto = JsonConvert.DeserializeObject<CatalogPhotoObject>(products[i].photoCarousel);
|
||||
|
||||
desc = products[i].descriptionCarousel; //+ " " + products[i].weight;
|
||||
if (desc.Length > 80) desc = desc.Substring(0, 77) + "..."; //desc.Substring(0, 77)
|
||||
|
||||
templateBuilder.AddTemplateElement(new CarouselElement()
|
||||
{
|
||||
Title = products[i].fullName,
|
||||
Description = desc,
|
||||
PhotoId = jsPhoto.owner_id + "_" + jsPhoto.id + "_" + jsPhoto.access_key,
|
||||
Buttons = new List<MessageKeyboardButton>()
|
||||
{
|
||||
new MessageKeyboardButton(){
|
||||
Action = new MessageKeyboardButtonAction(){ Label = "Подробнее",
|
||||
Payload = "{\"button\":\"selectAddToCart-" + curCategory + "-" + i + "\"}",
|
||||
Type = KeyboardButtonActionType.Text },
|
||||
Color = KeyboardButtonColor.Default
|
||||
},
|
||||
},
|
||||
Action = carouselElementAction,
|
||||
});
|
||||
|
||||
if (templateBuilder.Elements.Count == 9 && products.Count > 10)
|
||||
break;
|
||||
}
|
||||
|
||||
if (products.Count > 10)
|
||||
{
|
||||
switch (curWay)
|
||||
{
|
||||
case 1:
|
||||
if (curProduct < (products.Count - 9))
|
||||
naviState = 1;
|
||||
else
|
||||
naviState = 2;
|
||||
break;
|
||||
case 2:
|
||||
if (curProduct == 0)
|
||||
naviState = 1;
|
||||
else
|
||||
naviState = 2;
|
||||
break;
|
||||
default:
|
||||
naviState = 2;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (naviState)
|
||||
{
|
||||
case 1:
|
||||
templateBuilder.AddTemplateElement(new CarouselElement()
|
||||
{
|
||||
Title = ".",
|
||||
Description = ".",
|
||||
PhotoId = PHOTOIDNEXT,
|
||||
Buttons = new List<MessageKeyboardButton>()
|
||||
{
|
||||
new MessageKeyboardButton() {
|
||||
Action = new MessageKeyboardButtonAction(){ Label = ">> Следующие " + (products.Count - nextCatalog).ToString(),
|
||||
Payload = "{\"button\":\"selectProduct-" + products[nextCatalog].categoryId + "-" + nextCatalog + "-1\"}",
|
||||
Type = KeyboardButtonActionType.Text },
|
||||
Color = KeyboardButtonColor.Primary
|
||||
},
|
||||
},
|
||||
Action = carouselElementAction,
|
||||
});
|
||||
break;
|
||||
|
||||
case 2:
|
||||
templateBuilder.AddTemplateElement(new CarouselElement()
|
||||
{
|
||||
Title = ".",
|
||||
Description = ".",
|
||||
PhotoId = PHOTOIDNEXT,
|
||||
Buttons = new List<MessageKeyboardButton>()
|
||||
{
|
||||
new MessageKeyboardButton() {
|
||||
Action = new MessageKeyboardButtonAction(){ Label = "<< Предыдущие " + curProduct,
|
||||
Payload = "{\"button\":\"selectProduct-" + products[prevCatalog].categoryId + "-" + prevCatalog + "-2\"}",
|
||||
Type = KeyboardButtonActionType.Text },
|
||||
Color = KeyboardButtonColor.Primary
|
||||
},
|
||||
},
|
||||
Action = carouselElementAction,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@params.Template = templateBuilder.Build();
|
||||
@params.Message = "😍";
|
||||
@params.UserId = chatId;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в ShowProductCaruselMain: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
|
||||
private int GetRandomId()
|
||||
{
|
||||
|
||||
var intBytes = new byte[4];
|
||||
|
||||
Rng.GetBytes(intBytes);
|
||||
|
||||
return BitConverter.ToInt32(intBytes, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
using Newtonsoft.Json;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.RegularExpressions;
|
||||
using VkNet;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using VkNet.Model.Attachments;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Model.RequestParams;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu.Products
|
||||
{
|
||||
public class ShowProductMain
|
||||
{
|
||||
public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db, int curCategory, int curProduct, int curAddition, int plAction)
|
||||
{
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
var chatId = update.@object.message.from_id;
|
||||
int i = 0;
|
||||
int index = 0;
|
||||
Regex regex = new Regex("[^0-9]");
|
||||
int prevCatalog = 0, nextCatalog = 0;
|
||||
//double sumCart = 0;
|
||||
double sum = 0;
|
||||
Additions addition = new Additions();
|
||||
//DateTime startDate = new DateTime(2020, 09, 14);
|
||||
//DateTime currentDate = DateTime.Now;
|
||||
//TimeSpan offset = currentDate - startDate;
|
||||
//int curDay = offset.Days % 14 + 1;
|
||||
|
||||
string backPL = "selectMenu-0";
|
||||
|
||||
List<CategoriesDostavka> categories = new List<CategoriesDostavka>();
|
||||
List<ProductsDostavka> products = new List<ProductsDostavka>();
|
||||
var keyboardBuilder = new KeyboardBuilder().Clear();
|
||||
|
||||
try
|
||||
{
|
||||
var user = db.Users.Single(usr => usr.chatId == chatId);
|
||||
var cartObject = JsonConvert.DeserializeObject<CartObject>(user.cart);
|
||||
int curDay = user.day;
|
||||
|
||||
//int curDay = (int)DateTime.Now.DayOfWeek;
|
||||
//if (curDay == 0) curDay = 7;
|
||||
|
||||
if (curDay == 8) curDay = (int)DateTime.Now.DayOfWeek;
|
||||
|
||||
/*
|
||||
* foreach (var o in cartObject.orders)
|
||||
sum += o.price * o.number;
|
||||
*/
|
||||
|
||||
|
||||
categories = db.CategoriesDostavkas.ToList().ConvertAll<CategoriesDostavka>(c =>
|
||||
new CategoriesDostavka()
|
||||
{
|
||||
categoryId = c.categoryId,
|
||||
name = c.name,
|
||||
btnName = c.btnName,
|
||||
kbColor = c.kbColor,
|
||||
});
|
||||
|
||||
|
||||
products = db.ProductsDostavkas.Where(p => p.categoryId == curCategory &&
|
||||
JsonConvert.DeserializeObject<List<int>>(p.weekDays).Exists(day => day == curDay)).
|
||||
ToList().ConvertAll<ProductsDostavka>(e => new ProductsDostavka()
|
||||
{
|
||||
id = e.id,
|
||||
article = e.article,
|
||||
colorId = e.colorId,
|
||||
categoryId = e.categoryId,
|
||||
name = e.name,
|
||||
fullName = e.fullName,
|
||||
description = e.description,
|
||||
descriptionCarousel = e.descriptionCarousel,
|
||||
price = Convert.ToDouble(e.price),
|
||||
photo = e.photo,
|
||||
photoCarousel = e.photoCarousel,
|
||||
additions = JsonConvert.DeserializeObject<List<string>>(e.additions),
|
||||
recommends = JsonConvert.DeserializeObject<List<string>>(e.recommends),
|
||||
});
|
||||
|
||||
|
||||
switch (plAction)
|
||||
{
|
||||
case 1:
|
||||
var jsPhoto = JsonConvert.DeserializeObject<CatalogPhotoObject>(products[curProduct].photo);
|
||||
List<MediaAttachment> listPhoto = new List<MediaAttachment>()
|
||||
{
|
||||
new VkNet.Model.Attachments.Photo() { Id = jsPhoto.id, OwnerId = jsPhoto.owner_id, AccessKey = jsPhoto.access_key}
|
||||
};
|
||||
@params.Attachments = listPhoto;
|
||||
@params.Message = products[curProduct].fullName + "\n" +
|
||||
products[curProduct].description + "\n"; // + " " + products[curProduct].weight
|
||||
break;
|
||||
case 2:
|
||||
if (curAddition == 0)
|
||||
@params.Message = "✅ " + products[curProduct].fullName + " добавлен в корзину.";
|
||||
else
|
||||
{
|
||||
// Нет таблицы с модификаторами
|
||||
|
||||
//SqlCommand getAddition = new SqlCommand("SELECT * FROM Modifiers WHERE article = @article;", Con);
|
||||
//getAddition.Parameters.AddWithValue("@article", curAddition);
|
||||
//SqlDataReader rgetAddition = getAddition.ExecuteReader();
|
||||
//if (rgetAddition.HasRows)
|
||||
//{
|
||||
// rgetAddition.Read();
|
||||
// addition = new Additions()
|
||||
// {
|
||||
// name = rgetAddition["name"].ToString(),
|
||||
// price = Convert.ToInt32(rgetAddition["price"]),
|
||||
// fullName = rgetAddition["fullName"].ToString(),
|
||||
// article = rgetAddition["article"].ToString(),
|
||||
// };
|
||||
//}
|
||||
//rgetAddition.Close();
|
||||
|
||||
@params.Message = "✅ " + products[curProduct].fullName + " с " + addition.fullName + " добавлен в корзину.";
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (curProduct >= products.Count)
|
||||
{
|
||||
curProduct = products.Count - 1;
|
||||
prevCatalog = curProduct - 1;
|
||||
nextCatalog = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
prevCatalog = curProduct - 1;
|
||||
nextCatalog = curProduct + 1;
|
||||
}
|
||||
|
||||
if ((curProduct + 1) == products.Count)
|
||||
{
|
||||
prevCatalog = curProduct - 1;
|
||||
nextCatalog = 0;
|
||||
}
|
||||
if (curProduct == 0)
|
||||
{
|
||||
prevCatalog = products.Count - 1;
|
||||
nextCatalog = curProduct + 1;
|
||||
}
|
||||
if (products.Count == 1)
|
||||
{
|
||||
prevCatalog = 0;
|
||||
nextCatalog = 0;
|
||||
}
|
||||
|
||||
#region keyboard
|
||||
//keyboardBuilder
|
||||
// .AddButton("Подробнее", "selectAddToCart-" + curCategory + "-" + curProduct, KeyboardButtonColor.Primary)
|
||||
// .AddLine();
|
||||
|
||||
keyboardBuilder
|
||||
.AddButton("<<", "selectProduct-" + curCategory + "-" + prevCatalog + "-1", KeyboardButtonColor.Primary)
|
||||
.AddButton((curProduct + 1) + "/" + products.Count, "selectProduct-" + curCategory + "-" + curProduct + "-1", KeyboardButtonColor.Default)
|
||||
.AddButton(">>", "selectProduct-" + curCategory + "-" + nextCatalog + "-1", KeyboardButtonColor.Primary)
|
||||
.AddLine();
|
||||
|
||||
|
||||
keyboardBuilder
|
||||
.AddButton("Вернуться в меню", "selectDayMenu-" + curDay, KeyboardButtonColor.Default)
|
||||
//.AddButton("Корзина: " + sum + " р", "selectCart-0", KeyboardButtonColor.Negative)
|
||||
.AddLine()
|
||||
.AddButton("День недели", "selectMenu-0", KeyboardButtonColor.Default);
|
||||
//.AddLine();
|
||||
|
||||
|
||||
#endregion keyboard
|
||||
/*
|
||||
if (products.Count > 10) //14
|
||||
{
|
||||
if (curProduct < (products.Count - 10)) index = curProduct + 1;
|
||||
else index = products.Count - 10;
|
||||
}
|
||||
else index = 0;
|
||||
|
||||
int rowCount = 3;
|
||||
|
||||
for (i = index; i < products.Count; i += 2)
|
||||
{
|
||||
if (i + 1 < products.Count)
|
||||
keyboardBuilder
|
||||
.AddButton(products[i].name, "selectProduct-" + curCategory + "-" + i.ToString() + "-1", KeyboardButtonColor.Default)
|
||||
.AddButton(products[i + 1].name, "selectProduct-" + curCategory + "-" + (i + 1).ToString() + "-1", KeyboardButtonColor.Default)
|
||||
.AddLine();
|
||||
else
|
||||
keyboardBuilder
|
||||
.AddButton(products[i].name, "selectProduct-" + curCategory + "-" + i.ToString() + "-1", KeyboardButtonColor.Default)
|
||||
.AddLine();
|
||||
rowCount += 1;
|
||||
|
||||
if (rowCount == 8) break;
|
||||
}
|
||||
*/
|
||||
|
||||
@params.UserId = chatId;
|
||||
@params.Keyboard = keyboardBuilder.Build();
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в ShowProductMain: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
|
||||
private int GetRandomId()
|
||||
{
|
||||
|
||||
var intBytes = new byte[4];
|
||||
|
||||
Rng.GetBytes(intBytes);
|
||||
|
||||
return BitConverter.ToInt32(intBytes, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
60
SvetoforVKBot/Models/Commands/LK/Menu/SelectColorCommand.cs
Normal file
60
SvetoforVKBot/Models/Commands/LK/Menu/SelectColorCommand.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using VkNet;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Model.RequestParams;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu
|
||||
{
|
||||
public class SelectColorCommand : Command
|
||||
{
|
||||
public override string Name => "{\"button\":\"selectColor-";
|
||||
|
||||
public override void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
}
|
||||
|
||||
public override void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
var chatId = update.@object.message.from_id;
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
Regex regex = new Regex("[^0-9]");
|
||||
string msg = "";
|
||||
var keyboardBuilder = new KeyboardBuilder().Clear();
|
||||
List<Data.Svetofor> categories = new List<Data.Svetofor>();
|
||||
var row = new List<MessageKeyboardButton>();
|
||||
var listButtons = new List<ReadOnlyCollection<MessageKeyboardButton>>();
|
||||
var kbColor = KeyboardButtonColor.Positive;
|
||||
|
||||
try
|
||||
{
|
||||
string[] payload = update.@object.message.payload.Split('-');
|
||||
int colorId = Convert.ToInt32(regex.Replace(payload[1], ""));
|
||||
|
||||
var user = db.Users.Single(usr => usr.chatId == chatId);
|
||||
user.colorId = colorId;
|
||||
db.SaveChanges();
|
||||
|
||||
msg = "📋Основное меню\n";
|
||||
|
||||
ShowMenuDostavka showMenuDostavka = new ShowMenuDostavka();
|
||||
showMenuDostavka.Execute(update, client, db, msg, 2, 0);
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в SelectColorCommand: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using VkNet;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Model.RequestParams;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu
|
||||
{
|
||||
public class SelectDayMenuCommand : Command
|
||||
{
|
||||
public override string Name => "{\"button\":\"selectDayMenu-";
|
||||
|
||||
public override void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
}
|
||||
|
||||
public override void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
var chatId = update.@object.message.from_id;
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
Regex regex = new Regex("[^0-9]");
|
||||
string msg = "";
|
||||
var keyboardBuilder = new KeyboardBuilder().Clear();
|
||||
List<Data.Svetofor> categories = new List<Data.Svetofor>();
|
||||
var row = new List<MessageKeyboardButton>();
|
||||
var listButtons = new List<ReadOnlyCollection<MessageKeyboardButton>>();
|
||||
var kbColor = KeyboardButtonColor.Positive;
|
||||
|
||||
try
|
||||
{
|
||||
string[] payload = update.@object.message.payload.Split('-');
|
||||
int curDay = Convert.ToInt32(regex.Replace(payload[1], ""));
|
||||
|
||||
var user = db.Users.Single(usr => usr.chatId == chatId);
|
||||
user.tag = "Меню";
|
||||
user.day = curDay;
|
||||
db.SaveChanges();
|
||||
|
||||
keyboardBuilder.AddButton("Личный кабинет", "startPL", KeyboardButtonColor.Default);
|
||||
|
||||
msg = "📋Основное меню\n";
|
||||
|
||||
ShowMenuDostavka showMenuDostavka = new ShowMenuDostavka();
|
||||
showMenuDostavka.Execute(update, client, db, msg, 2, 0);
|
||||
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в SelectDayMenuCommand: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
65
SvetoforVKBot/Models/Commands/LK/Menu/SelectMenuCommand.cs
Normal file
65
SvetoforVKBot/Models/Commands/LK/Menu/SelectMenuCommand.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using VkNet;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Model.RequestParams;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu
|
||||
{
|
||||
public class SelectMenuCommand : Command
|
||||
{
|
||||
public override string Name => "{\"button\":\"selectMenu-";
|
||||
|
||||
public override void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
}
|
||||
|
||||
public override void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
var chatId = update.@object.message.from_id;
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
Regex regex = new Regex("[^0-9]");
|
||||
string msg = "";
|
||||
var keyboardBuilder = new KeyboardBuilder().Clear();
|
||||
try
|
||||
{
|
||||
string[] payload = update.@object.message.payload.Split('-');
|
||||
int curSubMenu = Convert.ToInt32(regex.Replace(payload[1], ""));
|
||||
|
||||
@params.Message = "📅На какой день показать меню? Выберите на клавиатуре\n";
|
||||
|
||||
keyboardBuilder
|
||||
.AddButton("Сегодня", "selectDayMenu-8", KeyboardButtonColor.Positive)
|
||||
.AddLine()
|
||||
.AddButton("Пн", "selectDayMenu-1", KeyboardButtonColor.Primary)
|
||||
.AddButton("Вт", "selectDayMenu-2", KeyboardButtonColor.Primary)
|
||||
.AddButton("Ср", "selectDayMenu-3", KeyboardButtonColor.Primary)
|
||||
.AddLine()
|
||||
.AddButton("Чт", "selectDayMenu-4", KeyboardButtonColor.Primary)
|
||||
.AddButton("Пт", "selectDayMenu-5", KeyboardButtonColor.Primary)
|
||||
.AddButton("Сб", "selectDayMenu-6", KeyboardButtonColor.Primary)
|
||||
.AddLine()
|
||||
.AddButton("Назад", "startPL", KeyboardButtonColor.Default);
|
||||
|
||||
//ShowMenuDostavka showMenuDostavka = new ShowMenuDostavka();
|
||||
//showMenuDostavka.Execute(update, client, Con, msg, 2, curSubMenu);
|
||||
@params.Keyboard = keyboardBuilder.Build();
|
||||
@params.UserId = chatId;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в SelectMenuCommand: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using VkNet;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Model.RequestParams;
|
||||
using System.Text.RegularExpressions;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu
|
||||
{
|
||||
public class SelectPersonalMenuCommand : Command
|
||||
{
|
||||
public override string Name => "{\"button\":\"selectPersonalMenu-";
|
||||
|
||||
public override void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
}
|
||||
|
||||
public override void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||||
{
|
||||
var chatId = update.@object.message.from_id;
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
Regex regex = new Regex("[^0-9]");
|
||||
string msg = "";
|
||||
var keyboardBuilder = new KeyboardBuilder().Clear();
|
||||
double sum = 0;
|
||||
string backPL = "startPL";
|
||||
int kkals = 0;
|
||||
|
||||
try
|
||||
{
|
||||
switch (DateTime.Now.DayOfWeek)
|
||||
{
|
||||
case DayOfWeek.Saturday:
|
||||
case DayOfWeek.Sunday:
|
||||
keyboardBuilder
|
||||
.AddButton("Назад", "startPL", KeyboardButtonColor.Default);
|
||||
@params.Message = "Рекомендации по дневному меню доступны с понедельника по пятницу.\n" +
|
||||
"Чтобы вернуться в Личный кабинет, нажмите кнопку \"Назад\"";
|
||||
@params.Keyboard = keyboardBuilder.Build();
|
||||
@params.UserId = chatId;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
return;
|
||||
}
|
||||
|
||||
string[] payload = update.@object.message.payload.Split('-');
|
||||
int foodType = Convert.ToInt32(regex.Replace(payload[1], ""));
|
||||
int curDayKkal = Convert.ToInt32(regex.Replace(payload[2], ""));
|
||||
|
||||
var user = db.Users.Single(usr => usr.chatId == chatId);
|
||||
var cartObject = JsonConvert.DeserializeObject<CartObject>(user.cart);
|
||||
|
||||
foreach (var o in cartObject.orders)
|
||||
{
|
||||
sum += o.price * o.number;
|
||||
kkals += o.kkal * o.number;
|
||||
}
|
||||
|
||||
//keyboardBuilder
|
||||
// .AddButton($"Завтрак (до {Math.Round(curDayKkal * 1.2 * 0.3)} ккал)", "selectPersonalMenu-1-" + curDayKkal, KeyboardButtonColor.Primary)
|
||||
// .AddLine()
|
||||
// .AddButton($"Обед (до {Math.Round(curDayKkal * 1.2 * 0.4)} ккал)", "selectPersonalMenu-2-" + curDayKkal, KeyboardButtonColor.Primary)
|
||||
// .AddLine()
|
||||
// .AddButton($"Перекус (до {Math.Round(curDayKkal * 1.2 * 0.1)} ккал)", "selectPersonalMenu-3-" + curDayKkal, KeyboardButtonColor.Primary)
|
||||
// .AddLine()
|
||||
// .AddButton($"Ужин (до {Math.Round(curDayKkal * 1.2 * 0.3)} ккал)", "selectPersonalMenu-4-" + curDayKkal, KeyboardButtonColor.Primary)
|
||||
// .AddLine();
|
||||
|
||||
//if (cartObject.orders.Count > 0)
|
||||
//{
|
||||
// keyboardBuilder
|
||||
// .AddButton("Личный кабинет", "startPL", KeyboardButtonColor.Default)
|
||||
// .AddButton("Корзина: " + sum + " р", "selectCart-0", KeyboardButtonColor.Negative);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// keyboardBuilder
|
||||
// .AddButton("Личный кабинет", "startPL", KeyboardButtonColor.Default);
|
||||
//}
|
||||
|
||||
////@params.Template = templateBuilder.Build();
|
||||
//@params.Keyboard = keyboardBuilder.Build();
|
||||
//@params.Message = "Если хотите подобрать блюда ещё раз, нажмите кнопку повторно😉";
|
||||
//@params.UserId = chatId;
|
||||
//@params.RandomId = GetRandomId();
|
||||
//client.Messages.SendAsync(@params);
|
||||
|
||||
ShowPersonalMenuCarusel showPersonalMenu = new ShowPersonalMenuCarusel();
|
||||
showPersonalMenu.Execute(update, client, db, foodType, 0, 1, curDayKkal);
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в SelectMenuCommand: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
139
SvetoforVKBot/Models/Commands/LK/Menu/ShowMenuDostavka.cs
Normal file
139
SvetoforVKBot/Models/Commands/LK/Menu/ShowMenuDostavka.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using Newtonsoft.Json;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using VkNet;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using VkNet.Model.Attachments;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Model.RequestParams;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu
|
||||
{
|
||||
public class ShowMenuDostavka
|
||||
{
|
||||
public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db, string msg, int plAction, int subMenu)
|
||||
{
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
var chatId = update.@object.message.from_id;
|
||||
var ids = new long[] { chatId };
|
||||
var user = client.Users.Get(ids);
|
||||
string voronka = "";
|
||||
double sum = 0;
|
||||
string backPL = "";
|
||||
List<CategoriesDostavka> categories = new List<CategoriesDostavka>();
|
||||
var row = new List<MessageKeyboardButton>();
|
||||
var listButtons = new List<ReadOnlyCollection<MessageKeyboardButton>>();
|
||||
var kbColor = KeyboardButtonColor.Positive;
|
||||
var keyboardBuilder = new KeyboardBuilder().Clear();
|
||||
|
||||
try
|
||||
{
|
||||
categories = db.CategoriesDostavkas.Where(c=>c.subMenu == subMenu).ToList().ConvertAll<CategoriesDostavka>(c =>
|
||||
new CategoriesDostavka()
|
||||
{
|
||||
categoryId = c.categoryId,
|
||||
inTheOrder = c.inTheOrder,
|
||||
name = c.name,
|
||||
btnName = c.btnName,
|
||||
kbColor = c.kbColor,
|
||||
});
|
||||
|
||||
//SqlCommand getCategories = new SqlCommand("SELECT * FROM CategoriesDostavka WHERE subMenu = @subMenu;", Con);
|
||||
//getCategories.Parameters.AddWithValue("@subMenu", subMenu);
|
||||
//SqlDataReader rgetCategories = getCategories.ExecuteReader();
|
||||
//while (rgetCategories.Read())
|
||||
// categories.Add(new CategoriesDostavka()
|
||||
// {
|
||||
// categoryId = Convert.ToInt32(rgetCategories["categoryId"]),
|
||||
// inTheOrder = Convert.ToInt32(rgetCategories["inTheOrder"]),
|
||||
// name = rgetCategories["name"].ToString(),
|
||||
// btnName = rgetCategories["btnName"].ToString(),
|
||||
// kbColor = rgetCategories["kbColor"].ToString(),
|
||||
// });
|
||||
//rgetCategories.Close();
|
||||
|
||||
categories.RemoveAll(c => c.inTheOrder.Equals(0));
|
||||
categories.Sort((x, y) => x.inTheOrder.CompareTo(y.inTheOrder));
|
||||
|
||||
int btnCount = 0;
|
||||
foreach (var c in categories)
|
||||
{
|
||||
switch (c.kbColor)
|
||||
{
|
||||
case "Default":
|
||||
kbColor = KeyboardButtonColor.Default;
|
||||
break;
|
||||
case "Positive":
|
||||
kbColor = KeyboardButtonColor.Positive;
|
||||
break;
|
||||
case "Negative":
|
||||
kbColor = KeyboardButtonColor.Negative;
|
||||
break;
|
||||
case "Primary":
|
||||
kbColor = KeyboardButtonColor.Primary;
|
||||
break;
|
||||
}
|
||||
|
||||
keyboardBuilder
|
||||
.AddButton(c.btnName, "selectProduct-" + c.categoryId + "-0-1", kbColor);
|
||||
|
||||
btnCount += 1;
|
||||
|
||||
if (btnCount % 2 == 0)
|
||||
{
|
||||
keyboardBuilder.AddLine();
|
||||
}
|
||||
}
|
||||
if (btnCount % 2 != 0)
|
||||
{
|
||||
keyboardBuilder.AddLine();
|
||||
}
|
||||
|
||||
if (subMenu == 0)
|
||||
{
|
||||
backPL = "startPL";
|
||||
}
|
||||
else
|
||||
backPL = "selectMenu-0";
|
||||
|
||||
|
||||
keyboardBuilder
|
||||
.AddButton("Назад", "selectMenu-0", KeyboardButtonColor.Default)
|
||||
.AddButton("Личный кабинет", "startPL", KeyboardButtonColor.Default);
|
||||
|
||||
|
||||
@params.UserId = chatId;
|
||||
@params.Keyboard = keyboardBuilder.Build();
|
||||
@params.Message = msg;
|
||||
@params.DontParseLinks = true;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в ShowMenuDostavka: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
|
||||
private int GetRandomId()
|
||||
{
|
||||
|
||||
var intBytes = new byte[4];
|
||||
|
||||
Rng.GetBytes(intBytes);
|
||||
|
||||
return BitConverter.ToInt32(intBytes, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
430
SvetoforVKBot/Models/Commands/LK/Menu/ShowPersonalMenuCarusel.cs
Normal file
430
SvetoforVKBot/Models/Commands/LK/Menu/ShowPersonalMenuCarusel.cs
Normal file
@@ -0,0 +1,430 @@
|
||||
using Newtonsoft.Json;
|
||||
using SvetoforVKBot.Models.Updates;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.SqlClient;
|
||||
using System.Security.Cryptography;
|
||||
using VkNet;
|
||||
using VkNet.Enums.SafetyEnums;
|
||||
using VkNet.Model.Keyboard;
|
||||
using VkNet.Model.RequestParams;
|
||||
using VkNet.Utils;
|
||||
using VkNet.Model.Template;
|
||||
using VkNet.Model.Template.Carousel;
|
||||
using System.Linq;
|
||||
|
||||
namespace SvetoforVKBot.Models.Commands.LK.Menu
|
||||
{
|
||||
public class ShowPersonalMenuCarusel
|
||||
{
|
||||
private const string PHOTOIDNEXT = "-194717824_457239273_446146426236760985";
|
||||
//{"id": 457239273, "owner_id": -194717824, "access_key": "446146426236760985"}
|
||||
public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db, int curCategory, int curProduct, int curWay, double curDayKkal)
|
||||
//curCategory - id Categories, curProduct - порядковый номер в массиве продуктов, curWay - для showProductCarusel направление навигации
|
||||
{
|
||||
MessagesSendParams @params = new MessagesSendParams();
|
||||
var chatId = update.@object.message.from_id;
|
||||
int i = 0;
|
||||
int prevCatalog = 0, nextCatalog = 0;
|
||||
int naviState = 0;
|
||||
double sum = 0;
|
||||
string desc = "";
|
||||
int curDay = (int)DateTime.Now.DayOfWeek;
|
||||
if (curDay == 0) curDay = 7;
|
||||
|
||||
List<CategoriesDostavka> categories = new List<CategoriesDostavka>();
|
||||
List<ProductsDostavka> products = new List<ProductsDostavka>();
|
||||
List<ProductsDostavka> allProducts = new List<ProductsDostavka>();
|
||||
List<ProductsDostavka> randomFood = new List<ProductsDostavka>();
|
||||
//List<ProductsDostavka> personalProducts = new List<ProductsDostavka>();
|
||||
var row = new List<MessageKeyboardButton>();
|
||||
var listButtons = new List<ReadOnlyCollection<MessageKeyboardButton>>();
|
||||
TemplateBuilder templateBuilder = new TemplateBuilder();
|
||||
CarouselElementAction carouselElementAction = new CarouselElementAction();
|
||||
carouselElementAction.Type = CarouselElementActionType.OpenPhoto;
|
||||
double belkiLow = 0, zhiryLow = 0, uglevodyLow = 0, belkiHigh = 0, zhiryHigh = 0, uglevodyHigh = 0;
|
||||
double curBelki = 0, curZhiry = 0, curUglevody = 0;
|
||||
double foodPercent = 0;
|
||||
double belkiPercent = 0, zhiryPercent = 0, uglevodyPercent = 0;
|
||||
double dayKkal = 0;
|
||||
string foodTime = "";
|
||||
try
|
||||
{
|
||||
|
||||
var user = db.Users.Single(usr => usr.chatId == chatId);
|
||||
var cartObject = JsonConvert.DeserializeObject<CartObject>(user.cart);
|
||||
var choosenProducts = JsonConvert.DeserializeObject<List<int>>(user.choosenProducts);
|
||||
List<int> sportDays = JsonConvert.DeserializeObject<List<int>>(user.sportDays);
|
||||
|
||||
//if (curDay == 8) curDay = (int)DateTime.Now.DayOfWeek;
|
||||
int nextDay = curDay + 1;
|
||||
|
||||
if (nextDay == 8)
|
||||
nextDay = 1;
|
||||
|
||||
foreach (var o in cartObject.orders)
|
||||
{
|
||||
sum += o.price * o.number;
|
||||
}
|
||||
|
||||
|
||||
categories = db.CategoriesDostavkas.ToList().ConvertAll<CategoriesDostavka>(c =>
|
||||
new CategoriesDostavka()
|
||||
{
|
||||
categoryId = c.categoryId,
|
||||
name = c.name,
|
||||
btnName = c.btnName,
|
||||
kbColor = c.kbColor,
|
||||
});
|
||||
|
||||
allProducts = db.ProductsDostavkas.Where(p => p.categoryId != 0 &&
|
||||
JsonConvert.DeserializeObject<List<int>>(p.weekDays).Exists(day => day == curDay)).
|
||||
ToList().ConvertAll<ProductsDostavka>(e=>new ProductsDostavka()
|
||||
{
|
||||
id = e.id,
|
||||
article = e.article,
|
||||
colorId = e.colorId,
|
||||
categoryId = e.categoryId,
|
||||
name = e.name,
|
||||
fullName = e.fullName,
|
||||
descriptionCarousel = e.descriptionCarousel,
|
||||
price = Convert.ToDouble(e.price),
|
||||
photo = e.photo,
|
||||
photoCarousel = e.photoCarousel,
|
||||
kkal = e.kkal,
|
||||
foodType = JsonConvert.DeserializeObject<List<int>>(e.foodType),
|
||||
belki = Convert.ToDouble(e.belki),
|
||||
zhiry = Convert.ToDouble(e.zhiry),
|
||||
uglevody = Convert.ToDouble(e.uglevody),
|
||||
});
|
||||
|
||||
ProductsDostavka water = new ProductsDostavka();
|
||||
water = allProducts.Find(p => p.article.Equals("55"));
|
||||
allProducts.Remove(water);
|
||||
//allProducts.RemoveAll(p => !p.foodType.Contains(curCategory));
|
||||
if (choosenProducts.Count > 0)
|
||||
allProducts.RemoveAll(p => choosenProducts.Contains(p.id));
|
||||
|
||||
switch (user.track)
|
||||
{
|
||||
case 1:
|
||||
belkiLow = 1.4;
|
||||
belkiHigh = 2;
|
||||
|
||||
zhiryLow = 1;
|
||||
zhiryHigh = 1.1;
|
||||
|
||||
uglevodyLow = 2.4;
|
||||
uglevodyHigh = 3;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
belkiLow = 1.2;
|
||||
belkiHigh = 1.7;
|
||||
|
||||
zhiryLow = 0.9;
|
||||
zhiryHigh = 1;
|
||||
|
||||
uglevodyLow = 2;
|
||||
uglevodyHigh = 2.6;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
belkiLow = 1.6;
|
||||
belkiHigh = 2.2;
|
||||
|
||||
zhiryLow = 1.1;
|
||||
zhiryHigh = 1.2;
|
||||
|
||||
uglevodyLow = 2.8;
|
||||
uglevodyHigh = 3.4;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (curCategory)
|
||||
{
|
||||
case 1:
|
||||
allProducts.RemoveAll(p => p.categoryId == 1);
|
||||
|
||||
foodPercent = 0.3;
|
||||
belkiPercent = 0.3;
|
||||
uglevodyPercent = 0.4;
|
||||
zhiryPercent = 0.2;
|
||||
foodTime = "🥐Завтрак";
|
||||
randomFood = allProducts.FindAll(ap => (ap.categoryId != 1) && ap.zhiry <= zhiryLow * user.weight * zhiryPercent && ap.uglevody <= uglevodyLow * user.weight * uglevodyPercent);
|
||||
|
||||
//Углеводы - 0.4
|
||||
//Белки - 0.3
|
||||
//Жиры - 0.2
|
||||
//Кнопка ещё один вариант
|
||||
//web
|
||||
break;
|
||||
|
||||
case 2:
|
||||
foodPercent = 0.4;
|
||||
belkiPercent = 0.3;
|
||||
uglevodyPercent = 0.3;
|
||||
zhiryPercent = 0.4;
|
||||
foodTime = "🍲Обед";
|
||||
randomFood = allProducts.FindAll(ap => ap.categoryId == 1 && ap.zhiry <= zhiryLow * user.weight * zhiryPercent && ap.uglevody <= uglevodyLow * user.weight * uglevodyPercent);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
allProducts.RemoveAll(p => p.categoryId == 1);
|
||||
|
||||
foodPercent = 0.1;
|
||||
belkiPercent = 0.2;
|
||||
uglevodyPercent = 0.2;
|
||||
zhiryPercent = 0.2;
|
||||
foodTime = "🥪Перекус";
|
||||
//ap.categoryId == 5 || ap.categoryId == 4
|
||||
randomFood = allProducts.FindAll(ap => (ap.categoryId != 1) && ap.zhiry <= zhiryLow * user.weight * zhiryPercent && ap.uglevody <= uglevodyLow * user.weight * uglevodyPercent);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
allProducts.RemoveAll(p => p.categoryId == 1);
|
||||
|
||||
foodPercent = 0.3;
|
||||
belkiPercent = 0.2;
|
||||
uglevodyPercent = 0.2;
|
||||
zhiryPercent = 0.2;
|
||||
randomFood = allProducts.FindAll(ap => (ap.categoryId != 1) && ap.zhiry <= zhiryLow * user.weight * zhiryPercent && ap.uglevody <= uglevodyLow * user.weight * uglevodyPercent);
|
||||
foodTime = "🥗Ужин";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
double maxKkal = Math.Round(curDayKkal * 1.2 * foodPercent);
|
||||
double minKkal = Math.Round(curDayKkal * 0.9 * foodPercent);
|
||||
|
||||
Random rand = new Random();
|
||||
if (choosenProducts.Count > 0)
|
||||
randomFood.RemoveAll(p => choosenProducts.Contains(p.id));
|
||||
|
||||
int randomBludo = rand.Next(0, randomFood.Count);
|
||||
|
||||
if (curCategory == 3)
|
||||
{
|
||||
products.Add(randomFood.Find(rf => rf.kkal <= maxKkal));
|
||||
}
|
||||
else
|
||||
{
|
||||
products.Add(randomFood[randomBludo]);
|
||||
}
|
||||
|
||||
curBelki = products[0].belki;
|
||||
curZhiry = products[0].zhiry;
|
||||
curUglevody = products[0].uglevody;
|
||||
dayKkal = products[0].kkal;
|
||||
choosenProducts.Add(products[0].id);
|
||||
ProductsDostavka tmp = new ProductsDostavka();
|
||||
for (i = 0; i < allProducts.Count; i++)
|
||||
{
|
||||
tmp = allProducts[i];
|
||||
allProducts.RemoveAt(i);
|
||||
allProducts.Insert(rand.Next(allProducts.Count), tmp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
foreach (var ap in allProducts)
|
||||
{
|
||||
//if (dayKkal + ap.kkal <= maxKkal) //curBelki <= belkiLow * weight * foodPercent && curUglevody + ap.uglevody < uglevodyHigh * weight * foodPercent)
|
||||
if (curBelki + ap.belki <= belkiHigh * user.weight * belkiPercent &&
|
||||
curUglevody + ap.uglevody <= uglevodyHigh * user.weight * uglevodyPercent)// &&
|
||||
//curZhiry + ap.zhiry <= zhiryHigh * weight * zhiryPercent)
|
||||
{
|
||||
if (!products.Exists(p => p.categoryId == ap.categoryId))
|
||||
{
|
||||
if (true)//curUglevody + ap.uglevody < uglevodyHigh * weight * foodPercent curBelki < belkiHigh * weight * foodPercent && curZhiry < zhiryHigh * weight * foodPercent && curUglevody < uglevodyHigh * weight * foodPercent)
|
||||
{
|
||||
|
||||
curBelki += ap.belki;
|
||||
curZhiry += ap.zhiry;
|
||||
curUglevody += ap.uglevody;
|
||||
dayKkal += ap.kkal;
|
||||
products.Add(ap);
|
||||
choosenProducts.Add(ap.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
//else break;
|
||||
}
|
||||
|
||||
//if (dayKkal < minKkal)
|
||||
//{
|
||||
// if (curCategory != 3)
|
||||
// {
|
||||
// products.Add(allProducts.Find(ap => !choosenProducts.Contains(ap.id) && ap.zhiry <= zhiryLow * weight * zhiryPercent && ap.uglevody <= uglevodyLow * weight * uglevodyPercent && ap.categoryId != 7));
|
||||
// curBelki += products[products.Count - 1].belki;
|
||||
// curZhiry += products[products.Count - 1].zhiry;
|
||||
// curUglevody += products[products.Count - 1].uglevody;
|
||||
// dayKkal += products[products.Count - 1].kkal;
|
||||
// choosenProducts.Add(products[products.Count - 1].id);
|
||||
// }
|
||||
// //else
|
||||
// // products.Add(allProducts.Find(ap => !choosenProducts.Contains(ap.id) && ap.kkal <= maxKkal - minKkal));
|
||||
// //choosenProducts.Add(products[products.Count - 1].id);
|
||||
//}
|
||||
|
||||
if (!products.Exists(p => p.categoryId == 7))
|
||||
products.Add(water);
|
||||
//(p => p.article.Equals("55")))
|
||||
|
||||
//if (curCategory != 4)
|
||||
//{
|
||||
// SqlCommand updCart = new SqlCommand("UPDATE Users SET choosenProducts = @choosenProducts WHERE chatId = @chatId;", Con);
|
||||
// updCart.Parameters.AddWithValue("@chatId", chatId);
|
||||
// updCart.Parameters.AddWithValue("@choosenProducts", JsonConvert.SerializeObject(choosenProducts));
|
||||
// updCart.ExecuteNonQuery();
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// SqlCommand updCart = new SqlCommand("UPDATE Users SET choosenProducts = @choosenProducts WHERE chatId = @chatId;", Con);
|
||||
// updCart.Parameters.AddWithValue("@chatId", chatId);
|
||||
// updCart.Parameters.AddWithValue("@choosenProducts", "[]");
|
||||
// updCart.ExecuteNonQuery();
|
||||
//}
|
||||
|
||||
nextCatalog = curProduct + 9;
|
||||
prevCatalog = curProduct - 9;
|
||||
|
||||
if (curProduct > 0 && products.Count > 6)
|
||||
{
|
||||
if (nextCatalog > products.Count) nextCatalog = products.Count - (products.Count - curProduct);
|
||||
|
||||
if (prevCatalog < 0) prevCatalog = 0;
|
||||
}
|
||||
|
||||
for (i = curProduct; i < products.Count; i++)
|
||||
{
|
||||
var jsPhoto = JsonConvert.DeserializeObject<CatalogPhotoObject>(products[i].photoCarousel);
|
||||
|
||||
desc = products[i].descriptionCarousel; //+ " " + products[i].weight;
|
||||
if (desc.Length > 80) desc = desc.Substring(0, 77) + "..."; //desc.Substring(0, 77)
|
||||
|
||||
templateBuilder.AddTemplateElement(new CarouselElement()
|
||||
{
|
||||
Title = products[i].fullName,
|
||||
Description = desc + " | " + products[i].kkal + " ккал\n" +
|
||||
$"Б\\Ж\\У: {products[i].belki}г\\{products[i].zhiry}г\\{products[i].uglevody}г",
|
||||
PhotoId = jsPhoto.owner_id + "_" + jsPhoto.id + "_" + jsPhoto.access_key,
|
||||
Buttons = new List<MessageKeyboardButton>()
|
||||
{
|
||||
new MessageKeyboardButton(){
|
||||
Action = new MessageKeyboardButtonAction(){ Label = "Добавить в корзину " + products[i].price + "р",
|
||||
Payload = "{\"button\":\"selectAddToCart-" + products[i].categoryId + "-" + products[i].id + "-" + curDayKkal + "\"}",
|
||||
Type = KeyboardButtonActionType.Text },
|
||||
Color = KeyboardButtonColor.Positive
|
||||
},
|
||||
//new MessageKeyboardButton(){
|
||||
// Action = new MessageKeyboardButtonAction(){ Label = "Подробнее",
|
||||
// Payload = "{\"button\":\"selectAddToCart-" + curCategory + "-" + i + "\"}",
|
||||
// Type = KeyboardButtonActionType.Text },
|
||||
// Color = KeyboardButtonColor.Default
|
||||
//},
|
||||
},
|
||||
Action = carouselElementAction,
|
||||
});
|
||||
|
||||
if (templateBuilder.Elements.Count == 9 && products.Count > 10)
|
||||
break;
|
||||
}
|
||||
|
||||
if (products.Count > 10)
|
||||
{
|
||||
switch (curWay)
|
||||
{
|
||||
case 1:
|
||||
if (curProduct < (products.Count - 9))
|
||||
naviState = 1;
|
||||
else
|
||||
naviState = 2;
|
||||
break;
|
||||
case 2:
|
||||
if (curProduct == 0)
|
||||
naviState = 1;
|
||||
else
|
||||
naviState = 2;
|
||||
break;
|
||||
default:
|
||||
naviState = 2;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (naviState)
|
||||
{
|
||||
case 1:
|
||||
templateBuilder.AddTemplateElement(new CarouselElement()
|
||||
{
|
||||
Title = ".",
|
||||
Description = ".",
|
||||
PhotoId = PHOTOIDNEXT,
|
||||
Buttons = new List<MessageKeyboardButton>()
|
||||
{
|
||||
new MessageKeyboardButton() {
|
||||
Action = new MessageKeyboardButtonAction(){ Label = ">> Следующие " + (products.Count - nextCatalog).ToString(),
|
||||
Payload = "{\"button\":\"selectProduct-" + products[nextCatalog].categoryId + "-" + nextCatalog + "-1\"}",
|
||||
Type = KeyboardButtonActionType.Text },
|
||||
Color = KeyboardButtonColor.Primary
|
||||
},
|
||||
},
|
||||
Action = carouselElementAction,
|
||||
});
|
||||
break;
|
||||
|
||||
case 2:
|
||||
templateBuilder.AddTemplateElement(new CarouselElement()
|
||||
{
|
||||
Title = ".",
|
||||
Description = ".",
|
||||
PhotoId = PHOTOIDNEXT,
|
||||
Buttons = new List<MessageKeyboardButton>()
|
||||
{
|
||||
new MessageKeyboardButton() {
|
||||
Action = new MessageKeyboardButtonAction(){ Label = "<< Предыдущие " + curProduct,
|
||||
Payload = "{\"button\":\"selectProduct-" + products[prevCatalog].categoryId + "-" + prevCatalog + "-2\"}",
|
||||
Type = KeyboardButtonActionType.Text },
|
||||
Color = KeyboardButtonColor.Primary
|
||||
},
|
||||
},
|
||||
Action = carouselElementAction,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@params.Message = foodTime + "\n" +
|
||||
"Калории: " + dayKkal + " ккал \n" +
|
||||
$"Б\\Ж\\У: {curBelki}г\\{curZhiry}г\\{curUglevody}г \n\n" +
|
||||
$"Если хотите подобрать блюда ещё раз, нажмите кнопку повторно😉";
|
||||
@params.Template = templateBuilder.Build();
|
||||
@params.UserId = chatId;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
@params.Message = "‼Ошибка в ShowProductCaruselMain: " + ee.Message;
|
||||
@params.Attachments = null;
|
||||
@params.Keyboard = null;
|
||||
@params.UserId = 59111081;
|
||||
@params.RandomId = GetRandomId();
|
||||
client.Messages.SendAsync(@params);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
|
||||
private int GetRandomId()
|
||||
{
|
||||
|
||||
var intBytes = new byte[4];
|
||||
|
||||
Rng.GetBytes(intBytes);
|
||||
|
||||
return BitConverter.ToInt32(intBytes, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user