WmsSnService.cs
5.34 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
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 Hh.Mes.POJO.WebEntity.bus;
using Hh.Mes.Service.Repository;
using System;
using System.Collections.Generic;
namespace Hh.Mes.Service
{
public class WmsSnService : RepositorySqlSugar<bus_sn_project_info_wms_head>
{
/// <summary>
/// Json 接受实体
/// </summary>
private WmsSnInfoEntity RequestData { get; set; }
/// <summary>
/// 新增标识 true 新增 反之删除
/// </summary>
public bool IsInsertType { get; set; }
/// <summary>
/// WMS-SN信息推送IOT
/// </summary>
public dynamic SendIoTDataBySN(WmsSnInfoEntity entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
RequestData = entity;
IsInsertType = entity.types == EnumAction.I.ToString();
var response = new ResponseWms();
// 类型验证
if (!typeValidation(entity.types, response).status) return response;
int resultCount = 0;
var validation = ExecuteQueuesBefore(entity, response);
if (!validation.status) return validation;
var listHead = new List<bus_sn_project_info_wms_head>();
var listDetail = new List<bus_sn_project_info_wms_detail>();
#region 赋值
foreach (var item in entity.result)
{
var head = CreateHead(item);
listHead.Add(head);
foreach (var components in item.components)
{
var detail = CreateDetail(head, item, components);
listDetail.Add(detail);
}
}
#endregion
// 执行插入或删除操作
if (IsInsertType)
{
Context.Insertable(listHead).AddQueue();
Context.Insertable(listDetail).AddQueue();
resultCount = ExecuteQueues(Context);
return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
}
return DeleteHandleData(response);
});
}
public ResponseWms ExecuteQueuesBefore(WmsSnInfoEntity entity, ResponseWms response)
{
var existingSnList = Context.Queryable<bus_sn_project_info_wms_head>().Select(x => x.sn).ToList();
foreach (var item in entity.result)
{
if (string.IsNullOrEmpty(item.sn)) return response.ResponseError($"【WMS系统】序列号为空【sn】,请核对数据!");
if (IsInsertType && existingSnList.Contains(item.sn))
return response.ResponseError($"WMS系统】序列号已存在IOT系统【{item.sn}】,请不要重复发送数据!");
}
return response;
}
/// <summary>
/// type is I or D value
/// </summary>
public ResponseWms typeValidation(string type, ResponseWms response)
{
if ("ID".IndexOf(type) == -1) return response.ResponseError($"【WMS系统】【type 只能是I 或者D标识】,请核实确认!");
return response;
}
/// <summary>
/// 删除
/// </summary>
public ResponseWms DeleteHandleData(ResponseWms response)
{
foreach (var item in RequestData.result)
{
var head = Context.Queryable<bus_sn_project_info_wms_head>().First(t => t.sn == item.sn);
if (head == null) return response.ResponseError($"WMS系统】删除失败,序列号不已存在IOT系统【{item.sn}】请核实确认!");
Context.Deleteable<bus_sn_project_info_wms_head>().Where(x => x.keys == head.keys).AddQueue();
Context.Deleteable<bus_sn_project_info_wms_detail>().Where(x => x.headKey == head.keys).AddQueue();
}
int resultCount = ExecuteQueues(Context);
return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
}
private bus_sn_project_info_wms_head CreateHead(Result item)
{
return new bus_sn_project_info_wms_head
{
keys = Guid.NewGuid(),
sn = item.sn,
correlatedCode = item.correlatedCode,
equipmentType = item.equipmentType,
userBy = item.user,
ordeCode=item.ordeCode,
ordeName=item.ordeName,
createBy = SystemVariable.DefaultCreated,
createTime = DateTime.Now
};
}
private bus_sn_project_info_wms_detail CreateDetail(bus_sn_project_info_wms_head head, Result item, Components component)
{
return new bus_sn_project_info_wms_detail
{
headKey = head.keys,
sn = item.sn,
correlatedCode = item.correlatedCode,
userBy = item.user,
partCode = component.partCode,
createBy = SystemVariable.DefaultCreated,
createTime = DateTime.Now
};
}
}
}