<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\api\service\Feedback;
use think\response\Json;
use app\api\model\User as UserModel;
use app\api\model\UserCoupon as UserCouponModel;
use app\api\service\User as UserService;
use cores\exception\BaseException;
use app\api\model\user\BalanceLog;
use app\api\model\user\GoodSource as GoodsSourceModel;

/**
 * 用户管理
 * Class User
 * @package app\api\controller
 */
class User extends Controller
{
    /**
     * 当前用户详情
     * @return Json
     * @throws BaseException
     */
    public function info(): Json
    {
        // 当前用户信息
        $userInfo = UserService::getCurrentLoginUser(true);
        // 获取用户头像和会员等级
        $userInfo = UserModel::related($userInfo, ['avatar', 'grade']);
        //获取提现次数
        $userInfo['finace_count'] = BalanceLog::where(['user_id' => $userInfo->user_id, 'scene' => 50])->count() ?? 0;
        //获取用户收入
        $userInfo['income'] = BalanceLog::where(['user_id' => $userInfo->user_id])
                ->whereIn('scene', [10, 30, 40, 60])
                ->where('money', '>', 0)
                ->sum('money') ?? 0;

        return $this->renderSuccess(compact('userInfo'));
    }

    /**
     * 账户资产
     * @return Json
     * @throws BaseException
     */
    public function assets(): Json
    {
        // 当前用户信息
        $userInfo = UserService::getCurrentLoginUser(true);
        // 用户优惠券模型
        $model = new UserCouponModel;
        // 返回数据
        return $this->renderSuccess([
            'assets' => [
                'balance' => $userInfo['balance'],  // 账户余额
                'points' => $userInfo['points'],    // 会员积分
                'coupon' => $model->getCount($userInfo['user_id']),    // 优惠券数量(可用)
            ]
        ]);
    }

    /**
     * 手机号绑定
     * @return Json
     * @throws BaseException
     */
    public function bindMobile(): Json
    {
        $model = new UserModel;
        if (!$model->bindMobile($this->postForm())) {
            return $this->renderSuccess($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('恭喜您,手机号绑定成功');
    }

    /**
     * 修改个人信息(头像昵称)
     * @return Json
     * @throws BaseException
     */
    public function personal(): Json
    {
        $model = new UserModel;
        if (!$model->personal($this->postForm())) {
            return $this->renderSuccess($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('恭喜您,信息修改成功');
    }

    /**
     * 提交反馈
     */
    public function addFeedback(): Json
    {
        // 当前用户信息
        $userInfo = UserService::getCurrentLoginUser(true);
        $params = $this->request->param();
        if (empty($params['content']) || (empty($params['shop_id']) && $params['object_type'] != 1) || empty($params['type'])) {
            return $this->renderError('请补全信息');
        }
        $userData = [
            'user_id' => $userInfo->user_id,
            'user_name' => $userInfo->nick_name,
            'mobile' => $userInfo->mobile,
        ];
        $service = new Feedback();
        $res = $service->addFeedback($params, $userData);
        if ($res) {
            return $this->renderSuccess('提交成功,请耐心等待');
        } else {
            return $this->renderError('提交失败');
        }
    }

    //获取反馈列表
    public function getFeedBack(): Json
    {
        $params = $this->request->param();
        // 当前用户信息
        if (!empty($params['is_my'])) {
            $userInfo = UserService::getCurrentLoginUser(true);
            $params['user_id'] = $userInfo->user_id;
        }
        $service = new Feedback();
        $list = $service->getFeedback($params);
        return $this->renderSuccess(compact('list'));
    }

    //我要货源
    public function addGoodsSource(): Json
    {
        $model = new GoodsSourceModel();
        if (!$model->addSource($this->request->param())) {
            return $this->renderSuccess($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('提交成功,请耐心等待');
    }

}