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

namespace App\Modules\Finance\Models;


use App\Modules\Finance\Services\WithdrawCashService;
use Illuminate\Database\Eloquent\Model;
use DB;

/**
 * Class WithdrawCash 提现
 * @package App\Modules\Finance\Models
 */
class WithdrawCash extends Model
{
    protected $table = 'withdraw_cashes';
    protected $fillable = ['distribution_channel_id','distribution_channel_name','amount', 'tallage', 'status','remark','serial_number', 'is_company', 'check_user_id','check_user_name'];

    /**
     * 获取最后一条提现记录
     * @param $channel_id
     * @return mixed
     */
    public static function getWithdrawCashLastRecord($channel_id) {
        $search_object = self::orderBy('id','desc');
        $search_object->where('distribution_channel_id', $channel_id);
        $result = $search_object->first();

        return $result;
    }

    /**
     * 根据渠道ID，时间，获取列表
     * @param $distribution_channel_id
     * @param $distribution_channel_name
     * @param string $start_date
     * @param string $end_date
     * @param string $status
     * @param bool $is_all
     * @return mixed
     */
    static function getListByDistributionChannel($distribution_channel_id='', $distribution_channel_name='', $start_date='',$end_date='', $status = '', $is_all=false)
    {

        $search_object = self::orderBy('created_at','desc');

        if($distribution_channel_id) {
            $search_object->where('distribution_channel_id', $distribution_channel_id);
        }
        if($distribution_channel_name) {
            $search_object->where('withdraw_cashes.distribution_channel_name', 'like', "%".$distribution_channel_name."%");
        }
        if($start_date) $search_object->where('created_at','>=',$start_date);

        if($end_date) $search_object->where('created_at','<=',$end_date);

        if($status) $search_object->whereIn('status', $status);

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

    }


    /**
     * 财务对账
     * @param $params[]
     *                  channel_id:渠道ID 可选
     *                  channel_name:渠道名称 可选
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  is_frozen:冻结  0； -1 可选
     *                  account_name:打款人姓名
     *                  status:打款状态 status 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList
     *                  search_name: 搜索名称
     *                  is_company: 0:私人； 1:公司
     * @param $isAll
     * @return mixed
     */
    static function getFinancialCountingParam($params=[], $isAll = '') {
        $search_object = self::select([
            'withdraw_cashes.id',
            'withdraw_cashes.distribution_channel_id as channel_id',
            'withdraw_cashes.distribution_channel_name as channel_name',
            'withdraw_cashes.amount',
            'withdraw_cashes.tallage',
            'withdraw_cashes.status as wc_status',
            'withdraw_cashes.remark',
            'withdraw_cashes.serial_number',
            'withdraw_cashes.is_company',
            'withdraw_cashes.check_user_id',
            'withdraw_cashes.check_user_name',
            'withdraw_cashes.updated_at',
            'withdraw_cashes.created_at',
            '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',
            DB::raw('IFNULL(financial_configs.is_frozen, 0) as is_frozen'),
            DB::raw('IFNULL(financial_configs.remark , "") as fc_remark'),

        ])
            ->leftjoin('cash_accounts','cash_accounts.distribution_channel_id','=','withdraw_cashes.distribution_channel_id')
            ->leftjoin('financial_configs','financial_configs.distribution_channel_id','=','withdraw_cashes.distribution_channel_id')
            ->where('withdraw_cashes.distribution_channel_id', '>', 0)
            ->orderBy('withdraw_cashes.id','desc');

        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('withdraw_cashes.updated_at','>=', $params['start_date']);
        if(isset($params['end_date']) && $params['end_date']) $search_object->where('withdraw_cashes.updated_at','<=', $params['end_date']);

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


        if(isset($params['status']) && $params['status']) {
            $search_object->whereIn('withdraw_cashes.status', $params['status']);
        } else {
            $search_object->whereIn('withdraw_cashes.status', WithdrawCashService::getWithdrawCashStatusStrSuccessAndFailList());
        }
        if(isset($params['is_frozen']) && is_numeric($params['is_frozen'])) {
            if($params['is_frozen'] == 0) {
                $search_object->where(function ($query) use($params) {
                    $query->where('financial_configs.is_frozen', 0)
                        ->orWhereNull('financial_configs.is_frozen');
                });
            } else {
                $search_object->where('financial_configs.is_frozen', $params['is_frozen']);
            }
        }
        if($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }

    /**
     * 财务审核
     * @param $params[]
     *                  channel_id:渠道ID 可选
     *                  channel_name:渠道名称 可选
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  is_frozen:冻结  0； -1 可选
     *                  account_name:打款人姓名
     *                  status:打款状态 status 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList
     *                  search_name: 搜索名称
     *                  is_company: 0:私人； 1:公司
     * @param $isAll
     * @return mixed
     */
    static function getFinancialAuditParam($params=[], $isAll = false) {
        $search_object = self::select([
            'withdraw_cashes.id',
            'withdraw_cashes.distribution_channel_id as channel_id',
            'withdraw_cashes.distribution_channel_name as channel_name',
            'withdraw_cashes.amount',
            'withdraw_cashes.tallage',
            'withdraw_cashes.status as wc_status',
            'withdraw_cashes.remark',
            'withdraw_cashes.serial_number',
            'withdraw_cashes.is_company',
            'withdraw_cashes.check_user_id',
            'withdraw_cashes.check_user_name',
            'withdraw_cashes.updated_at',
            'withdraw_cashes.created_at',
            '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',
            DB::raw('IFNULL(financial_configs.is_frozen, 0) as is_frozen'),
            DB::raw('IFNULL(financial_configs.remark , "") as fc_remark'),
        ])
            ->leftjoin('cash_accounts','cash_accounts.distribution_channel_id','=','withdraw_cashes.distribution_channel_id')
            ->leftjoin('financial_configs','financial_configs.distribution_channel_id','=','withdraw_cashes.distribution_channel_id')
            ->where('withdraw_cashes.distribution_channel_id', '>', 0)
            ->orderBy('withdraw_cashes.id','desc');

        if(isset($params['channel_id']) && $params['channel_id']) $search_object->where('withdraw_cashes.distribution_channel_id', $params['channel_id']);
        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['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['start_date']) && $params['start_date']) $search_object->where('withdraw_cashes.created_at','>=', $params['start_date']);
        if(isset($params['end_date']) && $params['end_date']) $search_object->where('withdraw_cashes.created_at','<=', $params['end_date']);

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

        if(isset($params['status']) && $params['status']) {
            $search_object->whereIn('withdraw_cashes.status', $params['status']);
        }
        if(isset($params['is_frozen']) && is_numeric($params['is_frozen'])) {
            if($params['is_frozen'] == 0) {
                $search_object->where(function ($query) use($params) {
                    $query->where('financial_configs.is_frozen', 0)
                        ->orWhereNull('financial_configs.is_frozen');
                });
            } else {
                $search_object->where('financial_configs.is_frozen', $params['is_frozen']);
            }
        }
        if($isAll) {
            return $search_object->get();

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