using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using WX.CRM.BLL.Base; using WX.CRM.Common; using WX.CRM.IBLL.Base; using WX.CRM.Model.Entity; using WX.CRM.WebHelper; namespace WX.CRM.WEB.Controllers.Base { public class CaseController : BaseController { private ValidationErrors errors = new ValidationErrors(); private readonly IBAS_CASE _basCase; private readonly IBAS_CASE_CATEGORY _basCaseCategory; public CaseController(IBAS_CASE basCase, IBAS_CASE_CATEGORY basCaseCategory) { _basCase = basCase; _basCaseCategory = basCaseCategory; } public ActionResult Index() { ToolBar tool = new ToolBar(); string[] toolbtn = new ToolButtonView().ToolButtonRight(InitRights.案例管理, userRightId); List listBtn = new List(toolbtn); List Buttons = new List(); Buttons.AddRange(listBtn); tool.AllowButton(Buttons.ToArray()); tool.AddOtherButton("Other1", "讲解分类", "icon-save", "Category_Click", true); tool.AddOtherButton("Other2", "向上", "icon-up", "Up_Click", true); tool.AddOtherButton("Other3", "向下", "icon-down", "Down_Click", true); ViewBag.ToolBar = tool; return View(); } [HttpPost] public JsonResult Index(string keyword) { IEnumerable list = null; IEnumerable category = _basCaseCategory.GetList(); if (string.IsNullOrEmpty(keyword)) list = _basCase.GetList(); else list = _basCase.GetList(p => p.QUESTION.Contains(keyword)); var data = from q in category select new { id = q.ID, text = q.CATEGORYNAME, children = GetTreeChildren(list, q.ID) }; return Json(data, JsonRequestBehavior.AllowGet); } private object GetTreeChildren(IEnumerable list, decimal id) { var ret = from q in list where q.CATEGORYID == id orderby q.SORT select new { id = q.ID, text = q.QUESTION, sortId = q.SORT }; return ret; } //[AuthorizeRedirect(Roles = InitRights.CONST_内部公告)] public ActionResult Add() { var model = new BAS_CASE(); return View(model); } [HttpPost] [ValidateInput(false)] public JsonResult Add(BAS_CASE model) { if (model == null) { errors.Add("参数不能为空!"); return JsonHandler.InsertMessage(errors, false); } if (model.CATEGORYID <= 0) { errors.Add("分类不能为空!"); return JsonHandler.InsertMessage(errors, false); } if (string.IsNullOrWhiteSpace(model.QUESTION)) { errors.Add("标题不能为空!"); return JsonHandler.InsertMessage(errors, false); } if (string.IsNullOrWhiteSpace(model.ANSWER)) { errors.Add("内容不能为空!"); return JsonHandler.InsertMessage(errors, false); } model.ID = new SEQUENCES_BL().Seq_base_get(); model.CREATEEID = UserId; model.CTIME = DateTime.Now; decimal sort = 0; var list = _basCase.GetList(); if (list.Any()) sort = list.Max(p => p.SORT) + 1; model.SORT = sort; var flag = _basCase.Add(model); return JsonHandler.InsertMessage(errors, flag == 1); } public ActionResult Edit(string id) { decimal i = 0; decimal.TryParse(id, out i); BAS_CASE model; if (string.IsNullOrWhiteSpace(id)) { model = new BAS_CASE(); } else { model = _basCase.Get(m => m.ID == i); } return View(model); } [HttpPost] [ValidateInput(false)] public JsonResult Edit(BAS_CASE model) { if (model == null) { errors.Add("参数不能为空!"); return JsonHandler.InsertMessage(errors, false); } if (model.CATEGORYID <= 0) { errors.Add("分类不能为空!"); return JsonHandler.InsertMessage(errors, false); } if (string.IsNullOrWhiteSpace(model.QUESTION)) { errors.Add("标题不能为空!"); return JsonHandler.InsertMessage(errors, false); } if (string.IsNullOrWhiteSpace(model.ANSWER)) { errors.Add("内容不能为空!"); return JsonHandler.InsertMessage(errors, false); } model.CREATEEID = UserId; var flag = _basCase.Update(model); return JsonHandler.UpdateMessage(errors, flag); } [HttpPost] public JsonResult Delete(string id) { decimal i = 0; decimal.TryParse(id, out i); BAS_CASE model; if (string.IsNullOrWhiteSpace(id)) { model = new BAS_CASE(); } else { model = _basCase.Get(m => m.ID == i); _basCase.Delete(model); } return Json(new { result = true }, JsonRequestBehavior.AllowGet); } public JsonResult Detail(string id) { decimal i = 0; decimal.TryParse(id, out i); BAS_CASE model; if (string.IsNullOrWhiteSpace(id)) { model = new BAS_CASE(); } else { model = _basCase.Get(m => m.ID == i); } return Json(model, JsonRequestBehavior.AllowGet); } public ActionResult Category() { return View(); } [HttpPost] public JsonResult CateogryHtml() { IEnumerable list = _basCaseCategory.GetList(); var query = from q in list select new { id = q.ID, text = q.CATEGORYNAME }; return Json(query, JsonRequestBehavior.AllowGet); } [HttpPost] public JsonResult CateogryTree() { IList list = _basCaseCategory.GetList().ToList(); list.Insert(0, new BAS_CASE_CATEGORY() { ID = 0, CATEGORYNAME = "请选择..." }); var query = from q in list select new { id = q.ID, text = q.CATEGORYNAME }; return Json(query, JsonRequestBehavior.AllowGet); } public JsonResult CateogryAddOrUpdate(decimal? id, string categoryname) { try { if (string.IsNullOrWhiteSpace(categoryname)) { return JsonHandler.ManageMessage("categoryname参数错误,请确认!", false); } if (id.HasValue) { var model = _basCaseCategory.Get(p => p.ID == id.Value); if (model != null) { model.CATEGORYNAME = categoryname; model.CREATEEID = UserId; model.CTIME = DateTime.Now; _basCaseCategory.Update(model); } else { return JsonHandler.ManageMessage("id参数错误,请确认!", false); } } else { var model = new BAS_CASE_CATEGORY() { ID = new SEQUENCES_BL().Seq_base_get(), CATEGORYNAME = categoryname, CREATEEID = UserId, CTIME = DateTime.Now }; _basCaseCategory.Add(model); } return Json(new { result = "ok", message = "操作成功!" }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { LogHelper.Error("Case/AddOrUpdate:" + ex.Message); return JsonHandler.ManageMessage(ex.Message, false); } } public JsonResult CategoryDelete(decimal id) { try { var caseInfo = _basCase.GetList(p => p.CATEGORYID == id); if (caseInfo.Any()) { return JsonHandler.ManageMessage("该分类下存在讲解数据,请先处理对应讲解数据,才能删除", false); } var categoryInfo = _basCaseCategory.Get(p => p.ID == id); _basCaseCategory.Delete(categoryInfo); return Json(new { result = "ok", message = "操作成功!" }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { LogHelper.Error("Case/Delete:" + ex.Message); return JsonHandler.ManageMessage(ex.Message, false); } } public JsonResult SaveSort(string ids, string sortIds) { bool result = _basCase.Sort(ref errors, ids, sortIds); return JsonHandler.ManageMessage(errors, result); } } }