public object Convert(object data, ConvertType convertType) { object value = null; switch (type) { case "String": value = data.ToStr(); break; case "Number": value = data.ToInt(); break; case "Double": value = data.ToDouble(); break; case "DateTime": try { value = DateTime.Parse(data.ToString()); } catch { return(null); } break; case "Files": FieldType.Files file = FieldType.Files.Parse(data.ToString()); if (convertType == ConvertType.UserData) { value = file; } else { if (file != null) { value = file.ToJson(); } else { value = ""; } } break; case "Pictures": FieldType.Pictures file2 = FieldType.Pictures.Parse(data.ToString()); if (convertType == ConvertType.UserData) { value = file2; } else { if (file2 != null) { value = file2.ToJson(); } else { value = ""; } } break; default: value = data; break; } return(value); }
/// <summary> /// 保存数据 /// </summary> /// <param name="model">数据模型</param> /// <returns></returns> public double Save(Dictionary <string, object> model) { if (TableName == "") { throw new Exception("表名不能为空"); } Dictionary <string, object> mainFields = new Dictionary <string, object>(); Dictionary <string, object> dataFields = new Dictionary <string, object>(); ColumnConfig config = null; if (model.ContainsKey("classId")) { config = GetConfig(model["classId"].ToDouble()); } foreach (var field in model) { try { Field f = Fields[field.Key]; object value = null; if (f.type == "Pictures") { FieldType.Pictures files = FieldType.Pictures.Parse(field.Value.ToStr()); for (int i = 0; i < files.Count; i++) { string kzm = ""; if (files[i].path.LastIndexOf(".") > -1) { kzm = files[i].path.Substring(files[i].path.LastIndexOf(".") + 1); } MWMS.Helper.Picture pic = new Helper.Picture(files[i].path); FileInfo _newfile = pic.PictureSize(new FileInfo(files[i].path.Replace("." + kzm, "_min." + kzm)), config.picWidth, config.picHeight, 100, config.picForce); string newfile = _newfile.FullName; if (newfile == "") { files[i].minPath = files[i].path; } else { files[i].minPath = newfile; } } value = files.ToJson(); } else { value = f.Convert(field.Value, Field.ConvertType.SqlData); } if (value != null) { if (f.isPublicField) { mainFields[field.Key] = value; } else { dataFields[field.Key] = value; } } } catch { } } if (mainFields.ContainsKey("id")) { dataFields["id"] = mainFields["id"]; } StringBuilder fieldstr = new StringBuilder(); MWMS.DAL.TableHandle t = new MWMS.DAL.TableHandle("maintable"); MWMS.DAL.TableHandle t1 = new MWMS.DAL.TableHandle(TableName); double id = 0; MWMS.DAL.TableHandle column = new MWMS.DAL.TableHandle("class"); if (mainFields.ContainsKey("classId")) { Dictionary <string, object> columnModel = column.GetModel((double)mainFields["classId"], "rootId,moduleId"); mainFields["rootId"] = columnModel["rootId"]; mainFields["moduleId"] = columnModel["moduleId"]; } mainFields["datatypeId"] = DatatypeId; if (mainFields.ContainsKey("id") && mainFields["id"].ToDouble() > 0) { mainFields["updateDate"] = DateTime.Now; t.Update(mainFields); id = t1.Update(dataFields); } else { id = double.Parse(Helper.Tools.GetId()); mainFields["id"] = id; mainFields["auditorid"] = 0; mainFields["updateDate"] = DateTime.Now; mainFields["createDate"] = DateTime.Now; dataFields["id"] = id; t.Append(mainFields); id = t1.Append(dataFields); } if (mainFields.ContainsKey("classId")) { ReplaceUrl(mainFields["classId"].ToDouble(), mainFields["id"].ToDouble()); } return(id); }