<?php
/**
 * 店铺模型
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <1094186793@sina.cn>
 */
 
namespace Admin\Model;
use Think\Model;
class ShopsModel extends Model
{
    /**
     * 根据店铺关键字找店铺id
     * @param $postdata
     * @return array|int|string
     */
    public function get_shop_ids($postdata){
        $s_id_map='';
        if (isset($postdata['shops_keyword'])) {
            if(!empty($postdata['shops_keyword'])) {
                if (is_numeric($postdata['shops_keyword'])) {
                    $shop_map['id|name'] = array(intval($postdata['shops_keyword']), array('like', '%' . $postdata['shops_keyword'] . '%'), '_multi' => true);
                } else if (!empty($postdata['shops_keyword'])) {
                    $shop_map['name'] = array('like', '%' . (string)$postdata['shops_keyword'] . '%');
                }
                $shop_ids = M('Shops')->where($shop_map)->getField('id',true);
                if (empty($shop_ids)) {
                    $s_id_map = array('lt', 0);
                } else {
                    $s_id_map = array('in', $shop_ids);
                }
            }else{
                $s_id_map = array('gt', 0);
            }
        } else {
            $s_id_map = 0;
        }
        return $s_id_map;
    }

    /**根据会员的收货地址所在的市找店铺
     * @param $user_id
     */
    public function get_shops_info($user_id){
        if(empty($user_id))return false;
        $map=array();
        $map['uid']=$user_id;
        $city_id=M("MemberAddress")->where($map)->getField('city');
        $shop_map=array();
        $shop_map['status']=1;
        $shop_map['city_id']=$city_id;
        $shop_info=M("Shops")->where($shop_map)->field('id,name')->select();
        return $shop_info;
    }
    public function save_member_config(){
        $map=array();
        $map['name']="MEMBERATTRIBUTE";
        $count=M("Config")->where($map)->getField('id');
        if(empty($count)) {
            $config_data = array();
            $config_data['name'] = "MEMBERATTRIBUTE";
            $config_data['title'] = "会员注册必填属性";
            $config_data['status'] = 1;
            $data= array(
                'real_name' => Array
                (
                    'is_member' => 1,
                    'is_shop' => 1,
                ),

                'birthday' => Array
                (
                    'is_member' => 0,
                    'is_shop' => 0,
                ),

                'sex' => Array
                (
                    'is_member' => 1,
                    'is_shop' => 1,
                ),

                'idcard' => Array
                (
                    'is_member' => 0,
                    'is_shop' => 1,
                ),

                'address' => Array
                (
                    'is_member' => 1,
                    'is_shop' => 1,
                ),
            );
            $config_data['value'] =serialize($data);
                $res = M("Config")->add($config_data);
                if ($res) {
                    S('DB_CONFIG_DATA', null);
                }

        }else{
            $must_attr_info=unserialize(C("MEMBERATTRIBUTE"));
            if (empty($must_attr_info)) {
                $map=array();
                $map['id']=$count;
                $config_data=array();
                $data= array(
                    'real_name' => Array
                    (
                        'is_member' => 1,
                        'is_shop' => 1,
                    ),

                    'birthday' => Array
                    (
                        'is_member' => 0,
                        'is_shop' => 0,
                    ),

                    'sex' => Array
                    (
                        'is_member' => 1,
                        'is_shop' => 1,
                    ),

                    'idcard' => Array
                    (
                        'is_member' => 0,
                        'is_shop' => 1,
                    ),

                    'address' => Array
                    (
                        'is_member' => 1,
                        'is_shop' => 1,
                    ),
                );
                $config_data['value'] =serialize($data);
                $res = M("Config")->where($map)->save($config_data);
                if ($res) {
                    S('DB_CONFIG_DATA', null);
                }
            }
        }
        return true;
    }
//    获取仓库列表
    public function shop_list(){
        $s_map = array();$s_map['id'] = array('gt',0);$s_map['status'] = 1;
        $shop_list_arr = M('Shops')->where($s_map)->field('id,name')->select();
        $shop_list = array();
        foreach($shop_list_arr as $k=>$v){
            $shop_list[$v['id']] = $v['name'];
        }
        return $shop_list;
    }
    //    获取收银员列表
    public function cashier_list(){
        $map=$cashier_list=array();
        $map['role_id']=1;
        $sa__id=M('ShopsRoleAccount')->where($map)->getField('account_id',true);
        $mapg['status']=1;
        $mapg['is_delete']=0;
        if($sa__id){
            $mapg['id']=array('in',$sa__id);
            $cashier_list_arr=M('ShopsAccount')->where($mapg)->field('id,name')->select();
            foreach($cashier_list_arr as $k=>$v) {
                $cashier_list[$v['id']] = $v['name'];
            }
        }

        return $cashier_list;
    }
    //    获取导购员列表
    public function guider_list(){
        $map=$guider_list=array();
        $map['role_id']=2;
        $guider_id=M('ShopsRoleAccount')->where($map)->getField('account_id',true);
        $mapg['status']=1;
        $mapg['is_delete']=0;
        if($guider_id){
            $mapg['id']=array('in',$guider_id);
            $guider_list_arr=M('ShopsAccount')->where($mapg)->field('id,name')->select();
            foreach($guider_list_arr as $k=>$v) {
                $guider_list[$v['id']] = $v['name'];
            }
        }

        return $guider_list;
    }
    //    获取供货商列表
    public function get_supplier_list(){
        $Suppliers=M('Suppliers')->where(1)->field('id,name')->order('name')->cache(true)->select();
        $suppliers_list=format_array2select($Suppliers);
        return $suppliers_list;
    }
}