<?php
/**
 * 设备管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Admin\Controller;
class ShopsFacedeviceController extends BaseController{

	public function _initialize(){
        parent::_initialize();
    }
    public function index(){
        $param=I('param.');
        $map=array();
        if($param['device_id']){
            $map['a.device_id']=array('like','%'.$param['device_id'].'%');
        }
        if($param['shop_id']){
            $map['a.shop_id']=$param['shop_id'];
        }
        if($param['ip']){
            $map['a.ip']=array('like','%'.$param['ip'].'%');
        }
        $device_count=M('ShopsFacedevice')->alias('a')->where($map)->count();
        $this->assign('device_count',$device_count);
    	$map['s.status']=1;
        $field='a.*,s.name,s.status';
    	$join = '__SHOPS__ s ON a.shop_id = s.id';
        $list=page_list('ShopsFacedevice',$map,'',$field,5,$join);
        $map=array();
        $map['status']=1;
        $shop_list=M('Shops')->where($map)->field('id,name,status')->select();
        $this->assign('shop_list',$shop_list);
        $this->assign($list);
        $this->assign($param);
        $this->display();
    }
    /**
     * *添加人脸设备
     */
    public function insert_deviceid(){
    	$param=I('param.');
        if($param){
            $face = new \Common\Vendor\Face();
            $map=array();
            $map['id']=$param['shop_id'];
            $shop_info=idtoname($param['shop_id'],'Shops','id,name,group_id','id',false);
            if(empty($shop_info['group_id']) || $shop_info['group_id']==0){
                $result=$face->queryGroup();
                if($result['stat']['code']==0){
                    if($result['data']){
                        foreach ($result['data'] as $key => $value) {
                            if($shop_info['name']==$value['groupname']){
                                $this->error('分组名称'.$value['groupname'].'重复');
                            }
                        }
                    }
                    $result=array();
                    $result=$face->addGroup($shop_info['name']);
                    $stat=$result['stat'];
                    if($result['stat']['code']==0){
                        $groupid=$result['data']['id'];
                        $data=array();
                        $data['group_id']=$groupid;
                        M('Shops')->where($map)->save($data);

                    }else{
                        $this->error($result['result']);
                    }
                }else{
                    $this->error($result['result']);
                }
            }else{
                $groupid=$shop_info['group_id'];
            }
            $temp_device_id=array_filter(explode(',',$param['device_id']));
            foreach ($temp_device_id as $key => $value) {
                $result=array();
                $result=$face ->bindingDevice($value,$groupid);
                if($result['stat']['code']==0){
                    $data=array();
                    $data['device_id']=$value;
                    $data['ip']=$param['ip'];
                    $data['shop_id']=$param['shop_id'];
                    M('ShopsFacedevice')->add($data);
                }else{
                    $this->error($result['result']);
                }
            }
            $this->success('添加成功');
        }else{
            $this->error("参数错误");
        }
    }
    public function edit_deviceid(){
        $id=I('id');
        if($id){
        	$map=array();
	        $map['id']=$id;
	        $info=M('ShopsFacedevice')->where($map)->find();
	        $this->assign($info);
        }
        $map=array();
        $map['status']=1;
        $list=M('Shops')->where($map)->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function update_deviceid(){
        $param=I('param.');
        if($param){
            $map=$data=array();
            $data['ip']=$param['ip'];
            $data['shop_id']=$param['shop_id'];
            $data['device_id']=$param['device_id'];
            $map['id']=$param['id'];
            $result=M('ShopsFacedevice')->where($map)->save($data);
            if($result>0){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }else{
            $this->error("参数错误");
        }
    }
    public function delete_device(){
        $id=I('id');
        if(!empty($id)){
            $map=array();
            $map['id']=$id;
            $result=M('ShopsFacedevice')->where($map)->delete();
            if($result>0){
                $this->success("删除成功");
            }else{
                $this->error("删除失败");
            }
        }else{
            $this->error("参数错误！");
        }
    }
    public function add_group(){
        $map=array();
        $map['group_id']=array('eq',0);
        $list=M('Shops')->where($map)->field('id,name')->select();
        if($list){
            $face = new \Common\Vendor\Face();
            foreach ($list as $key => $value) {
                $result=$face->addGroup($value['name']);
                if($result['stat']['code']==0){
                    $groupid=$result['data']['id'];
                    $data=$map=array();
                    $data['group_id']=$groupid;
                    $map['id']=$value['id'];
                    $res=M('Shops')->where($map)->save($data);
                }
            }
        }
        $this->success('同步成功');
    }

}
?>