using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Dynamic; using EMIS.ViewModel; using EMIS.Utility; using EMIS.CommonLogic.ScoreManage; using EMIS.Entities; using Bowin.Common.Mvc; using Bowin.Common.Linq.Entity; using Bowin.Web.Controls.Mvc; using EMIS.ViewModel.ScoreManage; using Bowin.Common.Exceptions; using EMIS.ViewModel.CacheManage; using Bowin.Common.JSON; namespace EMIS.Web.Controllers.ScoreManage { [Authorization] public class ScoreFormulaController : JsonNetController { public IScoreFormulaServices scoreFormulaServices { get; set; } /// /// 总分公式页面 /// /// public ActionResult List() { var scoreDynamicTypes = DictionaryHelper.GetDictionaryValue(DictionaryItem.CF_ScoreType); ViewBag.ScoreDynamicTypes = scoreDynamicTypes; return View(); } /// /// 总分公式列表查询 /// /// /// [HttpPost] public ActionResult List(QueryParamsModel pararms) { var name = pararms.getExtraString("textName"); var query = scoreFormulaServices.GetScoreFormulaGrid(name, (int)pararms.page, (int)pararms.rows); var differentDynamicTypes = DictionaryHelper.GetDictionaryValue(DictionaryItem.CF_ScoreType); var list = WrapResult(query.rows, differentDynamicTypes); return Json(new { total = list.Count, rows = list }); } /// /// 编辑 /// /// /// public ActionResult Edit(Guid? scoreFormulaID) { ScoreFormulaView scoreFormulaView = new ScoreFormulaView(); if (scoreFormulaID != null && scoreFormulaID != Guid.Empty) { ER_ScoreFormula scoreFormula = scoreFormulaServices.GetScoreFormula(scoreFormulaID); scoreFormulaView.ScoreFormulaID = scoreFormula.ScoreFormulaID; scoreFormulaView.Name = scoreFormula.Name; } return View(scoreFormulaView); } /// /// 编辑 /// /// /// [HttpPost] public ActionResult Edit(ScoreFormulaView scoreFormulaView) { try { var scoreFormulaDetailList = DataGrid.GetTableData("dgScoreFormulaDetailList"); scoreFormulaServices.ScoreFormulaAdd(scoreFormulaView, scoreFormulaDetailList); return Json(new ReturnMessage() { IsSuccess = true, Message = "保存成功。" }); } catch (Exception ex) { return Json(new ReturnMessage() { IsSuccess = false, Message = "保存失败,原因:" + ex.Message }); } } /// /// 删除 /// /// /// [HttpPost] public ActionResult Delete(string scoreFormulaIDs) { try { List list = new List(); for (int i = 0; i < scoreFormulaIDs.Split(',').Length; i++) { string id = scoreFormulaIDs.Split(',')[i]; if (!string.IsNullOrEmpty(id)) { Guid scoreFormulaID = new Guid(id); list.Add(scoreFormulaID); } } scoreFormulaServices.ScoreFormulaDelete(list); return Json("删除成功"); } catch (Exception ex) { string mge = ex.Message; System.Data.SqlClient.SqlException num = ExceptionHelper.GetSqlException(ex); if (num != null) { if (num.Number == 547) mge = "请先删除所有关联的数据,如参数设置!"; } return base.Json("删除失败,原因:" + mge); } } /// /// 总分公式明细页面 /// /// /// [HttpPost] public ActionResult ScoreFormulaDetailList(Guid? scoreFormulaID) { var listScoreFormulaDetail = scoreFormulaServices.GetScoreFormulaDetailListByScoreFormulaID(scoreFormulaID); return Json(new GridResultSet() { rows = listScoreFormulaDetail, total = listScoreFormulaDetail.Count }); } /// /// /// /// /// /// private List WrapResult(List result, IEnumerable differentDynamicTypes) { var list = new List(); if (result == null || result.Count == 0) { return list; } var scoreFormulas = result.Select(s => new { s.ScoreFormulaID, s.Name }).Distinct(); foreach (var scoreFormula in scoreFormulas) { dynamic item = new ExpandoObject(); item.ScoreFormulaID = scoreFormula.ScoreFormulaID; item.Name = scoreFormula.Name; ER_ScoreFormulaDetail scoreFormulaDetail = null; foreach (var differentDynamicType in differentDynamicTypes) { scoreFormulaDetail = scoreFormulaServices.GetScoreFormulaDetailList(scoreFormula.ScoreFormulaID).Where(x => x.ScoreType == differentDynamicType.Value).FirstOrDefault(); var percentage = scoreFormulaDetail == null ? 0 : scoreFormulaDetail.Percentage; ((IDictionary)item).Add("ScoreType" + differentDynamicType.Value.ToString(), percentage.ToString()); } list.Add(item); } return list; } /// /// 绑定下拉列表 /// /// /// [HttpPost] public ActionResult ScoreFormulaDataBind(DropdownListBindType? bindType) { List list = scoreFormulaServices.GetScoreFormulaList("").Select(x => new DropdownListItem { Text = x.Name, Value = x.ScoreFormulaID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } /// /// 绑定下拉列表 /// /// /// [HttpPost] public ActionResult ScoreFormulaDataJosnBind(DropdownListBindType? bindType) { List list = new List(); var scoreType = IdNameExt.GetDictionaryItem(DictionaryItem.CF_ScoreType.ToString()); List listScoreFormula = scoreFormulaServices.GetScoreFormulaList(""); foreach (var scoreFormula in listScoreFormula) { string reulst = "{\"ScoreFormulaID\":\"" + scoreFormula.ScoreFormulaID + "\","; foreach (var st in scoreType) { ER_ScoreFormulaDetail scoreFormulaDetail = scoreFormula.ER_ScoreFormulaDetail.ToList().Where(x => x.ScoreType == st.Value).FirstOrDefault(); if (scoreFormulaDetail != null) reulst += "\"Formula_" + st.Value + "\":\"" + scoreFormulaDetail.Percentage + "\","; else reulst += "\"Formula_" + st.Value + "\":\"0\","; } reulst = reulst.Substring(0, reulst.Length - 1) + "}"; DropdownListItem dlt = new DropdownListItem { Text = scoreFormula.Name, Value = reulst }; list.Add(dlt); } DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } } }