<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/21
 * Time: 8:59
 */

namespace App\Modules\Channel\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class ChannelUser extends Model
{
    protected $table = 'channel_users';
    protected $fillable = ['name', 'pay_merchant_id', 'phone', 'nickname', 'person_in_charge_name', 'latest_login_time', 'remark', 'latest_login_ip', 'password', 'register_ip', 'is_enabled', 'distribution_manages_id', 'company_id', 'company_name'];


    //创建渠道
    static function createUser($params)
    {
        $name = isset($params['name']) ? trim($params['name']) : '';
        $pay_type = isset($params['pay_type']) ? trim($params['pay_type']) : 'ALLINPAY_NALAN';
        $phone = isset($params['phone']) ? trim($params['phone']) : '';
        $password = isset($params['password']) ? trim($params['password']) : '';
        $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
        $latest_login_time = date("Y-m-d H:i:s");
        $remark = isset($params['remark']) ? trim($params['remark']) : '';
        $person_in_charge_name = isset($params['person_in_charge_name']) ? trim($params['person_in_charge_name']) : '';
        $distribution_manages_id = isset($params['distribution_manages_id']) ? trim($params['distribution_manages_id']) : 0;
        $latest_login_ip = $register_ip = isset($params['register_ip']) ? trim($params['register_ip']) : '';

        return self::firstOrCreate(compact('phone'), compact('password', 'name', 'pay_type', 'nickname', 'latest_login_ip', 'latest_login_time', 'remark', 'register_ip', 'distribution_manages_id', 'person_in_charge_name'));
    }

    //修改密码
    static function modifyPassword($phone, $password)
    {
        return self::where('phone', $phone)->update(compact('password'));
    }

    //手机号码、密码获取
    static function login($phone, $password)
    {
        return self::where('phone', $phone)->where('password', $password)->first();
    }

    static function isRegistered($phone)
    {
        return self::where('phone', $phone)->count();
    }

    static function getByPhone($phone)
    {
        return self::where('phone', $phone)->first();
    }

    static function getById($id)
    {
        return self::find($id);
    }

    static function getImpotantById($id)
    {
        return self::where('id', $id)->select('is_important')->first();
    }


    /**
     * 获取渠道列表
     * @param $id 渠道ID
     * @param $params []
     *                  name:渠道名称 可选
     *                  nickname:渠道昵称 可选
     *                  pay_merchant_id: 支付通道 可选
     *                  distribution_manages_id: 管理员 可选
     *                  phone:电话 可选
     *                  person_in_charge_name:负责人 可选
     *                  remark:备注 可选
     *                  is_enabled: 是否开通 0:未审核； 1:审核通过
     *                  company_id:
     *                  company_name:
     * @return mixed
     */
    public static function updateChannelUserData($id, $params = [])
    {
        $data = self::getById($id);
        if (!empty($data)) {
            if (isset($params['name']) && $params['name']) {
                $data['name'] = $params['name'];
            }
            if (isset($params['nickname']) && $params['nickname']) {
                $data['nickname'] = $params['nickname'];
            }
            if (isset($params['pay_merchant_id']) && $params['pay_merchant_id'] && is_numeric($params['pay_merchant_id'])) {
                $data['pay_merchant_id'] = $params['pay_merchant_id'];
            }
            if (isset($params['distribution_manages_id']) && $params['distribution_manages_id'] && is_numeric($params['distribution_manages_id'])) {
                $data['distribution_manages_id'] = $params['distribution_manages_id'];
            }
            if (isset($params['phone']) && $params['phone']) {
                $data['phone'] = $params['phone'];
            }
            if (isset($params['person_in_charge_name']) && $params['person_in_charge_name']) {
                $data['person_in_charge_name'] = $params['person_in_charge_name'];
            }
            if (isset($params['remark']) && $params['remark']) {
                $data['remark'] = $params['remark'];
            }
            if (isset($params['is_enabled']) && is_numeric($params['is_enabled']) && ($params['is_enabled'] == 0 || $params['is_enabled'] == 1)) {
                $data['is_enabled'] = $params['is_enabled'];
            }
            if (isset($params['company_id']) && is_numeric($params['company_id'])) {
                $data['company_id'] = $params['company_id'];
            }
            if (isset($params['company_name']) && $params['company_name']) {
                $data['company_name'] = $params['company_name'];
            }

            $data->save();
        }
        return $data;
    }

