using System; using System.Web; using DSWeb.MvcShipping.Comm.Cookie; using HcUtility.Comm; using HcUtility.Core; using System.Collections.Generic; namespace DSWeb.MvcShipping.Helper.Repository { /// wuwei 2011-01-25 CMP-8856 中石化:webpos保存数据报错导致数据保存重复的问题 public class 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 Save(ModelObjectBase data) { HanderDataCookie(data); List list = new List(); list.Add(data); return Save(list); } public DBResult Save(List dataList) { ModelObjectDBBill dbBill = new ModelObjectDBBill(); foreach (ModelObjectBase mob in dataList) { HanderDataCookie(mob); } return dbBill.Save(dataList); } public DBResult SaveComm(List dataList) { var dbModel = new ModelObjectDB(); foreach (ModelObjectBase mob in dataList) { HanderDataCookie(mob); } 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); } #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)); } } }