1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| using System; using System.Collections.Generic; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using RekTec.Crm.Common.Helper; using RekTec.Crm.DataManagement; using System.Data;
namespace RekTec.Crm.Workflow.Import { public class ImportInsuranceprice : ImportBase { protected override void Import(DataTable dt, out IList<string> errors) { errors = new List<string>(); if (dt == null || dt.Rows.Count == 0) { return; } var successcount = 0; var errorcount = 0;
for (var i = 0; i < dt.Rows.Count; i++) { DataRow dr = dt.Rows[i]; try { #region 校验输入数据 if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["物料名称"]))) { throw new Exception("物料名称不能为空!"); } var new_product_id = Cast.ConToString(dr["物料名称"]);
if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["物料编码"]))) { throw new Exception("物料编码不能为空!"); } var new_materialcode = Cast.ConToString(dr["物料编码"]);
if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["付款类型"]))) { throw new Exception("付款类型不能为空"); } var new_paymenttype = Cast.ConToString(dr["付款类型"]);
if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["首付比例(%)"]))) { throw new Exception("首付比例(%)不能为空!"); } var new_downpaymentsrat = Cast.ConToDecimal(dr["首付比例(%)"]);
if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["基准价格"]))) { throw new Exception("基准价格不能为空!"); } var new_standardprice = Cast.ConToDecimal(dr["基准价格"]);
if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["所属组织"]))) { throw new Exception("所属组织不能为空!"); } var new_organisation_id = Cast.ConToString(dr["所属组织"]); #endregion #region 查询对应的数据是否存在 var query = new QueryExpression("new_vehiclebenchmark"); query.ColumnSet = new ColumnSet(new string[] { "new_standardprice" }); query.Criteria.AddCondition("new_materialcode", ConditionOperator.Equal, new_materialcode); query.Criteria.AddCondition("new_downpaymentsrat", ConditionOperator.Equal, new_downpaymentsrat); var result = OrganizationServiceAdmin.RetrieveMultiple(query); #endregion if (result != null && result.Entities != null && result.Entities.Count > 0) { throw new Exception("已存在重复数据,不允许导入"); } #region 如果不存在 则新建 var createEntity = new Entity("new_vehiclebenchmark"); createEntity["new_product_id"] = GetEntityReference("product", "name", new_product_id, i, "物料名称"); createEntity["new_materialcode"] = new_materialcode; var new_paymenttypes = GetOptions(OrganizationServiceAdmin, "new_vehiclebenchmark", "new_paymenttype"); createEntity["new_paymenttype"] = new OptionSetValue(new_paymenttypes[new_paymenttype]); createEntity["new_downpaymentsrat"] = new_downpaymentsrat; createEntity["new_standardprice"] = new_standardprice; createEntity["new_organisation_id"] = GetEntityReference("businessunit", "name", new_organisation_id, i, "所属组织"); OrganizationService.Create(createEntity); #endregion
successcount++;
} catch (Exception ex) { errors.Add($"第{i + 1}行导入失败,原因:" + ex.Message + "\n"); errorcount++; continue; } }
}
public EntityReference GetEntityReference(string entityname, string field, string value, int i, string text) { QueryExpression qe = new QueryExpression(entityname); qe.ColumnSet.AddColumn(entityname + "id"); qe.Criteria.AddCondition(field, ConditionOperator.Equal, value); if (entityname.ToLower() != "businessunit" && entityname.ToLower() != "systemuser") { qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0); } EntityCollection ec = OrganizationService.RetrieveMultiple(qe); if (ec == null || ec.Entities.Count < 1) throw new Exception($"第{i + 1}行创建失败,原因:本行[" + text + "]字段未能查询到对应的信息!\n");
return new EntityReference(entityname, (Guid)ec.Entities[0].Attributes[entityname + "id"]); } private Dictionary<string, int> GetOptions(IOrganizationService organizationService, string entityName, string attributeName) { Dictionary<string, int> dictionary = new Dictionary<string, int>(); QueryExpression queryExpression = new QueryExpression { EntityName = "stringmap" }; queryExpression.ColumnSet.AddColumns(new string[] { "value", "attributevalue" }); queryExpression.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, new object[] { entityName }); queryExpression.Criteria.AddCondition("attributename", ConditionOperator.Equal, new object[] { attributeName });
queryExpression.AddOrder("attributevalue", OrderType.Ascending); EntityCollection entityCollection = organizationService.RetrieveMultiple(queryExpression); if (entityCollection != null && entityCollection.Entities != null && entityCollection.Entities.Count > 0) { foreach (Entity expr_10D in entityCollection.Entities) { int attributeValue = expr_10D.GetAttributeValue<int>("attributevalue"); string attributeValue2 = expr_10D.GetAttributeValue<string>("value"); dictionary.Add(attributeValue2, attributeValue); } } return dictionary; } } }
|