ch
2022-11-23 695d9c4cda618bae357d6f70a068b604dac4fdd3
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using Newtonsoft.Json.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Pub_Class;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.Http;
using System.Windows.Forms;
using WebAPI.Models;
namespace WebAPI.Controllers.基础资料.基础资料
{
    public class Gy_SOPBillEditController : ApiController
    {
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
 
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
        public DAL.ClsGy_SOPBill BillNew = new DAL.ClsGy_SOPBill();
        public DAL.ClsGy_SOPBill BillOld = new DAL.ClsGy_SOPBill();
        public bool BillChange;   //
        public DataGridView grdMain = new System.Windows.Forms.DataGridView();
        ClsGridViewSum oSumGrid = new ClsGridViewSum();        
 
        //定义
        public const Int16 HTagCol = 0;
        public const Int16 HSnoCol = 1;
        public const Int16 HProcNoCol = 2;
        public const Int16 HProcIDCol = 3;
        public const Int16 HProcNumberCol = 4;
        public const Int16 HProcNameCol = 5;
        public const Int16 HWorkStationIDCol = 6;
        public const Int16 HWorkStationNumberCol = 7;
        public const Int16 HWorkStationNameCol = 8;
        public const Int16 HCenterIDCol = 9;
        public const Int16 HCenterNumberCol = 10;
        public const Int16 HCenterNameCol = 11;
        public const Int16 HRemarkCol = 12;
        public const Int16 HEntryIDCol = 13;
 
        DAL.ClsIF_Material_View oMater = new DAL.ClsIF_Material_View();
        DAL.ClsIF_Unit_View oUnit = new DAL.ClsIF_Unit_View();
        DAL.ClsIF_Process_View oProc = new DAL.ClsIF_Process_View();
        DAL.ClsGy_WorkStation_View oStation = new DAL.ClsGy_WorkStation_View();
        DAL.ClsGy_WorkCenter_View oCenter = new DAL.ClsGy_WorkCenter_View();
 
