//解除绑定蓝牙
//http://www.520m.com.cn/api/pet/remove-binding?healthy_id=72&pet_id=100477&access-token=YWdqPCWdt3_IqkrTK-U5vS458ZWfcdeT
public function actionRemoveBinding(){
$healthy_id = Yii::$app->request->get('healthy_id');
if(empty($healthy_id)){
return Helper::format_data(PARAM_ERROR, ['healthy_id'=>'无效的参数healthy_id']);
}

$pet_id = Yii::$app->request->get('pet_id');
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, ['pet_id'=>'无效的参数pet_id']);
}

$healthyinfo = Healthy::find()->where(['healthy_id'=>$healthy_id])->asArray()->one();
if($healthyinfo['is_activation']=='2'){
return Helper::format_data(PARAM_ERROR, ['is_activation'=>'已经执行过解除绑定操作']);
}

$access_token = Yii::$app->request->get('access-token');
$userinfo = UserOperate::find()->where(['access_token'=>$access_token])->asArray()->one();
$user_id = $userinfo['id'];
$time = time();

$model = new Healthy();
$data = ['remove_binding'=>$time,'longitude'=>'','latitude'=>'','is_activation'=>'2'];
$where = ['healthy_id' => $healthy_id,'pet_id' => $pet_id,'user_id' => $user_id];

if($model->updateAll($data,$where)){
$model = new Pet();
$data = ['bluetooth_id'=>''];
$where = ['id' => $pet_id,'user_id' => $user_id];
$model->updateAll($data,$where);
return Helper::format_data(SUCC,null);
}else{
return Helper::format_data(PARAM_ERROR,null);
}
}

//绑定MAC地址
//http://www.520m.com.cn/api/pet/binding-mac?ak=OTAzMTY3Nzl=A1&pet_id=100477&mac=07-16-76-00-02-86&access-token=A20AgFR0C3Au0hS8zJ1kG2dH0bTXcQ6B
public function actionBindingMac(){

$data = Yii::$app->request->get();
$ak = $data['ak'];
if(empty($ak)){
return Helper::format_data(PARAM_ERROR, ['msg'=>'无效的参数ak']);
}

$count = Healthy::find()->where(['ak'=>$ak])->count();
if($count>=1){
return Helper::format_data(PARAM_ERROR, ['msg'=>'二维码已经被使用']);
}

$mac = $data['mac'];
if(empty($mac)){
return Helper::format_data(PARAM_ERROR, ['msg'=>'无效的参数mac']);
}

//从右向左截第一个来判断设备颜色
$color = substr($ak,-1);
if($color=='1'){
$hardware_color = '白色';
}else if($color=='2'){
$hardware_color = '黑色';
}else if($color=='3'){
$hardware_color = '金色';
}else{
$hardware_color = '粉色';
}

//从右向左截第一个来判断设备类型
$name = substr($ak,-2,1);
if($name=='A'){
$hardware_name = '蓝牙铃铛';
}else{
$hardware_name = '未识别的设备';
}

$pet_id = $data['pet_id'];
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, ['msg'=>'无效的参数pet_id']);
}

//根据pet_id查询宠物的信息
$petinfo=PetOperate::find()->where(['id'=>$pet_id])->asArray()->one();
if(empty($petinfo)){
return Helper::format_data(PARAM_ERROR, ['pet_id'=>'无效的参数pet_id']);
}
$user_id = $petinfo['user_id'];
$pet_name = $petinfo['pet_name'];
$bluetooth_id = $petinfo['bluetooth_id'];
if($bluetooth_id!=null){
return Helper::format_data(PARAM_ERROR, ['bluetooth_id'=>'已经绑定']);
}

