yxj
2022-11-16 f3e099f52dad5b1f5aef9f62970c121b46fe612c
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
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.基础资料;
using WebAPI.Models;
 
namespace WebAPI.Controllers.基础资料.基础资料
{
    public class Gy_DutyBillController : ApiController
    {
        private json objJsonResult = new json();
        public DataSet ds = new DataSet();
        public SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
 
        #region 组织架构设置 树形图
        public class TreeModel
        {
            public string id { get; set; }
            public string title { get; set; }
            public List<TreeModel> children = new List<TreeModel>();
        }
        [Route("Gy_DutyBill/Gy_DutyBillTreeList")]
        [HttpGet]
        public object Gy_DutyBillTreeList()
        {
            try
            {
                string sql1 = string.Format("select hitemid,hnumber,hname from Gy_Duty order by hnumber");
 
                ds = oCN.RunProcReturn(sql1, "Gy_Duty");
 
                List<TreeModel> treeModels = new List<TreeModel>();
                TreeModel first = new TreeModel();
                first.id = "0";
                first.title = "组织架构设置";
                treeModels.Add(first);
 
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    var strLen = row["hnumber"].ToString().Split('.');
                    if (strLen.Length == 1)
                    {
                        TreeModel tree = new TreeModel();
                        tree.id = row["hnumber"].ToString();
                        tree.title = row["hname"].ToString();
                        treeModels[0].children.Add(tree);
                    }
                }
 
                digui(ds.Tables[0], treeModels[0].children, 2);
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = Newtonsoft.Json.JsonConvert.SerializeObject(treeModels);
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
 
        /// <summary>
        /// 递归函数
        /// </summary>
        public void digui(DataTable dt, List<TreeModel> tree,int num)
        {
            for (int m = 0; m < tree.Count; m++)
            {
                tree[m].children = new List<TreeModel>();
                for (int i = 0; i < dt.Rows.Count; i++)//第一次循环,得到所有根节点的子集
                {
                    var strLen = dt.Rows[i]["hnumber"].ToString().Split('.');
                    if (strLen.Length == num && dt.Rows[i]["hnumber"].ToString().Contains(tree[m].id + "."))
                    {
                        TreeModel tbjson = new TreeModel();
                        tbjson.id = dt.Rows[i]["hnumber"].ToString();
                        tbjson.title = dt.Rows[i]["hname"].ToString();
                        tree[m].children.Add(tbjson);
                    }
                }
                var strLens = tree[m].id.Split('.');
                for (int i = 0; i < tree[m].children.Count; i++)
                {
                    digui(dt, tree[m].children, strLens.Length + 2);//再次用子集去循环,拿出子集的子集
                }
            }
 
        }
        #endregion
 
