<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/17
 * Time: 下午4:38
 */

namespace App\Modules\Finance\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Payment 打款表
 * @package App\Modules\Finance\Models
 */
class Payment extends Model
{
    protected $table = 'payments';
    protected $fillable = ['withdraw_cash_id', 'amount', 'amount_person', 'remark','pay_time', 'pay_merchant_source', 'trade_no', 'status', 'is_company', 'pay_merchant_source_msg', 'pay_merchant_source_result'];


    /**
     * 保存打款信息
     * @param $withdraw_cash_id
     * @param $amount
     * @param $remark
     * @param $pay_merchant_source 支付渠道
     * @param $trade_no 交易流水号
     * @param $status 打款状态
     * @param $pay_merchant_source_msg 支付通道返回信息
     * @param int $is_company 0:私人； 1:公司
     * @param int $amount_person 人工打款金额
     * @return mixed
     */
    public static function addPayment($withdraw_cash_id, $amount, $remark, $pay_merchant_source, $trade_no, $status, $pay_merchant_source_msg, $is_company = 0, $amount_person = 0) {
        $dataPayment['withdraw_cash_id'] = $withdraw_cash_id;
        $dataPayment['amount'] = $amount;
        $dataPayment['remark'] = $remark;
        $dataPayment['pay_time'] = date('Y-m-d H:i:s');
        $dataPayment['pay_merchant_source'] = $pay_merchant_source;
        $dataPayment['trade_no'] = $trade_no;
        $dataPayment['status'] = $status;
        $dataPayment['pay_merchant_source_msg'] = $pay_merchant_source_msg;
        $dataPayment['is_company'] = $is_company;
        $dataPayment['amount_person'] = $amount_person;
        $payment = Payment::create($dataPayment);

        return $payment;
    }

    /**
     * 更新打款状态
     * @param $id
     * @param $status
     * @param $pay_merchant_source_msg
     * @param $pay_merchant_source_result
     * @return mixed
     */
    public static function updatePaymentStatus($id, $status = '', $pay_merchant_source_msg, $pay_merchant_source_result) {
        $dataPayment = self::find($id);
        if(!empty($dataPayment)) {
            if($status) {
                $dataPayment['status'] = $status;
            }
            $dataPayment['pay_merchant_source_msg'] = $pay_merchant_source_msg;
            $dataPayment['pay_merchant_source_result'] = $pay_merchant_source_result;
            $dataPayment = $dataPayment->save();
        }

        return $dataPayment;
    }

    /**
     * 获取打款列表
     * @param $withdraw_cash_id
     * @param string $start_date
     * @param string $end_date
     * @param string $status
     * @param bool $is_all
     * @return mixed
     */
    static function getList($withdraw_cash_id, $start_date='',$end_date='', $status = '', $is_all=false) {
        $search_object = self::orderBy('pay_time','desc');

        if($withdraw_cash_id) {
            $search_object->where('withdraw_cash_id', $withdraw_cash_id);
        }
        if($start_date) $search_object->where('pay_time','>=',$start_date);
        if($end_date) $search_object->where('pay_time','<=',$end_date);

        if($status) {
            $search_object->where('status',$status);
        }

        if($is_all)
        {
            return $search_object->get();
        }else{
            return $search_object->paginate();
        }
    }

    /**
     * 更新打款状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public static function updateSimplePaymentStatus($id, $status)
    {
        return self::where('id', $id)->update(['status' => $status]);
    }

    /**
     * 打款详情列表
     * @param array $params[]
     *                  withdraw_cash_id : 提现ID
     *                  channel_id:渠道ID 可选
     *                  channel_name:渠道名称 可选
     *                  account_name: 打款人姓名
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  end_date:结束时间 可选
     *                  search_name: 搜索名称
     *                  status: 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList
     * @return mixed
     */
    static function getListDetail($params=[]) {
        $search_object = self::select([
            'payments.id',
            'payments.withdraw_cash_id as cash_id',
            'payments.amount',
            'payments.amount_person',
            'payments.pay_time',
            'payments.pay_merchant_source',
            'payments.trade_no',
            'payments.status as payments_status',
            'payments.is_company',
            'payments.pay_merchant_source_result',
            'payments.pay_merchant_source_msg',
            'withdraw_cashes.distribution_channel_id as channel_id',
            'withdraw_cashes.distribution_channel_name as channel_name',
            'withdraw_cashes.status as wc_status',
            'withdraw_cashes.remark',
            'withdraw_cashes.check_user_id',
            'withdraw_cashes.check_user_name',
            'withdraw_cashes.amount as withdraw_cashes_amount',
            'withdraw_cashes.tallage as withdraw_cashes_tallage',
            'cash_accounts.account_name',
            'cash_accounts.identity_card',
            'cash_accounts.card_number',
            'cash_accounts.account_bank',
            'cash_accounts.bank',
            'cash_accounts.province',
            'cash_accounts.status as ca_status '
        ])
            ->leftjoin('withdraw_cashes','withdraw_cashes.id','=','payments.withdraw_cash_id')
            ->leftjoin('cash_accounts','cash_accounts.distribution_channel_id','=','withdraw_cashes.distribution_channel_id')
            ->orderBy('payments.id','desc');


        if(isset($params['withdraw_cash_id']) && $params['withdraw_cash_id']) $search_object->where('payments.withdraw_cash_id', $params['withdraw_cash_id']);

        if(isset($params['channel_id']) && $params['channel_id']) $search_object->where('withdraw_cashes.distribution_channel_id', $params['channel_id']);

        if(isset($params['search_name']) && $params['search_name']) {
            $search_object->where(function ($query) use($params) {
                $query->where('withdraw_cashes.distribution_channel_name','like', "%".$params['search_name']."%")
                    ->orWhere('cash_accounts.account_name', 'like', "%".$params['search_name']."%");
            });
        }

        if(isset($params['channel_name']) && $params['channel_name']) $search_object->where('withdraw_cashes.distribution_channel_name','like', "%".$params['channel_name']."%");
        if(isset($params['account_name']) && $params['account_name']) $search_object->where('cash_accounts.account_name', 'like', "%".$params['account_name']."%");

        if(isset($params['start_date']) && $params['start_date']) $search_object->where('payments.pay_time','>=', $params['start_date']);
        if(isset($params['end_date']) && $params['end_date']) $search_object->where('payments.pay_time','<=', $params['end_date']);

        if(isset($params['is_company']) && is_numeric($params['is_company'])) $search_object->where('payments.is_company', $params['is_company']);

        if(isset($params['status']) && $params['status']) {
            $search_object->whereIn('withdraw_cashes.status', $params['status']);
        }
        return $search_object->paginate();
    }
}