<?php
namespace app\api\controller;
use app\model\User as UserModel;
use support\Request;
use hg\apidoc\annotation as Apidoc;

/**
 * 余额日志
 */
class BalanceLogController extends BaseController{
    /**
     * 不需要鉴权的方法
     * @var array
     */
    public $noNeedAuth = ['*'];

    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    public $noNeedLogin = [];
    /**
     * 余额日志
     * @Apidoc\Query("currency", type="string", require=true, desc="货币",default="money")
     * @Apidoc\Query("type", type="string", require=true, desc="类型")
     * @Apidoc\Query("startTime", type="string", require=true, desc="开始时间")
     * @Apidoc\Query("endTime", type="string", require=true, desc="结束时间")
     * @Apidoc\Query("page", type="int", require=true, desc="页码",default=1)
     * @Apidoc\Query("limit", type="int", require=true, desc="分页大小",default=10)
     */
    function list(Request $request){
        $user_id = \support\Jwt\JwtToken::getCurrentId();
        $type = Input('type',0);
        $currency = Input('currency','money');
        $startTime = Input('startTime');
        $endTime = Input('endTime');
        $list = \app\model\BalanceLog::queryLogs($user_id,$currency,$type,$startTime,$endTime);
        $BalanceTypeList= \app\enum\BalanceType::toArray();
        $list->each(function($item)use($BalanceTypeList){
            if($item->type == \app\enum\BalanceType::TRANSFER->value && $item->memo){
                $item['target'] = UserModel::where('id',$item->memo)->value('username');
                $item->memo = idEncode($item->memo);
            }
            $item->_type= $item->type;
            $item->type= $BalanceTypeList[$item->type];
        });
        return $this->success(__('successful'),$list);
    }
}