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.

605 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.Web;
using DSWeb.TruckMng.Comm.Cookie;
using HcUtility.Comm;
using HcUtility.Core;
using System.Collections.Generic;
namespace DSWeb.TruckMng.Helper.Repository
{
/// wuwei 2011-01-25 CMP-8856 中石化:webpos保存数据报错导致数据保存重复的问题
public class ModelObjectRepository
{
private string _dbname = "";
public ModelObjectRepository(string dbname)
{
_dbname = dbname;
}
public ModelObjectRepository()
{
}
public HttpRequestBase Request { get; set; }
public DBResult Save(ModelObjectBillHead headdata, List<ModelObjectBase> bodylist, List<ModelObjectBase> bodylistDel)
{
return Save(headdata, bodylist, bodylistDel, null, null);
}
public DBResult Save(ModelObjectBillHead headdata, List<ModelObjectBase> bodylist1, List<ModelObjectBase> bodylistDel1
, List<ModelObjectBase> bodylist2, List<ModelObjectBase> bodylistDel2)
{
DBResult result = null;
try
{
result = HandleHeadData(headdata);
if (!result.Success)
return result;
AddBodyToHead(headdata, bodylist1, bodylistDel1);
AddBodyToHead(headdata, bodylist2, bodylistDel2);
result = Save(headdata);
return result;
}
catch (Exception e)
{
if(result==null)
result=new DBResult();
result.Success = false;
result.Message = e.Message;
return result;
}
}
public DBResult Save(ModelObjectBillHead headdata, List<ModelObjectBase> bodylist1, List<ModelObjectBase> bodylistDel1
, List<ModelObjectBase> bodylist2, List<ModelObjectBase> bodylistDel2, List<ModelObjectBase> bodylist3, List<ModelObjectBase> bodylistDel3)
{
DBResult result = null;
try
{
result = HandleHeadData(headdata);
if (!result.Success)
return result;
AddBodyToHead(headdata, bodylist1, bodylistDel1);
AddBodyToHead(headdata, bodylist2, bodylistDel2);
AddBodyToHead(headdata, bodylist3, bodylistDel3);
result = Save(headdata);
return result;
}
catch (Exception e)
{
if (result == null)
result = new DBResult();
result.Success = false;
result.Message = e.Message;
return result;
}
}
//4个子表
public DBResult Save(ModelObjectBillHead headdata,
List<ModelObjectBase> bodylist1, List<ModelObjectBase> bodylistDel1,
List<ModelObjectBase> bodylist2, List<ModelObjectBase> bodylistDel2,
List<ModelObjectBase> bodylist3, List<ModelObjectBase> bodylistDel3,
List<ModelObjectBase> bodylist4, List<ModelObjectBase> bodylistDel4
)
{
DBResult result = null;
try
{
result = HandleHeadData(headdata);
if (!result.Success)
return result;
AddBodyToHead(headdata, bodylist1, bodylistDel1);
AddBodyToHead(headdata, bodylist2, bodylistDel2);
AddBodyToHead(headdata, bodylist3, bodylistDel3);
AddBodyToHead(headdata, bodylist4, bodylistDel4);
result = Save(headdata);
return result;
}
catch (Exception e)
{
if (result == null)
result = new DBResult();
result.Success = false;
result.Message = e.Message;
return result;
}
}
//5
public DBResult Save(ModelObjectBillHead headdata,
List<ModelObjectBase> bodylist1, List<ModelObjectBase> bodylistDel1,
List<ModelObjectBase> bodylist2, List<ModelObjectBase> bodylistDel2,
List<ModelObjectBase> bodylist3, List<ModelObjectBase> bodylistDel3,
List<ModelObjectBase> bodylist4, List<ModelObjectBase> bodylistDel4,
List<ModelObjectBase> bodylist5, List<ModelObjectBase> bodylistDel5
)
{
DBResult result = null;
try
{
result = HandleHeadData(headdata);
if (!result.Success)
return result;
AddBodyToHead(headdata, bodylist1, bodylistDel1);
AddBodyToHead(headdata, bodylist2, bodylistDel2);
AddBodyToHead(headdata, bodylist3, bodylistDel3);
AddBodyToHead(headdata, bodylist4, bodylistDel4);
AddBodyToHead(headdata, bodylist5, bodylistDel5);
result = Save(headdata);
return result;
}
catch (Exception e)
{
if (result == null)
result = new DBResult();
result.Success = false;
result.Message = e.Message;
return result;
}
}
public DBResult SaveHead ( ModelObjectBillHead data )
{
var result = Save(data);
return result;
}
public DBResult Save(ModelObjectBase data)
{
HanderDataCookie(data);
List<ModelObjectBase> list = new List<ModelObjectBase>();
list.Add(data);
return Save(list);
}
public DBResult SaveHeadList ( List<ModelObjectBillHead> headdata )
{
DBResult result = new DBResult();
result.Success=true;
result.Message="";
foreach (var head in headdata)
{
try
{
head.DbOperationType = DbOperationType.DbotUpd;
head.ModelUIStatus = "E";
var result2 = HandleHeadData(head);
if (!result2.Success)
return result;
var result3 = Save(head);
if (result3.Success == false) {
result.Success = false;
if (result.Message== "")
result.Message = head.GetBillNoValue().ToString() + result3.Message;
else
result.Message = result.Message + "//" + head.GetBillNoValue().ToString() + result3.Message;
}
}
catch (Exception e)
{
result.Success = false;
result.Message = e.Message;
return result;
}
}
return result;
}
public DBResult SaveHeadList_new(List<ModelObjectBillHead> headdata)
{
DBResult result = new DBResult();
result.Success = true;
result.Message = "";
foreach (var head in headdata)
{
try
{
var PKEYValue = head.GetBillNoValue().ToString();
var result2 = HandleHeadData(head);
if (!result2.Success)
return result;
var result3 = Save(head);
if (result3.Success == false)
{
result.Success = false;
if (result.Message == "")
result.Message = PKEYValue + result3.Message;
else
result.Message = result.Message + "//" + head.GetBillNoValue().ToString() + result3.Message;
}
}
catch (Exception e)
{
result.Success = false;
result.Message = e.Message;
return result;
}
}
return result;
}
//单独使用时主要用于cwaudit界面对ch_fee内amount的修改。只有修改不添加删除
public DBResult Save(List<ModelObjectBase> dataList)
{
ModelObjectDBBill dbBill = new ModelObjectDBBill();
foreach (ModelObjectBase mob in dataList)
{
HanderDataCookie(mob);
}
if (!string.IsNullOrEmpty(_dbname))
{
return dbBill.Save(dataList, _dbname);
}
return dbBill.Save(dataList);
}
public DBResult SaveComm(List<ModelObjectBase> dataList)
{
var dbModel = new ModelObjectDB();
foreach (ModelObjectBase mob in dataList)
{
HanderDataCookie(mob);
}
DealList(dataList, null);
return dbModel.Save(dataList);
}
public DBResult SaveComm(List<ModelObjectBase> dataList, List<ModelObjectBase> dataDelList)
{
var dbModel = new ModelObjectDB();
if (dataList!=null)
foreach (ModelObjectBase mob in dataList)
{
HanderDataCookie(mob);
}
if (dataDelList != null)
foreach (ModelObjectBase mob in dataDelList)
{
HanderDataCookie(mob);
}
DealList(dataList, dataDelList);
var _r = new DBResult();
if (dataList != null)
{
_r = dbModel.Save(dataList);
}
else { _r = dbModel.Save(dataDelList); }
return _r;
}
public DBResult SaveComm(string BSNONAME, string BSNO, List<ModelObjectBase> dataList, List<ModelObjectBase> dataDelList)
{
var dbModel = new ModelObjectDB();
if (dataList != null)
foreach (ModelObjectBase mob in dataList)
{
HanderDataCookie(mob);
}
if (dataDelList != null)
foreach (ModelObjectBase mob in dataDelList)
{
HanderDataCookie(mob);
}
DealList(BSNONAME, BSNO,dataList, dataDelList);
var _r = new DBResult();
if (dataList != null)
{
_r = dbModel.Save(dataList);
}
else { _r = dbModel.Save(dataDelList); }
return _r;
}
public DBResult SaveComm(string BSNONAME, string BSNO, List<ModelObjectBase> dataList, List<ModelObjectBase> dataDelList
, List<ModelObjectBase> dataList2, List<ModelObjectBase> dataDelList2)
{
var dbModel = new ModelObjectDB();
if (dataList != null)
foreach (ModelObjectBase mob in dataList)
{
HanderDataCookie(mob);
}
if (dataDelList != null)
foreach (ModelObjectBase mob in dataDelList)
{
HanderDataCookie(mob);
}
DealList(BSNONAME, BSNO, dataList, dataDelList);
if (dataList2 != null)
foreach (ModelObjectBase mob in dataList2)
{
HanderDataCookie(mob);
}
if (dataDelList2 != null)
foreach (ModelObjectBase mob in dataDelList2)
{
HanderDataCookie(mob);
}
DealList(BSNONAME, BSNO, dataList2, dataDelList2);
var _r = new DBResult();
if (dataList != null)
{ _r = dbModel.Save(dataList); }
if (dataDelList != null)
{ _r = dbModel.Save(dataDelList); }
if (dataList2 != null)
{ _r = dbModel.Save(dataList2); }
if (dataDelList2 != null)
{ _r = dbModel.Save(dataDelList2); }
return _r;
}
public DBResult SaveComm(List<ModelObjectBase> dataList, List<ModelObjectBase> dataDelList,
List<ModelObjectBase> dataList2, List<ModelObjectBase> dataDelList2)
{
var dbModel = new ModelObjectDB();
if (dataList != null)
foreach (ModelObjectBase mob in dataList)
{
HanderDataCookie(mob);
}
if (dataDelList != null)
foreach (ModelObjectBase mob in dataDelList)
{
HanderDataCookie(mob);
}
DealList(dataList, dataDelList);
if (dataList2 != null)
foreach (ModelObjectBase mob in dataList2)
{
HanderDataCookie(mob);
}
if (dataDelList2 != null)
foreach (ModelObjectBase mob in dataDelList2)
{
HanderDataCookie(mob);
}
DealList(dataList2, dataDelList2);
var _r = new DBResult();
if (dataList != null)
{ _r = dbModel.Save(dataList); }
else if (dataDelList != null)
{ _r = dbModel.Save(dataDelList); }
else if (dataList2 != null)
{ _r = dbModel.Save(dataList2); }
else
{ _r = dbModel.Save(dataDelList2); }
return _r;
}
public DBResult SaveHead ( List<ModelObjectBase> dataList )
{
var dbModel = new ModelObjectDB();
foreach (ModelObjectBase mob in dataList)
{
HanderDataCookie(mob);
}
//DealHeadList(dataList,null);
return dbModel.Save(dataList);
}
#region Private Function
private void HanderDataCookie(ModelObjectBase data)
{
if (Request != null)
{
if (data is ModelObjectBillHead)
{
ModelObjectBillHead headdata = (ModelObjectBillHead)data;
// headdata.LoginUserID = CookieConfig.GetCookie_UserId(Request);
headdata.LoginUserCode = CookieConfig.GetCookie_UserCode(Request);
headdata.LoginUserName = CookieConfig.GetCookie_UserName(Request);
}
}
}
private DBResult HandleHeadData(ModelObjectBillHead headdata)
{
var result = new DBResult();
string headBillno = headdata.GetBillNoValue().ToString();
if(String.IsNullOrEmpty(headBillno)||headBillno=="*")
{
result.Success = false;
result.Message = "单据没有相应的单据号,不允许保存";
return result;
}
if (headdata.ModelUIStatus == "0")
{
result.Success = false;
result.Message = "数据为浏览状态不允许保存!";
return result;
}
if(headdata.ModelUIStatus=="I")
{
headdata.DbOperationType = DbOperationType.DbotIns;
}
else if(headdata.ModelUIStatus=="E")
{
headdata.DbOperationType = DbOperationType.DbotUpd;
}
else
{
headdata.DbOperationType = DbOperationType.DbotDel;
}
result.Success = true;
result.Message = "处理主表数据成功";
return result;
}
private void AddBodyToHead(ModelObjectBillHead headdata, List<ModelObjectBase> bodylist, List<ModelObjectBase> bodylistDel)
{
List<ModelObjectBase> bodyListAll = new List<ModelObjectBase>();
//更加billno是否为空置更新或新增标志.
if (bodylist != null)
{
foreach (ModelObjectBillBody modbody in bodylist)
{
string bodyBillNo = modbody.GetBillNoValue().ToString();
if (bodyBillNo == String.Empty || bodyBillNo == "*")
modbody.DbOperationType = DbOperationType.DbotIns;
else
modbody.DbOperationType = DbOperationType.DbotUpd;
}
bodyListAll.AddRange(bodylist);
}
//置删除标志
if (bodylistDel != null)
{
foreach (ModelObjectBillBody modbodydel in bodylistDel)
{
modbodydel.DbOperationType = DbOperationType.DbotDel;
}
bodyListAll.AddRange(bodylistDel);
}
if (bodyListAll.Count > 0)
headdata.BodyList.Add(bodyListAll);
}
private void DealList(List<ModelObjectBase> bodylist, List<ModelObjectBase> bodylistDel)
{
//List<ModelObjectBase> bodyListAll = new List<ModelObjectBase>();
//更加billno是否为空置更新或新增标志.
if (bodylist != null)
{
foreach (ModelObjectBillBody modbody in bodylist)
{
string bodyBillNo = modbody.GetBillNoValue().ToString();
if (bodyBillNo == String.Empty || bodyBillNo == "*" || bodyBillNo == "")
modbody.DbOperationType = DbOperationType.DbotIns;
else
modbody.DbOperationType = DbOperationType.DbotUpd;
}
//bodyListAll.AddRange(bodylist);
}
//置删除标志
if (bodylistDel != null)
{
foreach (ModelObjectBillBody modbodydel in bodylistDel)
{
modbodydel.DbOperationType = DbOperationType.DbotDel;
}
//bodyListAll.AddRange(bodylistDel);
}
//if (bodylistDel != null || bodylist != null) {
// bodylist.AddRange(bodylistDel);
//}
}
private void DealList(String BSNONAME,String BSNO,List<ModelObjectBase> bodylist, List<ModelObjectBase> bodylistDel)
{
//List<ModelObjectBase> bodyListAll = new List<ModelObjectBase>();
//更加billno是否为空置更新或新增标志.
if (bodylist != null)
{
foreach (ModelObjectBillBody modbody in bodylist)
{
string bodyBillNo = modbody.GetBillNoValue().ToString();
if (bodyBillNo == String.Empty || bodyBillNo == "*" || bodyBillNo == "")
{
modbody.SetPropertyValue(BSNONAME, BSNO);
modbody.DbOperationType = DbOperationType.DbotIns;
}
else
modbody.DbOperationType = DbOperationType.DbotUpd;
}
//bodyListAll.AddRange(bodylist);
}
//置删除标志
if (bodylistDel != null)
{
foreach (ModelObjectBillBody modbodydel in bodylistDel)
{
modbodydel.DbOperationType = DbOperationType.DbotDel;
}
//bodyListAll.AddRange(bodylistDel);
}
//if (bodylistDel != null || bodylist != null) {
// bodylist.AddRange(bodylistDel);
//}
}
//20141030 增加对head类型数据的批量增删改 未测试
private void DealHeadList(List<ModelObjectBase> bodylist, List<ModelObjectBase> bodylistDel)
{
//List<ModelObjectBase> bodyListAll = new List<ModelObjectBase>();
//更加billno是否为空置更新或新增标志.
if (bodylist != null)
{
foreach (ModelObjectBillHead modbody in bodylist)
{
string State = modbody.ModelUIStatus;//.GetBillNoValue().ToString();
if (State == "I")
modbody.DbOperationType = DbOperationType.DbotIns;
else
modbody.DbOperationType = DbOperationType.DbotUpd;
}
//bodyListAll.AddRange(bodylist);
}
//置删除标志
if (bodylistDel != null)
{
foreach (ModelObjectBillBody modbodydel in bodylistDel)
{
modbodydel.DbOperationType = DbOperationType.DbotDel;
}
//bodyListAll.AddRange(bodylistDel);
}
//if (bodylistDel != null || bodylist != null) {
// bodylist.AddRange(bodylistDel);
//}
}
#endregion
}
public class ModelHeadConvert<T> where T : ModelObjectBillHead
{
private static ModelObjectBillHead Converter ( T inputObject )
{
return inputObject;
}
public static List<ModelObjectBillHead> ToModelObjectList ( List<T> objList )
{
if (objList == null)
return null;
return objList.ConvertAll<ModelObjectBillHead>(new Converter<T, ModelObjectBillHead>(Converter));
}
}
}