MaterialUpCict.cs
7.29 KB
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
using Hh.Mes.Common.config;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Newtonsoft.Json;
using NPOI.POIFS.FileSystem;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Hh.Mes.Service.ApiService
{
public partial class UpstreamService
{
/// <summary>
/// 信科物料基础信息 同步
/// </summary>
/// <param name=""></param>
/// <returns></returns>
public dynamic MaterialUpCict()
{
var response = new Response();
return ExceptionsHelp.Instance.ExecuteT<Response>(() =>
{
var urlDic = "UrlMaterialCict" + ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend1;//AppCustomExtend1:"_onLine/_off"
var url = GetDictionaryDictValue(urlDic, "GetUrl");
response = HttpMan(url, null, EnumLog.Cict物料信息同步.ToString(), method: "get");
if (response.Code == 200)
{
//do someing
var cictResult = JsonConvert.DeserializeObject<MaterialUpCictEntity>(response.Result);
Response res = HandleData(cictResult.data.list);
if (res.Code != 200)
{
return response.ResponseError($"【信科】物料基础信息有错误:{response.Message}");
}
response.Message = SystemVariable.dataActionOk + $"</br>在{DateTime.Now}收到【信科】物料基础信息数据共{cictResult.data.total}条,成功同步{res.Count}条";
response.Result = null;
response.Count = res.Count;
return response;
}
//Log4NetHelper.Instance.Error($"【返回结果】" + response.Result);
response.Result = null;
return response.ResponseError($"【信科】物料基础信息同步错误,原因:{response.Message}");
});
}
/// <summary>
/// 同步物料逻辑 存在就跳过,不存新增
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private Response HandleData(List<ListItem> list)
{
var response = new Response();
return ExceptionsHelp.Instance.ExecuteT(() =>
{
int updatedCount = 0;
foreach (var model in list)
{
var isExist = Context.Queryable<base_material>().Any(i => i.materialCode == model.sapCode);
if (!isExist)
{
var unit = Context.Queryable<base_material_unit>().Any(i => i.code == model.materialUnit);//存在单位名称,要取表里code值,后续展示用,展示区分大小写
var unitCode = unit ? Context.Queryable<base_material_unit>().Where(i => i.code == model.materialUnit).First().code : model.materialUnit;
var keys = Guid.NewGuid();
var material = new base_material
{
keys = keys,
materialCode = model.sapCode,
materialName = model.materialName,
unitCode = unitCode,
mtClassify = model.categoryCode,
mtTypeCode = model.materialType.ToString(),
specifications = model.spec,
createBy = model.createByName,
createTime = DateTime.Now,
};
var rate = new base_material_rate
{
materialCode = model.sapCode,
materialName = model.materialName,
mtClassify = model.categoryCode,
unitCode = unitCode,
specifications = model.spec,
materialKeys = keys,
quantityRate = 1,
createBy = model.createByName,
createTime = DateTime.Now,
};
var bmu = new base_material_unit();//物料单位(不存在的单位新增,统一大写)
var records = Context.Queryable<base_material_unit>().Where(t => true).ToList();
if (!records.Any(t => t.code.ToUpper() == model.materialUnit.ToUpper()))
{
bmu.code = model.materialUnit;
bmu.name = "unit_" + model.materialUnit;
bmu.createBy = model.createByName;
bmu.createTime = DateTime.Now;
Context.Insertable(bmu).AddQueue();
}
var bmt = new base_material_type();//物料类别
var bootKeys = Context.Queryable<base_material_type>().Where(i => i.mtTypeCode == "boot").First().keys;//根节点key
if (!Context.Queryable<base_material_type>().Any(i => i.mtTypeCode == model.materialType.ToString()))
{
bmt.keys = Guid.NewGuid();
bmt.mtTypeCode = model.materialType.ToString();
bmt.mtTypeName = "material_" + model.materialType.ToString();
bmt.parentTypeId = bootKeys;
bmt.isDelete = 1;
bmt.createBy = model.createByName;
bmt.createTime = DateTime.Now;
Context.Insertable(bmt).AddQueue();
}
var classify = new sys_dict_data();//物料分类
if (!Context.Queryable<sys_dict_data>().Any(i => i.dictValue == model.categoryCode && i.dictType == "mtClassify"))
{
classify.headerId = 2199;
classify.dictType = "mtClassify";
classify.dictValue = model.categoryCode;
classify.dictLabel = "label_" + model.categoryCode;
classify.createBy = model.createByName;
classify.createTime = DateTime.Now;
Context.Insertable(classify).AddQueue();
}
Context.Insertable(material).AddQueue();
Context.Insertable(rate).AddQueue();
Context.SaveQueues();
updatedCount++;
}
response.Count = updatedCount;
//else
//{
// response.Status = Context.Updateable(model).Where(u => u.id == model.id).ExecuteCommand() > 0;
// if (!response.Status) response.Message = SystemVariable.dataActionError;
//}
}
return response;
});
}
}
}