Files

98 lines
2.2 KiB
PHP

<?php
namespace app\api\model\app\exchange;
use think\Model;
use think\Db;
use app\api\model\app\agent\Level;
class Code extends Model
{
// 表名
protected $name = 'app_exchange_code';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
];
/**
* 检查兑换码
* @param $code
* @return void
*/
public function checkCode($code)
{
//判断有没有
$data = self::with(['batch'])->where([
'code'=>$code,
'code.status'=>1,
'batch.status'=>1,
'batch.starttime'=>['<',time()],
'batch.endtime'=>['>',time()]
])->find();
if(!$data){
return false;
}
return $data;
}
public function batch()
{
return $this->belongsTo('Batch', 'batch_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
/**
* 兑换
* @param $userId 用户
* @param $goodsId 商品
* @return bool
*/
public function exchange($userId,$batchId,$code)
{
$goods = \app\api\model\app\exchange\Goods::getBatchGoods($batchId);
if($goods){
$subscriptionModel = new \app\common\model\user\Subscription();
foreach ($goods as $item){
$subscriptionModel->setUserCourse($userId,$item['goods_id'],'exchange');
\app\api\model\app\exchange\UseLog::insert([
'user_id'=>$userId,
'uniacid'=>UNIACID,
'goods_id'=>$item['goods_id'],
'code'=>$code,
'batch_id'=>$batchId,
'createtime'=>time()
]);
self::where([
'code'=>$code,
'batch_id'=>$batchId
])->update([
'user_id'=>$userId,
'use_time'=>time(),
'status'=>2
]);
}
}
return true;
}
}