2023-09-25 14:49:32 +08:00

177 lines
5.0 KiB
PHP

<?php
namespace models;
use mt;
use phpcommon\SqlHelper;
use services\NftService;
class Avatar extends BaseModel
{
public static function find($avatarUniId){
return self::internalFind(myself()->_getAccountId(), myself()->_getAddress(), $avatarUniId);
}
private static function internalFind($accountId, $address, $avatarUniId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($accountId),
't_avatar',
array(
'idx' => $avatarUniId,
)
);
if ($row) {
$row['avatar_uniid'] = $row['idx'];
if ($row['account_id'] != $accountId) {
$openId = $address;
if (!NftService::isAvatarOwner($openId, $row['token_id'])) {
$row = null;
}
}
}
return $row;
}
public static function getAvatarByHeroIdx($heroUniid){
$rows = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_avatar',
array(
'hero_idx' => $heroUniid,
)
);
$avatarList = array();
if (count($rows) > 0){
foreach ($rows as $row){
$row['avatar_uniid'] = $row['idx'];
if ($row['account_id'] != myself()->_getAccountId()) {
$openId = myself()->_getAddress();
if (NftService::isAvatarOwner($openId, $row['token_id'])) {
array_push($avatarList,$row);
}
}else{
array_push($avatarList,$row);
}
}
}
return $avatarList;
}
public static function getOneByType($heroUniid,$itemType){
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_avatar',
array(
'hero_idx' => $heroUniid,
'item_type' => $itemType,
)
);
if ($row) {
$row['avatar_uniid'] = $row['idx'];
if ($row['account_id'] != myself()->_getAccountId()) {
$openId = myself()->_getAddress();
if (!NftService::isAvatarOwner($openId, $row['token_id'])) {
$row = null;
}
}
}
return $row;
}
public static function getAvatarList($cb){
SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_avatar',
array(
'account_id' => myself()->_getAccountId()
),
function ($row) use($cb) {
$cb($row);
}
);
foreach (NftService::getAvatar(myself()->_getAddress()) as $nftDb) {
if (! $nftDb['deleted']){
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_avatar',
array(
'token_id' => $nftDb['token_id'],
)
);
$cb($row);
}
}
}
public static function toDto($row){
$dto = array(
'idx' => $row['idx'],
'avatar_uniid' => $row['idx'],
'token_id' => $row['token_id'],
'hero_uniid' => $row['hero_idx'],
'item_id' => $row['item_id'],
'item_type' => $row['item_type'],
'status' => $row['status'],
);
return $dto;
}
public static function addAvatar($avatarMeta){
SqlHelper::insert(
myself()->_getSelfMysql(),
't_avatar',
array(
'account_id' => myself()->_getAccountId(),
'item_id' => $avatarMeta['id'],
'item_type' => $avatarMeta['sub_type'],
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime(),
)
);
}
public static function equipUpdate($avatarDb,$heroUniid){
$row = self::getOneByType($heroUniid,$avatarDb['item_type']);
if ($row){
SqlHelper::update(
myself()->_getSelfMysql(),
't_avatar',
array(
'idx' => $row['idx'],
),
array(
'status' => 0,
'hero_idx' => null,
'modifytime' => myself()->_getNowTime(),
)
);
}
SqlHelper::update(
myself()->_getSelfMysql(),
't_avatar',
array(
'idx' => $avatarDb['idx'],
),
array(
'status' => 1,
'hero_idx' => $heroUniid,
'modifytime' => myself()->_getNowTime(),
)
);
}
public static function update($avatarUniid,$feildKv){
SqlHelper::update(
myself()->_getSelfMysql(),
't_avatar',
array(
'idx' => $avatarUniid,
),
$feildKv
);
}
}