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 bodylist, List bodylistDel) { return Save(headdata, bodylist, bodylistDel, null, null); } public DBResult Save(ModelObjectBillHead headdata, List bodylist1, List bodylistDel1 , List bodylist2, List 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 bodylist1, List bodylistDel1 , List bodylist2, List bodylistDel2, List bodylist3, List 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 bodylist1, List bodylistDel1, List bodylist2, List bodylistDel2, List bodylist3, List bodylistDel3, List bodylist4, List 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 bodylist1, List bodylistDel1, List bodylist2, List bodylistDel2, List bodylist3, List bodylistDel3, List bodylist4, List bodylistDel4, List bodylist5, List 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 list = new List(); list.Add(data); return Save(list); } public DBResult SaveHeadList ( List 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 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 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 dataList) { var dbModel = new ModelObjectDB(); foreach (ModelObjectBase mob in dataList) { HanderDataCookie(mob); } DealList(dataList, null); return dbModel.Save(dataList); } public DBResult SaveComm(List dataList, List 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 dataList, List 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 dataList, List dataDelList , List dataList2, List 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 dataList, List dataDelList, List dataList2, List 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 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 bodylist, List bodylistDel) { List bodyListAll = new List(); //更加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 bodylist, List bodylistDel) { //List bodyListAll = new List(); //更加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 bodylist, List bodylistDel) { //List bodyListAll = new List(); //更加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 bodylist, List bodylistDel) { //List bodyListAll = new List(); //更加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 where T : ModelObjectBillHead { private static ModelObjectBillHead Converter ( T inputObject ) { return inputObject; } public static List ToModelObjectList ( List objList ) { if (objList == null) return null; return objList.ConvertAll(new Converter(Converter)); } } }