201 lines
8.4 KiB
PHP
201 lines
8.4 KiB
PHP
<?php
|
|
namespace app\controllers\channelApi;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yii;
|
|
use app\models\common\Config;
|
|
use app\models\common\Member;
|
|
use app\models\common\CallbackOrder;
|
|
class IntegralShopController extends CommonController
|
|
{
|
|
//商品列表
|
|
public function actionGoodsList(){
|
|
$result=axios_request();
|
|
$typeId=$result['typeId'];
|
|
$sort=$result['sort'];
|
|
$page=$result['page']?:1;
|
|
$size=$result['size']?:10;
|
|
$list=(new \yii\db\Query())
|
|
->select('id,name,icon,money,score,price')
|
|
->from('{{%ybwm_integral_goods}}')
|
|
->where(['display'=>1,'deleteAt'=>0,'uniacid'=>$this->wqData['uniacid']]);
|
|
if($typeId){
|
|
$list->andWhere('typeId='.$typeId);
|
|
}
|
|
if($sort=='1'){
|
|
$list->andWhere('isHot=1');
|
|
}
|
|
$res=$list->offset(($page - 1) * $size)->limit($size)->orderBy('sort asc,id desc')->all();
|
|
return $this->result(1, '成功',$res);
|
|
}
|
|
//分类列表
|
|
public function actionGoodsTypeList(){
|
|
$type=(new \yii\db\Query())
|
|
->from('{{%ybwm_core_category}}')
|
|
->where('uniacid=:uniacid AND item=8 AND deleteAt=0',[':uniacid'=>$this->wqData['uniacid']])->all();
|
|
$ad=(new \yii\db\Query())
|
|
->from('{{%ybwm_core_ad}}')
|
|
->where('uniacid=:uniacid AND location=1 AND deleteAt=0',[':uniacid'=>$this->wqData['uniacid']])->all();
|
|
$data['typeList']=$type;
|
|
$data['ad']=$ad;
|
|
$payList=(new \yii\db\Query())
|
|
->select('b.userName,a.goodsName')
|
|
->from('{{%ybwm_integral_order}} as a')
|
|
->join('LEFT JOIN', '{{%ybwm_member}} as b', 'b.id = a.userId')
|
|
->limit(10)
|
|
->where('a.uniacid=:uniacid AND a.state>1',[':uniacid'=>$this->wqData['uniacid']])->all();
|
|
$data['payList']=$payList;
|
|
return $this->result(1, '成功',$data);
|
|
}
|
|
//详情
|
|
public function actionGoodsInfo(){
|
|
$result=axios_request();
|
|
$list=(new \yii\db\Query())
|
|
->from('{{%ybwm_integral_goods}}')
|
|
->where(['id'=>$result['id']])->one();
|
|
return $this->result(1, '成功',$list);
|
|
}
|
|
//自提列表
|
|
public function actionSelfList(){
|
|
$list=(new \yii\db\Query())
|
|
->from('{{%ybwm_mention_point}}')
|
|
->where(['deleteAt'=>0,'display'=>1,'uniacid'=>$this->wqData['uniacid']])->orderBy('id desc')
|
|
->all();
|
|
return $this->result(1, '成功',$list);
|
|
}
|
|
|
|
//兑换
|
|
public function actionSaveIntegralOrder(){
|
|
$result=axios_request();
|
|
$userId = $this->wqData['userId'];
|
|
$uniacid = $this->wqData['uniacid'];
|
|
$goods = (new \yii\db\Query())
|
|
->from('{{%ybwm_integral_goods}}')
|
|
->where(['id'=>$result['goodsId']])->one();
|
|
if (intval($goods['stock']) <= 0) {
|
|
return $this->result(2, '商品库存不足!');
|
|
}
|
|
$user =(new \yii\db\Query())
|
|
->from('{{%ybwm_member}}')
|
|
->where(['id'=>$userId])->one();
|
|
if ($user['integral'] < bcmul($goods['score'], $result['convertNum'], 0)) {
|
|
return $this->result(2, '用户积分不足!');
|
|
}
|
|
$order =(new \yii\db\Query())
|
|
->from('{{%ybwm_integral_order}}')
|
|
->where('userId=:userId AND state!=1',['userId'=>$userId])->count();
|
|
if ($order['count'] >= $goods['num'] AND $goods['isLimit'] == 1) {
|
|
return $this->result(2, '超过限制兑换次数!');
|
|
}
|
|
$userAddress = (new \yii\db\Query())
|
|
->from('{{%ybwm_user_address}}')
|
|
->where('id=:id',['id'=>$result['userAddId']])->one();
|
|
$data['selfId'] = $result['selfId']?:0; //自提id
|
|
$data['userId'] = $userId;
|
|
$data['goodsId'] = $result['goodsId'];
|
|
$data['goodsName'] = $goods['name'];
|
|
$data['goodsIcon'] = $goods['icon'];
|
|
$data['goodsType'] = $goods['type'];
|
|
$data['deliveryMoney'] = 0;
|
|
if($userAddress){
|
|
$data['deliveryMoney'] = $goods['deliveryMoney'];
|
|
$data['receiveName'] = $userAddress['userName']; //收货人
|
|
$data['receiveTel'] = $userAddress['userTel']; //收货电话
|
|
$data['receiveAddress'] = $userAddress['address'] . $userAddress['details']; //详细地址
|
|
}
|
|
|
|
$data['convertNum'] = $result['convertNum'];
|
|
$data['score'] = bcmul($goods['score'], $result['convertNum'], 0);
|
|
$data['money'] = bcadd(bcmul($goods['money'], $result['convertNum'], 2),$data['deliveryMoney'],2);
|
|
$data['state'] = 1;
|
|
$data['note'] = $result['note']; //备注
|
|
$data['deliveryMode'] = $result['deliveryMode']; //1快递2到店
|
|
|
|
$data['createdAt'] = time();
|
|
$data['outTradeNo'] = date("YmdHis") . rand(111111, 999999);
|
|
$data['uniacid'] = $uniacid;
|
|
$data['price'] = bcadd($goods['money'],$data['deliveryMoney'],2); //商品兑换所需金额
|
|
if ($goods['type'] == 2) {
|
|
$data['goodsMoney'] =bcmul($goods['price'], $result['convertNum'], 0); //虚拟红包
|
|
}
|
|
if ($result['selfId']) {
|
|
$data['selfCode'] =rand(11111111, 99999999);
|
|
$self = (new \yii\db\Query())
|
|
->from('{{%ybwm_mention_point}}')
|
|
->where('id=:id',['id'=>$result['selfId']])->one();
|
|
$data['receiveName'] = $self['linkMan']; //收货人
|
|
$data['receiveTel'] = $self['tel']; //收货电话
|
|
$data['receiveAddress'] = $self['address']; //详细地址
|
|
}
|
|
$res = Yii::$app->db->createCommand()->insert('{{%ybwm_integral_order}}', $data)->execute();
|
|
$orderId = Yii::$app->db->getLastInsertID();
|
|
$goodsData= (new \yii\db\Query())
|
|
->from('{{%ybwm_integral_goods}}')
|
|
->where('id=:id',['id'=>$result['goodsId']])->one();
|
|
$num=bcsub($goodsData['stock'],$result['convertNum']);
|
|
$convertNum=bcadd($goodsData['convertNum'],1);
|
|
Yii::$app->db->createCommand()->update('{{%ybwm_integral_goods}}', ['stock'=>$num],'id=:id', ['id' =>$result['goodsId']])->execute();
|
|
Yii::$app->db->createCommand()->update('{{%ybwm_integral_goods}}', ['convertNum'=>$convertNum],'id=:id', ['id' =>$goodsData['id']])->execute();
|
|
if ($res) {
|
|
if ($data['price'] <= 0) {
|
|
CallbackOrder::integralOrder($orderId,0);
|
|
}
|
|
return $this->result(1, '兑换成功!', $orderId);
|
|
} else {
|
|
return $this->result(2, '兑换失败!');
|
|
}
|
|
|
|
}
|
|
|
|
//我的订单
|
|
public function actionMyIntegralOrder(){
|
|
$result=axios_request();
|
|
$userId = $this->wqData['userId'];
|
|
$type=$result['type'];
|
|
$page=$result['page']?:1;
|
|
$size=$result['size']?:10;
|
|
$list=(new \yii\db\Query())
|
|
->select('id,outTradeNo,goodsIcon,goodsName,goodsType,score,money,goodsMoney,convertNum,deliveryMode,state,selfCode')
|
|
->from('{{%ybwm_integral_order}}')
|
|
->where(['deleteAt'=>0,'userDeleteAt'=>0,'userId'=>$userId]);
|
|
if($type==1){
|
|
$list->andWhere(['in','state',[2,3]]);
|
|
}
|
|
if($type==2){
|
|
$list->andWhere('state=4');
|
|
}
|
|
$res=$list->offset(($page - 1) * $size)->limit($size)->orderBy('id desc')->all();
|
|
return $this->result(1, '成功!', $res);
|
|
|
|
}
|
|
//查看订单详情
|
|
public function actionIntegralOrderInfo(){
|
|
$result=axios_request();
|
|
$list=(new \yii\db\Query())
|
|
->from('{{%ybwm_integral_order}}')
|
|
->where(['id'=>$result['id']])->one();
|
|
return $this->result(1, '成功!', $list);
|
|
}
|
|
//确认收货
|
|
public function actionModifyOrder(){
|
|
$result=axios_request();
|
|
$order=(new \yii\db\Query())
|
|
->from('{{%ybwm_integral_order}}')
|
|
->where(['id'=>$result['id']])->one();
|
|
if($order['state']!=3 AND $order['deliveryMode']==1){
|
|
return $this->result(2, '订单状态异常');
|
|
}
|
|
if($order['state']!=2 AND $order['deliveryMode']==2){
|
|
return $this->result(2, '订单状态异常');
|
|
}
|
|
$data['state']=4;
|
|
$data['completeAt']=time();
|
|
$res = Yii::$app->db->createCommand()->update('{{%ybwm_integral_order}}', $data,'id=:id', ['id' =>$result['id']])->execute();
|
|
if($res){
|
|
return $this->result(1, '成功');
|
|
}else{
|
|
return $this->result(2, '失败');
|
|
}
|
|
}
|
|
|
|
}
|