You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/SysMng/DAL/DBTableConn/DBTableConnDAL.cs

585 lines
21 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.Areas.SysMng.Models.DBTableConn;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.TruckMng.Helper;
using DSWeb.TruckMng.Helper.Repository;
using System.Linq;
namespace DSWeb.Areas.SysMng.DAL.DBTableConn
{
public partial class DBTableConnDAL
{
static public List<SYS_TABLECONNMC> GetDataList(string strCondition, string sort) //
{
var strSql = new StringBuilder();
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" where " + strCondition);
}
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by ID ");
}
return SetData(strSql);
}
public static SYS_TABLECONNMC GetData( string condition)
{
SYS_TABLECONNMC head = new SYS_TABLECONNMC();
var _t = "";
var dataList = DBTableConnDAL.GetDataList(condition, _t);//, fieldlist
if (dataList.Count > 0)
head = dataList[0];
return head;
}
private static List<SYS_TABLECONNMC> SetData(StringBuilder strSql)//, List<HcUtility.Core.Fieldmb> fieldlist
{//扩展数据list的测试
var headList = new List<SYS_TABLECONNMC>();
Database db = DatabaseFactory.CreateDatabase();
//Database db = DatabaseFactory.CreateDatabase("连接其他数据库使用其他数据库名字");
string sql = SYS_TABLECONNMC.getSQL() + strSql;
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
SYS_TABLECONNMC data = new SYS_TABLECONNMC();
data.SetExtendValue(reader);
headList.Add(data);
}
reader.Close();
}
return headList;
}
static public List<SYS_TABLECONN_FIELDMC> GetBodyList(string strCondition, string sort="") //
{
var strSql = new StringBuilder();
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" where " + strCondition);
}
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by ID ");
}
return SetBodyData(strSql);
}
private static List<SYS_TABLECONN_FIELDMC> SetBodyData(StringBuilder strSql)//, List<HcUtility.Core.Fieldmb> fieldlist
{//扩展数据list的测试
var headList = new List<SYS_TABLECONN_FIELDMC>();
Database db = DatabaseFactory.CreateDatabase();
//Database db = DatabaseFactory.CreateDatabase("连接其他数据库使用其他数据库名字");
string sql = SYS_TABLECONN_FIELDMC.getSQL() + strSql;
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
SYS_TABLECONN_FIELDMC data = new SYS_TABLECONN_FIELDMC();
data.SetExtendValue(reader);
headList.Add(data);
}
reader.Close();
}
return headList;
}
private static Database getdb(string DBNAME) {
if (!string.IsNullOrEmpty(DBNAME))
{
Database db = DatabaseFactory.CreateDatabase(DBNAME);
return db;
}
else
{
Database db = DatabaseFactory.CreateDatabase();
return db;
}
}
static public List<BaseTableMC> BaseGetList(BaseTableMC Obj,string condition) //
{
var strSql = new StringBuilder();
strSql.Append(Obj.getSQL());
if (!string.IsNullOrEmpty(condition))
{
strSql.Append(" where " + condition);
}
var headList = new List<BaseTableMC>();
var db = getdb(Obj.DBNAME);
//Database db = DatabaseFactory.CreateDatabase("连接其他数据库使用其他数据库名字");
string sql = strSql.ToString();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
BaseTableMC data = new BaseTableMC(Obj);
data.SetExtendValue(reader);
headList.Add(data);
}
reader.Close();
}
return headList;
}
/// <summary>
/// 参数为
/// </summary>
/// <param name="Obj">包括dbname和sql已定义的【来源数据对象】</param>
/// <param name="CONNNAME">连接名称</param>
/// <param name="condition">来源表查询条件</param>
/// <returns></returns>
public static List<BaseTableMC> getToList(BaseTableMC Obj, string CONNNAME, string condition,string SQLStr="") {
var result = new List<BaseTableMC>();
var bodyList = new List<SYS_TABLECONN_FIELDMC>();
var _fromsql = getFromdbSql(CONNNAME,out bodyList);
if (!string.IsNullOrWhiteSpace(SQLStr)) {
_fromsql = SQLStr;
}
Obj.setSQL(_fromsql);
var fromlist = BaseGetList(Obj, condition);
if (fromlist.Count == 0)
{
return result;
}
else {
foreach (var from in fromlist) {
BaseTableMC todata = from.gettodata(bodyList);
result.Add(todata);
}
}
return result;
}
static public void DoSync_void(string data) {
var r=DoSync(data);
}
static public bool DoSync(string data)
{
Database db = DatabaseFactory.CreateDatabase();
var head = new SYS_TABLECONNMC();
head.SetValue(db, data);
return DoSync(head);
}
static public bool DoSync(SYS_TABLECONNMC head)
{
var modb = new ModelObjectRepository();
var savedb = new ModelObjectRepository();
Database savedbf = DatabaseFactory.CreateDatabase();
var todb = head.GetValue("TODB");
if (!string.IsNullOrEmpty(todb)) {
savedb = new ModelObjectRepository(todb);
savedbf = DatabaseFactory.CreateDatabase(todb);
}
//Database db = DatabaseFactory.CreateDatabase(head.GetValue("FROMDB"));
var fromdb = head.GetValue("FROMDB");
Database Portdb = DatabaseFactory.CreateDatabase();
if (!string.IsNullOrEmpty(fromdb))
Portdb = DatabaseFactory.CreateDatabase(fromdb);
BasicDataRefDAL.SaveLog("业务开始", 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, ""));
var _head = templist[0];
head.SetValue("TIMEMARK", _head.GetValue("TIMEMARK"));
#endregion
var FieldList = DBTableConnDAL.GetBodyList("CONNID=" + head.GetValue("ID"), "");
var FieldDicList = ModelObjectConvert<SYS_TABLECONN_FIELDMC>.ToExtendList(FieldList);
var _fromsql = getFromdbSql(FieldDicList, head);
var _tosql = getTodbSql(FieldDicList, head);
BaseTableMC FromTableObj = new BaseTableMC();
BaseTableMC SaveObj = new BaseTableMC();
//获取来源表对象列表
var from_condition = "";
var to_condition = "";
if (SYNCTYPE == "单号")
{
from_condition = string.Format(" {0} in({1}) ", head.GetValue("FROMTIMEMARK"), head.GetValue("CONDITION"));
to_condition = string.Format(" {0} in({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(savedbf);
//SaveObj.GetTableField(savedbf);
var HaveList = DBTableConnDAL.BaseGetList(SaveObj, to_condition);//首先取得已有数据
var SaveList = new List<BaseTableMC>();
var UPDList = new List<BaseTableMC>();
var havenumber = -1;
BasicDataRefDAL.SaveLog(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 false;
}
}
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";
if (needupd(head, fromObj, toObj,FieldDicList))
{
foreach (var field in FieldDicList)
{
toObj.SetPropertyValue(field["TOFIELDNAME"], fromObj.GetValue(field["FROMFIELDNAME"]));
}
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";
if (needupd(head, fromObj, toObj,FieldDicList))
{
foreach (var field in FieldDicList)
{
toObj.SetPropertyValue(field["TOFIELDNAME"], fromObj.GetValue(field["FROMFIELDNAME"]));
}
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(string.Format("比对循环结束,需储存{0}条,升级{1}条", SaveList.Count(), UPDList.Count()), "", "表同步模块", "时间记录");
//保存目标表对象列表
DBResult result = new DBResult();
var DoSaveList = ModelHeadConvert<BaseTableMC>.ToModelObjectList(SaveList);
foreach (var saveobj in DoSaveList)
{
result = savedb.SaveHead(saveobj);
if (result.Success == false)
{
BasicDataRefDAL.SaveLog("保存错误:" + result.Message, saveobj.ToString(), "表同步模块", "时间记录");
}
}
BasicDataRefDAL.SaveLog("储存" + result.Success.ToString() + ",储存" + DoSaveList.Count.ToString() + "条;" + result.Message, "", "表同步模块", "时间记录");
var DoUPDList = ModelHeadConvert<BaseTableMC>.ToModelObjectList(UPDList);
foreach (var saveobj in DoUPDList)
{
result = savedb.SaveHead(saveobj);
if (result.Success == false)
{
BasicDataRefDAL.SaveLog("更新错误:" + result.Message, saveobj.ToString(), "表同步模块", "时间记录");
}
}
result = modb.SaveHeadList_new(DoUPDList);
BasicDataRefDAL.SaveLog("更新" + 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<SYS_TABLECONNMC>.ToExtendList((DBTableConnDAL.GetDataList(condition, "")));
//var json = JsonConvert.Serialize(new { Success = result.Success, Message = result.Message, Data = _list[0] });
return true; //new ContentResult() { Content = json };
}
private static bool needupd(SYS_TABLECONNMC head, BaseTableMC fromObj, BaseTableMC toObj, List<Dictionary<string, string>> FieldDicList)
{
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 checkfields(FieldDicList, fromObj, toObj) == true;
}
else
{
return false;
}
}
return true;
}
private static bool checkfields(List<Dictionary<string, string>> FieldDicList, BaseTableMC fromObj, BaseTableMC toObj)
{
var result = false;
foreach (var field in FieldDicList)
{
if (toObj.GetValue(field["TOFIELDNAME"]) != fromObj.GetValue(field["FROMFIELDNAME"])) {
result = true;
break;
}
}
return result;
}
public static string getFromdbSql(List<Dictionary<string, string>> FieldDicList, SYS_TABLECONNMC head) {
var _fromsql = " select ";
foreach (var dic in FieldDicList)
{
if (_fromsql != " select " && !string.IsNullOrEmpty(dic["FROMFIELDNAME"]))
{
_fromsql += ",";
}
_fromsql += dic["FROMFIELDNAME"];
}
_fromsql += " from " + head.GetValue("FROMTABLE");
return _fromsql;
}
public static string getTodbSql(List<Dictionary<string, string>> FieldDicList, SYS_TABLECONNMC head)
{
var _tosql = " select ";
foreach (var dic in FieldDicList)
{
if (_tosql != " select " && !string.IsNullOrEmpty(dic["TOFIELDNAME"]))
{
_tosql += ",";
}
_tosql += dic["TOFIELDNAME"];
}
_tosql += " from " + head.GetValue("TOTABLE");
return _tosql;
}
public static string getFromdbSql(string CONNNAME,out List<SYS_TABLECONN_FIELDMC> bodyList) {
var result = "";
var connhead = GetData("NAME='" + CONNNAME + "'");
bodyList = new List<SYS_TABLECONN_FIELDMC>();
var CONNID = connhead.GetValue("ID");
if (CONNID == "") { return result; }
else
{
bodyList = GetBodyList("CONNID=" + CONNID);
var FieldDicList = ModelObjectConvert<SYS_TABLECONN_FIELDMC>.ToExtendList(bodyList);
result = getFromdbSql(FieldDicList, connhead);
}
return result;
}
public static List<SYS_TABLECONN_FIELDMC> getConn(string CONNNAME) {
var connhead = GetData("NAME='" + CONNNAME + "'");
var result = new List<SYS_TABLECONN_FIELDMC>();
var CONNID = connhead.GetValue("ID");
if (CONNID == "") { return result; }
else
{
result = GetBodyList("CONNID=" + CONNID);
}
return result;
}
}
}