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

148 lines
5.6 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2021/6/7 0007
* Time: 15:56
*/
namespace app\controllers\channelApi;
use app\models\common\Config;
use Yii;
class WineStorageController extends CommonController{
//酒品牌
public function actionStorageBrand() {
$post=axios_request();
$uniacid=$this->wqData['uniacid'];
$res=(new \yii\db\Query())
->from('{{%ybwm_storage_brand}}')
->where('uniacid=:uniacid AND deleteAt=0 AND storeId=:storeId AND display=1',[':storeId'=>$post['storeId'],':uniacid'=>$uniacid])->orderBy('sort asc,id desc')->all();
return $this->result(1, '成功', $res);
}
//存酒
public function actionSaveStorage(){
$post=axios_request();
$userId=$this->wqData['userId'];
$uniacid=$this->wqData['uniacid'];
$storeSet = Config::getStoreSet('storageSet', $post['storeId']);
$days=$storeSet['days']?:10;
$data['storeId'] = $post['storeId'];
$appType = $this->wqData['appType'];
$data['origin'] = $appType;
$data['userId'] = $userId;
$data['userName'] = $post['userName'];
$data['userTel'] = $post['userTel'];
$data['name'] = $post['name'];
$data['totalNum'] = $post['totalNum'];
$data['outTradeNo'] = date("YmdHis") . rand(111111, 999999);
$data['note'] = $post['note'];
$data['createdAt'] =time();
$data['endAt'] =time()+$days*86400;
$data['type'] =1;
$data['state'] =1;
if($storeSet['isCheck']==2){
$data['state'] =2;
}
$data['uniacid'] =$uniacid;
$res =Yii::$app->db->createCommand()->insert('{{%ybwm_storage_order}}', $data)->execute();
$orderId = Yii::$app->db->getLastInsertID();
if ($res) {
return $this->result(1, '成功', $orderId);
} else {
return $this->result(2, '失败');
}
}
//存/取酒记录
public function actionStorageList() {
$post=axios_request();
$type=$post['type'];
$userId=$this->wqData['userId'];
$result=axios_request();
$page=$result['page']?:1;
$state=$result['state'];
$size=$result['size']?:10;
$table=(new \yii\db\Query())
->select(['a.*','s.name as storeName','s.icon as logo'])
->from('{{%ybwm_storage_order}} as a')
->join('LEFT JOIN', '{{%ybwm_store}} as s', 's.id = a.storeId')
->where('a.userId=:userId AND a.state!=3 AND a.userDeleteAt=0 AND a.deleteAt=0 AND a.type=:type',[':type'=>$type,':userId'=>$userId]);
if($state){
if($state==3){
$table->andWhere('a.endAt<'.time());
}else{
$table->andWhere('a.state='.$state);
}
}
$table->offset(($page - 1) * $size)->limit($size);
$res=$table->orderby('a.id desc')->all();
return $this->result(1,'成功',$res);
}
//取酒
public function actionSaveTakeRecord() {
$post=axios_request();
$uniacid=$this->wqData['uniacid'];
$storageInfo=(new \yii\db\Query())
->from('{{%ybwm_storage_order}}')
->where('id=:id',[':id'=>$post['id']])->one();
$quInfo=(new \yii\db\Query())
->from('{{%ybwm_storage_order}}')
->where('storageId=:storageId AND state=1',[':storageId'=>$post['id']])->one();
if($quInfo){
return $this->result(2, '有取酒信息未审核,无法再次取酒');
}
if(!$storageInfo){
return $this->result(2, '存酒信息不存在!!!');
}
if(time()>$storageInfo['endAt']){
return $this->result(2, '存酒信息已过期!!!');
}
if($storageInfo['state']!=2){
return $this->result(2, '存酒信息待确认!!!');
}
$surplusNum=bcsub($storageInfo['totalNum'],$storageInfo['takeNum'],0);
$takeNum=intval($post['takeNum']);
if($takeNum<=0){
return $this->result(2, '取酒数量输入有误!!!');
}
if($takeNum>$surplusNum){
return $this->result(2, '库存不足请确认后重试!!!');
}
$appType = $this->wqData['appType'];
$data['origin'] = $appType;
$data['storeId'] = $storageInfo['storeId']; //自提id
$data['userId'] = $storageInfo['userId'];
$data['storageId'] = $storageInfo['id'];
$data['userName'] = $storageInfo['userName'];
$data['userTel'] = $storageInfo['userTel'];
$data['name'] = $storageInfo['name'];
$data['takeNum'] = $takeNum;
$data['surplusNum'] =$surplusNum-$takeNum;
$data['outTradeNo'] = $storageInfo['outTradeNo'];
$data['createdAt'] =time();
$data['endAt'] =$storageInfo['endAt'];
$data['state'] =1;
$data['type'] =2;
$data['uniacid'] =$uniacid;
$res =Yii::$app->db->createCommand()->insert('{{%ybwm_storage_order}}', $data)->execute();
$orderId = Yii::$app->db->getLastInsertID();
if ($res) {
Yii::$app->db->createCommand()->update('{{%ybwm_storage_order}}', ['takeNum'=>new \yii\db\Expression('takeNum +'.$takeNum)], 'id=:id', ['id' =>$storageInfo['id']])->execute();
if($surplusNum==$takeNum){
Yii::$app->db->createCommand()->update('{{%ybwm_storage_order}}', ['state'=>4], 'id=:id', ['id' =>$storageInfo['id']])->execute();
}
return $this->result(1, '成功', $orderId);
} else {
return $this->result(2, '失败');
}
}
}