<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/3
 * Time: 12:37
 * 本类待测试，请别直接使用！！！！！！！！！！！！！
 */

namespace App\Lib\Biz\Money;

use App\Models\Money_recharge as MoneyRecharge;
use App\Models\Account_detailed as AccountDetail;
use App\Models\Money_details;
use App\Models\MoneyRechargeRemark;
use DB;
class AddOrCutMoney
{
    public function __construct()
    {

    }

    /**
     * 添加资金充值记录
     * @param AccountDetail $accountDetail
     * @param string $account_name
     * @param string $order_id
     * @param float $money
     * @param string $recharge_type
     * @param string $operator
     * @param string $reason
     * @param string $detail
     * @param int $rate
     * @return mixed
     * 由 ikeke 修改于2018-12-18，主要添加status的默认值
     */
    public function addMoneyRechargeRec(AccountDetail $accountDetail, string $account_name, string $order_id, float $money, string $recharge_type, string $operator, string $reason, string $detail, int $rate = 1, int $status = 1)
    {
        $money_cash = $accountDetail ? $accountDetail->cash : 0;
        $money_cash += $money; //当前余额
        $info_id = UUID();
        $data = [
            'order_id' => $order_id,
            'info_identity' => $info_id,
            'account_identity' => $accountDetail->account_identity,
            'account_name' => $account_name,
            'money' => $money,
            'reason' => $reason,
            'money_cash' => $money_cash,
            'status' => $status,
            'apply_time' => date('Y-m-d H:i:s'),
            'complete_time' => date('Y-m-d H:i:s'),
            'recharge_type' => $recharge_type,
            'sysetem_user' => $operator,
            'rate' => $rate,
        ];
//        $detail = parseTag($detail, $data);
        $data['remark'] = $detail;

        try {
            (new MoneyRecharge())->insert($data);
            return $info_id;
        } catch (\Exception $e) {
            return $e->getMessage();
            return -70141;
        }

    }

    /**
     * 添加资金详情记录
     * @param AccountDetail $accountDetail
     * @param string $account_name
     * @param string $trade_id
     * @param float $money
     * @param int $money_type
     * @param string $trade_type
     * @param string $operator
     * @param string $trade_desc
     * @param string $reason
     * @return mixed
     */
    public function addMoneyDetailRec(AccountDetail $accountDetail, string $account_name, string $trade_id, float $money, int $money_type, string $trade_type, string $operator, string $trade_desc,string $reason='')
    {
        $oid = UUID();
        $money_cash = $accountDetail->cash; //当前余额
        if ($money_type === 1) {
            $money_cash += $money;
        } else {
            $money_cash -= $money;
        }
        $details['info_identity'] = $oid;
        $details['trade_id'] = $trade_id;
        $details['account_name'] = $account_name;
        $details['account_identity'] = $accountDetail->account_identity;
        $details['money'] = $money;
        $details['money_time'] = date('Y-m-d H:i:s');
        $details['money_type'] = $money_type;//2=扣款
        $details['money_cash'] = $money_cash;
        $details['trade_type'] = $trade_type;
        $details['sysetem_user'] = $operator;
        $details['reason'] = $reason;
        $details['status'] = 1;
//        $detail = parseTag($trade_desc, $details);
        $details['trade_desc'] = $trade_desc;
        try {
            (new Money_details())->insert($details);
            return $oid;
        } catch (\Exception $e) {
//            return ($e->getMessage());
            return -500142;
        }


    }

    /**
     * 更新流水差额
     * @param AccountDetail $accountDetail
     * @param float $money
     * @param int $rate
     * @return mixed
     */
    public function updateNeedMoneyRec(AccountDetail $accountDetail, float $money, int $rate = 1)
    {
        $withdrawRuleObj = lm('withdraw_rule', 'api');
        $ret = $withdrawRuleObj->updateRule($accountDetail->account_identity, $money, $rate);
        return intval($ret) ? $ret : -500143;
    }

    /**
     * 添加资金remark
     * @param AccountDetail $accountDetail
     * @param string $order_id
     * @param float $money
     * @return mixed
     */
    public function addMoneyRechargeRemark(AccountDetail $accountDetail, string $order_id, float $money)
    {
        $cjd = [
            'name' => $accountDetail->account_name,
            'order_id' => $order_id,
            'money' => $money,
            'recharge_time' => $operation_time = time(),
        ];
        try {
            return (new MoneyRechargeRemark())->insertGetId($cjd);
        } catch (\Exception $e) {

            return -500144;
        }
    }

    /**
     * 更新用户账户详情表中余额信息(加款操作)
     * @param AccountDetail $accountDetail
     * @param float $money
     * @return mixed
     */
    public function incAccountDetailMoney(AccountDetail $accountDetail, float $money)
    {
        $type = 1;
        $ret = DB::select("select update_money('" . $accountDetail->account_identity . "'," . $money . "," . $type . ")");
        if ($ret[0]->update_money !== 1) return -3;//更新失败
        return 1;
    }

    /**
     * 用户账户详情扣款操作
     * @param AccountDetail $accountDetail
     * @param float $money
     * @return int
     */
    public function decrAccountDetailMoney(AccountDetail $accountDetail, float $money): int
    {
        $accountDetail = (new AccountDetail())->where('account_identity', $accountDetail->account_identity)->first();
        if (!$accountDetail) {
            return -1;//用户不存在
        }
        $money_cash = $accountDetail->cash ? $accountDetail->cash : 0;
        $money_cash -= $money; //更新后当前余额
        if ($money_cash < 0) {
            return -2;
        }
        $type = 0;
        $ret = DB::select("select update_money('" . $accountDetail->account_identity . "'," . $money . "," . $type . ")");
        if ($ret[0]->update_money !== 1) return -3;//更新失败
        return 1;
    }
}