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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPI.Models;
 
namespace WebAPI.Controllers
{
    // 投料防错
    public class Sc_FeedingErrorProofingController : ApiController
    {
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        DataSet ds, ds2;
 
        #region 投料防错单 扫描 物料条码
        [Route("Sc_FeedingErrorProofingController/MaterErrorPreventionScanCode")]
        [HttpGet]
        public object MaterErrorPreventionScanCode(string HBarCode)
        {
            ds = oCN.RunProcReturn($@"select top 1 m.HNumber HMaterNumber, a.* from Gy_BarCodeBill a
                                    left join Gy_Material m with(nolock) on a.HMaterID = m.HItemID where a.HBarCode = '{HBarCode}'", "Gy_BarCodeBill");
            if(ds == null || ds.Tables[0].Rows.Count == 0)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "所扫描的条码不存在!";
                objJsonResult.data = null;
                return objJsonResult;
            }
 
            if(ds.Tables[0].Rows[0]["HStopFlag"].ToString() == "1")
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "条码已被关闭,不允许扫码!";
                objJsonResult.data = null;
                return objJsonResult;
            }
 
            if (ds.Tables[0].Rows[0]["HBarCodeStatus"].ToString() == "已投料")
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "该条码对应的物料已投料,不允许重复投料!";
                objJsonResult.data = null;
                return objJsonResult;
            }
 
