cloud-archive.go 2.99 KB
/**********************************************
 * 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
}