zhangzengfei
2023-09-04 e8e536d1cb52d2126c8c7ce2ba1c7a76f7208678
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
package models
 
import (
    "basic.com/valib/logger.git"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "strings"
    "vamicro/config"
)
 
var db *gorm.DB
var err error
 
// Init creates a connection to mysql database and
// migrates any new models
func Init() {
    db, err = gorm.Open(config.DBconf.Name, "../config/system-service.db")
    if err != nil {
        logger.Debug("db open error ", err)
    }
    db.LogMode(true)
    db.AutoMigrate(
        &SysInit{},
        &Device{},
        &DeviceApp{},
        &DeviceSdk{},
        &Dictionary{},
        &Voice{},
        &LocalConfig{},
        &Cluster{},
        &Node{},
        &ResourceConfig{},
        &SysUser{},
        &SysMenu{},
        &SysRole{},
        &SysRoleMenu{},
        &SysUserRole{},
        &SysUserMenu{},
        &SysSetting{},
        &DefHeadPic{},
        &AuthConfig{},
        &AuthDevice{},
    )
    InitSysSettingData()
    InitDefHeadPicData()
 
    initDic()
 
    //添加传感器字典配置
    addSensorDic()
    //初始化事件推送字典
    setDataPushDic()
    //初始化人脸跟踪的字典
    setFaceTrackDic()
}
 
//GetDB ...
func GetDB() *gorm.DB {
    return db
}
 
func CloseDB() {
    db.Close()
}
 
func initDic() {
    db.Exec("INSERT INTO dictionary SELECT 'dcf9a925-dd1d-4dc4-a30d-002f976366a4', '1', '是', 'endRecord', '是', 1, '0' where not exists (select 1 from dictionary where id='dcf9a925-dd1d-4dc4-a30d-002f976366a4')")
    db.Exec("INSERT INTO dictionary SELECT 'ec3e1a20-c5fd-4d3c-a57b-2d10f114bb8f', '0', '否', 'endRecord', '否', 2, '0' where not exists (select 1 from dictionary where id='ec3e1a20-c5fd-4d3c-a57b-2d10f114bb8f')")
    db.Exec("INSERT INTO dictionary SELECT '4a4a1f9c-1431-4c56-ac95-12792ff51fd1', '1', '是', 'change', '是', 1, '0' where not exists (select 1 from dictionary where id='4a4a1f9c-1431-4c56-ac95-12792ff51fd1')")
    db.Exec("INSERT INTO dictionary SELECT '8c330403-c36d-440f-92a1-b4101abcc168', '0', '否', 'change', '否', 2, '0' where not exists (select 1 from dictionary where id='8c330403-c36d-440f-92a1-b4101abcc168')")
    //添加默认的通道设置
}
 
func addSensorDic() {
    db.Exec("INSERT INTO dictionary SELECT 'edded040-f9a4-4bbb-9eb1-23a01eaf595b', 'FaceTemperature', '温度传感器', 'sensorType', '人脸测温', 1, '0' where not exists (select 1 from dictionary where id='edded040-f9a4-4bbb-9eb1-23a01eaf595b')")
}
 