            if (ds.Tables[0].Rows[0]["HBarCodeStatus"].ToString() != "正常" && ds.Tables[0].Rows[0]["HBarCodeStatus"].ToString() != "")
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "条码状态异常,不允许扫码!";
                objJsonResult.data = null;
                return objJsonResult;
            }
 
            objJsonResult.code = "1";
            objJsonResult.count = 1;
            objJsonResult.Message = "";
            objJsonResult.data = ds.Tables[0];
            return objJsonResult;
        }
        #endregion
 
        #region 投料放错单 保存单据 
        [Route("Sc_FeedingErrorProofingController/saveBill")]
        [HttpPost]
        public object saveBill([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string mainBill = sArray[0].ToString(); //主表数据
            string user = sArray[1].ToString();
            string operationType = sArray[2].ToString();
            string subBill = sArray[3].ToString(); // 子表数据
            oCN.BeginTran();
            try
            {
                JObject HMainBill = JObject.Parse(mainBill);
                JArray HSubBill = JArray.Parse(subBill);
                if(operationType.ToString() == "1")
                {
                    //  新增
                    oCN.RunProc($@"insert into Sc_FeedingErrorProofingBillMain (HYear, HPeriod, HBillType, HBillSubType, HInterID, HDate, HBillNo, HBillStatus, HMaker, HMakeDate, HEmpID
                                , HBarCode, HRemark, HBackRemark, HMainSourceBillType, HMainSourceInterID, HMainSourceEntryID, HMainSourceBillNo, HWorkStationID, HDeptID, HOrgID)
                                values('{DateTime.Now.Year}', '{DateTime.Now.Month}', '{HMainBill["HBillType"]}', '', '{HMainBill["HInterID"]}',  '{HMainBill["HDate"]}', 
                                 '{HMainBill["HBillNo"]}', 1,  'user', getdate(),{HMainBill["HEmpID"]}, '{HMainBill["HWorkCode"]}', '', '', '', 0, 0, '', {HMainBill["HWorkStationID"]}, 
                                {HMainBill["HDeptID"]},{HMainBill["HOrgID"]} )");
                }else if(operationType.ToString() == "4")
                {
                    // 更新
                    oCN.RunProc($@"");
                    // 删除子表
                    oCN.RunProc($@"");
                }
 
                int i = 1;
                // 插入子表
                foreach(JObject HSubItem in HSubBill)
                {   
                    oCN.RunProc($@"insert into Sc_FeedingErrorProofingBillSub (HInterID,HBillNo_bak, HEntryID, HRemark, HSourceInterID, HSourceEntryID, HSourceBillNo, HSourceBillType,
                                HRelationQty, HRelationMoney, HMaterID, HUnitID, HQty, HScanDate, HBarCode)
                                values('{HMainBill["HInterID"]}','{HMainBill["HBillNo"]}', {i++}, '', 0, 0, '', '', 0, 0, {HSubItem["HMaterID"]}, {HSubItem["HUnitID"]}, {HSubItem["HQty"]}, '{HSubItem["HScanDate"]}',
                                {HSubItem["HBarCode"]})");
 
                    // 通过系统参数判断是否需要 反写条码主档单据状态
                    if(oSystemParameter.ShowBill(ref DBUtility.ClsPub.sErrInfo))
                    { 
                        if(oSystemParameter.omodel.Sc_FeedingErrorProofingBill_BarCodeCTL == "Y")
                        {
                            oCN.RunProc($@"update Gy_BarCodeBill set HBarCodeStatus='已投料' where HBarCode = '{HSubItem["HBarCode"]}'");
                        }
                    }
                }
 
                //TODO 通过保存后控制反写条码状态
 
 
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Success!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region
        [Route("Sc_FeedingErrorProofingController/delBill")]
        [HttpGet]
        public object delBill(long HInterID)
        {
            oCN.BeginTran();
            try
            {
                ds = oCN.RunProcReturn($@"select * from Sc_FeedingErrorProofingBillSub where HInterID = {HInterID}", "Sc_FeedingErrorProofingBillSub");
 
                foreach(DataRow subItem in ds.Tables[0].Rows)
                {
                    // 反写 条码主档条码状态
                    oCN.RunProc($@"update Gy_BarCodeBill set HBarCodeStatus='' where HBarCode = '{subItem["HBarCode"]}'");
                }
 
                // 删除子表
                oCN.RunProc($@"delete from Sc_FeedingErrorProofingBillSub where HInterID = {HInterID}");
                // 删除主表
                oCN.RunProc($@"delete from Sc_FeedingErrorProofingBillMain where HInterID = {HInterID}");
 
 
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Success!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region
        [Route("Sc_FeedingErrorProofingController/getBillListPage")]
        [HttpGet]
        public object getBillListPage(string sWhere, string user, int page, int size)
        {
            try
            {
                List<object> columnNameList = new List<object>();
 
                //查看权限
 
                if (sWhere == null || sWhere.Equals(""))
                {
                    string sql = $@"
                    select count(1) count from h_v_Sc_FeedingErrorProofingBillList
                    select * from h_v_Sc_FeedingErrorProofingBillList order by 单据号 desc
                    offset {(page - 1) * size} rows fetch next {size} rows only";
                    ds = oCN.RunProcReturn(sql, "h_v_Sc_FeedingErrorProofingBillList");
                }
                else
                {
                    string sql1 = $@"
                         select count(1) count from h_v_Sc_FeedingErrorProofingBillList where 1 = 1 {sWhere}
                        select * from h_v_Sc_FeedingErrorProofingBillList where 1 = 1 
                        {sWhere} order by 单据号 desc
                        offset {(page - 1) * size} rows fetch next {size} rows only";
                    ds = oCN.RunProcReturn(sql1, "h_v_Sc_FeedingErrorProofingBillList");
                }
 
                //添加列名
                foreach (DataColumn col in ds.Tables[1].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[1];
                objJsonResult.list = columnNameList;
                return objJsonResult;
            }
            catch (Exception ex)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "查询数据异常,请与管理员联系!" + ex.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region
        [Route("Sc_FeedingErrorProofingController/getBillListDetai")]
        [HttpGet]
        public object getBillListDetai(int HInterID)
        {
            try
            {
                List<object> columnNameList = new List<object>();
 
                string sql = $@"
                    select top 1 * from h_v_Sc_FeedingErrorProofingBillDetai where HInterID1 = {HInterID}";
                ds = oCN.RunProcReturn(sql, "h_v_Sc_FeedingErrorProofingBillList");
 
                //添加列名
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                objJsonResult.list = columnNameList;
                return objJsonResult;
            }
            catch (Exception ex)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "查询数据异常,请与管理员联系!" + ex.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
    }
}