'ID', 'mall_id' => 'Mall ID', 'user_id' => 'User ID', 'item_id' => 'Item ID', 'log_type' => 'Log Type', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'material_log_id' => 'Material Log ID', 'material_id' => 'Material ID', 'material_times' => 'Material Times', ]; } public function getUser() { return $this->hasOne(User::class, ['id' => 'user_id'])->select('id, nickname, username, avatar_url, mobile'); } public function getCard() { return $this->hasOne(BusinessCard::class, ['id' => 'item_id']); } /** * 保存数据 * * @author hpei * @return bool|Exception */ public static function setData($params) { try { if (empty($params['id'])) { $model = new self(); $model->loadDefaultValues(); } else { $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) throw new \Exception('数据不存在或者已删除'); } if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage()); return $model->id; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 最近浏览 * @author hpei */ public static function latelyBrowse($card_id, $day = 100) { $start_time = strtotime("-$day day"); $user_list = self::find()->select(['user_id']) ->where(['item_id' => $card_id, 'log_type' => CardEnums::LOG_TYPE_LIKE]) ->andWhere(['>', 'created_at', $start_time]) ->andWhere(['<', 'created_at', time()]) ->column(); return User::find()->where(['id' => $user_list])->select(['avatar_url'])->column(); } /** * 秒数转时分格式 * @param $time int * @author jack * @throws string * @return string */ public static function sec2Time($time) { $value = [ "years" => 0, "days" => 0, "hours" => 0, "minutes" => 0, "seconds" => 0, ]; if ($time >= 31556926) { $value["years"] = floor($time/31556926); $time = ($time%31556926); } if ($time >= 86400) { $value["days"] = floor($time/86400); $time = ($time%86400); } if ($time >= 3600) { $value["hours"] = floor($time/3600); $time = ($time%3600); } if ($time >= 60) { $value["minutes"] = floor($time/60); $time = ($time%60); } $value["seconds"] = floor($time); $t = ''; if($value["years"]>0) $t .= $value["years"] . "年"; if($value["days"]>0) $t .= $value["days"] . "天"; if($value["hours"]>0) $t .= $value["hours"] . "小时"; if($value["minutes"]>0) $t .= $value["minutes"] . "分"; if($value["seconds"]>0) $t .= $value["seconds"] . "秒"; return $t; } }