<?php

namespace app\api\controller\many;


use app\admin\model\system\SystemConfig;
use app\models\many\Many;
use app\models\many\ManyGreen;
use app\models\many\ManyOrder;
use app\models\store\StoreOrder;
use app\models\user\User;
use app\models\user\UserBill;
use app\Request;
use crmeb\services\CacheService;
use crmeb\services\GroupDataService;
use crmeb\services\QrcodeService;
use crmeb\services\SystemConfigService;
use crmeb\services\UtilService;
use crmeb\services\upload\Upload;
use think\facade\Db;

/**
 * 账单类
 * Class UserBillController
 * @package app\api\controller\user
 */
class ManyGreenController
{
    /**
     * 绿卡记录
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $where = UtilService::getMore([
            ['page', 1],
            ['limit', 10],
            ['status'],
        ]);
        $where['uid'] = $request->uid();
        $list = \app\models\many\ManyGreen::list($where);
        return app('json')->success($list);
    }

    /**
     * 兑换绿卡
     * @param Request $request
     * @param $num
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function exchange(Request $request, $num)
    {
        if ($num < 1) return app('json')->fail('传入需要兑换数量');

        if (CacheService::get($request->uid())) return app('json')->fail('操作太快');
        $user = User::where('uid', $request->uid())->find();
        $money = $num * SystemConfig::getConfigValue('green');
        if ($user['green_integral'] < $money) return app('json')->fail('绿积分不足');
        $user['green_integral'] -= $money;
        Db::startTrans();
        try {
            for ($i = 1; $i <= (int)$num; $i++){
                ManyGreen::create(['uid' => $request->uid(), 'type' => 1]);
            }
            $user->save();
            UserBill::income('兑换绿卡',$request->uid(), 'green_integral', 'exchange', $money, 0,$user['green_integral'], '兑换'.$num.'张绿卡');
            CacheService::set($request->uid(), 1, 3);
            Db::commit();
            return app('json')->success('兑换成功');
        } catch (\Exception $e) {
            Db::rollback();
            return app('json')->fail($e->getMessage());
        }

    }

    /**
     * 赠送绿卡
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function give(Request $request)
    {
        $data = UtilService::postMore([
            ['id'],
            ['uid']
        ]);
        if (empty($data['uid']))  return app('json')->fail('填写赠送用户ID');
        $user = User::where('uid', $data['uid'])->find();
        $green = ManyGreen::where('id', $data['id'])->where('uid', $request->uid())->find();
        if (!$user)  return app('json')->fail('没有此用户');
        if (!$green) return app('json')->fail('没有这张绿卡');
        if ($green['type'] == 2) return app('json')->fail('赠送绿卡,不能进行赠送');

        $green['uid'] = $data['uid'];
        $green['type'] = 2;
        $res = $green->save();
        if ($res) return app('json')->success('赠送成功');
        return app('json')->fail('赠送失败');
    }

    public function ratio(Request $request)
    {
        return app('json')->success(['bl' => SystemConfig::getConfigValue('green')]);
    }


}