wtt
2025-09-14 c20cad9c252371c03288a964dd449feb807113c4
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using DBUtility;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SQLHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;
using WebAPI.Code;
using WebAPI.Models;
using WebAPI.Utility;
 
namespace WebAPI.Controllers
{
    public class SRM_MaterialBarCodeBillController : ApiController
    {
        private JsonResult objJsonResult = new JsonResult();
        private json objjson = new json();
        public new ClsCN oCn = new ClsCN();
        //获取系统参数
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        // GET: SRM_MaterialBarCodeBill
        #region 条码批量生辰
        [Route("GetSRMHBarCodeShowBillMain")]
        public object GetSRMHBarCodeShowBillMain(string HInterID)
        {
            try
            {
                DataSet ds;
                string s = "";
                string ShowItem = " * ";//显示的字段
                //if (DBUtility.ClsPub.isLong(HInterID) == 0)
                //{
                //    objjson.code = "0";
                //    objjson.count = 0;
                //    objjson.Message = "获取失败";
                //    objjson.data = null;
                //    return objjson;
                //}
                DAL.ClsWeb_BarCodeBill oClsWeb_BarCodeBill = new DAL.ClsWeb_BarCodeBill();
                //string sql = string.Format(@"select * from h_v_Cg_POOrderBillMain_ForWeb2 where HInterID = " + sMsg);
                string sql = string.Format(@"select * from h_v_SRM_BarCodeBill where HItemID in  ( " + HInterID + ")");
                ds = oCn.RunProcReturn(sql, "h_v_SRM_BarCodeBill");
                //ds = oClsWeb_BarCodeBill.ShowBillMain1(DBUtility.ClsPub.isLong(sMsg), ShowItem, ref DBUtility.ClsPub.sExeReturnInfo);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败";
                    objjson.data = null;
                    return objjson;
                }
                else
                {
 
                    objjson.code = "0";
                    objjson.count = 10000;
                    objjson.Message = "获取成功";
                    objjson.data = ds.Tables[0];
                    return objjson;
                }
            }
            catch (Exception e)
            {
                objjson.code = "0";
                objjson.count = 0;
                objjson.Message = "获取失败" + e.ToString();
                objjson.data = null;
                return objjson;
            }
        }
        #endregion
 
