forked from yunnet/gardens
/
EquipmentMeterConfigController.go
156 lines (135 loc) · 4.32 KB
/
EquipmentMeterConfigController.go
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
package controllers
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/orm"
"github.com/yunnet/gardens/enums"
"github.com/yunnet/gardens/models"
"strconv"
"strings"
"time"
)
type EquipmentMeterConfigController struct {
BaseController
}
func (this *EquipmentMeterConfigController) Prepare() {
this.BaseController.Prepare()
this.checkAuthor("DataGrid", "DataList")
}
func (this *EquipmentMeterConfigController) Index() {
this.Data["pageTitle"] = "电表管理"
this.Data["showMoreQuery"] = true
this.Data["activeSidebarUrl"] = this.URLFor(this.controllerName + "." + this.actionName)
this.setTpl()
this.LayoutSections = make(map[string]string)
this.LayoutSections["headcssjs"] = "equipmentmeterconfig/index_headcssjs.html"
this.LayoutSections["footerjs"] = "equipmentmeterconfig/index_footerjs.html"
//页面里按钮权限控制
this.Data["canEdit"] = this.checkActionAuthor("EquipmentMeterConfigController", "Edit")
this.Data["canDelete"] = this.checkActionAuthor("EquipmentMeterConfigController", "Delete")
}
func (this *EquipmentMeterConfigController) DataGrid() {
var params models.EquipmentMeterConfigQueryParam
json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms)
data, total := models.EquipmentMeterConfigPageList(¶ms)
result := make(map[string]interface{})
result["total"] = total
result["rows"] = data
this.Data["json"] = result
this.ServeJSON()
}
func (this *EquipmentMeterConfigController) DataList() {
var params = models.EquipmentMeterConfigQueryParam{}
data := models.EquipmentMeterConfigDataList(¶ms)
this.jsonResult(enums.JRCodeSucc, "", data)
}
func (this *EquipmentMeterConfigController) Edit() {
if this.Ctx.Request.Method == "POST" {
this.Save()
}
Id, _ := this.GetInt(":id", 0)
m := models.EquipmentMeterConfig{Id: Id}
if Id > 0 {
o := orm.NewOrm()
err := o.Read(&m)
if err != nil {
this.pageError("数据无效,请刷新后重试")
}
} else {
m.Used = 0
m.Ct = 1
m.Pt = 1
m.MeterAddress = 1
}
this.Data["m"] = m
this.setTpl("equipmentmeterconfig/edit.html", "shared/layout_pullbox.html")
this.LayoutSections = make(map[string]string)
this.LayoutSections["footerjs"] = "equipmentmeterconfig/edit_footerjs.html"
}
//add | update
func (this *EquipmentMeterConfigController) Save() {
var err error
m := models.EquipmentMeterConfig{}
//获取form里的值
if err = this.ParseForm(&m); err != nil {
this.jsonResult(enums.JRCodeFailed, "获取数据失败", m.Id)
}
//id := this.Input().Get("Id")
//m.Id, _ = strconv.Atoi(id)
//m.DTU_no = this.GetString("DTU_no")
//
//tmpInt := this.Input().Get("MeterAddress")
//m.MeterAddress, _ = strconv.Atoi(tmpInt)
//
//m.MeterTypeNO = this.GetString("MeterTypeNO")
//m.GatewayNO = this.GetString("GatewayNO")
//
//tmpInt = this.Input().Get("GatewayQzone")
//m.GatewayQzone, _ = strconv.Atoi(tmpInt)
//
//tmpInt = this.Input().Get("GatewayAddress")
//m.GatewayAddress, _ = strconv.Atoi(tmpInt)
//
//tmpInt = this.Input().Get("GatewaySite")
//m.GatewaySite, _ = strconv.Atoi(tmpInt)
//
//tmpInt = this.Input().Get("Pt")
//m.Pt, _ = strconv.Atoi(tmpInt)
//
//tmpInt = this.Input().Get("Ct")
//m.Ct, _ = strconv.Atoi(tmpInt)
//
//m.LoopName = this.GetString("LoopName")
m.ChangeUser = this.curUser.RealName
//m.ChangeDate = time.Now()
o := orm.NewOrm()
if m.Id == 0 {
m.CreateUser = this.curUser.RealName
m.CreateDate = time.Now()
if _, err = o.Insert(&m); err == nil {
this.jsonResult(enums.JRCodeSucc, "添加成功", m.Id)
} else {
this.jsonResult(enums.JRCodeFailed, "添加失败", m.Id)
}
} else {
if _, err = o.Update(&m, "DTU_no", "MeterAddress", "MeterTypeNO", "GatewayNO", "GatewayQzone", "GatewayAddress", "GatewaySite", "Pt", "Ct", "LoopName", "Used", "ChangeUser", "ChangeDate"); err == nil {
this.jsonResult(enums.JRCodeSucc, "编辑成功", m.Id)
} else {
this.jsonResult(enums.JRCodeFailed, "编辑失败", m.Id)
}
}
}
func (this *EquipmentMeterConfigController) Delete() {
strs := this.GetString("ids")
ids := make([]int, 0, len(strs))
for _, str := range strings.Split(strs, ",") {
if id, err := strconv.Atoi(str); err == nil {
ids = append(ids, id)
}
}
if num, err := models.EquipmentMeterConfigBatchDelete(ids); err == nil {
this.jsonResult(enums.JRCodeSucc, fmt.Sprintf("成功删除 %d 项", num), 0)
} else {
this.jsonResult(enums.JRCodeFailed, "删除失败", 0)
}
}