<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/9/20
 * Time: 14:22
 */

namespace App\Http\Controllers\Manage\User;

use App\Http\Controllers\Manage\BaseController;
use App\Modules\Subscribe\Models\YearOrder;
use App\Modules\Subscribe\Services\YearOrderService;
use App\Modules\User\Models\User;
use App\Modules\User\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use DB;

class UserWealthController extends BaseController
{

    public function addBookCoin(Request $request)
    {
        $uid = $request->input('uid', '');
        $coinNum = $request->input('coinNum', 0);
        if (!$uid) {
            return response()->error('PARAM_EMPTY');
        }
        $userInfo = User::where('id', $uid)->first();
        if (!$userInfo) {
            return response()->error('PARAM_ERROR');
        }

        //加币
        if ($coinNum >= 0) {
            $userInfo->balance += $coinNum;
            $userInfo->reward_balance += $coinNum;
            $userInfo->save();
            //减币
        } else {
            if ($userInfo->balance + $coinNum >= 0) {
                //reword 够减除
                if ($userInfo->reward_balance + $coinNum >= 0) {
                    $userInfo->reward_balance += $coinNum;
                    //reword 不够减除
                } else {
                    //先把reword减完
                    $rewordCoin = $userInfo->reward_balance;
                    $userInfo->reward_balance = 0;
                    $userInfo->charge_balance = $userInfo->charge_balance + $coinNum + $rewordCoin;
                }
                $userInfo->balance += $coinNum;
                $userInfo->save();
                //余额不够
            } else {
                return response()->error('BALANCE_NOT_ENOUGH');
            }
        }

        $record = array(
            'uid' => $uid,
            'operator' => $request->session()->get('manage_auth'),
            'coin_num' => $coinNum,
            'type' => 'addCoin',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        );
        DB::table('user_wealthy_operate_record')->insert($record);
        return response()->success();
    }

    /**
     * 用户间转移书币
     * @param Request $request
     * @param  from_uid
     * @param  to_uid
     * @return mixed
     */
    function transBalanceByUser(Request $request)
    {
        $to_uid = $request->has('to_uid') ? $request->input('to_uid') : '';
        $from_uid = $request->has('from_uid') ? $request->input('from_uid') : '';
        if (empty($to_uid) || empty($from_uid)) {
            return response()->error('PARAM_EMPTY');
        }

        if ($from_uid == $to_uid) {
            return response()->error('SAME_ACCOUNT_ERROR');
        }

        $result = UserService::transfromBalanceByUser($from_uid, $to_uid);
        if ($result) {
            return response()->success();
        } else {
            return response()->error('HANDLE_FAILED');
        }
    }

    public function updateUserYearOrders(Request $request)
    {
        $uid = $request->input('uid', '');
        $distribution = $request->input('channelId', '');
        if (!$uid || !$distribution) {
            return response()->error('PARAM_EMPTY');
        }
        //$start_time = $request->input('start_time','');
        $end_time = $request->input('end_time', '');

        if (!$end_time) {
            return response()->error('PARAM_EMPTY');
        }
        if (!strtotime($end_time)) {
            return response()->error('PARAM_ERROR');
        }

        $data = [
            'uid' => $uid,
            'begin_time' => date('Y-m-d H:i:s'),
            'end_time' => $end_time . ' ' . date('H:i:s'),
            'u' => 0,
            'send_order_id' => 0,
            'fee' => 0,
            'distribution_channel_id' => $distribution
        ];

        $yearOrder = YearOrderService::getRecord($uid);
        if ($yearOrder) {
            $yearOrderEndTime = $yearOrder->end_time;
            //更新的时间 小于 现在包年的到期时间
            if (strtotime($end_time) < strtotime($yearOrderEndTime)) {
                return ['code' => 304, 'msg' => '用户在该渠道已存在包年！'];
            } else {
                //延长到期时间
                $updateEndTime = $end_time . ' ' . date('H:i:s');
                $res = YearOrder::updateEndtime($yearOrder->id, $updateEndTime);
                if (!$res) {
                    return response()->error("HANDLE_FAILED");
                }
            }
        } else {
            YearOrder::create($data);
        }
        $record = array(
            'operator' => $request->session()->get('manage_auth'),
            'uid' => $uid,
            'endtime' => $data['end_time'],
            'channel_id' => $distribution,
            'type' => 'yearRecharge',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        );
        DB::table('user_wealthy_operate_record')->insert($record);
        return response()->success();
    }
}