        #region 条码保存
        [Route("SRM_MaterialBarCodeBillController/GetBarcodeSaveBillBoxBycg")]
        [HttpPost]
        /// <summary>
        /// 生成条码 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="msg2"></param>
        /// <returns></returns>
        public object GetBarcodeSaveBillBoxBycg([FromBody] JObject msg)
        {
            string FCusName = oSystemParameter.GetSingleSystemParameter("WMS_CampanyName", ref DBUtility.ClsPub.sExeReturnInfo);
            var _value = msg["msg"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();//
            string msg3 = sArray[1].ToString();// 
            string UserName = sArray[2].ToString();
            string OrganizationID = sArray[3].ToString();
            ListModels oListModels = new ListModels();
            try
            {
                #region 获取用户以及相关参数
                List<Model.Cls_MaterialHandingModel> lsmain = new List<Model.Cls_MaterialHandingModel>();
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");  //\n
                lsmain = oListModels.getObjectByJson_MaterialHanding(msg2);
                int get_BarCodecount = 0;
                
                foreach (Model.Cls_MaterialHandingModel oItem in lsmain)
                {
                    get_BarCodecount++;
                    //UserName = oItem.HMaker;
                }
                DAL.ClsWeb_BarCodeBill oBill = new DAL.ClsWeb_BarCodeBill();
                msg3 = msg3.Substring(1, msg3.Length - 2);
                msg3 = msg3.Replace("\\", "");
                msg3 = msg3.Replace("\n", "");  //\n
                #endregion
                List<Model.ClsGy_MaterialBarCodeBillModel> ls = new List<Model.ClsGy_MaterialBarCodeBillModel>();
 
                ls = oListModels.getObjectByJson_BarCodeBill(msg3);//列表数据model
 
                if (ls.Count > 1)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "每次只允许选择一个物料生成条码,请退出重新选择一行物料,不允许选择多行物料!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                #region 数据准备
                long linterid = Pub_Class.ClsPub.CreateBillID_SRMProd("8888", ref DBUtility.ClsPub.sExeReturnInfo);
                int LSHlen = 6;             //流水号长度
                int SumLen = 10;            //总长度
                string TM = "";             //条码
                double HSumQty = 0;         //产品数量
                int LSH = 0;                //流水号
                string LSH2 = "";           //流水号转换成字符
                string sDate = "";            //日期
                string sYear = "";          //年
                string sPeriod = "";        //月
                string sDay = "";            //日
                string WeiShu = ""; //尾数
                string HKFDQDate= ls[0].HKFDQDate;//到期日期
                long HKFPeriod= (ls[0].HKFPeriod.ToString()==""?0: ls[0].HKFPeriod);//保质期
                string HKFDate= ls[0].HKFDQDate==null? DateTime.Now.ToString("yyyy/MM/dd"): ls[0].HKFDQDate;//生产日期
                string HKFDate_1 = string.Join("", HKFDate.Split('/').Select(x => x.PadLeft(2, '0')));
                double HMinQty= ls[0].HMinQty;//标准包装数
                string HStatusMan = ls[0].HStatusMan;//工号
                //string HBarCodeDate = ls[0].HBarCodeDate;//工号
                string HBarCodeDate = ls[0].HBarCodeDate == null ? DateTime.Now.ToString("yyyy/MM/dd") : ls[0].HBarCodeDate;//条码日期
                string HBarCodeDate_1 = string.Join("", HBarCodeDate.Split('-').Select(x => x.PadLeft(2, '0')));
 
                double HBQty = ls[0].HBQty;//最小包装数
                long HISKFPERIOD= ls[0].HISKFPERIOD;//是否启用保质期
                long HBatchManager= ls[0].HBatchManager;//是否启用批号
 
                string HSourceBillType= ls[0].HSourceBillType;//条码类型
 
                string HBatchNo= ls[0].HBatchNo==null?"": ls[0].HBatchNo;//批号
                //物料
                long HMaterID=ls[0].HMaterID;
                string HMaterNumber= ls[0].HMaterNumber;
                string HMaterShortNumber = ls[0].HMaterShortNumber;
                string HMaterName = ls[0].HMaterName;
                //供应商
                long HSupID = lsmain[0].HSupID;
                string HMaker= ls[0].HMaker;
                DateTime HMakeDate= ls[0].HMakeDate;
                double HQty= ls[0].HQty; //本次生成条码数量
                double biaoqianzhangshu= ls[0].biaoqianzhangshu;
        //流水号
        DataSet Ds;
                SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                oCn.BeginTran();
                //Ds = oCn.RunProcReturn("exec GetLSH '" + DateTime.Today + "'", "GetLSH");
                Ds = oCn.RunProcReturn("exec h_p_WMS_GetMaxNo '" + HSupID+"_"+ HMaterID+"_" + HBarCodeDate + "'", "GetLSH");
                LSH = ClsPub.isInt(Ds.Tables[0].Rows[0][0]);
                #endregion
 
                //// 存储生成的条码列表
                List<string> generatedBarcodes = new List<string>();
 
                var biaoqianCount = 0;
                foreach (Model.ClsGy_MaterialBarCodeBillModel oItemSub in ls)
                {
                    biaoqianzhangshu = ls[biaoqianCount].biaoqianzhangshu;
                    biaoqianCount++;
                    //数量为0 跳过
                    if (oItemSub.HQty == 0 || oItemSub.HMinQty == 0 )
                        continue;
                    if(oItemSub.HBatchManager!=0&&oItemSub.HBatchNo=="")
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "已启用批号,批号不能为空,请核对";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    if (oItemSub.HISKFPERIOD != 0 && oItemSub.HKFPeriod == 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "已启用保质期,保质期不能为0,请核对";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    string sql = "";
                    if (ClsPub.isLong(oItemSub.HMaterID) != 0)
                    {
 
                        HMaterID = oItemSub.HMaterID;//物料代码
                        HQty = ClsPub.isDoule(oItemSub.HQty);//本次生成条码数量
                        HBQty = ClsPub.isInt(oItemSub.HBQty);//最小包装数(内箱数)
                        HMinQty = ClsPub.isDoule(oItemSub.HMinQty);//标准包装数
                        int virtualCount = 1;//实际上插入数量
                        // HSumQty = ClsPub.isDoule(oItemSub.HQty);  //产品数量
                       for (int i = 0; i < biaoqianzhangshu; i++)
                        {
                            double currentQty = (i == biaoqianzhangshu - 1 && (HQty % HMinQty != 0))
                              ? (HQty % HMinQty)
                              : HMinQty;
                            ///供应商名称+@+物料代码+@+物料简称+@+数量+@条码数+@+日期+@+流水号  
                            LSH += 1;
                            TM = $"{oItemSub.HSupName}@{oItemSub.HMaterNumber}@{oItemSub.HSubjoin}@{currentQty}@{HBarCodeDate_1}@{LSH.ToString("D7")}";
                            //TM = $"{oItemSub.HSupName}@{oItemSub.HMaterID}@{oItemSub.HMaterName}@{oItemSub.biaoqianzhangshu}@{HKFDate_1}@{LSH}";
                            // 添加到生成的条码列表
                            generatedBarcodes.Add(TM);
                            Model.ClsGy_BarCodeBill_WMS_Model bar = new Model.ClsGy_BarCodeBill_WMS_Model();
 
                           // var HBarCode_verify = TM.Split('@'); //校验条码信息
                           // var HMaterNumber_verify = HBarCode_verify[0]; //校验物料编码
                           // var HMaterName_verify = HBarCode_verify[1]; //校验物料名称
                           // //var HMaterModel_verify = HBarCode_verify[2]; //校验规格型号
                           // var HSupName_verify = HBarCode_verify[7]; //校验供应商
                           //// var HSourceBillNo_verify = HBarCode_verify[8]; //校验采购订单号
 
                           // sql = $"select * from Gy_Material where HNumber = '{HMaterNumber_verify}' and HName = '{HMaterName_verify}'";
                           // DataSet verifyMatInfo = oCn.RunProcReturn(sql, "tables");
 
 
                           // sql = $"select * from Gy_Supplier where HName = '{HSupName_verify}'";
                           // DataSet verifySupInfo = oCn.RunProcReturn(sql, "tables");
                            string HBarCodeType = "唯一条码";
                            //sql = "insert into Gy_BarCodeBill (HInterID,HBarCode,HBarCodeType,HMaterID,HUnitID,HQty,HKFDate,HKFPeriod,HKFDQDate" +
                            //               ",HBatchNo,HBarcodeQtys,HBarcodeNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HEndQty,HSupflag" +
                            //               ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HMTONO,HProduceDate,HExpiryDate,HISKFPERIOD" +
                            //               ") values ("
                            //               + linterid.ToString() + ",'" + TM + "','" + HBarCodeType + "'," + HMaterID + "," + "" + "," + HQty + ",'" + HKFDate + "'," + HKFPeriod + ",'" + HKFDQDate
                            //               + "','" + HBatchNo + "','" + get_BarCodecount + "','" + virtualCount + "'," + HSupID + "," + "" + ",'" + ClsPub.CurUserName + "','" + DateTime.Today + "'," + "1" + ",'" + DateTime.Today + "',1"
                            //               + "," + "1" + "," + "1" + ",'" + "1" + "','" + HSourceBillType + "','" + "1" + "','"
                            //               + HKFDate + "','" + HKFDQDate + "'," + HISKFPERIOD + ")";
                            sql = "INSERT INTO Gy_BarCodeBill (HInterID, HBarCode, HBarCodeType, HMaterID, HUnitID, HQty, HKFDate, HKFPeriod, HKFDQDate, " +
       "HBatchNo, HBarcodeQtys, HBarcodeNo, HSupID, HGroupID, HMaker, HMakeDate, HPrintQty, HEndQty, HSupflag, " +
       "HSourceInterID, HSourceEntryID, HSourceBillNo, HSourceBillType, HMTONO, HProduceDate, HExpiryDate, HISKFPERIOD,HStatusMan,HBarCodeDate,HinitQty,HSTOCKORGID,HOWNERID) " +
      "VALUES (" +
       linterid.ToString() + ", '" + TM + "', '" + HBarCodeType + "', " + HMaterID + ", " + "0" + ", " + currentQty + ", '" + null + "', " + HKFPeriod + ", '" + null + "', " +
       "'" + HBatchNo + "', " + get_BarCodecount + ", '" + virtualCount + "', " + HSupID + ", " + "0" + ", '" + UserName + "',getdate()" + "," + "0" + ", " + "0" + ", 1, " +
       "0, 0, '', '" + HSourceBillType + "', '', '" +
       null + "', '" + null + "', " + HISKFPERIOD + ",'" + HStatusMan + "','" + HBarCodeDate + "','" + currentQty + "'," + OrganizationID + "," + OrganizationID + ")";
                            LogService.Write("生成条码: " + sql);
                            oCn.RunProc(sql); 
                            oCn.RunProc("exec h_p_WMS_SetMaxNo '" + HSupID + "_" + HMaterID + "_" + HBarCodeDate + "'");
                            virtualCount++;
                           }
 
                        }
                }
                oCn.Commit();
                // 返回生成的条码列表
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "生成条码成功!";
                objJsonResult.data =string.Join("~", generatedBarcodes); // 返回生成的条码列表
                WebAPIController.Add_Log("条码下推", UserName, "生成条码");
                return objJsonResult;
            }
            catch (Exception e)
            {
                LogService.Write("生成条码发生错误: " + e);
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "" + e.Message + DBUtility.ClsPub.sExeReturnInfo;
                objJsonResult.data = null;
                return objJsonResult;
            }
 
            
        }
        #endregion
 
        #region 条码列表
        [Route("showHBarCodeBillDetail")]
        [HttpGet]
        public object showHBarCodeBillDetail(string sMsg,string user)
        {
            try
            {
                DataSet ds;
                string s = "";
                string ShowItem = " * ";//显示的字段
                if (sMsg == "")
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败";
                    objjson.data = null;
                    return objjson;
                }
                string sql = string.Format(@"select * from h_v_Gy_UserMaterRelationPrint where HItemID in (   " + sMsg)+")";
                ds = oCn.RunProcReturn(sql, "h_v_Gy_UserMaterRelationPrint");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败" + DBUtility.ClsPub.sExeReturnInfo;
                    objjson.data = null;
                    return objjson;
                }
                else
                {
                    objjson.code = "0";
                    objjson.count = 1;
                    objjson.Message = "获取成功!";
                    objjson.data = ds.Tables[0];
                    return objjson;
 
                }
            }
            catch (Exception e)
            {
                objjson.code = "0";
                objjson.count = 0;
                objjson.Message = "获取单据表体失败:" + e.Message;
                objjson.data = null;
                return objjson;
            }
        }
        #endregion
 