func setDataPushDic() {
    var sqls []string
    //sqls = append(sqls, "delete from dictionary where type='EVENTRULETOPIC';")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select 'B2C94522-9077-768C-595D-85502C6A315E', 'camera', '摄像机', 'EVENTRULETOPIC', '主题-摄像机', 1, '0' where not exists (select 1 from dictionary where id='B2C94522-9077-768C-595D-85502C6A315E');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select 'E945DFFC-CD7D-AEED-3F0B-7C88FCD9134E', 'dbtable', '底库', 'EVENTRULETOPIC', '主题-底库', 2, '0' where not exists (select 1 from dictionary where id='E945DFFC-CD7D-AEED-3F0B-7C88FCD9134E');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '213E9B4A-4E5F-8E56-8279-6C9045621068', 'task', '场景', 'EVENTRULETOPIC', '主题-场景', 3, '0' where not exists (select 1 from dictionary where id='213E9B4A-4E5F-8E56-8279-6C9045621068');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '83536D8B-83D2-59B1-655B-1636DBFD8201', 'alarmLevel', '事件等级', 'EVENTRULETOPIC', '主题-报警等级', 5, '0' where not exists (select 1 from dictionary where id='83536D8B-83D2-59B1-655B-1636DBFD8201');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '0F082502-CEA8-AAA5-FB43-DD555F0BE7D7', 'cameraName', '设备名称', 'EVENTRULETOPIC', 'custom,option', 1, 'B2C94522-9077-768C-595D-85502C6A315E' where not exists (select 1 from dictionary where id='0F082502-CEA8-AAA5-FB43-DD555F0BE7D7');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '1DEC0839-850A-C600-9070-69C871654CE6', 'cameraAddr', '设备地址', 'EVENTRULETOPIC', 'custom,option', 2, 'B2C94522-9077-768C-595D-85502C6A315E' where not exists (select 1 from dictionary where id='1DEC0839-850A-C600-9070-69C871654CE6');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '806E7B4A-9A42-4E79-8708-481BA748EEC8', 'alarmRules.#.alarmLevel', '级别', 'EVENTRULETOPIC', 'option', 0, '83536D8B-83D2-59B1-655B-1636DBFD8201' where not exists (select 1 from dictionary where id='806E7B4A-9A42-4E79-8708-481BA748EEC8');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '7BBDDF13-AF28-49B9-AFAF-78E535C7CDDB', 'baseInfo.#.tableName', '名称', 'EVENTRULETOPIC', '底库的子选项', 0, 'E945DFFC-CD7D-AEED-3F0B-7C88FCD9134E' where not exists (select 1 from dictionary where id='7BBDDF13-AF28-49B9-AFAF-78E535C7CDDB');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '1C9392CD-9FC4-4A23-8DCD-AE2B8E53AF49', 'baseInfo.#.targetName', '人员姓名', 'EVENTRULETOPIC', '底库的子选项', 1, 'E945DFFC-CD7D-AEED-3F0B-7C88FCD9134E' where not exists (select 1 from dictionary where id='1C9392CD-9FC4-4A23-8DCD-AE2B8E53AF49');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '7455827A-43A6-4A9D-9D6F-F2D030E3C7D9', 'baseInfo.#.monitorLevel', '人员等级', 'EVENTRULETOPIC', '底库的子选项', 2, 'E945DFFC-CD7D-AEED-3F0B-7C88FCD9134E' where not exists (select 1 from dictionary where id='7455827A-43A6-4A9D-9D6F-F2D030E3C7D9');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '950DA518-8D94-4293-A686-D85E8F94BD0B', 'baseInfo.#.labels.idCard', '身份证号', 'EVENTRULETOPIC', '底库的子选项', 3, 'E945DFFC-CD7D-AEED-3F0B-7C88FCD9134E' where not exists (select 1 from dictionary where id='950DA518-8D94-4293-A686-D85E8F94BD0B');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '7B72769F-CAFE-458B-8589-B792B4BF123C', 'baseInfo.#.labels.phone', '手机号', 'EVENTRULETOPIC', '底库的子选项', 4, 'E945DFFC-CD7D-AEED-3F0B-7C88FCD9134E' where not exists (select 1 from dictionary where id='7B72769F-CAFE-458B-8589-B792B4BF123C');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '58B67911-CF9F-4468-9B46-3DB9F7765816', 'baseInfo.#.labels.plate', '车牌号', 'EVENTRULETOPIC', '底库的子选项', 5, 'E945DFFC-CD7D-AEED-3F0B-7C88FCD9134E' where not exists (select 1 from dictionary where id='58B67911-CF9F-4468-9B46-3DB9F7765816');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '1EC955EA-E6A5-4CA6-AD9A-4769D3CE96D6', 'taskName', '名称', 'EVENTRULETOPIC', '场景的子选项', 5, '213E9B4A-4E5F-8E56-8279-6C9045621068' where not exists (select 1 from dictionary where id='1EC955EA-E6A5-4CA6-AD9A-4769D3CE96D6');")
    //sqls = append(sqls, "update dictionary set value='alarmRules.#.alarmLevel' where id='806E7B4A-9A42-4E79-8708-481BA748EEC8';")
    db.Exec(strings.Join(sqls, ""))
}
 
