Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Anexs.Lib.Extensoes;
  7. using ALConsultoria.Web.Helpers;
  8. using ALConsultoria.Web.Models;
  9. using AlconsultoriaModel;
  10.  
  11. namespace ALConsultoria.Web.Controllers
  12. {
  13. public class PlanoDeTreinoController : ALConsultoria.Web.Helpers.AnexsController
  14. {
  15. // GET: PlanoDeTreino
  16. public ActionResult Index(int? id)
  17. {
  18. ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
  19. var treino = new ALConsultoria.Web.Models.Treinos();
  20.  
  21. if (id > 0)
  22. {
  23. var obj = db.Treinos.Single(c => c.Id == id);
  24. treino = Newtonsoft.Json.JsonConvert.DeserializeObject<ALConsultoria.Web.Models.Treinos>(obj.Dados);
  25.  
  26. treino.Cliente = obj.Cliente;
  27. treino.Intensidade = obj.Intensidade;
  28. treino.DivisaoFicha = obj.DivisaoFicha;
  29. treino.Semana1 = obj.Semana1;
  30.  
  31. // return View();
  32. }
  33.  
  34. return View(treino);
  35. }
  36.  
  37. public ActionResult Cadastro(int? id, int idcliente = 0, int IdSemana = 0, bool copia = false)
  38. {
  39. ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
  40. var treino = new ALConsultoria.Web.Models.Treinos();
  41. treino.Exercicio = "Musculação";
  42.  
  43. if (idcliente > 0)
  44. {
  45. treino.Idcliente = idcliente;
  46. }
  47.  
  48. if (IdSemana > 0)
  49. {
  50. treino.IdSemana = IdSemana;
  51. }
  52.  
  53. ViewBag.Clientes = ComboBox.GerarBox(db.Clientes.Select(c => new SelectItem
  54. {
  55. Id = c.Id,
  56. Texto = c.Nome
  57. }).ToList(), true, false);
  58.  
  59. ViewBag.Classes = ComboBox.GerarBox(db.Classes.Select(c => new SelectItem
  60. {
  61. Id = c.Id,
  62. Texto = c.Exercicioclasse
  63. }).ToList(), true, false);
  64.  
  65. ViewBag.Grupos = ComboBox.GerarBox(db.Grupos.Select(c => new SelectItem
  66. {
  67. Id = c.Id,
  68. Texto = c.Nome
  69. }).ToList(), true, false);
  70.  
  71. ViewBag.Intensidade = ComboBox.GerarBox(db.Intensidades.Select(c => new SelectItem
  72. {
  73. Id = c.Id,
  74. Texto = c.Nome
  75. }).ToList(), true, false);
  76.  
  77. ViewBag.DivisaoFichas = ComboBox.GerarBox(db.DivisaoFichas.Select(c => new SelectItem
  78. {
  79. Id = c.Id,
  80. Texto = c.Nome
  81. }).ToList(), true, false);
  82.  
  83. ViewBag.Semanas = ComboBox.GerarBox(db.Semanas.Select(c => new SelectItem
  84. {
  85. Id = c.Id,
  86. Texto = c.Nome
  87. }).ToList(), true, false);
  88.  
  89. if (id > 0)
  90. {
  91. var obj = db.Treinos.Single(c => c.Id == id);
  92. treino = Newtonsoft.Json.JsonConvert.DeserializeObject<ALConsultoria.Web.Models.Treinos>(obj.Dados);
  93. }
  94. else if (idcliente > 0 && IdSemana > 0)
  95. {
  96. var obj = db.Treinos.FirstOrDefault(c => c.Idcliente == idcliente && c.IdSemana == IdSemana);
  97. if (obj != null)
  98. treino = Newtonsoft.Json.JsonConvert.DeserializeObject<ALConsultoria.Web.Models.Treinos>(obj.Dados);
  99. }
  100.  
  101. if (copia == true)
  102. {
  103. treino.Id = 0;
  104. if (idcliente > 0)
  105. {
  106. treino.Idcliente = idcliente;
  107. }
  108.  
  109. if (IdSemana > 0)
  110. {
  111. treino.IdSemana = IdSemana;
  112. }
  113.  
  114. }
  115.  
  116. return View(treino);
  117. }
  118.  
  119. public ActionResult Salvar(ALConsultoria.Web.Models.Treinos model)
  120. {
  121.  
  122. ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
  123. var transaction = db.Database.BeginTransaction();
  124.  
  125. try
  126. {
  127. var obj = model.Id > 0 ? db.Treinos.SingleOrDefault(c => c.Id == model.Id) : new Treino();
  128. obj.Idcliente = model.Idcliente;
  129. obj.Idintensidade = model.Idintensidade;
  130. obj.Objetivo = model.Objetivo;
  131. obj.Tempo = model.Tempo;
  132. obj.Frequencia = model.Frequencia;
  133. obj.Dataval = model.Dataval;
  134. obj.Dataini = model.Dataini;
  135. obj.Exercicio = model.Exercicio;
  136. obj.IdDivisaoFicha = model.IdDivisaoFicha;
  137. obj.IdSemana = model.IdSemana;
  138. obj.Orientacoe = model.Orientacoe;
  139. obj.ObservacaoCliente = model.ObservacaoCliente;
  140. obj.Cliente.Observacoes = model.Cliente.Observacoes;
  141.  
  142. obj.Dados = Newtonsoft.Json.JsonConvert.SerializeObject(model);
  143.  
  144. if (obj.Id == 0)
  145. {
  146. db.Treinos.Add(obj);
  147. }
  148.  
  149. db.SaveChanges();
  150. transaction.Commit();
  151. MensagemSucesso();
  152.  
  153. return RedirectToAction("Index");
  154.  
  155. }
  156. catch (Exception ex)
  157. {
  158. transaction.Rollback();
  159. MensagemErro(ex);
  160. return RedirectToAction("Cadastro", model);
  161. }
  162. finally
  163. {
  164. transaction.Dispose();
  165. db.Dispose();
  166. }
  167. return RedirectToAction("Cadastro", model);
  168. }
  169.  
  170. public ActionResult Excluir(int? id)
  171. {
  172. if (id > 0)
  173. {
  174. ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
  175. var transaction = db.Database.BeginTransaction();
  176. try
  177. {
  178. db.Treinos.Remove(db.Treinos.Single(c => c.Id == id));
  179.  
  180. db.SaveChanges();
  181.  
  182. transaction.Commit();
  183. MensagemSucesso();
  184. }
  185. catch (Exception ex)
  186. {
  187. transaction.Rollback();
  188. MensagemErro(ex);
  189. }
  190. finally
  191. {
  192. transaction.Dispose();
  193. db.Dispose();
  194. }
  195. }
  196.  
  197. return RedirectToAction("Index");
  198. }
  199.  
  200. [HttpGet]
  201. [ValidateInput(false)]
  202. public JsonResult GetDivisaoTreino(int iddivisao, List<int> idsmembros)
  203. {
  204. ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
  205.  
  206. var div = db.DivisaoFichas.Single(c => c.Id == iddivisao);
  207. var lista = new List<ALConsultoria.Web.Models.Treinos.Divisao>();
  208. var dados = new List<ALConsultoria.Web.Models.Treinos.DadoTreino>();
  209.  
  210. var membros = db.Membros.Select(c => new SelectItem
  211. {
  212. Id = c.Id,
  213. Texto = c.Nome
  214. }).ToList();
  215. membros.Insert(0, new SelectItem
  216. {
  217. Id = 0,
  218. Texto = "Selecione"
  219. });
  220.  
  221. char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
  222.  
  223. for (int i = 0; i < div.Divisoes; i++)
  224. {
  225. lista.Add(new Treinos.Divisao(i, membros, div.Divisoes)
  226. {
  227. IdMembro = idsmembros != null && idsmembros.Count > i ? idsmembros[i] : 0,
  228. Nome = alpha[i].ToString()
  229. });
  230.  
  231. dados.Add(new Treinos.DadoTreino(i)
  232. {
  233. IdMembro = idsmembros != null && idsmembros.Count > i ? idsmembros[i] : 0,
  234. Nome = alpha[i].ToString()
  235. });
  236. }
  237.  
  238. return Json(new { lista, dados }, JsonRequestBehavior.AllowGet);
  239. }
  240.  
  241. [HttpGet]
  242. [ValidateInput(false)]
  243. public JsonResult GetExercicio(int index, int numero, int indexdado, int idsemana, bool hiit = false)
  244. {
  245. ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
  246. if (hiit == true)
  247. {
  248. var model = new ALConsultoria.Web.Models.Treinos.HiitTreino(index, numero, indexdado);
  249. return Json(model, JsonRequestBehavior.AllowGet);
  250. }
  251. else
  252. {
  253. var semana = db.Semanas.FirstOrDefault(c => c.Id == idsemana);
  254.  
  255. var model = new ALConsultoria.Web.Models.Treinos.ExercicioTreino(index, numero, semana, indexdado);
  256. return Json(model, JsonRequestBehavior.AllowGet);
  257. }
  258. }
  259.  
  260. [HttpGet]
  261. [ValidateInput(false)]
  262. public JsonResult PesquisaExercicio(string pesquisa, int idmembro = 0, int idgrupo = 0, bool hiit = false, bool aquecimento = false, int start = 0, int length = 20)
  263. {
  264. ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
  265.  
  266. if (Request.QueryString["search[value]"] != null && !Request.QueryString["search[value]"].IsNullOrEmpty())
  267. {
  268. pesquisa = Request.QueryString["search[value]"];
  269. }
  270. var query = db.Exercicios.AsQueryable();
  271. if (idmembro > 0)
  272. {
  273. var ids = db.MembrosGrupos.Where(c => c.IdMembro == idmembro).Select(c => c.IdGrupo).ToList();
  274. query = query.Where(c => ids.Contains(c.Idgrupo.Value));
  275. }
  276.  
  277. if (hiit)
  278. {
  279. var ids = db.Grupos.Where(c => c.Hiit == true).Select(c => c.Id).ToList();
  280. query = query.Where(c => ids.Contains(c.Idgrupo.Value));
  281. }
  282.  
  283. if (aquecimento)
  284. {
  285. var ids = db.Grupos.Where(c => c.Aquecimento == true).Select(c => c.Id).ToList();
  286. query = query.Where(c => ids.Contains(c.Idgrupo.Value));
  287. }
  288.  
  289. if (idgrupo > 0)
  290. {
  291. query = query.Where(c => idgrupo == c.Idgrupo);
  292. }
  293.  
  294. if (!pesquisa.IsNullOrEmpty())
  295. query = query.Where(c => c.Nome != "" && c.Nome.Trim().ToUpper().Contains(pesquisa.Trim().ToUpper()));
  296.  
  297. return Json(query.OrderBy(c => c.Nome).Skip(start).Take(length).ToList().Select(c =>
  298. new
  299. {
  300. id = c.Id.ToString(),
  301. text = c.Nome.Trim(),
  302. }).ToList(), JsonRequestBehavior.AllowGet);
  303. }
  304.  
  305. [HttpGet]
  306. [ValidateInput(false)]
  307. public JsonResult PesquisaMetodo(string pesquisa, int start = 0, int length = 20)
  308. {
  309. ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
  310.  
  311. if (Request.QueryString["search[value]"] != null && !Request.QueryString["search[value]"].IsNullOrEmpty())
  312. {
  313. pesquisa = Request.QueryString["search[value]"];
  314. }
  315. var query = db.Metodos.AsQueryable();
  316.  
  317.  
  318. if (!pesquisa.IsNullOrEmpty())
  319. query = query.Where(c => c.Nome != "" && c.Nome.Trim().ToUpper().Contains(pesquisa.Trim().ToUpper()));
  320.  
  321. return Json(query.OrderBy(c => c.Nome).Skip(start).Take(length).ToList().Select(c =>
  322. new
  323. {
  324. id = c.Id.ToString(),
  325. text = c.Nome.Trim(),
  326. }).ToList(), JsonRequestBehavior.AllowGet);
  327. }
  328.  
  329.  
  330. public JsonResult GetObservacoes(int id)
  331. {
  332. Data.DataContext db = new Data.DataContext();
  333. var cliente = new ALConsultoria.Web.Models.Clientes();
  334. var obsvazio = "";
  335. var obj = db.Clientes.Single(c => c.Id == id);
  336. cliente.Observacoes = obj.Observacoes;
  337. var obscliente = cliente.Observacoes;
  338.  
  339. if (obj == null)
  340. {
  341. return this.Json(obsvazio, JsonRequestBehavior.AllowGet);
  342. }
  343.  
  344. if (id == 0)
  345. {
  346. return this.Json(obsvazio, JsonRequestBehavior.AllowGet);
  347. }
  348.  
  349. if (obscliente != null)
  350. {
  351. return this.Json(obscliente, JsonRequestBehavior.AllowGet);
  352. }
  353.  
  354. return this.Json(obsvazio, JsonRequestBehavior.AllowGet);
  355. }
  356. }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement