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

namespace App\Modules\Manage\Models;
use Illuminate\Database\Eloquent\Model;

class Manage extends Model
{
    protected  $table = 'distribution_manages';
    protected  $fillable = ['id','account','role','nickname', 'latest_login_time','remark','latest_login_ip','password','register_ip','is_enabled','number'];

    static function getByAccount($account)
    {
    	return self::where('account', $account)->first();
    }
    
    static function getAll($is_all=1)
    {
    	if($is_all){
    		return self::orderBy('is_enabled', 'desc')->orderBy('id', 'desc')->paginate();
    	}else{
    		return self::where('is_enabled', 1)->orderBy('id', 'desc')->paginate();
    	}
    }

    static function add($params)
    {
    	return self::firstOrCreate($params);
    }
    
    static function getById($id)
    {
        return self::where('id', $id)->first();
    }

    //创建管理后台
    static function createManage($params)
    {
        $name = isset($params['name']) ? trim($params['name']) : '';
        $account = isset($params['account']) ? trim($params['account']) : '';
        $password = isset($params['password']) ? trim($params['password']) : '';
        $role = isset($params['role']) ? trim($params['role']) : '';
        $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
        $latest_login_time = date("Y-m-d H:i:s");
        $remark = isset($params['remark']) ? trim($params['remark']) : '';
        $latest_login_ip = $register_ip = isset($params['register_ip']) ? trim($params['register_ip']) : '';

        if(!$account || !$password) return response()->error('PARAM_ERROR');

        return self::firstOrCreate(compact('account'),compact('password','role','nickname','latest_login_ip','latest_login_time','remark','register_ip'));
    }

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

    //根据商务编号获取商务用户信息
    static function getBusinesserByNumber($number)
    {
        return self::where('number',$number)->first();
    }

    /**
     * 获取商务列表
     * @param $params[]
     *                  search_name: 搜索名称 可选
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  is_enabled: 0:不可用； 1:可用
     * @param $isAll
     * @return mixed
     */
    public static function getBusinessManageList($params=[], $isAll = false) {
        $search_object = self::orderBy('id','asc');
        $search_object->whereIn('role', ['business', 'business_leader']);

        if(isset($params['search_name']) && $params['search_name']) {
            $search_object->where('nickname', 'like', "%".$params['search_name']."%");
        }
        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']);

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

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

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