/**
  * 用户发布需求
  */
 public function add_demand()
 {
     $publish = isset($_POST['publish']) ? (int) htmlspecialchars($_POST['publish']) : '0';
     // 发布需求方式默认0,0 项目发布需求 1保养发布需求
     $cart_id = isset($_POST['cart_id']) ? (int) htmlspecialchars($_POST['cart_id']) : '';
     $reach_time = isset($_POST['reach_time']) ? htmlspecialchars($_POST['reach_time']) : '';
     $desc = isset($_POST['desc']) ? htmlspecialchars($_POST['desc']) : '';
     // 		$range_km = isset ( $_POST ['range_km'] ) ? htmlspecialchars ( $_POST ['range_km'] ) : '';
     $category_ids = isset($_POST['category_ids']) ? htmlspecialchars($_POST['category_ids']) : '';
     $member_session_id = $_POST['member_session_id'];
     $member_id = $this->session_handle->getsession_userid($member_session_id);
     $longitude = $_POST['longitude'];
     $latitude = $_POST['latitude'];
     if (empty($longitude) || empty($latitude)) {
         $this->jsonUtils->echo_json_msg(4, '经度或者纬度为空...');
         exit;
     }
     // 		Log::write($cart_id,'ERR');
     if (empty($cart_id)) {
         $this->jsonUtils->echo_json_msg(4, '车辆为空...');
         exit;
     }
     if (empty($reach_time)) {
         $this->jsonUtils->echo_json_msg(4, '预约时间为空');
         exit;
     }
     $arr_ids = explode(",", $category_ids);
     $count = count($arr_ids);
     $name = $this->getDemandName($arr_ids, $publish);
     if (!$name) {
         $this->jsonUtils->echo_json_msg(4, '发布需求的项目存在错误');
         exit;
     }
     $reach_time = strtotime($reach_time);
     if ($reach_time < time()) {
         $this->jsonUtils->echo_json_msg(4, '发布到店时间要大于当前时间');
         exit;
     }
     $auth = $this->auth_add_demand($member_id);
     $ll_arr = rangekm(C('PUSH_RANGE_KM'), $longitude, $latitude);
     //获取最大最小经纬度
     $maxLng = $ll_arr['maxLng'];
     $minLng = $ll_arr['minLng'];
     $maxLat = $ll_arr['maxLat'];
     $minLat = $ll_arr['minLat'];
     $data['publish'] = $publish;
     if ($publish == 1) {
         $km = isset($_POST['km']) ? htmlspecialchars($_POST['km']) : '';
         if (empty($km)) {
             $this->jsonUtils->echo_json_msg(4, '用户公里数为空');
             exit;
         }
         $param = array('km' => $km, 'category_ids' => $category_ids);
         $data['param'] = json_encode($param);
     }
     $data['member_id'] = $member_id;
     $data['cart_id'] = $cart_id;
     $data['cart_data'] = $this->getcart($cart_id);
     $data['title'] = $name;
     // $data['city_id']=$area['city'];
     // $data['area_id']=$area_id;
     $data['reach_time'] = $reach_time;
     $data['description'] = $desc;
     $data['addtime'] = time();
     $data['expire_time'] = time() + 24 * 60 * 60;
     $data['range_km'] = C('PUSH_RANGE_KM');
     $data['longitude'] = $longitude;
     $data['latitude'] = $latitude;
     $data['pics'] = json_encode(array());
     if ($_FILES) {
         $f_arr = mul_upload('/Demand/', 1);
         if ($f_arr) {
             $data['pics'] = json_encode($f_arr);
             // 把多张图片数组格式转json保存数据库
         }
     }
     $result = $this->dao->add($data);
     if ($result) {
         $data['demand_id'] = $result;
         $subitems = M("member_demand_subitems");
         if (!empty($category_ids)) {
             $data1["member_id"] = $member_id;
             // 会员ID
             $data1["demand_id"] = $result;
             // 需求id
             foreach ($arr_ids as $key => $value) {
                 $data1['category_id'] = $value;
                 // 子项目ID
                 $subitems->add($data1);
             }
         }
         //查询出范围内的商家,给其调整需求配置
         $sql = "select a.business_time,a.id,b.id as jid from " . C('DB_PREFIX') . "merchant as a \r\n\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\twhere a.longitude <={$maxLng} and a.longitude>={$minLng} and a.latitude <={$maxLat} and a.latitude>={$minLat} and ( a.status = 0 or a.status = 1)";
         $ids = M('')->query($sql);
         //echo M('')->getLastSql();
         if ($ids) {
             foreach ($ids as $key => $row) {
                 $addAll[$key]['id'] = null;
                 $addAll[$key]['merchant_id'] = $row['id'];
                 $addAll[$key]['demand_id'] = $result;
                 if (timeCompare($row['business_time'])) {
                     $jid[] = $row['jid'];
                 }
             }
             M('DemandMerchantEnable')->addAll($addAll);
             $jpush = new \App\Model\JpushModel();
             $jpush->user = 2;
             $jpush->push(1, $jid, $data);
             $xmpp = new \App\Model\XmppApiModel();
             $xmpp->requestPush(1, $jid, $data);
             //云推送
         }
         $this->jsonUtils->echo_json_msg(0, '发布需求成功!');
         exit;
     } else {
         $this->jsonUtils->echo_json_msg(1, 'failed');
         exit;
     }
 }
 /**
  * 发布需求 规则::(以下数值均可调整)
  * 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));
 }
 /**
  * 遇见模块 列表
  */
 public function getNearbyRecentV2()
 {
     $session_id = isset($_POST['session_id']) ? htmlspecialchars($_POST['session_id']) : '';
     $member_id = $this->session_handle->getsession_userid($session_id, 1);
     $systemid = CommonController::getSystemUserid($member_id['id'], $member_id['type']);
     $longitude = isset($_POST['longitude']) ? htmlspecialchars($_POST['longitude']) : '';
     $latitude = isset($_POST['latitude']) ? htmlspecialchars($_POST['latitude']) : '';
     $page = isset($_POST['page']) ? htmlspecialchars($_POST['page']) : '1';
     $num = isset($_POST['num']) ? htmlspecialchars($_POST['num']) : '6';
     //type =1 附近的动态 =2 已关注的人的动态
     $type = isset($_POST['type']) ? htmlspecialchars($_POST['type']) : '1';
     $db = M('');
     if ($type == 1) {
         if (empty($longitude)) {
             $this->jsonUtils->echo_json_msg(4, '精度错误');
             exit;
         }
         if (empty($latitude)) {
             $this->jsonUtils->echo_json_msg(4, '维度错误');
             exit;
         }
         $ll_arr = rangekm(3, $longitude, $latitude);
         //获取最大最小经纬度
         $maxLng = $ll_arr['maxLng'];
         $minLng = $ll_arr['minLng'];
         $maxLat = $ll_arr['maxLat'];
         $minLat = $ll_arr['minLat'];
         //$where = " a.longitude <=$maxLng and a.longitude>=$minLng and a.latitude <=$maxLat and a.latitude>=$minLat and a.status=0 and a.addtime <".time();
         $where = "  a.status=0 ";
         $data = $db->table(C("DB_PREFIX") . "answer_user_recent as a ")->field("b.header,b.name,a.id,a.system_user_id,a.content,a.pics,a.addtime,a.longitude,a.latitude,a.comment_count,a.laud_count,b.brand_icon")->join(C('DB_PREFIX') . "system_user as b on a.system_user_id = b.id")->where($where)->order("a.addtime desc")->limit($num)->page($page)->select();
     } elseif ($type == 2) {
         $data = $db->table(C("DB_PREFIX") . "answer_user_recent as a")->field("b.header,b.name,a.id,a.system_user_id,a.content,a.pics,a.addtime,a.longitude,a.latitude,a.comment_count,a.laud_count,b.brand_icon")->join(C('DB_PREFIX') . "answer_user_attention as c on c.attented = a.system_user_id ")->join(C('DB_PREFIX') . "system_user as b on a.system_user_id = b.id", 'LEFT')->where("c.host = {$systemid} and a.status=0 ")->order("a.addtime desc")->limit($num)->page($page)->select();
         // 			echo $db->getLastSql();
     } elseif ($type == 3) {
         $data = $db->table(C("DB_PREFIX") . "answer_user_recent as a")->field("b.header,b.name,a.id,a.system_user_id,a.content,a.pics,a.addtime,a.longitude,a.latitude,a.comment_count,a.laud_count,b.brand_icon")->join(C('DB_PREFIX') . "system_user as b on a.system_user_id = b.id", 'LEFT')->where("a.system_user_id = {$systemid} and a.status=0 ")->order("a.addtime desc")->limit($num)->page($page)->select();
         // 			echo $db->getLastSql();
     } else {
         $this->jsonUtils->echo_json_msg(4, 'type错误');
         exit;
     }
     if ($data) {
         foreach ($data as $key => $row) {
             unset($data[$key]['longitude']);
             unset($data[$key]['latitude']);
             $data[$key]['header'] = imgUrl($row['header']);
             $data[$key]['brand_icon'] = imgUrl($row['brand_icon']);
             $data[$key]['addtime'] = dealtime($row['addtime']);
             $data[$key]['pics'] = imgUrl(json_decode($row['pics'], true));
             if (!empty($longitude) && !empty($latitude)) {
                 $data[$key]['distance'] = getDistance($longitude, $latitude, $row['longitude'], $row['latitude']);
             }
             //	$data [$key]['list'] = $this->getRecentComment($row['id']);
             $data[$key]['comment_count'] = $row['comment_count'];
         }
     } else {
         $data = array();
     }
     $arr['list'] = $data;
     $this->jsonUtils->echo_json_data(0, 'ok', $arr);
     exit;
 }