//添加一些是否的字典值
func setFaceTrackDic() {
    var sqls []string
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '51739a24-20bd-4440-8ef5-47fe93200536', '1', '是', 'bForceSend', '是', 1, '0' where not exists (select 1 from dictionary where id='51739a24-20bd-4440-8ef5-47fe93200536');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select 'fd8f9b4b-679b-4504-a295-7bb64fcd9ec5', '0', '否', 'bForceSend', '否', 2, '0' where not exists (select 1 from dictionary where id='fd8f9b4b-679b-4504-a295-7bb64fcd9ec5');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select 'a25055c1-50be-4ed8-ab29-da670958fbd4', '1', '是', 'bForcePush', '是', 1, '0' where not exists (select 1 from dictionary where id='a25055c1-50be-4ed8-ab29-da670958fbd4');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '3c68a1fa-ae57-4e36-b418-719ef28db71e', '0', '否', 'bForcePush', '否', 2, '0' where not exists (select 1 from dictionary where id='3c68a1fa-ae57-4e36-b418-719ef28db71e');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '93d0d77a-ba85-47c7-867c-34f7fe1be553', '1', '是', 'bForceSave', '是', 1, '0' where not exists (select 1 from dictionary where id='93d0d77a-ba85-47c7-867c-34f7fe1be553');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '23dc5d8b-7894-4692-9042-2fa0aaf13375', '0', '否', 'bForceSave', '否', 2, '0' where not exists (select 1 from dictionary where id='23dc5d8b-7894-4692-9042-2fa0aaf13375');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select 'e1677c52-844a-4f03-afcd-391b477de87b', '1', '是', 'bReid', '是', 1, '0' where not exists (select 1 from dictionary where id='e1677c52-844a-4f03-afcd-391b477de87b');")
 
    // 进出入统计, 方向
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select 'f6e4104a-cb6b-41bc-b368-69c8a104466b', 'up', '上', 'inside', '上', 1, '0' where not exists (select 1 from dictionary where id='f6e4104a-cb6b-41bc-b368-69c8a104466b');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '7f588975-ab59-4ab5-9c9e-af92f64d5353', 'down', '下', 'inside', '下', 2, '0' where not exists (select 1 from dictionary where id='7f588975-ab59-4ab5-9c9e-af92f64d5353');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select 'b24196de-c891-42b2-bf4a-dabfb2870397', 'right', '左', 'inside', '左', 3, '0' where not exists (select 1 from dictionary where id='b24196de-c891-42b2-bf4a-dabfb2870397');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select 'b4535930-e1a9-41b1-a4e5-2b3831f37748', 'left', '右', 'inside', '右', 4, '0' where not exists (select 1 from dictionary where id='b4535930-e1a9-41b1-a4e5-2b3831f37748');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '1a691da7-989f-4492-958a-7e1f4a49b374', 'up', '上', 'outside', '上', 1, '0' where not exists (select 1 from dictionary where id='1a691da7-989f-4492-958a-7e1f4a49b374');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '34aadb9b-3132-4e60-a73f-dc4b6f67a619', 'down', '下', 'outside', '下', 2, '0' where not exists (select 1 from dictionary where id='34aadb9b-3132-4e60-a73f-dc4b6f67a619');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '04d56fa3-2285-470c-be42-a15d7ac4273f', 'right', '左', 'outside', '左', 3, '0' where not exists (select 1 from dictionary where id='04d56fa3-2285-470c-be42-a15d7ac4273f');")
    sqls = append(sqls, "INSERT INTO dictionary(`id`,`value`,`name`,`type`,`description`,`sort`,`parent_id`) select '5c7a68f2-dd0f-4c28-b703-f9626e0d19a2', 'left', '右', 'outside', '右', 4, '0' where not exists (select 1 from dictionary where id='5c7a68f2-dd0f-4c28-b703-f9626e0d19a2');")
 
    db.Exec(strings.Join(sqls, ""))
}