schangxiang@126.com
2025-09-17 ab9d9126ced7d6dac0e14c3ede5a49fdb7fc94df
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using AutoMapper;
using iWareCommon.Common.Entity;
using iWareCommon.Utils;
using iWareDataCore.BASE.Entity;
using iWareDataCore.BASE.Service;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
using WebWIPAPI.Models;
using WebWIPAPI.Properties;
using WebWIPAPI.Utils;
using LogTextHelper = WebWIPAPI.Utils.LogTextHelper;
 
namespace WebWIPAPI.Controllers
{
    /// <summary>
    /// MES对接接口
    /// </summary>
    public class MesIntegrController : Controller
    {
 
        /// <summary>
        /// 提供库存信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult syncMaterialInfo()
        {
            var responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>();
            try
            {
                var msg = "";
                var datalist = PlaceMaterialViewService.GetInstance().GetPlacePreview(out msg);
                if (!string.IsNullOrEmpty(msg))
                {
                    responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "获取异常:" + msg,
                        Data = null,
                    };
                }
                else
                {
                    responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>()
                    {
                        Code = 200,
                        Success = true,
                        Message = "成功",
                        Data = datalist,
                    };
                }
            }
            catch (Exception ex)
            {
                responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>()
                {
                    Code = 500,
                    Success = false,
                    Message = "异常:" + ex.Message,
                    Data = null,
                };
            }
            //将对象转化为json格式
            var responseStr = JsonConvert.SerializeObject(responseMessage);
            LogTextHelper.WriteLine(Resources.LogDir, "WIPAPI:{0},{1}, {2}", "syncMaterialInfo", "请求参数:" + "", "响应信息;" + responseStr);
            return Json(responseMessage, JsonRequestBehavior.DenyGet);
        }
 
