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

134 lines
5.4 KiB
PHP

<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use app\models\common\Store;
use Yii;
use EasyWeChat\Factory;
use app\models\common\Config;
use app\models\common\Power;
class RollBagController extends CommonController{
public $enableCsrfValidation = false;
private $shop_id;
function init(){
parent::init();
$this->shop_id=$this->storeId?:Store::getMainStore($this->wqData['uniacid'])['id'];
$this->shop_id=$this->shop_id?:0;
$power=Power::getPayPower($this->wqData['uniacid'],'rollBag');
if(!$power){
echo json_encode(['code'=>2,'msg'=>'暂无权限']);die;
}
}
//券包活动列表
public function actionRollBagList(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$page=$result['page']?:1;
$num=($page-1)*10;
$limit=10;
$table=(new \yii\db\Query())
->from('{{%ybwm_roll_bag}}')
->select('salesNum,id,name,from_unixtime(createdAt) createdAt,display,from_unixtime(startTime) startTime,from_unixtime(endTime) endTime,price,money,stock')
->where(['deleteAt'=>0,'uniacid'=>$this->wqData['uniacid'],'storeId'=>$storeId]);
$list=$table->offset($num)
->limit($limit)->orderBy('id desc')->all();
$count=$table->count();
// for($i=0;$i<count($list);$i++){
// $list[$i]['salesNum']=(new \yii\db\Query())
// ->from('{{%ybwm_roll_bag_order}}')
// ->where('bagId=:bagId AND state=2',[':bagId'=>$list[$i]['id']])->count();
// }
return $this->result(1, '成功',$list,$count);
}
//保存券包
public function actionSaveRollBag(){
$result=axios_request();
$request = Yii::$app->request;
$storeId=$result['storeId']?:$this->shop_id;
if($request->isGet){
$info=(new \yii\db\Query())
->from('{{%ybwm_roll_bag}}')
->where('id=:id',[':id'=>$result['id']])->one();
$info['coupon']=json_decode($info['coupon'],true);
$info['media']=json_decode($info['media'],true);
$info['startTime']=date("Y-m-d H:i:s",$info['startTime']);
$info['endTime']=date("Y-m-d H:i:s",$info['endTime']);
return $this->result(1, '成功',$info);
}
if(!$result['name']){
return $this->result(2,'活动名称不能为空!');
}
$num=0;
for($i=0;$i<count($result['coupon']);$i++){
$num +=$result['coupon'][$i]['num'];
}
$data['name']=$result['name'];
$data['couponNum']=$num;
$data['icon']=$result['icon'];
$data['price']=$result['price'];
$data['stock']=$result['stock'];
$data['dayNum']=$result['dayNum'];
$data['days']=$result['days'];
$data['details']=$result['details'];
$data['media']=json_encode($result['media']);
$data['startTime']=strtotime($result['startTime']);
$data['endTime']=strtotime($result['endTime']);
$data['money']=$result['money']?:0;
$data['coupon']=json_encode($result['coupon'])?:'';
$data['storeId']=$storeId;
if($result['id']){
$info=(new \yii\db\Query())
->from('{{%ybwm_roll_bag}}')
->where('id!=:id AND deleteAt=0 AND uniacid=:uniacid AND storeId=:storeId AND (startTime<=:endTime AND endTime>=:startTime)',[':id'=>$result['id'],':endTime'=>strtotime($result['endTime']),':startTime'=>strtotime($result['startTime']),':storeId'=>$storeId,':uniacid'=>$this->wqData['uniacid']])->one();
if($info){
return $this->result(2,'同一时间段只能创建一个活动!');
}
$data['changeAt']=time();
$re = Yii::$app->db->createCommand()->update('{{%ybwm_roll_bag}}', $data, 'id=:id', ['id' =>$result['id']])->execute();
}else{
$info=(new \yii\db\Query())
->from('{{%ybwm_roll_bag}}')
->where('uniacid=:uniacid AND deleteAt=0 AND storeId=:storeId AND (startTime<=:endTime AND endTime>=:startTime)',[':endTime'=>strtotime($result['endTime']),':startTime'=>strtotime($result['startTime']),':storeId'=>$storeId,':uniacid'=>$this->wqData['uniacid']])->one();
if($info){
return $this->result(2,'同一时间段只能创建一个活动!');
}
$data['uniacid']=$this->wqData['uniacid'];
$data['createdAt']=time();
try{
$re = Yii::$app->db->createCommand()->insert('{{%ybwm_roll_bag}}', $data)->execute();
}catch (\Exception $e){
print_R($e);die;
}
}
if($re){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//修改券包活动
public function actionModifyRollBag(){
$post=axios_request();
if($post['type']==1){
//显示隐藏
$data['display']=$post['display'];
}
if($post['type']==2){
//删除
$data['deleteAt']=time();
}
$data['changeAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_roll_bag}}', $data, 'id=:id', ['id' =>$post['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
}