        #region 组织架构设置 列表
        [Route("Gy_DutyBill/Gy_DutyBillList")]
        [HttpGet]
        public object Gy_DutyBillList(string sWhere)
        {
            try
            {
                string sql1 = string.Format(@"select  * from h_v_Gy_DutyList where 1=1 ");
 
                ds = oCN.RunProcReturn(sql1+ sWhere+ " order by 组织架构代码", "h_v_Gy_DutyList");
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 组织架构设置 保存
        [Route("Gy_DutyBill/Gy_DutyBillEdit")]
        [HttpPost]
        public object Xt_CheckFlowBillEdit([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string msg3 = _value.ToString();
            string[] sArray = msg3.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg1 = sArray[0].ToString();
            string msg2 = sArray[1].ToString();
            string msg4 = sArray[2].ToString();
            SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
          
            try
            {
                Gy_Duty Duty = JsonConvert.DeserializeObject<Gy_Duty>(msg1);
 
 
                //获取上级的代码
                var topHNum = DBUtility.ClsPub.GetParentCode(Duty.HNumber);
 
                if (topHNum == "")
                {
                    Duty.HParentID = 0;
                }
                else
                {
                    ds = oCN.RunProcReturn("select * from Gy_Duty where HStopflag=0  and HNumber='" + topHNum + "'  or HNumber='" + Duty.HNumber + "' ", "Gy_Duty");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "上级代码不存在或被禁用!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    if (msg4 == "1") {
                        if (ds.Tables[0].Rows.Count == 2)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "代码重复!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                   
                    Duty.HParentID = int.Parse(ds.Tables[0].Rows[0]["HItemID"].ToString());
                }
 
                if (Duty.HItemID != 0)
                {
                    ds = oCN.RunProcReturn("select * from Gy_Duty where HItemID='" + Duty.HItemID + "' ", "Gy_Duty");
                    var newHNum = Duty.HNumber.Split('.');
                    var oldHNum = ds.Tables[0].Rows[0]["HNumber"].ToString().Split('.');
 
                    if (newHNum.Length > oldHNum.Length && Duty.HItemID == Duty.HParentID)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "新代码不能是自己的下级的子项目!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    ds = oCN.RunProcReturn("select * from Gy_Duty where HParentID='" + Duty.HItemID + "' ", "Gy_Duty");
 
                    if (ds.Tables[0].Rows.Count>0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "该项目下面有子项目,不能修改!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
 
                Duty.HEndFlag = 1;
                Duty.HShortNumber = DBUtility.ClsPub.GetShortNumber(Duty.HNumber);
                Duty.HLevel = DBUtility.ClsPub.GetLevel(Duty.HNumber);
 
                if (Duty.HShortNumber == "")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "短代码为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                oCN.BeginTran();
 
                if (msg4 == "1")
                {  
                    //保存
                    //保存完毕后处理
                    oCN.RunProc("Insert into Gy_Duty " +
                       " (HNumber,HName,HHelpCode,HShortNumber,HParentID,HUserGroupID" +
                       ",HLevel,HEndFlag,HStopflag,HRemark) " +
                       " Values('" + Duty.HNumber + "','" + Duty.HName + "','" + Duty.HHelpCode + "','" + Duty.HShortNumber + "'," + Duty.HParentID.ToString() + "," + Duty.HUserGroupID.ToString() +
                       "," + Duty.HLevel.ToString() + "," + Duty.HEndFlag + "," + Convert.ToString(Duty.HStopflag ? 1 : 0) + ",'" + Duty.HRemark + "')", ref DBUtility.ClsPub.sExeReturnInfo);
                }
                else
                {
                    oCN.RunProc("Update Gy_Duty set " +
                        " HNumber='" + Duty.HNumber + "'" +
                        ",HName='" + Duty.HName + "'" +
                        ",HShortNumber='" + Duty.HShortNumber + "'" +
                        ",HHelpCode='" + Duty.HHelpCode + "'" +
                        ",HLevel=" + Duty.HLevel.ToString() +
                        ",HParentID=" + Duty.HParentID.ToString() +
                        ",HUserGroupID=" + Duty.HUserGroupID.ToString() +
                        ",HEndflag=" + Duty.HEndFlag +
                        ",HStopflag=" + Convert.ToString(Duty.HStopflag ? 1 : 0) +
                        ",HRemark= '" + Duty.HRemark + "' Where HItemID=" + Duty.HItemID, ref DBUtility.ClsPub.sExeReturnInfo);
                }
 
                //修改上级为非末级代码
                oCN.RunProc("Update Gy_Duty set HEndflag=0 where HItemID=" + Duty.HParentID, ref DBUtility.ClsPub.sExeReturnInfo);
                oCN.Commit();
 
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "保存成功!";
                objJsonResult.data = 1;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "保存失败!" + e.ToString();
                objJsonResult.data = 1;
                return objJsonResult;
            }
        }
 
        #endregion
 
        #region  组织架构设置 删除
        [Route("Gy_DutyBill/Gy_DutyBillDel")]
        [HttpGet]
        public object Gy_DutyBillDel(string HItemID, string user)
        {
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (string.IsNullOrWhiteSpace(HItemID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "HItemID为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                ds = oCN.RunProcReturn("select * from Gy_Duty where HParentID='" + HItemID + "' ", "Gy_Duty");
 
                if (ds.Tables[0].Rows.Count > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "该项目下面有子项目,不能删除!,请先删除子项目!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                ds = oCN.RunProcReturn("select * from Gy_Duty where HItemID='" + HItemID + "' ", "Gy_Duty");
 
                oCN.BeginTran();//开始事务
 
                //删除数据
                oCN.RunProc("Delete from Gy_Duty where HItemID='" + HItemID + "'");
                //修改上级为末级
                oCN.RunProc("Update Gy_Duty set HEndflag=1 where HItemID=" + ds.Tables[0].Rows[0]["HParentID"].ToString());
 
                oCN.Commit();//提交事务
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "* 数据删除成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
 
            }
            catch (Exception e)
            {
                //回滚
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region  组织架构设置 检测
        [Route("Gy_DutyBill/Gy_DutyBillTest")]
        [HttpGet]
        public object Gy_DutyBillTest(string HItemID, string user)
        {
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (string.IsNullOrWhiteSpace(HItemID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "HItemID为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                oCN.BeginTran();//开始事务
 
                //检测数据
                oCN.RunProc("exec h_p_Gy_Duty " + HItemID );
 
                oCN.Commit();//提交事务
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
 
            }
            catch (Exception e)
            {
                //回滚
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
    }
}