public Guid Create(Entity entity) { StringBuilder sql = new StringBuilder(); entity = UnwrapAttributeValue(entity); sql.AppendFormat("INSERT INTO [{0}]", entity.Name); sql.Append(" (" + string.Join(",", entity.Keys.Select(x => "[" + x + "]")) + ")"); sql.Append(" VALUES("); List <string> parameters = new List <string>(); int i = 0; foreach (var k in entity.Keys) { parameters.Add("@" + i); i++; } sql.Append(string.Join(",", parameters)); sql.Append(")"); var flag = _repository.Execute(sql.ToString(), entity.Values.ToArray()) > 0; if (!flag) { return(Guid.Empty); } return(entity.Id); }
public bool UpdateControlMap(EntityMap entityMap, IEnumerable <AttributeMap> attributeMaps, Guid sourceRecordId, Schema.Domain.Attribute refAttribute, IEnumerable <Schema.Domain.Attribute> sourceAttributeMeta, IEnumerable <Schema.Domain.Attribute> targetAttributeMeta, bool onDelete = false) { Sql s = Sql.Builder.Append("UPDATE [" + entityMap.SourceEnttiyName + "] SET "); foreach (var ca in attributeMaps) { //查找所有目标单据,钩稽字段的总和 s.Append(string.Format("[{0}]=(SELECT SUM([{1}]) AS SumValue FROM [{2}] WHERE [{3}] = '{4}')" , ca.RemainAttributeName, ca.TargetAttributeName, entityMap.TargetEnttiyName, refAttribute.Name, sourceRecordId)); s.Append(string.Format(",[{0}]=(CASE WHEN ([{1}]-(SELECT SUM([{2}]) AS SumValue FROM [{3}] WHERE [{4}] = '{5}'))<=0 THEN 1 ELSE 0 END)" , ca.ClosedAttributeName, ca.SourceAttributeName, ca.TargetAttributeName, entityMap.TargetEnttiyName, refAttribute.Name, sourceRecordId)); if (!onDelete && entityMap.MapType == MapType.ForceControl) { var data = _repository.ExecuteQuery(string.Format("SELECT 1 FROM [{0}] a LEFT JOIN (SELECT SUM([{1}]) AS SumValue,[{3}] FROM [{2}] WHERE [{3}] = '{4}' GROUP BY [{3}]) b ON a.{0}id=b.[{3}] WHERE a.{0}id='{4}' AND b.SumValue>a.[{5}]" , entityMap.SourceEnttiyName, ca.TargetAttributeName, entityMap.TargetEnttiyName, refAttribute.Name, sourceRecordId, ca.SourceAttributeName)); if (data.NotEmpty()) { var attr = sourceAttributeMeta.First(n => n.AttributeId.Equals(ca.SourceAttributeId)); var attr2 = targetAttributeMeta.First(n => n.AttributeId.Equals(ca.TargetAttributeId)); _repository.RollBackTransaction(); throw new XmsException(string.Format("'{0}' " + _loc["entitymap_greaterthan_error"] + " '{1}'", attr2.LocalizedName, attr.LocalizedName)); } } } s.Append(" WHERE " + entityMap.SourceEnttiyName + "id='" + sourceRecordId + "'"); _repository.Execute(s); return(true); }