188 lines
7.9 KiB
PHP
188 lines
7.9 KiB
PHP
<?php
|
|
namespace app\controllers\channelApi;
|
|
|
|
use app\models\common\Config;
|
|
use app\models\common\Power;
|
|
use Yii;
|
|
class DividendController extends CommonController{
|
|
public $enableCsrfValidation = false;
|
|
|
|
function init()
|
|
{
|
|
parent::init();
|
|
$power = Power::getPayPower($this->wqData['uniacid'], 'dividend');
|
|
if (!$power) {
|
|
echo json_encode(['code' => 1, 'msg' => '暂无权限']);
|
|
die;
|
|
}
|
|
}
|
|
public function actionIsPop() {
|
|
$now=time();
|
|
$result=axios_request();
|
|
$res =(new \yii\db\Query())
|
|
->select('a.id,b.shareImg,b.shareTitle')
|
|
->from('{{%ybwm_dividend_join}} as a')
|
|
->leftJoin('{{%ybwm_dividend}} as b','a.divideId=b.id')
|
|
->where('a.orderId=:orderId AND b.startTime<=:startTime AND b.endTime>=:endTime AND b.couponStartAt<=:startTime AND b.couponEndAt>=:endTime',[':startTime'=>$now,':endTime'=>$now,':orderId'=>$result['orderId']])->one()?:[];
|
|
if($res){
|
|
return $this->result(1, '成功', $res);
|
|
}else{
|
|
return $this->result(1, '成功', []);
|
|
}
|
|
}
|
|
|
|
//瓜分红包详情
|
|
public function actionInfo() {
|
|
$now=time();
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$res =(new \yii\db\Query())
|
|
->select('a.id,b.details')
|
|
->from('{{%ybwm_dividend_join}} as a')
|
|
->leftJoin('{{%ybwm_dividend}} as b','a.divideId=b.id')
|
|
->where('a.id=:id AND startTime<=:startTime AND endTime>=:endTime AND couponStartAt<=:startTime AND couponEndAt>=:endTime',[':startTime'=>$now,':endTime'=>$now,':id'=>$result['id']])->one()?:[];
|
|
if (!$res) {
|
|
return $this->result(2, '该活动不存在或已结束');
|
|
}
|
|
$receive=(new \yii\db\Query())
|
|
->from('{{%ybwm_dividend_receive}}')
|
|
->where('joinId=:joinId AND userId=:userId',[':joinId'=>$res['id'],':userId'=>$userId])
|
|
->one();
|
|
$data['money']=$receive['money'];
|
|
$data['fullMoney']=$receive['fullMoney'];
|
|
$data['useDay']=$receive['useDay'];
|
|
$data['details']=$res['details'];
|
|
$data['lucky']=$receive['lucky'];
|
|
return $this->result(1, '成功', $data);
|
|
}
|
|
//瓜分红包领取列表
|
|
public function actionReceiveList() {
|
|
$now=time();
|
|
$result=axios_request();
|
|
$res =(new \yii\db\Query())
|
|
->select('a.id,b.details')
|
|
->from('{{%ybwm_dividend_join}} as a')
|
|
->leftJoin('{{%ybwm_dividend}} as b','a.divideId=b.id')
|
|
->where('a.id=:id AND startTime<=:startTime AND endTime>=:endTime AND couponStartAt<=:startTime AND couponEndAt>=:endTime',[':startTime'=>$now,':endTime'=>$now,':id'=>$result['id']])->one()?:[];
|
|
if (!$res) {
|
|
return $this->result(2, '该活动不存在或已结束');
|
|
}
|
|
$receive=(new \yii\db\Query())
|
|
->select('a.money,a.lucky,from_unixtime(a.createdAt) as createdAt,b.userName,b.portrait')
|
|
->from('{{%ybwm_dividend_receive}} as a')
|
|
->leftJoin('{{%ybwm_member}} as b','a.userId=b.id')
|
|
->where('a.joinId=:joinId',[':joinId'=>$res['id']])
|
|
->orderBy('a.id desc')
|
|
->all();
|
|
return $this->result(1, '成功', $receive);
|
|
}
|
|
//领取红包
|
|
public function actionReceive() {
|
|
$now=time();
|
|
$result=axios_request();
|
|
$request = Yii::$app->request;
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
if ($request->isGet) {
|
|
return $this->result(2, '请求异常');
|
|
}
|
|
$res =(new \yii\db\Query())
|
|
->select('a.totalNum,a.luck,b.opFullMoney,b.opReduce,b.orFullMoney,b.orReduce,b.useDay,b.useType,b.couponName')
|
|
->from('{{%ybwm_dividend_join}} as a')
|
|
->leftJoin('{{%ybwm_dividend}} as b','a.divideId=b.id')
|
|
->where('a.id=:id AND startTime<=:startTime AND endTime>=:endTime AND couponStartAt<=:startTime AND couponEndAt>=:endTime',[':startTime'=>$now,':endTime'=>$now,':id'=>$result['id']])->one()?:[];
|
|
if (!$res) {
|
|
return $this->result(2, '该活动不存在或已结束');
|
|
}
|
|
// $startToday = strtotime(date('Y-m-d', time()));
|
|
// $endToday = strtotime(date('Y-m-d', time())) + 3600 * 24;
|
|
$receive = (new \yii\db\Query())
|
|
->from('{{%ybwm_dividend_receive}}')
|
|
->where(['joinId'=>$result['id'],'userId' =>$userId])->one();
|
|
if ($receive) {
|
|
return $this->result(2, '您已经领过了');
|
|
}
|
|
//领取数量
|
|
$receiveNum=(new \yii\db\Query())
|
|
->from('{{%ybwm_dividend_receive}}')
|
|
->where(['joinId'=>$result['id']])->count()?:0;
|
|
$surplusNum=$res['totalNum']-$receiveNum;
|
|
if ($surplusNum <= 0) {
|
|
return $this->result(2, '红包已经抢完了');
|
|
}
|
|
// $num = (new \yii\db\Query())
|
|
// ->from('{{%ybwm_dividend_receive}}')
|
|
// ->where('createdAt>=:startTime AND createdAt<:endTime AND userId=:userId',
|
|
// [':startTime'=>$startToday,':endTime'=>$endToday,':userId'=>$userId])->all();
|
|
|
|
// $divideset = Config::getSystemSet('divideset', $uniacid);
|
|
// if ($divideset['num'] <= count($num) AND $divideset['num'] > 0) {
|
|
// return $this->result(2, '今天领取次数超限');
|
|
// }
|
|
$lucky=$receiveNum+1;
|
|
if($lucky==$res['luck']){
|
|
//手气王
|
|
$isLick=1;
|
|
$fullMoney=$res['opFullMoney'];
|
|
$money=$res['opReduce'];
|
|
}else{
|
|
$isLick=2;
|
|
$fullMoney=$res['orFullMoney'];
|
|
$money=$res['orReduce'];
|
|
}
|
|
|
|
$startAt = $now;
|
|
$endAt = $startAt + $res['useDay'] * 24 * 3600;
|
|
$userCoupon['couponId']=0;
|
|
$userCoupon['userId']=$userId;
|
|
$userCoupon['type']=1;
|
|
$userCoupon['money']=$money?:0;
|
|
$userCoupon['fullMoney']=$fullMoney?:0;
|
|
$userCoupon['useStartTime']=$startAt;
|
|
$userCoupon['useEndTime']=$endAt;
|
|
$userCoupon['useType']=$res['useType'];
|
|
$userCoupon['receiveType']=16;
|
|
$userCoupon['couponName']=$res['couponName'];
|
|
$userCoupon['couponDetails']='';
|
|
$userCoupon['storeType']=2;
|
|
$userCoupon['storeArr']='';
|
|
$userCoupon['goodsType']=1;
|
|
$userCoupon['goodsArr']='';
|
|
$userCoupon['memberLabel']=1;
|
|
$userCoupon['memberType']=1;
|
|
$userCoupon['uniacid']=$uniacid;
|
|
$userCoupon['storeId']=0;
|
|
$userCoupon['subsidy']=0;
|
|
$userCoupon['createdAt']=time();
|
|
$transaction = Yii::$app->db->beginTransaction(); //开始事务
|
|
$rst =Yii::$app->db->createCommand()->insert('{{%ybwm_user_coupon}}', $userCoupon)->execute();
|
|
$id = Yii::$app->db->getLastInsertID();
|
|
|
|
if ($rst) {
|
|
$numRes=Yii::$app->db->createCommand()->update('{{%ybwm_dividend_join}}', ['surplusNum'=>new \yii\db\Expression('surplusNum -1')], 'id=:id', ['id' =>$result['id']])->execute();
|
|
if(!$numRes){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败');
|
|
}
|
|
$data['joinId'] = $result['id'];
|
|
$data['userId'] = $userId;
|
|
$data['couponId'] = $id;
|
|
$data['createdAt'] = $now;
|
|
$data['uniacid'] = $uniacid;
|
|
$data['lucky'] = $isLick;
|
|
$data['fullMoney'] = $fullMoney;
|
|
$data['money'] = $money;
|
|
$data['useDay'] = $res['useDay'];
|
|
$receiveRes=Yii::$app->db->createCommand()->insert('{{%ybwm_dividend_receive}}', $data)->execute();
|
|
if(!$receiveRes){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败');
|
|
}
|
|
$transaction->commit();
|
|
return $this->result(1, '成功');
|
|
}else{
|
|
return $this->result(2, '失败');
|
|
}
|
|
|
|
}
|
|
} |