        #region  读写类
        //显示单据
        public void Sub_ShowBill()
        {
            DAL.ClsIF_Material_View oMater = new DAL.ClsIF_Material_View();
            DAL.ClsIF_Unit_View oUnit = new DAL.ClsIF_Unit_View();
            DAL.ClsIF_Process_View oProc = new DAL.ClsIF_Process_View();
            DAL.ClsGy_WorkStation_View oStation = new DAL.ClsGy_WorkStation_View();
            DAL.ClsGy_WorkCenter_View oCenter = new DAL.ClsGy_WorkCenter_View();
            //ClsGy_SourceHlp oSource = new ClsGy_SourceHlp();
            //判断是否存在单据
            if (BillOld.ShowBill(BillOld.omodel.HInterID, ref ClsPub.sExeReturnInfo) == false)
            {
                MessageBox.Show(ClsPub.sExeReturnInfo, "提示");
                return;
            }
            //grid禁止刷新 
           
 
            //加载表体
            int i = 0;
            foreach (Model.ClsGy_SOPBillSub oSub in BillOld.DetailColl)
            {
                if (i >= grdMain.RowCount - 1)
                    grdMain.Rows.Add();
                grdMain.Rows[i].Cells[HTagCol].Value = "*";
 
                grdMain.Rows[i].Cells[HProcIDCol].Value = oSub.HProcID.ToString();
                if (oProc.GetInfoByID(oSub.HProcID))
                {
                    grdMain.Rows[i].Cells[HProcNumberCol].Value = oProc.omodel.HNumber;
                    grdMain.Rows[i].Cells[HProcNameCol].Value = oProc.omodel.HName;
                }
                else
                {
                    grdMain.Rows[i].Cells[HProcNumberCol].Value = "";
                    grdMain.Rows[i].Cells[HProcNameCol].Value = "";
                }
                //
                grdMain.Rows[i].Cells[HCenterIDCol].Value = oSub.HCenterID.ToString();
                if (oCenter.GetInfoByID(oSub.HCenterID))
                {
                    grdMain.Rows[i].Cells[HCenterNumberCol].Value = oCenter.omodel.HNumber;
                    grdMain.Rows[i].Cells[HCenterNameCol].Value = oCenter.omodel.HName;
                }
                else
                {
                    grdMain.Rows[i].Cells[HCenterNameCol].Value = "";
                    grdMain.Rows[i].Cells[HCenterNumberCol].Value = "";
                }
                //
                grdMain.Rows[i].Cells[HWorkStationIDCol].Value = oSub.HWorkStationID.ToString();
                if (oStation.GetInfoByID(oSub.HWorkStationID))
                {
                    grdMain.Rows[i].Cells[HWorkStationNumberCol].Value = oStation.omodel.HNumber;
                    grdMain.Rows[i].Cells[HWorkStationNameCol].Value = oStation.omodel.HName;
                }
                else
                {
                    grdMain.Rows[i].Cells[HWorkStationNumberCol].Value = "";
                    grdMain.Rows[i].Cells[HWorkStationNameCol].Value = "";
                }
                //
                grdMain.Rows[i].Cells[HProcNoCol].Value = oSub.HProcNo.ToString();
 
                grdMain.Rows[i].Cells[HRemarkCol].Value = oSub.HRemark;
                grdMain.Rows[i].Cells[HEntryIDCol].Value = oSub.HEntryID;
                i = i + 1;
            }
            //GRID允许刷新 
            DBUtility.Xt_BaseBillFun.SetSumGrid(oSumGrid);
            //
            //Sub_OperStatus();
        }
        #endregion
 
 
        #region 保存单据
        [Route("Gy_SOPBill/ModifyByID")]
        [HttpPost]
        public object Sub_SaveBill([FromBody] JObject oMain)
        {
            try
            {
                var _value = oMain["oMain"].ToString();
                string msg1 = _value.ToString();
                string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                string msg2 = sArray[0].ToString();
                string msg3 = sArray[2].ToString();//当前人
 
                //反序列化
                msg2 = "[" + msg2.ToString() + "]";
 
                List<SOPBill> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SOPBill>>(msg2);
 
                string HBillNo = list[0].HBillNo;
                DateTime HDate = list[0].HDate;
                string HName = list[0].HName;
                int HMaterID = list[0].HMaterID;
                string HMaterNumber = list[0].HMaterNumber;
                string HMaterName = list[0].HMaterName;
                string HMaterModel = list[0].HMaterModel;
                int HUnitID = list[0].HUnitID;
                string HUnitNumber = list[0].HUnitNumber;
                string HUnitName = list[0].HUnitName;
                string HRemark = list[0].HRemark;
                bool HStandard = list[0].HStandard;
                bool HUse = list[0].HUse;
                string HMaker = msg3;
                int HMaterTypeID = list[0].HMaterTypeID;
 
                //子表
                List<SOPBillSub> list2 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SOPBillSub>>(msg2);
 
                int HProcNo = list2[0].HProcNo;
                long HProcID = list2[0].HProcID;
                long HWorkStationID = list2[0].HWorkStationID;
                long HCenterID = list2[0].HCenterID;
                string HRemark2 = list2[0].HRemark;
 
                //判断权限
                //if (!ClsPub.Security_Log("Gy_Repair_Edit", 1, false, msg3))
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "没有找到该功能模块!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
 
                DataSet CS;
                Int64 lID = 0;
                string BillType = "";
                CS = oCN.RunProcReturn("select * from Gy_BillNumber with (nolock) where BillCode='" + 3305 + "'", "Gy_BillNumber");
 
                if (CS.Tables[0].Rows.Count != 0)
                {
                    lID = ClsPub.isLong(CS.Tables[0].Rows[0]["IDNow"].ToString());
                    BillType = CS.Tables[0].Rows[0]["BillType"].ToString().Trim();
                }
               
                //若MAINDI重复则重新获取
                oCN.BeginTran();
                //主表
                oCN.RunProc("Insert Into Gy_SOPBillMain   " +
                 "(HBillType,HInterID,HBillNo,HDate" +
                 ",HYear,HPeriod,HRemark,HMaker,Hmakedate,HMaterID,HName,HMaterTypeID" +
                 ",HUnitID,HMaterNumber,HUnitNumber,HStandard" +
                 ") " +
                  " values('" + 3305 + "'," + lID + ",'" + HBillNo + "'," + "getdate()" +
                "," + "datepart(yyyy,GETDATE())" + "," + "datepart(mm,getdate())" + ",'" + HRemark + "','" + HMaker + "'," + "getdate()" + "," + HMaterID.ToString() + ",'" + HName + "'," + HMaterTypeID +
                "," + HUnitID.ToString() + ",'" + HMaterNumber + "','" + HUnitNumber + "','" + HStandard.ToString() +
                "') ");
            
                oCN.RunProc("update Gy_BillNumber set IDNow=IDNow+1 where BillType='" + BillType.Trim() + "'");
                //" values(" + 3305 + "','" + 12333 + "','" + "test" + "','" + HDate + "','" +
                //2022 + "','" + 11 + "','" + HRemark + "','" + "Test" + "','" + "2022-11-04" + "','" + HMaterID.ToString() + "','" + HName + "','" + "656" +
                //"','" + HUnitID.ToString() + "','" + HMaterNumber + "','" + HUnitNumber + "','" + HStandard.ToString() +
                //") ");
                //修改上级为非末级代码
                //oCN.RunProc("Update Gy_MouldType set HEndflag=0 where HItemID=" + HParentID, ref DBUtility.ClsPub.sExeReturnInfo);
 
                //插入子表
              oCN.RunProc("Insert into Gy_SOPBillSub " +
                "(HInterID,HEntryID,HProcID,HProcNO" +
                ",HWorkStationID,HCenterID,HRemark" +
                ") values("
                + lID + "," + 1 + "," + HProcID + "," + HProcNo +
                "," + HWorkStationID + "," + HCenterID + ",'" + HRemark2 + "'" +
                ") ");
              
 
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "新增成功!";
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.Message;
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        [Route("Gy_SOPBill/Use")]
        [HttpGet]
        public object sy_Click()
        {
            if (BillOld.omodel.HInterID == 0)
            {
                objJsonResult.Message = "使用失败!当前作业指导书不是浏览状态!";
                return objJsonResult;
            }
            if (BillOld.Sub_RoutingCheckAndUsed(BillOld.omodel.HInterID))
            {
                objJsonResult.count = 1;
                objJsonResult.Message = "使用成功!";
                return objJsonResult;
            }
            else
            {
                objJsonResult.Message = "使用失败!";
                return objJsonResult;
            }
        }
 
        [Route("Gy_SOPBill/Cancel")]
        [HttpGet]
        public object fsy_Click()
        {
            if (BillOld.omodel.HInterID == 0)
            {
                objJsonResult.Message = "反使用失败!当前作业指导书不是浏览状态!";
                return objJsonResult;
            }
            if (BillOld.Sub_RoutingUsed_Back(BillOld.omodel.HInterID))
            {
                objJsonResult.count = 1;
                objJsonResult.Message = "反使用成功!";
                return objJsonResult;
            }
            else
            {
                objJsonResult.Message = "反使用失败!";
                return objJsonResult;
            }
        }
 
        [Route("Gy_SOPBill/UploadFile1")]
        [HttpPost]
        public object UploadFile1()
        {
 
            string HBillNo = HttpContext.Current.Request.Params["HBillNo"];  //单据号
            string HRemark = HttpContext.Current.Request.Params["HRemark"];  //备注
            string HUserName = HttpContext.Current.Request.Params["HUserName"];  //创建人
            HttpPostedFile files = HttpContext.Current.Request.Files["file"];
            string path = HttpContext.Current.Server.MapPath("~/../Files/" + HBillNo);
            //string path = @"D:\\Files\\"+ HBillNo;
            dynamic dyResult = UpLoadFile(files, path, HBillNo, HRemark, HUserName);
            if (dyResult != null && dyResult.result == 1)
            {
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "上传成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            else
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = dyResult.returnval;
                objJsonResult.data = null;
                return objJsonResult;
            }
 
        }
 
        public dynamic UpLoadFile(HttpPostedFile files, string path, string HBillNo, string HRemark, string HUserName)
        {
            dynamic Result_Ob = new { result = 1, returnval = "上传成功!" };
            string filePath = Path.GetFullPath(files.FileName);//文件上传路径
            string fileExtension = Path.GetExtension(files.FileName);// 文件扩展名
            string filename = files.FileName;//文件名
            string fileSavePath = path;// 上传保存路径
            int filesize = files.ContentLength;//获取上传文件的大小单位为字节byte
            int Maxsize = 40000 * 1024;//定义上传文件的最大空间大小为40M
 
            DataSet CS;
            Int64 lID = 0;
            string BillType = "";
            CS = oCN.RunProcReturn("select * from Gy_BillNumber with (nolock) where BillCode='" + 3305 + "'", "Gy_BillNumber");
            if (CS.Tables[0].Rows.Count != 0)
            {
                lID = ClsPub.isLong(CS.Tables[0].Rows[0]["IDNow"].ToString())-1;
                BillType = CS.Tables[0].Rows[0]["BillType"].ToString().Trim();
            }
 
            DataSet Ds;
            Int64 Sno = 1;
            Ds = oCN.RunProcReturn("select MAX(HSno) HSno from Gy_SOPBillSub2" , "Gy_SOPBillSub2");
            if (Ds.Tables[0].Rows.Count != 0 && ClsPub.isLong(Ds.Tables[0].Rows[0]["HSno"].ToString())!= 0)
            {
                Sno = ClsPub.isLong(Ds.Tables[0].Rows[0]["HSno"].ToString());
                Sno += 1;
            }
 
            try
            {
                if (files == null || files.ContentLength <= 0)
                {
                    Result_Ob = new { result = 0, returnval = "文件不能为空!" };
                    return Result_Ob;
                }
                if (filesize >= Maxsize)
                {
                    Result_Ob = new { result = 0, returnval = "上传文件超过40M,不能上传!" };
                    return Result_Ob;
                }
 
                string fileurl = Path.Combine(fileSavePath, filename);
                if (Directory.Exists(fileurl) == true)  //如果存在重名文件就提示  
                {
                    Result_Ob = new { result = 0, returnval = "存在同名文件!" };
                    return Result_Ob;
                }
                //删除数据表数据
                ds = oCN.RunProcReturn("delete from Gy_SOPBillSub2 where HFileName='" + filename + "'", "Gy_SOPBillSub2");
                if (Directory.Exists(path))
                {
                    File.Delete(fileurl);      //删除指定文件
                    files.SaveAs(fileurl);
                    string StrPath = "/files/" + HBillNo + "/" + filename;
                    if (File.Exists(fileurl))
                    {
                        //这里可以执行一些其它的操作,比如更新数据库
                        //写入数据表
                        oCN.RunProc("Insert into Gy_SOPBillSub2 (HFileName,HPath,HFileType" +
                               ",HUpMan,HUpTime,HInterID,HEntryID,HSno,HFileID,HFileSize" +
                              ") values('"
                               + filename.ToString() + "','" + filePath.ToString() + "','" +  fileExtension.ToString() + "'" +
                               ",'" + HUserName + "',getdate()"  + ",'" + Sno + "','" + 0 + "','" + Sno + "','" + lID + "','" + 0 +
                        "') ");
                    }
                    else
                    {
                        Result_Ob = new { result = 0, returnval = "上传失败!此文件为恶意文件" };
                    }
                }
                else
                {
                    Directory.CreateDirectory(fileSavePath); //添加文件夹
                    files.SaveAs(fileurl);
                    string StrPath = "/files/" + HBillNo + "/" + filename;
                    if (File.Exists(fileurl))
                    {
                        //这里可以执行一些其它的操作,比如更新数据库
                        //写入数据表
                        oCN.RunProc("Insert into Gy_SOPBillSub2 (HFileName,HPath,HFileType" +
                                     ",HUpMan,HUpTime,HInterID,HEntryID,HSno,HFileID,HFileSize" +
                                    ") values('"
                                     + filename.ToString() + "','" + filePath.ToString() + "','" + fileExtension.ToString() + "'" +
                                     ",'" + HUserName + "',getdate()" + ",'" + 111 + "','" + 222 + "','" + 333 + "','" + 444 + "','" + 555 +
                                    "') ");
                    }
                    else
                    {
                        Result_Ob = new { result = 0, returnval = "上传失败!此文件为恶意文件" };
                    }
                }
 
            }
            catch (Exception e)
            {
                Result_Ob = new { result = 0, returnval = e.Message };
            }
            return Result_Ob;
        }
 
    }
}