canyin-project/ybcy/controllers/channelApi/RollBagController.php
2024-11-01 16:07:54 +08:00

114 lines
4.3 KiB
PHP

<?php
namespace app\controllers\channelApi;
use Illuminate\Support\Facades\DB;
use Yii;
use app\models\common\Config;
use app\models\common\Drag;
use app\models\common\WeChat;
use app\models\common\File;
use app\models\common\Power;
class RollBagController extends CommonController{
public $enableCsrfValidation = false;
function init(){
parent::init();
$power=Power::getPayPower($this->wqData['uniacid'],'rollBag');
if(!$power){
echo json_encode(['code'=>2,'msg'=>'暂无权限']);die;
}
}
//券包详情
public function actionRollBagInfo()
{
$userId=$this->wqData['userId'];
$result=axios_request();
$id=$result['id'];
$res=(new \yii\db\Query())
->select("a.*,s.name as storeName,s.icon as storeIcon")
->from('{{%ybwm_roll_bag}} as a')
->join('LEFT JOIN', '{{%ybwm_store}} as s', 's.id = a.storeId')
->where('a.id=:id',[':id'=>$id])->one();
$res['coupon']=json_decode($res['coupon'],true);
$res['media']=json_decode($res['media'],true);
$res['canBuy']=1;
if($res['stock']<=0){
$res['canBuy']=2;
}
$startTime=strtotime(date("Y-m-d")." 00:00:00");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
$myNum=(new \yii\db\Query())
->from('{{%ybwm_roll_bag_order}}')
->where('userId=:userId AND state=2 AND payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime,':userId'=>$userId])
->count();
if($myNum>$res['dayNum']){
$res['canBuy']=2;
}
return $this->result(1, '成功', $res);
}
//券包下订单
public function actionRollBagOrder()
{
$request = Yii::$app->request;
$result=axios_request();
$userId=$this->wqData['userId'];
$uniacid=$this->wqData['uniacid'];
if($request->isGet){
return $this->result(2,'请求异常');
}
$rollBag=(new \yii\db\Query())
->from('{{%ybwm_roll_bag}}')
->where('id=:id',[':id'=>$result['rollBagId']])
->one();
if($rollBag['stock']<=0){
return $this->result(2,'库存不足');
}
$startTime=strtotime(date("Y-m-d")." 00:00:00");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
$myNum=(new \yii\db\Query())
->from('{{%ybwm_roll_bag_order}}')
->where('userId=:userId AND state=2 AND payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime,':userId'=>$userId])
->count();
if($myNum+$result['num']>$rollBag['dayNum']){
return $this->result(2,'超过每人每日购买限制');
}
$data['userId'] = $userId;
$data['money'] = bcmul($rollBag['money'],$result['num'],2);
$data['storeId'] = $rollBag['storeId'];
$data['bagId'] = $result['rollBagId'];
$data['bagIcon'] = $rollBag['icon'];
$data['bagName'] = $rollBag['name'];
$data['coupon'] = $rollBag['coupon'];
$data['days'] = $rollBag['days'];
$data['num'] = $result['num'];
$data['outTradeNo'] = date("YmdHis") . rand(111111, 999999);
$data['state'] = 1;
$data['createdAt'] = time();
$data['uniacid'] = $uniacid;
$order = Yii::$app->db->createCommand()->insert('{{%ybwm_roll_bag_order}}', $data)->execute();
$orderId = Yii::$app->db->getLastInsertID();
if ($order) {
return $this->result(1, '成功', $orderId);
} else {
return $this->result(2, '失败');
}
}
//购买记录
public function actionOrderList(){
$userId=$this->wqData['userId'];
$result=axios_request();
$page=$result['page']?:1;
$size=$result['size']?:10;
$table=(new \yii\db\Query())
->select('bagIcon,bagName,money,from_unixtime(createdAt) createdAt,payMode')
->from('{{%ybwm_roll_bag_order}}')
->where('userId=:userId AND state=2',[':userId'=>$userId]);
$table->offset(($page - 1) * $size)->limit($size);
$res=$table->orderby('id desc')->all();
for($i=0;$i<count($res);$i++){
$res[$i]['payModeName']=$this->payMode[$res[$i]['payMode']];
}
return $this->result(1, '成功', $res);
}
}