/
CustomerForMeter.go
109 lines (92 loc) · 2.9 KB
/
CustomerForMeter.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
// Copyright 2018 gardens Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"encoding/json"
"github.com/astaxie/beego/orm"
"strconv"
)
type (
CustomerForMeter struct {
CustomerName string `orm:"column(customer_name)"`
DTU_no string `orm:"column(dtu_no)"`
MeterAddress int `orm:"column(meter_address)"`
MeterTypeNO string `orm:"column(meter_type_no)"`
LoopName string `orm:"column(loop_name)"`
}
RootItem struct {
Name string `json:"name"`
CompanyItemList []*CompanyItem `json:"children"`
}
CompanyItem struct {
Name string `json:"name"`
DtuItemList []*DtuItem `json:"children"`
}
DtuItem struct {
Name string `json:"name"`
MeterItemList []*MeterItem `json:"children"`
}
MeterItem struct {
Name string `json:"name"`
Desc string `json:"desc"`
}
)
func CustomerForMeterDataList() ([]*CustomerForMeter, error) {
data := make([]*CustomerForMeter, 0)
sql := "SELECT customer_name, dtu_no, meter_address, meter_type, loop_name FROM v_customer_room_meter"
if _, err := orm.NewOrm().Raw(sql).QueryRows(&data); err != nil {
return nil, err
}
return data, nil
}
func GetCustomerForMeter() (string, error) {
data, err := CustomerForMeterDataList()
if err != nil {
return "", err
}
var master *RootItem
var company *CompanyItem
var dtu *DtuItem
master = &RootItem{Name: "电可托"}
for _, row := range data {
meter := MeterItem{strconv.Itoa(row.MeterAddress), row.LoopName}
var dtu_cur *DtuItem
var company_cur *CompanyItem
if company == nil {
company_cur = &CompanyItem{Name: row.CustomerName}
master.CompanyItemList = append(master.CompanyItemList, company_cur)
} else {
company_cur = company
if company_cur.Name != row.CustomerName {
company_cur = &CompanyItem{Name: row.CustomerName}
master.CompanyItemList = append(master.CompanyItemList, company_cur)
}
}
if dtu == nil {
dtu_cur = &DtuItem{Name: row.DTU_no}
company_cur.DtuItemList = append(company_cur.DtuItemList, dtu_cur)
} else {
dtu_cur = dtu
if dtu_cur.Name != row.DTU_no {
dtu_cur = &DtuItem{Name: row.DTU_no}
company_cur.DtuItemList = append(company_cur.DtuItemList, dtu_cur)
}
}
dtu_cur.MeterItemList = append(dtu_cur.MeterItemList, &meter)
dtu = dtu_cur
company = company_cur
}
jsondata, _ := json.Marshal(master)
return string(jsondata), nil
}