    /**
     * 获取渠道列表
     * @param $params []
     *                  channel_id:渠道ID 可选
     *                  channel_name:渠道名称 可选
     *                  search_name: 搜索名称
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  is_enabled: 是否开通 0:未审核； 1:审核通过
     *                  distribution_manages_id: 管理员 可选
     *                  company_id: 公司ID 可选
     *                  is_important: 公司是否重要 可选
     * @param $isAll
     * @return mixed
     */
    public static function getChannelUserList($params = [], $isAll = '')
    {
        $search_object = self::select([
            'channel_users.id',
            'channel_users.name',
            'channel_users.pay_merchant_id',
            'channel_users.phone',
            'channel_users.nickname',
            'channel_users.person_in_charge_name',
            'channel_users.latest_login_time',
            'channel_users.remark',
            'channel_users.latest_login_ip',
            'channel_users.password',
            'channel_users.register_ip',
            'channel_users.is_enabled',
            'channel_users.distribution_manages_id',
            'channel_users.created_at',
            'channel_users.company_id',
            'distribution_manages.account as distribution_manages_account',
            'distribution_manages.number as distribution_manages_number',
            'distribution_manages.nickname as distribution_manages_nickname',
            DB::raw('IFNULL(companies.name, "") as company_name'),
            DB::raw('IFNULL(companies.is_important, 0) as is_important'),
        ])
            ->leftjoin('distribution_manages', 'distribution_manages.id', '=', 'channel_users.distribution_manages_id')
            ->leftjoin('companies', 'companies.id', '=', 'channel_users.company_id');
        //->orderBy('channel_users.created_at', 'desc')
        //->orderBy('channel_users.id', 'asc');

        if (isset($params['orderByDesc']) && $params['orderByDesc']) {

            if (in_array($params['orderByDesc'], ['phone', 'nickname', 'company_id'])) {
                //dd('channel_users.'.$params['orderByDesc']);
                $search_object->orderBy('channel_users.' . $params['orderByDesc'], 'desc');
                $search_object->orderBy('channel_users.created_at', 'desc');
            }
            if ($params['orderByDesc'] == 'create_time') {
                $search_object->orderBy('channel_users.created_at', 'desc');
            }
        } else {
            $search_object->orderBy('channel_users.created_at', 'desc');
        }
        $search_object->orderBy('channel_users.id', 'asc');

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

        if (isset($params['is_important']) && is_numeric($params['is_important'])) {
            if ($params['is_important'] == 0) {
                $search_object->where(function ($query) use ($params) {
                    $query->where('companies.is_important', 0)
                        ->orWhereNull('companies.is_important');
                });
            } else {
                $search_object->where('companies.is_important', $params['is_important']);
            }
        }

        if (isset($params['channel_id']) && $params['channel_id']) $search_object->where('channel_users.id', $params['channel_id']);
        if (isset($params['channel_name']) && $params['channel_name']) $search_object->where('channel_users.name', 'like', "%" . $params['channel_name'] . "%");

        if (isset($params['search_name']) && $params['search_name']) {
            $search_object->where(function ($query) use ($params) {
                $query->where('channel_users.name', 'like', "%" . $params['search_name'] . "%")
                    ->orWhere('channel_users.nickname', 'like', "%" . $params['search_name'] . "%")
                    ->orWhere('channel_users.person_in_charge_name', 'like', "%" . $params['search_name'] . "%")
                    ->orWhere('channel_users.company_name', 'like', "%" . $params['search_name'] . "%");
            });
        }
        if (isset($params['phone']) && $params['phone']) {
            $search_object->where('channel_users.phone', $params['phone']);
        }

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

        if (isset($params['is_enabled']) && is_numeric($params['is_enabled'])) {
            $search_object->where('channel_users.is_enabled', $params['is_enabled']);
        }

        if (isset($params['distribution_manages_id']) && is_numeric($params['distribution_manages_id'])) {
            $search_object->where('channel_users.distribution_manages_id', $params['distribution_manages_id']);
        }

        if ($isAll) {
            return $search_object->get();

        } else {

            return $search_object->paginate();
        }
    }

    public static function getCount($params = [])
    {
        $search_object = self::orderBy('created_at', 'desc');
        if (isset($params['start_date']) && $params['start_date']) $search_object->where('created_at', '>=', $params['start_date']);
        if (isset($params['end_date']) && $params['end_date']) $search_object->where('created_at', '<=', $params['end_date']);
        return $search_object->count();
    }
}