where('charge_station_number', $station_number)->value('charge_station_id'); $query = Db::table($table)->where('openid', $openid)->where('charge_station_id', $station_id)->find(); if (empty($query)) { $data = ['openid' => $openid, 'charge_station_id' => $station_id]; Db::table($table)->save($data); return json(['code' => 200, 'message' => '新增收藏成功', 'is_collect' => 1, 'collect_message' => '取消收藏']); } else { return json(['code' => 1, 'message' => '收藏过了,代码可能有问题']); } } elseif (intval($is_collect) == 1) { $station_id = Db::table('charge_station')->where('charge_station_number', $station_number)->value('charge_station_id'); $query = Db::table($table)->where('openid', $openid)->where('charge_station_id', $station_id)->find(); if (!empty($query)) { Db::table($table)->where('openid', $openid)->where('charge_station_id', $station_id)->delete(); return json(['code' => 200, 'message' => '取消收藏成功', 'is_collect' => 0, 'collect_message' => '收藏']); } else { return json(['code' => 1, 'message' => '没有收藏呀,代码可能有问题']); } } } public function MyCollect($openid, $lat, $lng) { $ss = array(); $table = 'zxc_user_collect'; $station = Db::table($table)->where('openid', $openid)->select(); $sql = "SELECT charge_station_id,count(charge_station_id) pile_number FROM charge_pile GROUP BY charge_station_id HAVING count(charge_station_id)>1 ORDER BY charge_station_id"; $message = Db::query($sql); foreach ($message as $m) { $charge_station_id = $m['charge_station_id']; $pile_number = $m['pile_number']; ChargeStationModel::where('charge_station_id', $charge_station_id)->update(['charge_pile_num' => $pile_number]); } foreach ($station as $st) { $station_id = $st['charge_station_id']; $lat1 = $lat; $lng1 = $lng; $lat2 = Db::table('charge_station')->where('charge_station_id', $station_id)->value('latitude'); $lng2 = Db::table('charge_station')->where('charge_station_id', $station_id)->value('longitude'); $distance = $this->getDistance($lat1, $lng1, $lat2, $lng2); $station_name = Db::table('charge_station')->where('charge_station_id', $station_id)->value('charge_station_name'); $station_number = Db::table('charge_station')->where('charge_station_id', $station_id)->value('charge_station_number'); $free = 0; $occupy = 0; $fault = 0; $i = 1; $mess = Db::table('charge_pile')->where('charge_station_id', $station_id)->select(); foreach ($mess as $m) { if ($m['status'] == 1) { $free += 1; } elseif ($m['status'] == 2) { $occupy += 1; } elseif ($m['status'] == 3) { $fault += 1; } $no = Db::table('charge_pile')->where('charge_pile_id', $m['charge_pile_id'])->value('no'); if ($no == null) { Db::table('charge_pile')->where('charge_pile_id', $m['charge_pile_id'])->update(['no' => $i]); } $i += 1; } $data_station = [ 'pile_free' => $free, 'pile_occupy' => $occupy, 'pile_fault' => $fault ]; Db::table('charge_station')->where('charge_station_id', $station_id)->update($data_station); $collect = Db::table($table)->where('openid', $openid)->where('charge_station_id', $station_id)->find(); if (empty($collect)) { $is_collect = 0; $collect_message = '收藏'; } else { $is_collect = 1; $collect_message = '取消收藏'; } $kk['ChargeStationNumber'] = $station_number; $kk['ChargeStationName'] = $station_name; $kk['Free'] = $free; $kk['Occupy'] = $occupy; $kk['Fault'] = $fault; $kk['Distance'] = $distance; $kk['is_collect'] = $is_collect; $kk['message'] = $collect_message; $ss[] = $kk; } return json($ss); } /** * 显示资源列表 * * @return \think\Response */ public function index_ToWechat(Request $request) { $data = $request->param(); $openid = $data['openid']; $table = 'zxc_user_collect'; $charge_station_id = Db::table($table)->where('openid', $openid)->column('charge_station_id'); $infomation = []; foreach ($charge_station_id as $id) { $charge_station_number = Db::table('charge_station')->where('charge_station_id', $id)->value('charge_station_number'); $charge_station_name = Db::table('charge_station')->where('charge_station_id', $id)->value('charge_station_name'); $sql = "SELECT charge_station_id,count(charge_station_id) pile_number FROM charge_pile GROUP BY charge_station_id HAVING count(charge_station_id)>1 ORDER BY charge_station_id"; $message = Db::query($sql); foreach ($message as $m) { $charge_station_id = $m['charge_station_id']; $pile_number = $m['pile_number']; ChargeStationModel::where('charge_station_id', $charge_station_id)->update(['charge_pile_num' => $pile_number]); } $free = 0; $occupy = 0; $fault = 0; $i = 1; $mess = Db::table('charge_pile')->where('charge_station_id', $id)->select(); foreach ($mess as $m) { if ($m['status'] == 1) { $free += 1; } elseif ($m['status'] == 2) { $occupy += 1; } elseif ($m['status'] == 3) { $fault += 1; } $no = Db::table('charge_pile')->where('charge_pile_id', $m['charge_pile_id'])->value('no'); if ($no == null) { Db::table('charge_pile')->where('charge_pile_id', $m['charge_pile_id'])->update(['no' => $i]); } $i += 1; } $data_station = [ 'pile_free' => $free, 'pile_occupy' => $occupy, 'pile_fault' => $fault ]; Db::table('charge_station')->where('charge_station_id', $id)->update($data_station); $lat1 = $data['lat']; $lng1 = $data['lng']; $lat2 = Db::table('charge_station')->where('charge_station_id', $id)->value('latitude'); $lng2 = Db::table('charge_station')->where('charge_station_id', $id)->value('longitude'); $distance = $this->getDistance($lat1, $lng1, $lat2, $lng2); $info = [ 'charge_station_id' => $id, 'charge_station_number' => $charge_station_number, 'station_name' => $charge_station_name, 'distance' => $distance, 'free' => $free, 'occupy' => $occupy, 'fault' => $fault ]; array_push($infomation, $info); } return json($infomation); } /** * 显示创建资源表单页. * * @return \think\Response */ public function create() { // } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function save(Request $request) { $data = $request->param(); $table = 'zxc_user_collect'; // $sql = "CREATE TABLE IF NOT EXISTS ".$table." ( // `usercollect_id` int unsigned NOT NULL AUTO_INCREMENT, // `openid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, // `charge_station_id` int DEFAULT NULL, // `charge_pile_number` varchar(50) DEFAULT NULL, // `area` varchar(50) DEFAULT NULL, // PRIMARY KEY (`usercollect_id`) USING BTREE, // KEY `user_id` (`openid`) // ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb3"; // Db::execute($sql); // $openid = $data['openid']; $charge_pile_number = $data['charge_pile_number']; $charge_station_id = Db::table('charge_pile')->where('charge_pile_number', $charge_pile_number)->value('charge_station_id'); $pile_data = [ 'openid' => $openid, 'charge_station_id' => $charge_station_id, 'charge_pile_number' => $charge_pile_number, 'area' => '' ]; $mess = Db::table($table)->where([['openid', '=', $openid], ['charge_pile_number', '=', $charge_pile_number]])->find(); if ($mess == null) { $result = Db::table($table)->insert($pile_data); if ($result == 1) { return json(['code' => 200, 'massage' => '收藏成功']); } else { return json(['code' => 1, 'massage' => '收藏失败']); } } else { return json(['code' => 1, 'massage' => '已收藏过']); } } /** * 显示指定的资源 * * @param int $id * @return \think\Response */ public function read($id) { // } /** * 显示编辑资源表单页. * * @param int $id * @return \think\Response */ public function edit($id) { // } /** * 保存更新的资源 * * @param \think\Request $request * @param int $id * @return \think\Response */ public function update(Request $request, $id) { // } /** * 删除指定资源 * * @return \think\Response */ public function delete(Request $request) { $data = $request->param(); $table = 'zxc_user_collect'; $openid = $data['openid']; $charge_pile_number = $data['charge_pile_number']; $massage = [ ['openid', '=', $openid], ['charge_pile_number', '=', $charge_pile_number] ]; $result = Db::table($table)->where($massage)->delete(); if ($result != 0) { return json(['code' => 200, 'massage' => '取消收藏成功']); } else { return json(['code' => 1, 'massage' => '取消收藏失败']); } } }