using System; using System.Linq; using System.Web.Mvc; using DSWeb.MvcShipping.DAL.MsChDui; using DSWeb.MvcShipping.Models.MsChDui; using DSWeb.MvcShipping.Models.MsChDuiView; using DSWeb.MvcShipping.Helper; using DSWeb.MvcShipping.Comm.Cookie; using DSWeb.Areas.CommMng.DAL; using System.Collections.Generic; using HcUtility.Comm; using HcUtility.Core; using DSWeb.MvcShipping.DAL.MsSysBillNoSet; using DSWeb.Areas.MvcShipping.DB; using DSWeb.Areas.MvcShipping.Models.MsOpRule; using System.IO; namespace DSWeb.MvcShipping.Controllers { [JsonRequestBehavior] public class MsOpRuleController : Controller { private RuleContext ruleContext = new RuleContext(); [HttpGet] public ActionResult RuleList() { if (Session["SHOWNAME"] == null) { return Content("登录超时"); } ViewData["Fields"] = ruleContext.EnumValues.Where(e => e.EnumTypeID == 91001).Select(e => e.EnumValueName).ToList(); return View(); } //分页规则列表 [HttpPost] public JsonResult RuleListData(int offset, int limit) { OpRuleListViewModelList rtnList = new OpRuleListViewModelList(); var count = ruleContext.Rules.Count(); var list = ruleContext.Rules.OrderBy(x => x.SortOrder).Skip(offset).Take(limit).ToList(); rtnList.Total = count; rtnList.List = list.AsListViewModelList(); return Json(rtnList); } //可用规则列表 [HttpPost] public JsonResult RuleListEnable() { List listRtn = new List(); var list = ruleContext.Rules.Where(r => r.Enable).OrderBy(ri => ri.SortOrder).ToList(); foreach (var rule in list) { var ruleVM = rule.AsCheckListViewModel(); listRtn.Add(ruleVM); ruleVM.Items = ruleContext.RuleItems.Where(ri => ri.RuleId == rule.GID).OrderBy(ri => ri.SortOrder).ToList().AsCheckListViewModelList(); } return Json(listRtn); } //保存规则 [HttpPost] //public JsonResult SaveRule(OpRuleEditViewModel viewModel) public JsonResult SaveRule() { var sr = new StreamReader(Request.InputStream); var stream = sr.ReadToEnd(); OpRuleEditViewModel viewModel = Newtonsoft.Json.JsonConvert.DeserializeObject(stream); OpRule rule = null; if (!string.IsNullOrWhiteSpace(viewModel.GID)) { rule = ruleContext.Rules.FirstOrDefault(r => r.GID == viewModel.GID); } if (rule == null) { var findRule = ruleContext.Rules.FirstOrDefault(r => r.Code == viewModel.Code); if (findRule != null) { return Json(new { Success = false, Message = "编号已存在" }); } rule = new OpRule(); viewModel.AsModel(rule); rule.GID = Guid.NewGuid().ToString(); rule.CreateTime = DateTime.Now; rule.CreateUser = Session["SHOWNAME"].ToString(); rule.Enable = true; rule.EnableDisableTime = DateTime.Now; int ruleOrder = 1; if (ruleContext.Rules.Count() > 0) { ruleOrder = ruleContext.Rules.Max(r => r.SortOrder) + 1; } rule.SortOrder = ruleOrder; ruleContext.Rules.Add(rule); var items = viewModel.Items.AsModelList(); int order = 1; items.ForEach(i => { i.GID = Guid.NewGuid().ToString(); i.RuleId = rule.GID; i.SortOrder = order++; }); ruleContext.RuleItems.AddRange(items); } else { var findRule = ruleContext.Rules.FirstOrDefault(r => r.Code == viewModel.Code && r.GID != viewModel.GID); if (findRule != null) { return Json(new { Success = false, Message = "编号已存在" }); } bool enableChanged = rule.Enable != viewModel.Enable; viewModel.AsModel(rule); rule.ModifyTime = DateTime.Now; rule.ModifyUser = Session["SHOWNAME"].ToString(); if (enableChanged) { rule.EnableDisableTime = DateTime.Now; } var items = ruleContext.RuleItems.Where(r => r.RuleId == rule.GID).ToList(); ruleContext.RuleItems.RemoveRange(items); items = viewModel.Items.AsModelList(); int order = 1; items.ForEach(i => { i.GID = Guid.NewGuid().ToString(); i.RuleId = rule.GID; i.SortOrder = order++; }); ruleContext.RuleItems.AddRange(items); } ruleContext.SaveChanges(); return Json(new { Success = true, Message = "保存成功" }); } //获取单条规则 [HttpPost] public JsonResult GetRule(string gid) { var rule = ruleContext.Rules.FirstOrDefault(r => r.GID == gid); if (rule != null) { var viewModel = rule.AsEditViewModel(); viewModel.Items = ruleContext.RuleItems.Where(ri => ri.RuleId == rule.GID).AsEditViewModelList(); return Json(viewModel); } return null; } //删除规则 [HttpPost] public JsonResult DeleteRules(string strIds) { string[] arrId = strIds.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var rules = ruleContext.Rules.Where(r => arrId.Contains(r.GID)).ToList(); var items = ruleContext.RuleItems.Where(r => arrId.Contains(r.RuleId)).ToList(); ruleContext.Rules.RemoveRange(rules); ruleContext.RuleItems.RemoveRange(items); ruleContext.SaveChanges(); return Json(new { Success = true, Message = "操作成功" }); } //生效/失效规则 [HttpPost] public JsonResult EnableDisableRules(string strIds, bool enable = true) { string[] arrId = strIds.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var rules = ruleContext.Rules.Where(r => arrId.Contains(r.GID)).ToList(); rules.ForEach(r => { if (r.Enable != enable) { r.Enable = enable; r.EnableDisableTime = DateTime.Now; } }); ruleContext.SaveChanges(); return Json(new { Success = true, Message = "操作成功" }); } } }