using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using DSWeb.Areas.SysMng.Models.DBTableConn; using DSWeb.Areas.SysMng.DAL; using DSWeb.Areas.SysMng.DAL.DBTableConn; using DSWeb.MvcShipping.Helper; using DSWeb.MvcShipping.Helper.Repository; using HcUtility.Comm; using HcUtility.Core; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.SoftMng.Filter; namespace DSWeb.Areas.SysMng.Controllers { [JsonRequestBehavior] public class DBTableConnController : Controller { public ActionResult Index() { return View(); } public ActionResult Edit() { return View(); } [SqlKeyWordsFilter(Type = "Action")]//sql 防注入过滤器 public ContentResult GetDataList(int start, int limit, string sort, string condition) { var dataList = DBTableConnDAL.GetDataList(condition, sort); var DicList = ModelObjectConvert.ToExtendList(dataList); var list = DicList.Skip(start).Take(limit); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() //,fieldlist = fieldlist.ToList() }); return new ContentResult() { Content = json }; } [SqlKeyWordsFilter(Type = "Action")]//sql 防注入过滤器 public ContentResult GetData(string handle, string condition) { Dictionary head = new Dictionary(); //var bj = new BJmb(); //List fieldlist = bj.GetFieldList(); if (handle == "edit"||handle =="view") { var _t = ""; var dataList = DBTableConnDAL.GetDataList(condition, _t);//, fieldlist var DicList = ModelObjectConvert.ToExtendList(dataList); if (DicList.Count > 0) head = DicList[0]; } if (head == null) { head = new Dictionary(); } var json = JsonConvert.Serialize( new { Success = true, Message = "查询成功", data = head }); return new ContentResult() { Content = json }; } public ContentResult Save(string opstatus, string data,string Body, string DelBody) { var modb = new ModelObjectRepository(); Database db = DatabaseFactory.CreateDatabase(); var head = new SYS_TABLECONNMC(); head.SetValue(db, data); //存储时不保存两个同步时间和同步状态 head.NoSave("SYNCSTARTTIME"); head.NoSave("SYNCTIME"); head.NoSave("SYNCSTATES"); var BodyList = new List(); var DelBodyList = new List(); if (Body != "") { BodyList = JsonConvert_extend.Deserialize(db,Body); } if (DelBody != "") { DelBodyList = JsonConvert_extend.Deserialize(db,DelBody); } if (opstatus == "add") { head.DbOperationType = DbOperationType.DbotIns; head.ModelUIStatus = "I"; head.SetExtendValue("ID", "1"); } else if (opstatus == "edit") { head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; } else { head.DbOperationType = DbOperationType.DbotDel; } DBResult result = modb.Save(head, ModelObjectConvert.ToModelObjectList(BodyList), ModelObjectConvert.ToModelObjectList(DelBodyList) ); //BasicDataRefDAL.SaveLog(head, Session["USERID"].ToString(), "Demo 学生管理_班级", "保存"); //刷新父窗口上的父节点 var jsonRespose = new JsonResponse(); jsonRespose.Success = result.Success; if (result.Success) { var condition = ""; if (opstatus == "add") { condition = " id=(select MAX(id) from SYS_TABLECONN) "; } if (opstatus == "edit") { condition = " id="+head.GetValue("ID")+" "; } var _list = ModelObjectConvert.ToExtendList((DBTableConnDAL.GetDataList(condition, ""))); if (_list.Count > 0) { jsonRespose.Message = "保存成功"; jsonRespose.Data = _list[0]; } } else { jsonRespose.Message = result.Message; jsonRespose.Data = null; } return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult Delete(string data, string USERID) { var modb = new ModelObjectDB(); Database db = DatabaseFactory.CreateDatabase(); var head = new SYS_TABLECONNMC(); head.SetValue(db, data); DBResult result = modb.Delete(head, "delete from SYS_TABLECONN_FIELD where CONNID='" + head.GetBillNoValue() + "'"); //真删除 //DBResult result = modb.Delete(head, USERID,true, "delete from Demo_XSGL_XS where BJ_GID='"+head.GetBillNoValue()+"'" );//带userid参数的为标记删除 var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } [SqlKeyWordsFilter(Type = "Action")]//sql 防注入过滤器 public ContentResult GetBodyList(int start, int limit, string sort, string condition) { var dataList = DBTableConnDAL.GetBodyList(condition, sort); var DicList = ModelObjectConvert.ToExtendList(dataList); var list = DicList.Skip(start).Take(limit); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() }); return new ContentResult() { Content = json }; } //public ContentResult DoSync_old(string data) //{ // var modb = new ModelObjectRepository(); // Database db = DatabaseFactory.CreateDatabase(); // Database Portdb = DatabaseFactory.CreateDatabase("ERPPort"); // var head = new SYS_TABLECONNMC(); // head.SetValue(db, data); // BasicDataRefDAL.SaveLog_str("业务开始", head.GetValue("NAME"), "表同步模块", "时间记录"); // var condition = ""; // condition = " id=" + head.GetValue("ID") + " "; // var SYNCTYPE = head.GetValue("SYNCTYPE"); // #region 主数据保存 // //状态变为同步中 同步开始时间设为当前时间 同步完成时间设为空 // head.SetPropertyValue("SYNCSTARTTIME", System.DateTime.Now.ToString()); // head.SetPropertyValue("SYNCSTATES", "同步中"); // head.SetPropertyValue("SYNCTIME", ""); // head.DbOperationType = DbOperationType.DbotUpd; // head.ModelUIStatus = "E"; // var _r = modb.SaveHead(head); // head.TimeMarkNext();//保存后时间标签+1,用于在同一过程中二次存储 // var templist = (DBTableConnDAL.GetDataList(condition, "")); // head = templist[0]; // #endregion // var FieldList = DBTableConnDAL.GetBodyList("CONNID="+head.GetValue("ID"), ""); // var FieldDicList = ModelObjectConvert.ToExtendList(FieldList); // var _fromsql = "select "; // var _tosql = "select "; // foreach (var dic in FieldDicList) { // if (_fromsql != "select " && !string.IsNullOrEmpty(dic["FROMFIELDNAME"])) // { // _fromsql += ","; // } // _fromsql += dic["FROMFIELDNAME"]; // if (_tosql != "select " && !string.IsNullOrEmpty(dic["TOFIELDNAME"])) // { // _tosql += ","; // } // _tosql += dic["TOFIELDNAME"]; // } // _fromsql += " from " + head.GetValue("FROMTABLE"); // _tosql += " from " + head.GetValue("TOTABLE"); // BaseTableMC FromTableObj = new BaseTableMC(); // BaseTableMC SaveObj = new BaseTableMC(); // //获取来源表对象列表 // var from_condition = ""; // var to_condition = ""; // if (SYNCTYPE == "单号") // { // from_condition = string.Format(" {0}='{1}' ", head.GetValue("FROMTIMEMARK"), head.GetValue("CONDITION")); // to_condition = string.Format(" {0}='{1}' ", head.GetValue("TOTIMEMARK"), head.GetValue("CONDITION")); // } // if (SYNCTYPE == "日期") // { // from_condition = string.Format(" {0}>='{1}' ", head.GetValue("FROMTIMEMARK"), head.GetValue("CONDITION")); // to_condition = string.Format(" {0}>='{1}' ", head.GetValue("TOTIMEMARK"), head.GetValue("CONDITION")); // } // if (SYNCTYPE == "时间标签" && head.GetValue("CONDITION")!="") // { // from_condition = string.Format(" {0}>='{1}' ", head.GetValue("FROMTIMEMARK"), head.GetValue("CONDITION")); // } // FromTableObj = new BaseTableMC(head.GetValue("FROMTABLE"), head.GetValue("FROMPKEY"), _fromsql, head.GetValue("FROMDB")); // FromTableObj.GetTableField(Portdb); // var FromObjList = DBTableConnDAL.BaseGetList(FromTableObj, from_condition);//需要限制修改时间大于多少什么的where子句写在这里 // //生成目标表对象列表 // SaveObj = new BaseTableMC(head.GetValue("TOTABLE"), head.GetValue("TOPKEY"), _tosql, head.GetValue("TODB")); // SaveObj.GetTableField(db); // var HaveList = DBTableConnDAL.BaseGetList(SaveObj, to_condition);//首先取得本地已有数据 // var SaveList = new List(); // var UPDList = new List(); // var havenumber = -1; // BasicDataRefDAL.SaveLog_str(string.Format("两个列表获取完成(from{0},Have{1}),开始比对。", FromObjList.Count, HaveList.Count), "", "表同步模块", "时间记录"); // var TOPKEY = head.GetValue("TOPKEY"); // var FROMPEKY = head.GetValue("FROMPKEY"); // string[] TOPEKYArray = { }; // string[] FROMPEKYArray = { }; // if (TOPKEY.Contains(",")) // { // //首先将两个关键词都拆分成数组 // TOPEKYArray = TOPKEY.Split(','); // FROMPEKYArray = FROMPEKY.Split(','); // if (TOPEKYArray.Count() != FROMPEKYArray.Count() ) { // var json1 = JsonConvert.Serialize(new { Success = false, Message = "主键数量不匹配" }); // return new ContentResult() { Content = json1 }; // } // } // foreach (var fromObj in FromObjList) // { // var alreadyhave = false; // var haveobj = new BaseTableMC(head.GetValue("TOTABLE"), TOPKEY, _tosql, head.GetValue("TODB")); // havenumber = -1; // foreach (var toObj in HaveList) // { // if (!TOPKEY.Contains(",")) // { // if (toObj.GetValue(TOPKEY) == fromObj.GetValue(FROMPEKY)) // { // toObj.DbOperationType = DbOperationType.DbotUpd; // toObj.ModelUIStatus = "E"; // foreach (var field in FieldDicList) // { // toObj.SetPropertyValue(field["TOFIELDNAME"], fromObj.GetValue(field["FROMFIELDNAME"])); // } // if (needupd(head, fromObj, toObj)) // { // toObj.Copy(haveobj); // UPDList.Add(haveobj); // } // alreadyhave = true; // havenumber = HaveList.IndexOf(toObj); // break; // } // } // else // { // var PKOK = true; // for (var _i = 0; _i < TOPEKYArray.Count(); _i++) // { // if (toObj.GetValue(TOPEKYArray[_i]) != fromObj.GetValue(FROMPEKYArray[_i])) // { // PKOK = false; // break; // } // } // if (PKOK) // { // toObj.DbOperationType = DbOperationType.DbotUpd; // toObj.ModelUIStatus = "E"; // foreach (var field in FieldDicList) // { // toObj.SetPropertyValue(field["TOFIELDNAME"], fromObj.GetValue(field["FROMFIELDNAME"])); // } // if (needupd(head, fromObj, toObj)) // { // toObj.Copy(haveobj); // UPDList.Add(haveobj); // } // alreadyhave = true; // havenumber = HaveList.IndexOf(toObj); // break; // } // } // } // if (!alreadyhave) // { // var saveobj = new BaseTableMC(SaveObj); // saveobj.DbOperationType = DbOperationType.DbotIns; // saveobj.ModelUIStatus = "I"; // foreach (var field in FieldDicList) // { // saveobj.SetPropertyValue(field["TOFIELDNAME"], fromObj.GetValue(field["FROMFIELDNAME"])); // } // SaveList.Add(saveobj); // } // else // { // if (havenumber >= 0) // { // HaveList.RemoveAt(havenumber); // havenumber = -1; // } // } // } // BasicDataRefDAL.SaveLog_str(string.Format("比对循环结束,需储存{0}条,升级{1}条", SaveList.Count(), UPDList.Count()), "", "表同步模块", "时间记录"); // //保存目标表对象列表 // DBResult result = new DBResult(); // var DoSaveList = ModelHeadConvert.ToModelObjectList(SaveList); // foreach (var saveobj in DoSaveList) // { // result = modb.SaveHead(saveobj); // if (result.Success == false) { // BasicDataRefDAL.SaveLog_str("保存错误:"+result.Message , saveobj.ToString(), "表同步模块", "时间记录"); // } // } // BasicDataRefDAL.SaveLog_str("储存"+result.Success.ToString()+",储存" + DoSaveList.Count.ToString() + "条;"+result.Message, "", "表同步模块", "时间记录"); // var DoUPDList = ModelHeadConvert.ToModelObjectList(UPDList); // foreach (var saveobj in DoUPDList) // { // result = modb.SaveHead(saveobj); // if (result.Success == false) // { // BasicDataRefDAL.SaveLog_str("更新错误:" + result.Message, saveobj.ToString(), "表同步模块", "时间记录"); // } // } // result = modb.SaveHeadList_new(DoUPDList); // BasicDataRefDAL.SaveLog_str("更新" + result.Success.ToString() + ",更新" + DoUPDList.Count.ToString() + "条;" + result.Message, "", "表同步模块", "时间记录"); // if (result.Success) // { // //升级本条数据的同步时间至当前时间 // head.SetPropertyValue("SYNCTIME", System.DateTime.Now.ToString()); // head.SetPropertyValue("SYNCSTATES", "同步完成"); // head.DbOperationType = DbOperationType.DbotUpd; // head.ModelUIStatus = "E"; // result = modb.Save(head); // } // var _list = ModelObjectConvert.ToExtendList((DBTableConnDAL.GetDataList(condition, ""))); // var json = JsonConvert.Serialize(new { Success = result.Success, Message = result.Message, Data = _list[0] }); // return new ContentResult() { Content = json }; //} public ContentResult DoSync(string data) { DBTableConnDAL.DoSync_void(data); var json = JsonConvert.Serialize(new { Success = true, Message = "", Data = "" }); return new ContentResult() { Content = json }; } private bool needupd(SYS_TABLECONNMC head, BaseTableMC fromObj, BaseTableMC toObj) { if (head.GetValue("SYNCTYPE") == "时间标签") { var frommark = Convert.ToDateTime(fromObj.GetValue(head.GetValue("FROMTIMEMARK"))); var tomark = Convert.ToDateTime(toObj.GetValue(head.GetValue("TOTIMEMARK"))); if (frommark > tomark) { return true; } else { return false; } } if (head.GetValue("SYNCTYPE") == "单号") { var frommark = Convert.ToString(fromObj.GetValue(head.GetValue("FROMTIMEMARK"))); var tomark = Convert.ToString(toObj.GetValue(head.GetValue("TOTIMEMARK"))); if (frommark == tomark) { return true; } else { return false; } } return true; } } }