<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/3/21
 * Time: 下午2:52
 */

require_once(BASE_ROOT_PATH . '/helper/algorithm.php');


class member_helper
{
    const member_normal_type  = 0;
    const member_lrlz_type    = 1;
    const member_brand_type   = 2;

    public static function from_mobiles($inputs,&$errno)
    {
        $items = preg_split('/,/',$inputs);

        $errno = [];
        $mobiles = [];
        foreach ($items as $item)
        {
            $mobile = trim($item);
            $validate = new Validator();
            $validate->setValidate(Validator::verify_mobile($mobile));
            $err = $validate->validate();
            if(empty($err))
            {
                $nMobile = intval($mobile);
                if(algorithm::binary_search($mobiles,$nMobile) == false) {
                    $pos = algorithm::lower_bonud($mobiles,$nMobile);
                    algorithm::array_insert($mobiles,$pos,$nMobile);
                    continue;
                }
            }
            $errno[] = $mobile;
        }

        if(!empty($mobiles)) {
            $member = Model('member');
            $items = $member->getMemberList(array('member_mobile' => array('in',$mobiles)),'member_id,member_mobile');
            return $items;
        } else {
            return false;
        }
    }

    public static function from_sql($sql)
    {
        if(empty($sql)) {
            return false;
        }
        else
        {
            $result = [];
            $items = DB::getAll($sql);
            foreach ($items as $item) {
                $member = [];
                $member['member_id'] = $item['member_id'];
                $member['member_mobile'] = $item['member_mobile'];
                $result[] = $member;
            }
        }
        return $result;
    }

    public static function exmembers($members, $fExLrlz, $fExBrans)
    {
        if($fExLrlz == false && $fExBrans == false) {
            return $members;
        }

        $ids = [];
        foreach ($members as $member) {
            $ids[] = intval($member['member_id']);
        }
        $cond['member_id'] = array('in',$ids);
        if($fExLrlz && $fExBrans) {
            $cond['member_user_type'] = array(array('neq',self::member_lrlz_type), array('neq',self::member_brand_type),'and');
        }
        elseif($fExLrlz) {
            $cond['member_user_type'] = array('neq',self::member_lrlz_type);
        }
        else {
            $cond['member_user_type'] = array('neq',self::member_brand_type);
        }

        $mod_member = Model('member');
        $items = $mod_member->field('member_id,member_mobile')->where($cond)->limit(false)->select();
        return $items;
    }
}