//实现入库操作
$model = new Healthy();
$model->run = '0';
$model->go = '0';
$model->jump = '0';
$model->sleep = '0';
$model->pet_id = $pet_id;
$model->user_id = $user_id;
$model->pet_name = $pet_name;
$model->hardware_name = $hardware_name;
$model->hardware_color = $hardware_color;
$model->remove_binding = '';
$model->mac = $mac;
$model->longitude = '';
$model->latitude = '';
$model->is_activation = '1';
$model->ak = $ak;
if($model->insert()){
$id = $model->attributes['healthy_id'];
}

//给宠物表填充bluetooth_id
$model2 = new Pet();
$data = ['bluetooth_id'=>$id];
$where = ['id'=>$pet_id];

if($model2->updateAll($data,$where)){
$dat['healthy_id'] = $id;
$dat['pet_name'] = $pet_name;
$dat['hardware_name'] = $hardware_name;
$dat['hardware_color'] = $hardware_color;
return Helper::format_data(SUCC,$dat);
}else{
return Helper::format_data(PARAM_ERROR,null);
}
}

//mac激活的时候专用接口
//http://www.520m.com.cn/api/pet/mac?healthy_id=106&mac=07-16-76-00-02-86&pet_id=100477&access-token=A20AgFR0C3Au0hS8zJ1kG2dH0bTXcQ6B
/*public function actionMac(){
$healthy_id = Yii::$app->request->get('healthy_id');
if(empty($healthy_id)){
return Helper::format_data(PARAM_ERROR, ['healthy_id'=>'无效的参数healthy_id']);
}

$mac = Yii::$app->request->get('mac');
if(empty($mac)){
return Helper::format_data(PARAM_ERROR, ['mac'=>'无效的参数mac']);
}

$pet_id = Yii::$app->request->get('pet_id');
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, ['pet_id'=>'无效的参数pet_id']);
}

$macinfo = Healthy::find()->where(['healthy_id'=>$healthy_id])->asArray()->one();
//print_r($macinfo);die;
if($macinfo['mac']!=null){
return Helper::format_data(PARAM_ERROR, ['mac'=>'已经绑定mac地址']);
}

$access_token = Yii::$app->request->get('access-token');
$userinfo = UserOperate::find()->where(['access_token'=>$access_token])->asArray()->one();
$user_id = $userinfo['id'];

$model = new Healthy();
$data = ['mac'=>$mac,'is_activation'=>'1'];
$where = ['healthy_id' => $healthy_id,'pet_id' => $pet_id,'user_id' => $user_id];

if($model->updateAll($data,$where)){
$petinfo=PetOperate::find()->where(['id'=>$pet_id])->asArray()->one();
$pet_name = $petinfo['pet_name'];

$healthyinfo = Healthy::find()->where(['mac'=>$mac])->asArray()->one();

$data['pet_name'] = $pet_name;
$data['hardware_name'] = $healthyinfo['hardware_name'];
$data['hardware_color'] = $healthyinfo['hardware_color'];

return Helper::format_data(SUCC,$data);
}else{
return Helper::format_data(PARAM_ERROR,null);
}
}*/

//扫描时判断mac地址是否存在
//http://www.520m.com.cn/api/pet/is-mac?mac=07-16-76-00-02-86&access-token=YWdqPCWdt3_IqkrTK-U5vS458ZWfcdeT
public function actionIsMac(){
$mac = Yii::$app->request->get('mac');
if(empty($mac)){
return Helper::format_data(PARAM_ERROR, ['mac'=>'无效的参数mac']);
}
$healthyinfo = Healthy::find()->where(['mac'=>$mac])->asArray()->one();
//print_r($healthyinfo);die;
$pet_id = $healthyinfo['pet_id'];
$petinfo=PetOperate::find()->where(['id'=>$pet_id])->asArray()->one();
//print_r($petinfo);die;
$pet_name = $petinfo['pet_name'];

if(empty($healthyinfo)){
return Helper::format_data(SUCC,null);
}else{
$data['pet_name'] = $pet_name;
$data['hardware_name'] = $healthyinfo['hardware_name'];
$data['hardware_color'] = $healthyinfo['hardware_color'];
return Helper::format_data(SUCC,$data);
}
}

