public function test1()
 {
     $push = array('demand_id' => 1, 'total_price' => 2, 'total_time' => 3, 'merchant_id' => 4);
     $jid = '10';
     // 		// 		$jpush = D('Jpush');
     // 		// 		$jpush->user = 2;
     // 		// 		$jpush ->push(1, $jid, $push);
     // 	//聊天内推送
     $xmpp = new \App\Model\XmppApiModel();
     $xmpp->requestPush(2, array($jid), $push);
 }
 public function test()
 {
     //商户给用户报价 jpush
     // 		$jid = 12;
     // 				//云推送
     // 			$jpush = new \App\Model\JpushModel();
     // 			$jpush->user = 0;
     // 			$jpush ->push(2, array($jid), $data);
     // 			//聊天内推送
     // 			$xmpp = new \App\Model\XmppApiModel();
     // 			$xmpp ->requestPush(2, array($jid), $data);
     //商户	//xmpp
     $jid = array(18);
     //用户发需求
     $jpush = D('Jpush');
     $jpush->user = 2;
     $jpush->push(1, $jid, $data);
     $xmpp = new \App\Model\XmppApiModel();
     $xmpp->requestPush(1, $jid, $data);
     //云推送
 }
 /**
  * 发布需求 规则::(以下数值均可调整)
  * 1 5分钟内商家报价达到10 或者 用户选择商户,停止推送,(达到10家报价 不从未保价消失,只提示已超过10家商户报价)
  * 2 5分钟后,没有达到10家报价,推送半径增加2km,且剔除已推送过的商家。如范围内没有商家,立即叠加2km (最多5次,即范围13km后不在推送)
  * 3 5分钟为一次循环点,每次增加2km,直到报价到达10家,或者用户选择商家 或者总半径范围达到13km
  * 4    报价个数只增不减,即使商家撤销了报价。
  */
 public function demand_push()
 {
     set_time_limit(0);
     //初始公里数
     $initKm = C('PUSH_RANGE_KM');
     $initBindingNUm = C('PUSH_MAX_BINDING_NUM');
     $initRangeKm = C('PUSH_MAX_RANGE_KM');
     $initPerKm = C('PUSH_PER_KM');
     $time = time();
     //筛选出哪些是应该推送的 报价小于{}、未选定商家、范围小于{}、未过期需求[未删除]
     // range_km 不等于0 ;等于0为只推送一次,无需再次扩充范围
     $map = array('is_bidding' => array('lt', $initBindingNUm), 'merchant_id' => 0, 'range_km' => array(array('lt', $initRangeKm), array('neq', 0)), 'status' => 0, 'expire_time' => array('gt', $time));
     $demandDb = M('MemberDemand');
     $data = $demandDb->where($map)->field('id,range_km,longitude,latitude')->select();
     // 		echo $demandDb->getlastSql();
     // dump($data);die();
     $log = M('RunLog');
     $merchantDb = M('Merchant');
     $merchantEnable = M('DemandMerchantEnable');
     // 		dump($data);die();
     if (!empty($data)) {
         foreach ($data as $key => $row) {
             //再次确认是否需要推送
             $mapCheck = $map;
             $mapCheck['id'] = $row['id'];
             $mapCheck['expire_time'] = array('gt', time());
             $check = $demandDb->where($mapCheck)->find('id');
             if ($check !== false) {
                 $current = $row['range_km'];
                 do {
                     $loop = false;
                     $current += $initPerKm;
                     // dump($current);
                     //范围不超过最大值
                     if ($current > $initRangeKm) {
                         break;
                     }
                     //获取最大最小经纬度
                     $ll_arr = rangekm($current, $row['longitude'], $row['latitude']);
                     $maxLng = $ll_arr['maxLng'];
                     $minLng = $ll_arr['minLng'];
                     $maxLat = $ll_arr['maxLat'];
                     $minLat = $ll_arr['minLat'];
                     //获取范围内商家
                     $merchantList = array();
                     $sql = "select a.id,b.id as jid,a.business_time from " . C('DB_PREFIX') . "merchant as a\r\n\t\t\t\t\tleft join " . C('DB_PREFIX') . "system_user as b on (a.id = b.sub_id and b.type =2)\r\n\t\t\t\t\twhere a.longitude <={$maxLng} and a.longitude>={$minLng} and a.latitude <={$maxLat} and a.latitude>={$minLat} and (a.status = 0 or a.status =1) ";
                     $merchantList = M('')->query($sql);
                     $merchantListCount = count($merchantList);
                     // dump($merchantListCount);
                     //剔除已推送的商家
                     $merchantEnableList = $merchantEnable->where(array('demand_id' => $row['id']))->field('merchant_id')->select();
                     $merchantEnableListCount = count($merchantEnableList);
                     // dump($merchantEnableListCount);
                     //如果有商家的话
                     if ($merchantListCount > $merchantEnableListCount) {
                         $MerchantLeft = array();
                         foreach ($merchantList as $ke => $ro) {
                             foreach ($merchantEnableList as $k => $r) {
                                 $enable = 0;
                                 if ($ro['id'] == $r['merchant_id']) {
                                     $enable = 1;
                                     break;
                                 }
                             }
                             if ($enable == 0) {
                                 $MerchantLeft[] = $ro;
                             }
                         }
                         //对剩余商家进行推送
                         // $MerchantLeft = array(array('id'=>105,'jid'=>18));
                         $jid = array();
                         foreach ($MerchantLeft as $mk => $mr) {
                             $addAll[$mk]['id'] = null;
                             $addAll[$mk]['merchant_id'] = $mr['id'];
                             $addAll[$mk]['demand_id'] = $row['id'];
                             if (timeCompare($mr['business_time'])) {
                                 $jid[] = $mr['jid'];
                             }
                         }
                         $addMerchant = $merchantEnable->addAll($addAll);
                         // $log->add(array('position'=>'cronPush','msg'=>json_encode($addAll),'addtime'=>date('Y-m-d H:i:s',time()),'status'=>1));
                         if (!$addMerchant) {
                             $log->add(array('position' => 'cronPush', 'msg' => '执行id为' . $row['id'] . '的定时任务add数据库(DemandMerchantEnable)失败', 'addtime' => date('Y-m-d H:i:s', time()), 'status' => 1));
                         }
                         if ($addMerchant === false) {
                             $log->add(array('position' => 'cronPush', 'msg' => '执行id为' . $row['id'] . '的需求时,周围' . $current . 'km无商家', 'addtime' => date('Y-m-d H:i:s', time()), 'status' => 1));
                         }
                         //云推送
                         $push['demand_id'] = $row['id'];
                         // dump($push);
                         $jpush = new \App\Model\JpushModel();
                         $jpush->user = 2;
                         $jpush->push(1, $jid, $push);
                         $xmpp = new \App\Model\XmppApiModel();
                         $xmpp->requestPush(1, $jid, $push);
                     } else {
                         $loop = true;
                     }
                     //更新已推送的公里数
                     $saveRange = $demandDb->where(array('id' => $row['id']))->save(array('range_km' => $current));
                 } while ($loop);
             }
         }
     }
     $log->add(array('position' => 'cronPush', 'msg' => '定时任务更新数据库(MemberDemand)成功', 'addtime' => date('Y-m-d H:i:s', time()), 'status' => 0));
 }
 /**
  * 修改用户名 密码 更新system——user 表
  *
  */
 public static function saveHeader($sub_id, $type, $header)
 {
     $db = M('SystemUser');
     $id = $db->where(array('type' => $type, 'sub_id' => $sub_id))->getField('id');
     $data = $db->where(array('id' => $id))->save(array('header' => $header));
     set_time_limit(20);
     $xmpp = new \App\Model\XmppApiModel($id, $id);
     $xmpp->updataHeader($header);
     //     	dump('123');
     return $data;
 }
 /**
  * 用户确定需求
  */
 public function confirm_demand()
 {
     $id = (int) $_POST['id'];
     $merchant_id = isset($_POST['merchant_id']) ? htmlspecialchars($_POST['merchant_id']) : '';
     $member_session_id = $_POST['member_session_id'];
     $member_id = $this->session_handle->getsession_userid($member_session_id);
     if (empty($id)) {
         $this->jsonUtils->echo_json_msg(4, '需求ID为空...');
         exit;
     }
     if (empty($merchant_id)) {
         $this->jsonUtils->echo_json_msg(4, '商家ID为空...');
         exit;
     }
     $order = M('order');
     // 需求是否属于这个用户
     $demand = $this->dao->where(array('id' => $id, 'member_id' => $member_id))->find();
     if (!$demand) {
         $this->jsonUtils->echo_json_msg(4, '数据异常1');
         exit;
     }
     if (time() > $demand['expire_time']) {
         $this->jsonUtils->echo_json_msg(4, '已过期');
         exit;
     }
     if (time() > $demand['reach_time']) {
         $this->jsonUtils->echo_json_msg(4, '到店时间已过期');
         exit;
     }
     if ($demand['merchant_id']) {
         $this->jsonUtils->echo_json_msg(4, '已选择商户');
         exit;
     }
     // 检测商家是否有报价
     $bidding = M('MerchantBidding')->where(array('demand_id' => $id, 'merchant_id' => $merchant_id))->find();
     if (!$bidding) {
         $this->jsonUtils->echo_json_msg(4, '数据异常2');
         exit;
     }
     $o_arr = $order->where("sub_id={$id} and member_id={$member_id}")->select();
     if ($o_arr) {
         $this->jsonUtils->echo_json_msg(1, '此需求你已经提交过订单');
         exit;
     }
     // 查询需求
     $arr = $this->dao->query("select id,reach_time  as reach_time ,cart_data,description,pics,publish,member_id,longitude,latitude,cart_id ,merchant_id as demand_status from " . C('DB_PREFIX') . "member_demand  where id={$id}");
     if ($arr) {
         $arr[0]['demand_status'] = $arr[0]['demand_status'] > 0 ? 1 : 0;
         $model = new Model();
         $merchant = M('merchant');
         $mer_arr = $merchant->field("longitude,latitude")->where("id={$merchant_id}")->select();
         $longitude = $arr[0]['longitude'];
         // 用户发布需求的经纬度
         $latitude = $arr[0]['latitude'];
         $demand_id = $arr[0]['id'];
         // 计算商家店铺和用户需求距离
         $arr[0]['distance'] = getDistance($latitude, $longitude, $mer_arr[0]['latitude'], $mer_arr[0]['longitude']);
         // 需求详情项目
         if ($arr[0]['publish'] == 0) {
             $s_arr = $model->query("select b.name,b.id from " . C('DB_PREFIX') . "member_demand_subitems as a left join " . C('DB_PREFIX') . "category as b on a.category_id=b.id  where a.demand_id={$demand_id} ");
         } else {
             $s_arr = $model->query("select b.name,b.id from " . C('DB_PREFIX') . "member_demand_subitems as a left join " . C('DB_PREFIX') . "car_maintain_category as b on a.category_id=b.id  where a.demand_id={$demand_id} ");
         }
         $perlist = array();
         if ($s_arr) {
             // 查询 商家已有的服务 ,商家未有的服务 不允许报价
             // $server_ids = CommonController::getServerListByMer (
             // $merchant_id );
             // $string = implode ( ',', $server_ids );
             // 允许报价 所提示的价格
             // 已报价 bidding
             $arr[0]['merchant_remark'] = M('MerchantBiddingRemark')->where(array('demand_id' => $id))->getField('remark');
             $merchant_remark = empty($arr[0]['merchant_remark']) ? '' : $arr[0]['merchant_remark'];
             $map['demand_id'] = $id;
             // $map ['sub_id'] = array (
             // 'in',
             // $string
             // );
             $map['merchant_id'] = $merchant_id;
             $alert_price = M('MerchantBidding')->where($map)->field('id as bidding_id,price,sub_id as cat_id,out_time as time')->select();
             foreach ($alert_price as $tem) {
                 $price[$tem['cat_id']] = $tem;
             }
             // 区分是报价为0 还是未报价,-1标识未报价
             // foreach ( $server_ids as $tem => $row ) {
             // if (! isset ( $price [$row] )) {
             // $price [$row] = array (
             // 'cat_id' => $row,
             // 'price' => - 1,
             // 'bidding_id' => '-1'
             // );
             // }
             // }
             $total_price = 0;
             $total_time = 0;
             $service_name = array();
             // dump($price);
             foreach ($s_arr as $key => $row) {
                 $perlist[$key]['id'] = $row['id'];
                 $perlist[$key]['name'] = $row['name'];
                 $service_name[] = $row['name'];
                 // if (in_array ( $row ['id'], $server_ids )) {
                 $perlist[$key]['is_server'] = 1;
                 $perlist[$key]['time'] = !isset($price[$row['id']]['time']) ? '-1' : $price[$row['id']]['time'];
                 $perlist[$key]['price'] = !isset($price[$row['id']]['price']) ? '-1' : $price[$row['id']]['price'];
                 $perlist[$key]['bidding_id'] = !isset($price[$row['id']]['bidding_id']) ? '-1' : $price[$row['id']]['bidding_id'];
                 // } else {
                 // $perlist [$key] ['is_server'] = 0;
                 // $perlist [$key] ['price'] = 0;
                 // }
                 if ($perlist[$key]['price'] > 0) {
                     $total_price += $perlist[$key]['price'];
                 }
                 if ($perlist[$key]['price'] > 0) {
                     $total_time += $perlist[$key]['time'];
                 }
             }
         }
         // 拼接过程
         $arr[0]['list'] = $perlist;
         if ($arr[0]['pics']) {
             $json_obj = json_decode($arr[0]['pics'], true);
         } else {
             $json_obj = array();
         }
     } else {
         $this->jsonUtils->echo_json_msg(1, '没有该用户需求...');
         exit;
     }
     $param = json_decode($demand['param'], true);
     $rel['param'] = count($param) > 0 ? $param : array();
     $rel['distance'] = $arr[0]['distance'];
     $rel['pics'] = $json_obj;
     $rel['list'] = $perlist;
     $order_no = time() . rand(1000, 9999);
     $data['order_no'] = $order_no;
     $data['service_name'] = !empty($service_name) ? implode('、', $service_name) : '';
     $data['status'] = 0;
     $data['merchant_id'] = $merchant_id;
     $data['member_id'] = $member_id;
     if ($arr[0]['publish'] == 0) {
         $data['type'] = 1;
         // 项目需求订单
     } else {
         $data['type'] = 3;
         // 保养需求订单
     }
     $data['goods_count'] = 1;
     $data['total_price'] = $total_price;
     $data['unit_price'] = $total_price;
     $data['total_time'] = $total_time;
     $data['sub_id'] = $id;
     $data['sub_data'] = json_encode($rel);
     $data['reach_time'] = $arr[0]['reach_time'];
     $data['cart_id'] = $arr[0]['cart_id'];
     $data['cart_data'] = $arr[0]['cart_data'];
     // dump($arr[0]['cart_data']);
     $data['merchant_remark'] = $merchant_remark;
     $data['member_remark'] = $arr[0]['description'];
     $data['addtime'] = time();
     $result = $order->add($data);
     // echo $order->getLastSql();
     if ($result) {
         // 修改需求 确认订单已被商家完成
         CommonController::order_done($id, $merchant_id);
         $jid = CommonController::getJid($merchant_id, 2);
         //云推送
         $jpush = new \App\Model\JpushModel();
         $jpush->user = 2;
         $jpush->push(3, array($jid), array('order_no' => $order_no));
         $xmpp = new \App\Model\XmppApiModel();
         $xmpp->requestPush(3, array($jid), array('order_no' => $order_no));
         $this->jsonUtils->echo_json_msg(0, 'ok');
         exit;
     } else {
         $this->jsonUtils->echo_json_msg(1, '确认需求错误...');
         exit;
     }
 }
 /**
  * 添加动态的消息通知
  */
 protected function addRecentMessage($arr)
 {
     $db = M('AnswerUserRecentMessage');
     $data = $db->add($arr);
     if ($data) {
         $user = M('SystemUser')->where(array('id' => $arr['belong']))->getField('type');
         //jpush
         $jpush = new \App\Model\JpushModel();
         $jpush->user = $user;
         $jpush->push(4, array($arr['belong']), array());
         $xmpp = new \App\Model\XmppApiModel();
         $xmpp->requestPush(4, array($arr['belong']), array());
     }
     return $data;
 }