* @version : HUOSDK 8.5 */ namespace huoIdentify\model; class IdentifyDayMotModel extends LogModel { protected $name = 'identify_day_mot'; protected $pk = 'id'; protected $table_date = ''; protected $table_prefix = ''; protected $partition_data = []; protected $partition_field = 'date'; protected $partition_rule = ['type' => 'month', 'expr' => 1]; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { if (empty($data['date'])) { return false; } if (empty($data['create_time'])) { $data['create_time'] = time(); } if (empty($data['update_time'])) { $data['update_time'] = time(); } $data['date'] = date('Y-m-d', strtotime($data['date'])); /* 字段校验 */ $_fields = $this->computeTable($data['date'])->getTableFields(); $_data = []; foreach ($_fields as $_v) { if (isset($data[$_v])) { $_data[$_v] = $data[$_v]; } } $_id = $this->computeTable($_data['date'])->insert($_data, true, true); if (false === $_id) { return false; } /* TAG缓存操作 */ return $_id; } /** * 获取信息 * * @param string $date 日期 * @param int $mem_id 玩家ID * * @return array|false */ public function getInfoByDateMem($date, $mem_id) { $date = date('Y-m-d', strtotime($date)); $_map = [ 'date' => $date, 'mem_id' => $mem_id ]; $_data = $this->computeTable($_map['date'])->where($_map)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } if (empty($_data)) { return []; } return $_data; } /** * 更新单条数据 * * @param array $data 数据 * @param int $id ID * * @return bool */ public function updateData($data, $id) { if (empty($data['date'])) { return false; } $data['date'] = date('Y-m-d', strtotime($data['date'])); $_data = $this->getInfoByDateMem($data['date'], $data['mem_id']); if (!empty($_data)) { $_data = array_merge($_data, $data); } $_id = $this->addData($_data); return $_id; } /** * 生成表 * * @param int $create_time * * @return bool|int * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ function checkTable($create_time = 0) { parent::checkTable($create_time); $_sql = "CREATE TABLE IF NOT EXISTS `$this->table` ("; $_sql .= " `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',"; $_sql .= " `date` date NOT NULL DEFAULT '".$this->partition_data[$this->partition_field]."' COMMENT '日期',"; $_sql .= " `mem_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '玩家ID',"; $_sql .= " `online_duration` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '在线时长',"; $_sql .= " `create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '创建时间',"; $_sql .= " `update_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '更新时间',"; $_sql .= " PRIMARY KEY (`id`) USING BTREE,"; $_sql .= " UNIQUE KEY `idm_dm_unique` (`date`,`mem_id`) USING BTREE"; $_sql .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='玩家每日在线时长统计 mot:mem_online_time';"; return $this->execute($_sql); } }