//每个用户的蓝牙铃铛历史绑定记录
//http://www.520m.com.cn/api/pet/history-binding?access-token=YWdqPCWdt3_IqkrTK-U5vS458ZWfcdeT
public function actionHistoryBinding(){
$access_token = Yii::$app->request->get('access-token');
$userinfo = UserOperate::find()->where(['access_token'=>$access_token])->asArray()->one();
$user_id = $userinfo['id'];

$historyinfo = Healthy::find()->select(['healthy_id','pet_name','hardware_name','hardware_color','remove_binding'])->where(['user_id'=>$user_id,'is_activation'=>'2'])->asArray()->all();
if(empty($historyinfo)){
return Helper::format_data(SUCC,null);
}else{
return Helper::format_data(SUCC,$historyinfo);
}
}

//历史绑定设备重新激活
//http://www.520m.com.cn/api/pet/activation?pet_id=100477healthy_id=71&access-token=YWdqPCWdt3_IqkrTK-U5vS458ZWfcdeT
public function actionActivation(){
$healthy_id = Yii::$app->request->get('healthy_id');
if(empty($healthy_id)){
return Helper::format_data(PARAM_ERROR, ['healthy_id'=>'无效的参数healthy_id']);
}

$pet_id = Yii::$app->request->get('pet_id');
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, ['pet_id'=>'无效的参数pet_id']);
}

$bluetoothinfo=PetOperate::find()->where(['id'=>$pet_id])->asArray()->one();
if($bluetoothinfo['bluetooth_id']===$healthy_id){
return Helper::format_data(PARAM_ERROR, ['bluetooth_id'=>'已经执行激活操作']);
}

$access_token = Yii::$app->request->get('access-token');
$userinfo = UserOperate::find()->where(['access_token'=>$access_token])->asArray()->one();
$user_id = $userinfo['id'];

$healthyinfo = Healthy::find()->where(['pet_id'=>$pet_id,'user_id'=>$user_id])->asArray()->all();
foreach($healthyinfo as $key=> $v){
$is_activation[] = $v['is_activation'];
}

if(in_array('1',$is_activation)){
return Helper::format_data(PARAM_ERROR, ['is_activation'=>'请先解除当前绑定']);
}else{
$model = new Healthy();
$data = ['is_activation'=>'1'];
$where = ['healthy_id'=>$healthy_id];
$model->updateAll($data,$where);

$model2 = new Pet();
$data2 = ['bluetooth_id'=>$healthy_id];
$where2 = ['id'=>$pet_id];
$model2->updateAll($data2,$where2);

return Helper::format_data(SUCC,null);

}

}

//体能日志
public function actionEnergyAdd(){
$data = Yii::$app->request->get("work");
$arr = json_decode($data,true);

foreach($arr as $key => $v){
$model = new EnergyLog();
$model->time = $v['time'];
$model->run = $v['run'];
$model->go = $v['go'];
$model->jump = $v['jump'];
$model->sleeping = $v['sleeping'];
$model->pet_id = $v['pet_id'];
$model->save();
}
return Helper::format_data(SUCC,null);
}

//体能日志查询列表
public function actionEnergyList(){
$pet_id = Yii::$app->request->get('pet_id');
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, ['pet_id'=>'无效的参数pet_id']);
}

$energyinfo = EnergyLog::find()->select(['time','run','go','jump','sleeping'])->where(['pet_id'=>$pet_id])->orderBy('energy_id ASC')->limit('7')->asArray()->all();

$data['healthy'] = '99';
$data['love'] = '88';
$data['motion'] = '89';
$data['social'] = '77';
$data['neat'] = '67';
$data['current_time'] = time();
$data['week'] = $energyinfo;

return Helper::format_data(SUCC,$data);
}

