//解除绑定蓝牙
//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. osip2 代码分析

    主要类型定义: 1.osip_t /** * Structure for osip handling. * In order to use osip, you have to manage at le ...

  2. NorthWind 数据库整体关系

    http://blog.csdn.net/bergn/article/details/1502150 今天看到一张非常有用的图,说明有关Northwind数据库整体关系的图,以前一直在用,但是没有一个 ...

  3. 安装oracle 11g时出现启动服务出现错误,找不到OracleMTSRecoveryService

    运行注册表(cmd-输入regedit),到 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services下,找到OracleMTSRecoveryServ ...

  4. 读取config配置

    在搭建自动化测试框架时,经常会使用config.properties文件存储配置,文件内容格式如下: 读取config.properties文件代码如下: public class Putils { ...

  5. IntelliJ Idea 常用快捷键列表

    Alt+回车 导入包,自动修正 sout+Tab Syso快捷键 Ctrl+Alt+F 局部变量变为全局变量 Ctrl+Alt+V 类似于eclipse中Ctrl+2+L的快捷键 Ctrl+Alt+T ...

  6. ajax请求成功后新窗口window.open()被拦截的解决方法

    ajax 异步请求成功后需要新开窗口打开 url,使用的是 window.open() 方法,但是该操作并不是用户主动触发的,所以它认为这是不安全的就拦截了(不过如果是 _self 的话就不会有这个限 ...

  7. GC

    垃圾回收机制的优点:释放无用的对象所占用的空间.方式:自动回收.手动回收.使用System.gc实际上是调用Runtime.getRuntime().gc()

  8. 升级win8.1后mysql服务不能启动的问题

    升级win8.1后发现服务列表中MySQL55不见了. 1. 先把服务恢复. mysql没有看到maitenance,找到安装文件 mysql-installer-community-5.6.12.0 ...

  9. http的一些事

    查找资料将http中缓存相关的知识记录下 一般来说:Last-Modifed和Expires是一对,ETag和Cache-Control是一对 Last-Modified Client端跟Server ...

  10. 控制反转IOC的依赖注入方式

    引言: 项目中遇到关于IOC的一些内容,因为和正常的逻辑代码比较起来,IOC有点反常.因此本文记录IOC的一些基础知识,并附有相应的简单实例,而在实际项目中再复杂的应用也只是在基本应用的基础上扩展而来 ...