package log

import (
	"fmt"
	"log-server/global"
	"time"
)

type Loging struct {
	Id           uint   `json:"id"`
	SimulatorIp  string `json:"simulator_ip"`
	SimulatorMac string `json:"simulator_mac"`
	PcCode       string `json:"pc_code"`
	PcIp         string `json:"pc_ip"`
	PcMac        string `json:"pc_mac"`
	DeviceId     string `json:"device_id"`
	Account      string `json:"account"`
	GameId       int    `json:"game_id"`
	Coding       int    `json:"coding"`
	NodeCoding   int    `json:"node_coding"`
	TypeCoding   int    `json:"type_coding"`
	ComputerType int    `json:"computer_type"`
	EnvCode      int    `json:"env_code"`
	LogUuid      string `json:"log_uuid"`
	Operator     string `json:"operator"`
	Status       int    `json:"status"` // 1成功2失败
	AccountType  int    `json:"account_type"`
	Remarks      string `json:"remarks"`
	TaskType     int    `json:"task_type"` //新增0 活跃1
	ScriptType   int    `json:"script_type"`
	CreateDate   string `json:"create_date"`
	CreateTime   string `json:"create_time"` // 创建时间
}

func (Loging) TableName() string {
	return "loging"
}

func (Loging) CreateLogingTable() (err error) {
	tableName := fmt.Sprintf("loging_%s", time.Now().Add(+time.Hour*24*31).Format("2006-01"))
	sql := "DROP TABLE IF EXISTS `" + tableName + "`"
	err = global.GVA_DB.Exec(sql).Error
	if err != nil {
		return err
	}
	sql = fmt.Sprintf("CREATE TABLE `%s` (", tableName)
	sql += "  `id` int(11) NOT NULL AUTO_INCREMENT,"
	sql += " `simulator_ip` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '模拟器ip',"
	sql += " `simulator_mac` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '模拟器mac地址',"
	sql += " `pc_code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '租机编号',"
	sql += " `pc_mac` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '租机mac地址',"
	sql += " `pc_ip` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '租机ip',"
	sql += " `device_id` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '绑定设备id',"
	sql += " `account` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '账号',"
	sql += " `game_id` int NOT NULL COMMENT '游戏id',"
	sql += " `coding` int NOT NULL COMMENT '日志编号',"
	sql += " `node_coding` int NOT NULL COMMENT '日志节点编号',"
	sql += " `type_coding` int NOT NULL COMMENT '日志类型编号',"
	sql += " `computer_type` int DEFAULT '1' COMMENT '电脑类型1电脑2板子',"
	sql += " `env_code` int NOT NULL COMMENT '环境编号',"
	sql += " `account_type` int DEFAULT '1' COMMENT '账号类型',"
	sql += " `status` tinyint(1) DEFAULT '1' COMMENT '状态1成功2失败',"
	sql += " `remarks` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '备注',"
	sql += " `log_uuid` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '日志唯一id',"
	sql += " `operator` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '脚本开发员',"
	sql += " `create_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',"
	sql += " `create_date` date DEFAULT NULL COMMENT '创建日期',"
	sql += " `task_type` tinyint(1) DEFAULT '0' COMMENT '新增0 活跃1',"
	sql += " `script_type` tinyint(1) DEFAULT '0',"
	sql += " PRIMARY KEY (`id`) USING BTREE,"
	sql += " KEY `log_uuid` (`log_uuid`) USING BTREE,"
	sql += " KEY `date_game_id` (`create_date`,`game_id`),"
	sql += " KEY `coding` (`coding`)"
	sql += ") ENGINE=InnoDB DEFAULT CHARSET=utf8;"
	err = global.GVA_DB.Exec(sql).Error
	if err != nil {
		return err
	}
	return
}
