<?php


namespace app\api\controller\manege;


use app\models\manage\BorrowMoneyProduct;
use app\models\manage\UserBorrowMoney;
use app\models\trade\CashTradeOrder;
use app\Request;
use crmeb\services\CacheService;
use crmeb\services\UtilService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

class BorrowMoneyController
{
    /**
     * 算力产品
     * @param Request $request
     * @return mixed
     */
    public function lst(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
        return app('json')->success('ok', BorrowMoneyProduct::getList((int)$page, (int)$limit));
    }

    /**
     * @param $id
     * @param Request $request
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail($id, Request $request)
    {
        return app('json')->success('ok', BorrowMoneyProduct::valid()->where('id', $id)->find()->toArray());
    }

    public function buy($id, Request $request)
    {
        $user = $request->user();
        list($num,) = UtilService::postMore(
            [
                ['num', 0,],
                ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
//                var_dump($user);
                    return md5(md5($item)) == $user['trade_pwd'];
                }], ['请输入交易密码', '交易密码错误']],
            ], $request, true);
        $res = BorrowMoneyProduct::buyPoroduct($id, $request->uid(), $num);
        if ($res) {
            return app('json')->success('借款成功');
        } else {
            return app('json')->fail(BorrowMoneyProduct::getErrorInfo());
        }
    }

    /**
     * 我的理财
     * @param Request $request
     * @return mixed
     */
    public function myList(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
        $where['status'] = $request->get('status', '');
        return app('json')->success('ok', UserBorrowMoney::getList((int)$page, (int)$limit, $where));
    }

    public function finish($id, Request $request)
    {
        $num = $request->put('num', 0);
        $info = UserBorrowMoney::valid()->where('uid', $request->uid())->where('id', $id)->find();
        if (!$info) {
            return app('json')->fail('数据参数异常');
        }
        UserBorrowMoney::beginTrans();
        try {
            $res = UserBorrowMoney::endManege($id, $num);
            if ($res) {
                UserBorrowMoney::commitTrans();
                return app('json')->success('已还款');
            } else {
                UserBorrowMoney::rollbackTrans();
                return app('json')->fail(UserBorrowMoney::getErrorInfo());
            }
        } catch (Exception $e) {
            UserBorrowMoney::rollbackTrans();
            return app('json')->fail($e->getMessage());
        }
    }
}