yii2框架增删改查案例的更多相关文章

  1. yii2.0增删改查实例讲解

    yii2.0增删改查实例讲解一.创建数据库文件. 创建表 CREATE TABLE `resource` ( `id` int(10) NOT NULL AUTO_INCREMENT, `textur ...

  2. 快速入门GreenDao框架并实现增删改查案例

    大家的项目中不可避免的使用到SQLite,为此我们要花费心思编写一个增删改查框架.而一个好的ORM框架则能够给我们带来极大的方便,今天给大家讲解一个非常火热的ORM-GreenDao. 基本概念 Gr ...

  3. ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)

    Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...

  4. Mybatis的简介+简单实现增删改查案例

    @ 目录 总结内容 1. 基本概念 2. Mybatis的使用 需求 配置文件简介 总结 总结内容 1. 基本概念 Mybatis是一款优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射.My ...

  5. YII2生成增删改查

    下载完成后在basic/db.php配置数据库参数. 1.配置虚拟主机后进入YII入口文件 index.php 进行get传值 ?r=gii ,进入创建界面 2.点击 Model Generator下 ...

  6. Spring4.0+Hibernate4.0+Struts2.3整合包括增删改查案例,解决整合中出现的异常

    源码下载:http://download.csdn.net/detail/cmcc_1234/7034775 ======================Application.xml======== ...

  7. YII2的增删改查

    insert into table (field1,field2)values('1','2');delete from table where   condition update  table s ...

  8. IOS Sqlite用户界面增删改查案例

    1.案例简单介绍 对SQLite操作进行了简单的封装,将对数据表操作转变成对对象的操作,并通过UI界面完毕对用户表的增.删.改.查,执行界面例如以下图所看到的 a 2.项目project文件夹 Use ...

  9. Laravel框架——增删改查

    增: //save返回true false $res = new member(); res->username = 'a'; $res->password = 'b'; dd($res- ...

随机推荐

  1. OLAP在大数据时代的挑战

    转行做数据相关的工作有近两年时间,除了具体技术,还有许多其它思考. 数据的价值 在涉及具体的技术前,先想一想为什么需要OLAP这样的系统,它有什么价值或者说在公司或部门这是不可取代的么? 可以带来哪些 ...

  2. jdbc向各种数据库发送sql语句

    1.有了JDBC,向各种关系数据发送SQL语句就是一件很容易的事.换言之,有了JDBC API,就不必为访问Sybase数据库专门写一个程序,为访问Oracle数据库又专门写一个程序,或为访问Info ...

  3. C# 动态调用webservice

    最近项目中,用到动态调用webservice的内容,此处记录下来,留着以后COPY(我们只需要在XML,config文件,或者数据库中配置webservice连接地址和方法名即可使用): using ...

  4. selenium python 安装

    环境为Win64位系统,默认已经安装python2.7到D:\Python27,此次使用的浏览器为chrome 下面是selenium的安装和chromedriver.exe的下载 1.安装selen ...

  5. 多线程编程-工具篇-BlockingQueue

    在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全"传输"数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序 ...

  6. EditText添加了ImageSpan后,在两者中间不能输入纯文本

    严格来说是连续插入两个ImageSpan之后,在其中间不能够输入纯文本内容. 最后发现问题出现在了SpannableString在设置ImageSpan的时候第四个参数flag的问题. spannab ...

  7. WINDOWS下PhoneGap(Cordova)安装笔记

    1.首先下载Node.js  安装nodejs很简单直接点击安装文件下一步直至成功即可,安装notejs的同时npm也会同时安装 成功后打开notejs的命令行工具 输入“node -v”," ...

  8. 百度echarts

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</t ...

  9. Linux 下应用程序最大打开文件数的理解和修改

    运行在Linux系统上的Java程序运行了一段时间后出现"Too many open files"的异常情况. 这种情况常见于高并发访问文件系统,多线程网络连接等场景.程序经常访问 ...

  10. Tomcat-问题解决

    1,两种方法解决tomcat的 Failed to initialize end point associated with ProtocolHandler ["http-apr-8080& ...