<?php

namespace app\common\dao\user;

use app\common\dao\BaseDao;
use app\common\model\BaseModel;
use app\common\model\user\UserSignVr;
use app\entity\CommonEntity;
use app\entity\data\user\UserSignVrEntity;

class UserSignVrDao extends BaseDao
{
    /**
     * @return BaseModel
     */
    protected function getModel(): string
    {
        return UserSignVr::class;
    }

    /**
     * @param array $idList
     * @return CommonEntity[]
     */
    public function getUserSignVrEntityListByIdList(array $idList): array
    {
        $entityList = [];
        try {
            /** @var UserSignVr $model */
            $model = $this->getModel();
            $dataRes = $model::getDB()
                ->whereIn('id', $idList)
                ->select()
                ->toArray();
            if (!empty($dataRes)) {
                $entityList = array_map(function ($data) {
                    return UserSignVrEntity::newInstance($data);
                }, $dataRes);
            }
        } catch (\Exception $e) {

        }
        return $entityList;
    }

    /**
     * 批量更新
     * @param $dataList
     * @return array
     */
    public function saveAll($dataList): array
    {
        try {
            return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
        } catch (\Exception $e) {
            return [];
        }
    }
    /**
     * 根据订单id和type类型查找vr
     */
    public function getVrFromTypeOrderId($orderId)
    {
        try {
            return $this::getModel()::getDB()->where('order_id', '=', $orderId)->where('type','=',2)->value('number');
        } catch (\Exception $e){
            return [];
        }
    }
    /**
     * 根据order_id获取vr值（用于备份用户变更记录）
     */
    public function getVrForBackupFromOrderId($orderId)
    {
        try {
            $value = $this::getModel()::getDB()
                ->where('order_id', '=', $orderId)
                ->where('type', '=', 2)
                ->value('number');
            return $value ?: 0;
        } catch (\Exception $e) {
            return 0;
        }
    }
}
