Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Anexs.Lib.Extensoes;
- using ALConsultoria.Web.Helpers;
- using ALConsultoria.Web.Models;
- using AlconsultoriaModel;
- namespace ALConsultoria.Web.Controllers
- {
- public class PlanoDeTreinoController : ALConsultoria.Web.Helpers.AnexsController
- {
- // GET: PlanoDeTreino
- public ActionResult Index(int? id)
- {
- ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
- var treino = new ALConsultoria.Web.Models.Treinos();
- if (id > 0)
- {
- var obj = db.Treinos.Single(c => c.Id == id);
- treino = Newtonsoft.Json.JsonConvert.DeserializeObject<ALConsultoria.Web.Models.Treinos>(obj.Dados);
- treino.Cliente = obj.Cliente;
- treino.Intensidade = obj.Intensidade;
- treino.DivisaoFicha = obj.DivisaoFicha;
- treino.Semana1 = obj.Semana1;
- // return View();
- }
- return View(treino);
- }
- public ActionResult Cadastro(int? id, int idcliente = 0, int IdSemana = 0, bool copia = false)
- {
- ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
- var treino = new ALConsultoria.Web.Models.Treinos();
- treino.Exercicio = "Musculação";
- if (idcliente > 0)
- {
- treino.Idcliente = idcliente;
- }
- if (IdSemana > 0)
- {
- treino.IdSemana = IdSemana;
- }
- ViewBag.Clientes = ComboBox.GerarBox(db.Clientes.Select(c => new SelectItem
- {
- Id = c.Id,
- Texto = c.Nome
- }).ToList(), true, false);
- ViewBag.Classes = ComboBox.GerarBox(db.Classes.Select(c => new SelectItem
- {
- Id = c.Id,
- Texto = c.Exercicioclasse
- }).ToList(), true, false);
- ViewBag.Grupos = ComboBox.GerarBox(db.Grupos.Select(c => new SelectItem
- {
- Id = c.Id,
- Texto = c.Nome
- }).ToList(), true, false);
- ViewBag.Intensidade = ComboBox.GerarBox(db.Intensidades.Select(c => new SelectItem
- {
- Id = c.Id,
- Texto = c.Nome
- }).ToList(), true, false);
- ViewBag.DivisaoFichas = ComboBox.GerarBox(db.DivisaoFichas.Select(c => new SelectItem
- {
- Id = c.Id,
- Texto = c.Nome
- }).ToList(), true, false);
- ViewBag.Semanas = ComboBox.GerarBox(db.Semanas.Select(c => new SelectItem
- {
- Id = c.Id,
- Texto = c.Nome
- }).ToList(), true, false);
- if (id > 0)
- {
- var obj = db.Treinos.Single(c => c.Id == id);
- treino = Newtonsoft.Json.JsonConvert.DeserializeObject<ALConsultoria.Web.Models.Treinos>(obj.Dados);
- }
- else if (idcliente > 0 && IdSemana > 0)
- {
- var obj = db.Treinos.FirstOrDefault(c => c.Idcliente == idcliente && c.IdSemana == IdSemana);
- if (obj != null)
- treino = Newtonsoft.Json.JsonConvert.DeserializeObject<ALConsultoria.Web.Models.Treinos>(obj.Dados);
- }
- if (copia == true)
- {
- treino.Id = 0;
- if (idcliente > 0)
- {
- treino.Idcliente = idcliente;
- }
- if (IdSemana > 0)
- {
- treino.IdSemana = IdSemana;
- }
- }
- return View(treino);
- }
- public ActionResult Salvar(ALConsultoria.Web.Models.Treinos model)
- {
- ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
- var transaction = db.Database.BeginTransaction();
- try
- {
- var obj = model.Id > 0 ? db.Treinos.SingleOrDefault(c => c.Id == model.Id) : new Treino();
- obj.Idcliente = model.Idcliente;
- obj.Idintensidade = model.Idintensidade;
- obj.Objetivo = model.Objetivo;
- obj.Tempo = model.Tempo;
- obj.Frequencia = model.Frequencia;
- obj.Dataval = model.Dataval;
- obj.Dataini = model.Dataini;
- obj.Exercicio = model.Exercicio;
- obj.IdDivisaoFicha = model.IdDivisaoFicha;
- obj.IdSemana = model.IdSemana;
- obj.Orientacoe = model.Orientacoe;
- obj.ObservacaoCliente = model.ObservacaoCliente;
- obj.Cliente.Observacoes = model.Cliente.Observacoes;
- obj.Dados = Newtonsoft.Json.JsonConvert.SerializeObject(model);
- if (obj.Id == 0)
- {
- db.Treinos.Add(obj);
- }
- db.SaveChanges();
- transaction.Commit();
- MensagemSucesso();
- return RedirectToAction("Index");
- }
- catch (Exception ex)
- {
- transaction.Rollback();
- MensagemErro(ex);
- return RedirectToAction("Cadastro", model);
- }
- finally
- {
- transaction.Dispose();
- db.Dispose();
- }
- return RedirectToAction("Cadastro", model);
- }
- public ActionResult Excluir(int? id)
- {
- if (id > 0)
- {
- ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
- var transaction = db.Database.BeginTransaction();
- try
- {
- db.Treinos.Remove(db.Treinos.Single(c => c.Id == id));
- db.SaveChanges();
- transaction.Commit();
- MensagemSucesso();
- }
- catch (Exception ex)
- {
- transaction.Rollback();
- MensagemErro(ex);
- }
- finally
- {
- transaction.Dispose();
- db.Dispose();
- }
- }
- return RedirectToAction("Index");
- }
- [HttpGet]
- [ValidateInput(false)]
- public JsonResult GetDivisaoTreino(int iddivisao, List<int> idsmembros)
- {
- ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
- var div = db.DivisaoFichas.Single(c => c.Id == iddivisao);
- var lista = new List<ALConsultoria.Web.Models.Treinos.Divisao>();
- var dados = new List<ALConsultoria.Web.Models.Treinos.DadoTreino>();
- var membros = db.Membros.Select(c => new SelectItem
- {
- Id = c.Id,
- Texto = c.Nome
- }).ToList();
- membros.Insert(0, new SelectItem
- {
- Id = 0,
- Texto = "Selecione"
- });
- char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
- for (int i = 0; i < div.Divisoes; i++)
- {
- lista.Add(new Treinos.Divisao(i, membros, div.Divisoes)
- {
- IdMembro = idsmembros != null && idsmembros.Count > i ? idsmembros[i] : 0,
- Nome = alpha[i].ToString()
- });
- dados.Add(new Treinos.DadoTreino(i)
- {
- IdMembro = idsmembros != null && idsmembros.Count > i ? idsmembros[i] : 0,
- Nome = alpha[i].ToString()
- });
- }
- return Json(new { lista, dados }, JsonRequestBehavior.AllowGet);
- }
- [HttpGet]
- [ValidateInput(false)]
- public JsonResult GetExercicio(int index, int numero, int indexdado, int idsemana, bool hiit = false)
- {
- ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
- if (hiit == true)
- {
- var model = new ALConsultoria.Web.Models.Treinos.HiitTreino(index, numero, indexdado);
- return Json(model, JsonRequestBehavior.AllowGet);
- }
- else
- {
- var semana = db.Semanas.FirstOrDefault(c => c.Id == idsemana);
- var model = new ALConsultoria.Web.Models.Treinos.ExercicioTreino(index, numero, semana, indexdado);
- return Json(model, JsonRequestBehavior.AllowGet);
- }
- }
- [HttpGet]
- [ValidateInput(false)]
- public JsonResult PesquisaExercicio(string pesquisa, int idmembro = 0, int idgrupo = 0, bool hiit = false, bool aquecimento = false, int start = 0, int length = 20)
- {
- ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
- if (Request.QueryString["search[value]"] != null && !Request.QueryString["search[value]"].IsNullOrEmpty())
- {
- pesquisa = Request.QueryString["search[value]"];
- }
- var query = db.Exercicios.AsQueryable();
- if (idmembro > 0)
- {
- var ids = db.MembrosGrupos.Where(c => c.IdMembro == idmembro).Select(c => c.IdGrupo).ToList();
- query = query.Where(c => ids.Contains(c.Idgrupo.Value));
- }
- if (hiit)
- {
- var ids = db.Grupos.Where(c => c.Hiit == true).Select(c => c.Id).ToList();
- query = query.Where(c => ids.Contains(c.Idgrupo.Value));
- }
- if (aquecimento)
- {
- var ids = db.Grupos.Where(c => c.Aquecimento == true).Select(c => c.Id).ToList();
- query = query.Where(c => ids.Contains(c.Idgrupo.Value));
- }
- if (idgrupo > 0)
- {
- query = query.Where(c => idgrupo == c.Idgrupo);
- }
- if (!pesquisa.IsNullOrEmpty())
- query = query.Where(c => c.Nome != "" && c.Nome.Trim().ToUpper().Contains(pesquisa.Trim().ToUpper()));
- return Json(query.OrderBy(c => c.Nome).Skip(start).Take(length).ToList().Select(c =>
- new
- {
- id = c.Id.ToString(),
- text = c.Nome.Trim(),
- }).ToList(), JsonRequestBehavior.AllowGet);
- }
- [HttpGet]
- [ValidateInput(false)]
- public JsonResult PesquisaMetodo(string pesquisa, int start = 0, int length = 20)
- {
- ALConsultoria.Data.DataContext db = new ALConsultoria.Data.DataContext();
- if (Request.QueryString["search[value]"] != null && !Request.QueryString["search[value]"].IsNullOrEmpty())
- {
- pesquisa = Request.QueryString["search[value]"];
- }
- var query = db.Metodos.AsQueryable();
- if (!pesquisa.IsNullOrEmpty())
- query = query.Where(c => c.Nome != "" && c.Nome.Trim().ToUpper().Contains(pesquisa.Trim().ToUpper()));
- return Json(query.OrderBy(c => c.Nome).Skip(start).Take(length).ToList().Select(c =>
- new
- {
- id = c.Id.ToString(),
- text = c.Nome.Trim(),
- }).ToList(), JsonRequestBehavior.AllowGet);
- }
- public JsonResult GetObservacoes(int id)
- {
- Data.DataContext db = new Data.DataContext();
- var cliente = new ALConsultoria.Web.Models.Clientes();
- var obsvazio = "";
- var obj = db.Clientes.Single(c => c.Id == id);
- cliente.Observacoes = obj.Observacoes;
- var obscliente = cliente.Observacoes;
- if (obj == null)
- {
- return this.Json(obsvazio, JsonRequestBehavior.AllowGet);
- }
- if (id == 0)
- {
- return this.Json(obsvazio, JsonRequestBehavior.AllowGet);
- }
- if (obscliente != null)
- {
- return this.Json(obscliente, JsonRequestBehavior.AllowGet);
- }
- return this.Json(obsvazio, JsonRequestBehavior.AllowGet);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement