yzh
2022-05-26 3b18a48485f7207438d9d0eb3038d979e069431d
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
<%@ WebHandler Language="C#" Class="GwOpGroupHandler" %>
 
using System;
using System.Web;
using Model;
using Common;
using Dao;
using System.Linq;
 
public class GwOpGroupHandler : PageHandler<Model.SysUser>
{
    private GwOpGroupDao _Dao = new GwOpGroupDao();
    
    public override JsonPageResult ProcessRequestInternal(PageContext<Model.SysUser> context)
    {
        string action = context.GetString("action");
 
        switch (action)
        {
            case "delete":
                return Delete(context);
                
            case "save":
                return Save(context);
                
            case "getGroup":
                return GetGroup(context);
 
            default:
                throw new Exception("Invalid Action=" + action);
        }
    }
 
    private JsonPageResult GetGroup(PageContext<SysUser> context)
    {
        int groupID = context.GetInt("groupid");
        
        return new JsonPageResult(true, _Dao.GetGroup(groupID));
    }
 
    private JsonPageResult Delete(PageContext<SysUser> context)
    {
        int groupID = context.GetInt("groupID");
        
        _Dao.Delete(groupID);
        
        return new JsonPageResult(true, "通道组删除成功!");
    }
    
    private JsonPageResult Save(PageContext<SysUser> context)
    {
        int groupID = context.GetInt("groupID");
        string groupName = context.GetString("groupName");
        string groupData = context.GetString("groupData");
 
        if (string.IsNullOrEmpty(groupName))
        {
            throw new ArgumentException("通道组名称不能为空!");
        }
        
        var array = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JArray>(groupData) ;
 
        if (string.IsNullOrEmpty(groupData) || array == null)
        {
            throw new ArgumentException("通道数据异常,请重新设置!");
        }
 
        foreach (var a in array)
        {
            if (a.Value<int>("weight") < 0 || a.Value<int>("weight") > 10)
            {
                throw new ArgumentException("每个通道权重范围必须在0-10之间!");
            }
        }
 
        if (array.Sum(c => c.Value<int>("weight")) == 0)
        {
            throw new ArgumentException("通道权重不能全部为0!");
        }
 
        if (_Dao.IsGroupNameExists(groupID, groupName))
        {
            throw new ArgumentException("通道组名称重复,请重新输入!");
        }
 
        if (groupID > 0)
        {
            _Dao.Update(groupID, groupName, groupData);
        }
        else
        {
            _Dao.Add(groupName, groupData);
        }
 
                
        return new JsonPageResult(true, "通道组保存成功!");
    }
}