        /// <summary>
        /// 导入入库单
        /// </summary>
        /// <param name="inputmaterials"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult importInOrder(List<MaterialViewForMesEntity> importMaterials)
        {
            var responseMessage = new ApiResponse<string>();
            var msg = "";
 
            try
            {
                #region 业务处理
 
                //第一步:先新增到物料基础表
                List<MaterialViewEntity> materials = new List<MaterialViewEntity>();
 
                materials = ClassHelper.RotationMapping_Json<List<MaterialViewEntity>, List<MaterialViewForMesEntity>>(importMaterials);
 
 
                MaterialViewService.GetInstance().ImportExcelFromMes(materials, out msg);
                if (!string.IsNullOrEmpty(msg))
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "新增到物料基础表异常:" + msg,
                        Data = null,
                    };
                }
                else
                {
                    Thread.Sleep(400);//注意:休眠毫秒,用于数据库事务提交
 
                    string typeName = "入库";
                    List<InputMaterialEntity> inputmaterials = new List<InputMaterialEntity>();
                    inputmaterials = ClassHelper.RotationMapping_Json<List<InputMaterialEntity>, List<MaterialViewForMesEntity>>(importMaterials);
                    handler_importOutOrder(typeName, inputmaterials, out msg);
 
                    #endregion
 
                    if (!string.IsNullOrEmpty(msg))
                    {
                        responseMessage = new ApiResponse<string>()
                        {
                            Code = 500,
                            Success = false,
                            Message = "生成入库任务异常:" + msg,
                            Data = null,
                        };
                    }
                    else
                    {
                        responseMessage = new ApiResponse<string>()
                        {
                            Code = 200,
                            Success = true,
                            Message = "成功",
                            Data = null,
                        };
                    }
                }
 
            }
            catch (Exception ex)
            {
                responseMessage = new ApiResponse<string>()
                {
                    Code = 500,
                    Success = false,
                    Message = "异常:" + ex.Message,
                    Data = null,
                };
            }
 
 
            //将对象转化为json格式
            var responseStr = JsonConvert.SerializeObject(responseMessage);
            LogTextHelper.WriteLine(Resources.LogDir, "WIPAPI:{0},{1}, {2}", "importInOrder", "请求参数:" + "", "响应信息;" + responseStr);
            return Json(responseMessage, JsonRequestBehavior.DenyGet);
        }
 
 
        /// <summary>
        /// 导入出库单
        /// </summary>
        /// <param name="inputmaterials"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult importOutOrder(List<InputMaterialEntity> inputmaterials)
        {
            var responseMessage = new ApiResponse<string>();
            try
            {
                var msg = "";
 
                #region 业务处理
 
                string typeName = "出库";
                handler_importOutOrder(typeName, inputmaterials, out msg);
 
                #endregion
 
                if (!string.IsNullOrEmpty(msg))
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "生成出库任务异常:" + msg,
                        Data = null,
                    };
                }
                else
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 200,
                        Success = true,
                        Message = "成功",
                        Data = null,
                    };
                }
 
            }
            catch (Exception ex)
            {
                responseMessage = new ApiResponse<string>()
                {
                    Code = 500,
                    Success = false,
                    Message = "异常:" + ex.Message,
                    Data = null,
                };
            }
 
            //将对象转化为json格式
            var responseStr = JsonConvert.SerializeObject(responseMessage);
            LogTextHelper.WriteLine(Resources.LogDir, "WIPAPI:{0},{1}, {2}", "importOutOrder", "请求参数:" + "", "响应信息;" + responseStr);
            return Json(responseMessage, JsonRequestBehavior.DenyGet);
        }
 
 
        /// <summary>
        /// 公共导入 入库单或出库单
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="inputmaterials"></param>
        /// <param name="msg"></param>
        private void handler_importOutOrder(string typeName, List<InputMaterialEntity> inputmaterials, out string msg)
        {
            try
            {
                #region 业务处理
 
                msg = "";
                //var inputmaterials = InputMaterialService.GetInstance().QueryByParam(new QueryParam { Filter = new Dictionary<string, object> { { "Status", "未生成" } } }, out msg);
                if (inputmaterials != null && inputmaterials.Count > 0)
                {
                    var placeMaterial = PlaceMaterialViewService.GetInstance().QueryByParam(new QueryParam { }, out msg);
                    var inoutdetails = InOutListDetailViewService.GetInstance().QueryByParam(new QueryParam { }, out msg);
                    List<string> codes = new List<string>();
                    inputmaterials.ForEach(x => codes.Add(x.Code));
                    List<int> ids = new List<int>();
                    var materials = InputMaterialService.GetInstance().GetIds(codes);
                    List<InOutListDetailEntity> detail = new List<InOutListDetailEntity>();
                    for (int i = 0; i < materials.Count; i++)
                    {
                        if (typeName == "入库")
                        {
 
                            if (placeMaterial.Select(x => x.MaterialId).Contains(materials[i].id))
                            {
                                msg = materials[i].code + "已存在立库中,不能添加到入库单明细中!";
                                break;
                            }
                            if (inoutdetails.Select(x => x.MaterialId).Contains(materials[i].id))
                            {
                                msg = materials[i].code + "已存在之前的入库单明细中,不能添加到当前入库单明细中!";
                                break;
                            }
 
                        }
                        else
                        {
                            if (!placeMaterial.Select(x => x.MaterialId).Contains(materials[i].id))
                            {
                                msg = materials[i].code + "不在立库中,不能添加到出库单明细中!";
                                break;
                            }
                            if (inoutdetails.Select(x => x.MaterialId).Contains(materials[i].id))
                            {
                                var inoutdeta = inoutdetails.FirstOrDefault(x => x.MaterialId == materials[i].id);
                                if (inoutdeta.TypeName == "出库")
                                {
                                    msg = materials[i].name + "已存在之前的出库单明细中,不能添加到当前出库单明细中!";
                                    break;
                                }
                            }
                        }
                        InOutListDetailEntity inoutdetail = new InOutListDetailEntity()
                        {
                            MaterialId = materials[i].id,
                            IsFinish = 0
                        };
                        detail.Add(inoutdetail);
                    }
                    if (!string.IsNullOrEmpty(msg))
                    {
                        return;
                    }
                    InOutListService.GetInstance().CreateListAndDetial(
                        new InOutListEntity
                        {
                            ListNo = inputmaterials.First().ListNo,
                            CreateTime = DateTime.Now,
                            TypeName = typeName,
                            Status = 0,
                            Remark = "",
                            InOutListDetail = detail
                        }, out msg);
                }
                else
                {
                    msg = ("请先确保出入库单物料明细不为空!");
                }
 
 
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }
 
    }
}