        #region 根据条码获取条码详情
        [Route("GetBarcodeDetails")]
        [HttpGet]
        public object GetBarcodeDetails(string barcodes)
        {
            try
            {
                if (string.IsNullOrEmpty(barcodes))
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "条码参数不能为空";
                    objjson.data = null;
                    return objjson;
                }
 
                // 将逗号分隔的条码转换为SQL IN查询条件
                var barcodeList = barcodes.Split('~');
 
                // 构建IN子句,为每个条码添加单引号
                var inValues = new List<string>();
                foreach (var barcode in barcodeList)
                {
                    inValues.Add("'" + barcode.Replace("'", "''") + "'"); // 处理单引号转义
                }
 
                string inClause = string.Join(",", inValues);
                string sql = $"SELECT * FROM h_v_Gy_UserMaterRelationPrint WHERE HBarCode IN ({inClause})" + " order by 物料代码,cast(流水号 as int)";
 
                // 直接执行SQL查询
                DataSet ds = oCn.RunProcReturn(sql, "h_v_Gy_UserMaterRelationPrint");
 
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "未找到相关条码信息";
                    objjson.data = null;
                    return objjson;
                }
 
                objjson.code = "0";
                objjson.count = ds.Tables[0].Rows.Count;
                objjson.Message = "获取成功";
                objjson.data = ds.Tables[0];
                return objjson;
            }
            catch (Exception e)
            {
                objjson.code = "0";
                objjson.count = 0;
                objjson.Message = "获取条码详情失败:" + e.Message;
                objjson.data = null;
                return objjson;
            }
        }
        #endregion
 
 
 
    }
}