<?php
namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Request;

class PayExtendModel extends Model
{
    protected $connection = 'order';
    protected $table      = 'lie_pay_extend';
    protected $primaryKey = 'id';
    protected $guarded    = ['id'];
    public $timestamps    = true;
    const CREATED_AT      = 'create_time';
    const UPDATED_AT      = 'update_time';

    public $pay_type = [1 => '线下微信', 2 => '线下支付宝', 3 => '交通银行'];

    public function fromDateTime($value)
    {
        return strtotime(parent::fromDateTime($value));
    }

    // 设置支付记录
    public function setPay($request)
    {
        $id                 = $request->input('id', 0);
        $data['order_id']   = $request->input('order_id', 0);
        $data['type']       = $request->input('pay_type', 0);
        $data['file_name']  = $request->input('file_name', '');
        $data['remark']     = $request->input('remarks', '');   

        $event = '申请收款';

        $pay_extend = $this->where('order_id', $data['order_id'])->first();

        if ($pay_extend) { // 更新
            $data['update_uid'] = $request->user->userId;
            $data['is_finance'] = 0;

            $pay_extend_info = $this->find($pay_extend->id);
            $event .= ',修改前:支付方式 - '.$this->pay_type[$pay_extend_info['type']].';转账凭证 - '.$pay_extend_info['file_name'].',备注:'.$pay_extend_info['remark'];

            $res = $this->where('id', $pay_extend->id)->update($data);

            if ($res === false) return [-1, '申请收款更新失败'];
        } else { // 新增
            $data['create_uid'] = $request->user->userId;

            $res = $this->create($data);

            if ($res === false) return [-1, '申请收款新增失败'];
        }

        $OrderActionLogModel = new OrderActionLogModel;
        $OrderActionLogModel->addLog($data['order_id'], $request->user->userId, 2, $event);

        return [0, '操作成功'];
    }

}