cloud-archive.go
2.99 KB
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
/**********************************************
* Copyright(c) 2017 IceSimba
* All rights reserved.
*
* FileName: icesimba.cloud.go
* Author: dsl
* Version: 1.0
* Date: 2017/08/03
* Description: 云存档model
* Others:
* Function List:
1.…………
2.…………
* History:
1.Date:
Author:
Modification:
**********************************************/
package models
import (
"database/sql"
"errors"
"fmt"
// "log"
// "runtime"
"time"
. "client/icesimba.cloud/utils"
db "client/icesimba.cloud/database"
)
type CloudArchive struct {
Id int
UserId string `json:"user_id" form:"user_id"`
GameId string `json:"game_id" form:"game_id"`
DataPacket string `json:"data_packet" form:"data_packet"`
}
type CloudArchiveJson struct {
DataPacket string `json:"data_packet" form:"data_packet"`
CreateTime int64 `json:"createtime"`
}
type CreateCAJson struct {
CreateTime int64 `json:"createtime"`
}
func (cloud *CloudArchive) PostCloudArchive() (ca CreateCAJson, err error) {
createtime := time.Now().UnixNano() / 1e6
_, err = cloud.GetCloudArchive()
if err != nil {
err = cloud.InsertCloudArchive(createtime)
} else {
err = cloud.UpdateCloudArchive(createtime)
}
ca.CreateTime = createtime
return
}
func (cloud *CloudArchive) InsertCloudArchive(createtime int64) (err error) {
uuid := GetUuid()
sqlIn := "INSERT INTO ICE_CLOUDARCHIVE(caid,uid,gid,datapacket,createtime) VALUES (?, ?, ?, ?, ?)"
_, err = db.SqlDB.Exec(
sqlIn,
uuid,
cloud.UserId,
cloud.GameId,
cloud.DataPacket,
createtime,
)
if err != nil {
err = errors.New("failed to insert data")
return
}
return
}
func (cloud *CloudArchive) UpdateCloudArchive(createtime int64) (err error) {
sqlUp := "update ice_cloudarchive set datapacket=?, createtime=? where uid=? and gid=?"
stmt, err := db.SqlDB.Prepare(sqlUp)
defer stmt.Close()
_, err = stmt.Exec(
cloud.DataPacket,
createtime,
cloud.UserId,
cloud.GameId,
)
if err != nil {
err = errors.New("failed to update data")
return
}
return
}
func (cloud *CloudArchive) GetCloudArchive() (data CloudArchiveJson, err error) {
fmt.Printf("SELECT datapacket FROM ICE_CLOUDARCHIVE WHERE uid=%s AND gid=%s\n", cloud.UserId, cloud.GameId)
row := db.SqlDB.QueryRow("SELECT datapacket,createtime FROM ICE_CLOUDARCHIVE WHERE uid=? AND gid=?", cloud.UserId, cloud.GameId)
var (
createtime sql.NullInt64
packet sql.NullString
)
err = row.Scan(
&packet,
&createtime,
)
if createtime.Valid {
data.CreateTime = createtime.Int64
}
if packet.Valid {
data.DataPacket = packet.String
}
if err != nil {
fmt.Println(err)
err = errors.New("cloud archive not exist")
}
return
}
func QueryCloudArchiveAll() (out []CloudArchive, err error) {
rows, err := db.SqlDB.Query("SELECT uid,gid,datapacket FROM ICE_CLOUDARCHIVE")
for rows.Next() {
cloud := CloudArchive{}
err = rows.Scan(
&cloud.UserId,
&cloud.GameId,
&cloud.DataPacket,
)
out = append(out, cloud)
}
if err != nil {
return
}
return
}