<?php
/** 店铺登录
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace App\Model;
use Think\Model;
class ShopsModel extends Model{
    protected $tableName = 'shops';
	public function shops_info($shop_id,$sid){
		$result = $map = $data = array();
		$result['status'] = 1;
		if(empty($shop_id)){
			$result['status'] = 2;
			$result['msg']='门店ID不能为空';
		}
		$map['id'] = $shop_id;
		$map['status'] = 1;
		$field = 'id,name,login_name,phone,thumb,picture,contacts,contacts_phone,business_hours_start,business_hours_end,pic_list,address,xpoint,ypoint,province,city_id,district,poi_id,credential';
		$data = M('Shops')->where($map)->field($field)->find();
		if(empty($data)){
			$result['status'] = 2;
			$result['msg']='该门店已闭店或不存在';
		}else{
			$province_name=idtoname($data['province'],"Area",'name');
			$city_id_name=idtoname($data['city_id'],"Area",'name');
			$district=idtoname($data['district'],"Area",'name');
			$data['region']=$province_name.$city_id_name.$district;
			$data['pic_list']=unserialize($data['pic_list']);
			$data['pid'] = idtoname($sid,'ShopsAccount','pid');
			$result['data'] = $data;
		}
		return $result;
	}
	public function pid_info($sid){
		$result = $map = array();
		$result['status'] = 1;
		if(empty($sid)){
			$result['status'] = 2;
			$result['msg']='参数不能为空';
		}
		$map['id'] = $sid;
		$map['status'] = 1;
		$pid = M('ShopsAccount')->where($map)->getField('pid');
		$result['data'] = $pid;
		return $result;
	}
	// 获取门店列表
    public function shops_lists(){
		$result = array();
		$result['status'] = 1;
		$map['a.status'] = 1;
		$map['a.id'] = array('gt',0);
		$map['s.pid'] = 0;
		$map['group'] = 'a.id';
		$field = 'a.id,a.name,a.thumb,a.updatetime,s.id as sid,s.password';
		$module = 'Shops';
		$join = '__SHOPS_ACCOUNT__ s ON a.id = s.shop_id';
		$data = page_list($module,$map,'',$field,'',$join);
		foreach($data['list'] as $k => $v){
			$data['list'][$k]['updatetime'] = date('Y-m-d',$v['updatetime']);
		}
		if(empty($data['list'])){
			$result['status'] = 2;
			$result['msg']='没有获取到门店列表';
		}else{
			$result['data'] = $data;
		}
		return $result;
	}
	// 添加门店
    public function add_shops($postdata){
        $return = $sa_data = array();
        $return['status'] = 1;
		if(empty($postdata)){
            $return['status'] = 3;
            $return['msg'] = '参数不能为空';
            return $return;
        }
        if(!$postdata['business_hours_start']){
			$postdata['business_hours_start'] = '0:00';
		}
		if(!$postdata['business_hours_end']){
			$postdata['business_hours_end'] = '23:00';
		}
        //请求接口用到的门店信息
		$post = array();
		$post['map_poi_id'] = $postdata['map_poi_id'];
		$post['contract_phone'] = $postdata['phone'];
		$post['hour'] = $postdata['business_hours_start'].'-'.$postdata['business_hours_end'];
		$post['credential'] = $postdata['credential'];
		$post['card_id'] = $postdata['card_id'];
		$img_list = array();
		foreach($postdata['pic_list'] as $k=>$v){
			$img_list[$k] = thumb($v,300,300);
		}
		$images['list']=array_values($img_list);
		$postdata['pic_list'] = $post['pic_list'] = json_encode($images);
        if(!empty($postdata['is_weixin']) && $postdata['is_weixin'] != "false"){
            $Weixin = new \Common\Vendor\WeixinShops();
            $add_store = $Weixin->add_store($post);
            $postdata['poi_id'] = $add_store['data']['poi_id'];
            //微信接口返回结果
			if($add_store['errmsg'] != 'ok'){
				$return['status'] = 3;
	            $return['msg'] = '您输入的参数有误，门店添加失败!';
	            return $return;
			}
        }
        $model = M("Shops");
        $sa_data['mobile'] =  $postdata['contacts_phone'] = $postdata['phone'];

        // if ($postdata['password'] != $postdata['password_true']) {
		// if(!empty($postdata['login_name'])){
			// $return = $this->check_login_name($postdata['login_name'], 0);
			// if($return['status'] == 3){
				// return $return;
			// }
			// $sa_data['login_name'] = $postdata['login_name'] = $postdata['login_name'];
			// if (empty($postdata['password'])) {
				// $return['status'] = 2;
				// $return['msg'] = '密码不能为空';
				// return $return;
			// }
			// $sa_data['salt'] = $postdata['salt'] = randStr();
			// $sa_data['password'] = $postdata['password'] = creatpwd($postdata['password'], 'HANCHAO'.$postdata['salt']);
		// }else{
			// $sa_data['salt'] = $postdata['salt'] = randStr();
			// $sa_data['password'] = $postdata['password'] = creatpwd($postdata['phone'], 'HANCHAO'.$postdata['salt']);
		// }
		
        // $postdata['paypassword'] = creatpwd($postdata['paypassword'], 'HANCHAO'.$postdata['salt']);
        // $postdata['card_id'] = $postdata['card_id'];
        
		$sa_data['salt'] = $postdata['salt'] = randStr();
		$sa_data['password'] = $postdata['password'] = creatpwd($postdata['phone'], 'HANCHAO'.$postdata['salt']);
        
		$check_post = $this->check_post($postdata, 0);
        if($check_post['status'] == 0){
			$return['status'] = 3;
            $return['msg'] = $check_post['msg'];
            return $return;
        }
		
        $postdata = $this->format_post($postdata);
        // $model->create($postdata);
		
        $result = $model->add($postdata);
		
        if ($result) {
            // 门店账号表里面加信息
            $sa_data['login_name'] = shop_account_sn();
            $sa_data['shop_id'] = intval($result);
            $sa_data['name'] = $postdata['name'];
            $sa_data['is_main'] = 0;
            $sa_data['updatetime'] = NOW_TIME;
			if(isset($postdata['registration_id'])){
				$sa_data['registration_id'] = $postdata['registration_id'];
			}
			$sa_data['registration_type'] = $postdata['registration_type'];
            $sid = M('ShopsAccount')->add($sa_data);
			
			$add_data = array();
			$add_data['role_id'] = 4;
			$add_data['account_id'] = $sid;
			M('ShopsRoleAccount')->add($add_data);
			
			$return['status'] = 1;
			$return['msg'] = '添加仓库成功';
			$return['shop_id'] = $result;
			$return['sid'] = $sid;
			$return['token'] = $sa_data['password'];
        } else {
			$return['status'] = 3;
			$return['msg'] = '添加仓库失败';
        }
        return $return;
    }
	public function edit_shops($postdata){
		$return = $sa_data = array();
		if(empty($postdata)){
            $return['status'] = 3;
            $return['msg'] = '参数不能为空';
            return $return;
        }
		$model = M("Shops");
		$sa_data['mobile'] = $postdata['contacts_phone'] = $postdata['phone'];
		if(!$postdata['business_hours_start']){
			$postdata['business_hours_start'] = '0:00';
		}
		if(!$postdata['business_hours_end']){
			$postdata['business_hours_end'] = '23:00';
		}
		// $ss_map = $sa_where = array();
		
		$where = $sa_where = array();
		$where['id'] = $postdata['shop_id'];
		
		$check_post = $this->check_post($postdata, $postdata['sid']);
        if($check_post['status'] == 0){
            $return['status'] = 3;
            $return['msg'] = $check_post['msg'];
            return $return;
        }
		$postdata = $this->format_post($postdata);
		if($postdata['is_weixin']){
			$Weixin = new \Common\Vendor\WeixinShops();
			//微信接口更新门店信息 
			$post = array();
			$post['map_poi_id'] = $postdata['map_poi_id'];
			$post['contract_phone'] = $postdata['phone'];
			$post['hour'] = $postdata['business_hours_start'].'-'.$postdata['business_hours_end'];
			$img_list = array();			
			foreach($postdata['pic_list'] as $k=>$v){
				$img_list[$k] = thumb($v,300,300);
			}
			$images['list']=array_values($img_list);
			$postdata['pic_list'] = $post['pic_list'] = json_encode($images);
			if($postdata['poi_id']){
				$post['poi_id'] = $postdata['poi_id'];	
				$update_store = $Weixin->update_store($post);
			}else{
				$post['credential'] = $postdata['credential'];
				$add_store = $Weixin->add_store($post);
                $postdata['poi_id'] = $add_store['data']['poi_id'];
			}
		}
		$result = M('Shops')->where($where)->save($postdata);
		if (false !== $result) {
			// 门店账号表里面加信息
			$sa_where['id'] = $postdata['sid'];
			$sa_where['pid'] = 0;
			$sa_data['name'] = $postdata['name'];
			$sa_data['updatetime'] = NOW_TIME;
			M('ShopsAccount')->where($sa_where)->save($sa_data);
			
			$return['status'] = 1;
			$return['msg'] = '编辑仓库成功';
		} else {
			$return['status'] = 3;
            $return['msg'] = '编辑仓库失败';
        }
        return $return;
	}
	public function check_login_name($login_name, $sid){
		$return=array('status'=>3,'msg'=>'');
		if(empty($sid)){
			$map['login_name'] = array('eq',$login_name);
			$res = M('ShopsAccount')->where($map)->getField('id');
		}else{
			$map['login_name'] = $login_name;
			$map['id'] = array('neq',$sid);
			$res = M('ShopsAccount')->where($map)->getField('id');
		}
		if($res>0){
			$return['msg'] = '该登陆账号已经存在';
		}else{
			$return['status'] = 1;
		}
		
		return $return;
	}
	public function check_phone($mobile, $sid){
		$return=array('status'=>3,'msg'=>'');
		if(empty($sid)){
			$res = checkmobile($mobile,2);
		}else{
			$map['mobile'] = $mobile;
			$map['id'] = array('neq',$sid);
			$res = M('ShopsAccount')->where($map)->getField('id');
		}
		if($res>0){
			$return['msg'] = '该手机已经存在';
		}else{
			$return['status'] = 1;
		}
		
		return $return;
	}
    /**
     * 检查提交的数据是否合法
     * @param array $postdata
     * @param $sid account表的id
     */
    private function check_post($postdata = array(), $sid = 0){
        $return = array();
        $return['status'] = 0;
        $return['msg'] = '';
        if (empty($postdata))$return['msg'] = "非法数据";
        if ($postdata['name'] == '')$return['msg'] = "门店名称不得为空";
        if ($postdata['contacts'] == '')$return['msg'] = "联系人不得为空";
        if ($postdata['phone'] == '')$return['msg'] = "手机号不得为空";
		$res = $this->check_phone($postdata['phone'], $sid);
		if($res['status'] == 3){
			$return['status'] = 0;
			$return['msg'] = '该手机号已经存在';
			return $return;
		}
		if(empty($return['msg'])){
			$return['status'] = 1;
		}
        return $return;
    }
    /**验证会员是否重复
     * @param $uid
     * @return true是重复false是不重复
     */
    private function check_uid($uid, $id){
        $map['uid'] = $uid;
        $shop_id = M('Shops')->where($map)->getField('id');
        if (empty($shop_id) || (!empty($shop_id) && $shop_id == $id)) return false;
        return true;
    }
    /**
     * 格式化提交的数据
     * @param array $postdata
     * @return array
     */
    public function format_post($postdata = array()){
        $postdata['qq'] = intval($postdata['qq']);
        $postdata['updatetime'] = $postdata['last_login'] = NOW_TIME;
        return $postdata;
    }
	// 添加门店
	public function add_receipt($postdata){
		$return = $sa_data = array();
		$return['status'] = 1;
		$paramdata=$postdata;
		unset($postdata['is_root'],$postdata['root_id'],$postdata['root_token']);
		if(empty($postdata)){
			$return['status'] = 2;
			$return['msg'] = '没有数据可保存';
			return $return;
		}
		if($paramdata['alipay_img']){
			$sa_data['alipay_img']=$paramdata['alipay_img'];
		}
		if($paramdata['weixin_img']){
			$sa_data['weixin_img']=$paramdata['weixin_img'];
		}

		if($paramdata['other_img']){
			$sa_data['other_img']=$paramdata['other_img'];
		}
		$where=array();
		$where['id']=intval($paramdata['root_id']);
		$result=M('Admin')->where($where)->save($sa_data);
		if ($result) {

			$return['status'] = 1;
			$return['msg'] = '保存成功';
		} else {
			$return['status'] = 3;
			$return['msg'] = '保存失败';
		}
		return $return;
	}
}
?>