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

1018 lines
48 KiB
PHP

<?php
namespace app\controllers\merchant;
use app\models\common\Store;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\web\Controller;
use app\models\common\StoreBill;
use app\models\common\Good;
class FoodController extends CommonController{
public $enableCsrfValidation = false;
public function actionGetFoodList(){
$uniacid=$this->wqData['uniacid'];
$storeId=$this->wqData['storeId'];
$result=axios_request();
if(!$uniacid||!$storeId){
return $this->result(2,'门店不存在');
}
$type=$result['goodsType']?:1;
$page=$result['page']?$result['page']:1;
$num=($page-1)*10;
$limit=10;
//判断门店的模式
$storeRes=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,'id'=>$storeId])
->one();
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
//门店商品模式是模式一或者模式二
$query= (new \yii\db\Query())
->select('b.id,b.name,b.icon,a.salesNum,typePid,typeId,a.price,a.stock,a.isRecommend,a.display,a.maxPrice,b.isSpecs,goodsPid')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'b.goodsType'=>$type,'a.storeId'=>$storeId]);
}else{
$query= (new \yii\db\Query())
->select('discountOpen,id,name,icon,salesNum,typePid,typeId,price,stock,isRecommend,display,maxPrice,isSpecs,storeId')
->from('{{%ybwm_core_goods}} a')
->where(['uniacid'=>$uniacid,'goodsType'=>$type,'storeId'=>$storeId]);
}
if($result['type']==1){
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$query->andWhere(['b.deleteAt'=>0]);
}else{
$query->andWhere(['a.deleteAt'=>0]);
}
$query->andWhere(['a.display'=>1])->andWhere(['>','a.stock',0]);
}
if($result['type']==2){
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$query->andWhere(['b.deleteAt'=>0]);
}else{
$query->andWhere(['a.deleteAt'=>0]);
}
$query->andWhere(['a.display'=>1])->andWhere(['<=','a.stock',YII::$app->params['stock']])->andWhere(['>','a.stock',0]);
}
if($result['type']==3){
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$query->andWhere(['b.deleteAt'=>0]);
}else{
$query->andWhere(['a.deleteAt'=>0]);
}
$query->andWhere('a.stock<=0 or a.stock is null');
}
if($result['type']==4){
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$query->andWhere(['b.deleteAt'=>0]);
}else{
$query->andWhere(['a.deleteAt'=>0]);
}
$query->andWhere(['a.display'=>2]);
}
if($result['type']==5){
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$query->andWhere(['b.deleteAt'=>0]);
}else{
$query->andWhere(['a.deleteAt'=>0]);
}
$query->andWhere(['a.display'=>3]);
}
if($result['type']==6){
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$query->andWhere(['>','b.deleteAt',0]);
}else{
$query->andWhere(['>','a.deleteAt',0]);
}
}
if($result['categoryId']){
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$query= $query->andWhere(['b.typePid'=>$result['categoryId']]);
}else{
$query=$query->andWhere(['a.typePid'=>$result['categoryId']]);
}
}
//var_dump(ddSql($query));die;
$count=$query->count();
$re=$query->offset($num)
->limit($limit)
->orderBy('sort asc,id desc')
->all();
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$allTotal=(new \yii\db\Query())
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'b.goodsType'=>$type,'a.storeId'=>$storeId,'a.display'=>1,'b.deleteAt'=>0])
->andWhere(['>','a.stock',0])
->count();
//var_dump(ddSql($allTotal));die;
$noStock=(new \yii\db\Query())
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'b.goodsType'=>$type,'a.storeId'=>$storeId,'a.display'=>1,'b.deleteAt'=>0])
->andWhere(['<=','a.stock',YII::$app->params['stock']])->andWhere(['>','a.stock',0])
->count();
$errDisplay=(new \yii\db\Query())
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'b.goodsType'=>$type,'a.storeId'=>$storeId,'a.display'=>2,'b.deleteAt'=>0])
->count();
$noDisplay=(new \yii\db\Query())
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'b.goodsType'=>$type,'a.storeId'=>$storeId,'a.display'=>3,'b.deleteAt'=>0])
->count();
$sellOutTotal=(new \yii\db\Query())
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'b.goodsType'=>$type,'a.storeId'=>$storeId,'b.deleteAt'=>0])
->andWhere('a.stock<=0 or a.stock is null')
->count();
$recycleTotal=(new \yii\db\Query())
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'b.goodsType'=>$type,'a.storeId'=>$storeId])
->andWhere(['>','b.deleteAt',0])
->count();
}else{
$allTotal=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['uniacid'=>$uniacid,'goodsType'=>$type,'display'=>1,'deleteAt'=>0,'storeId'=>$storeId])
->andWhere(['>','stock',0])
->count();
$noStock=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['uniacid'=>$uniacid,'goodsType'=>$type,'deleteAt'=>0,'storeId'=>$storeId,'display'=>1])
->andWhere(['<=','stock',YII::$app->params['stock']])
->andWhere(['>','stock',0])
->count();
$errDisplay=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['uniacid'=>$uniacid,'goodsType'=>$type,'deleteAt'=>0,'storeId'=>$storeId,'display'=>2])
->count();
$noDisplay=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['uniacid'=>$uniacid,'goodsType'=>$type,'display'=>3,'deleteAt'=>0,'storeId'=>$storeId])
->count();
$sellOutTotal=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['uniacid'=>$uniacid,'goodsType'=>$type,'deleteAt'=>0,'storeId'=>$storeId])
->andWhere('stock<=0 or stock is null')
->count();
$recycleTotal=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['uniacid'=>$uniacid,'goodsType'=>$type,'storeId'=>$storeId])
->andWhere(['>','deleteAt',0])
->count();
}
$new_data=array(
'allTotal'=>$allTotal,
'noStock'=>$noStock,
'errDisplay'=>$errDisplay,
'noDisplay'=>$noDisplay,
'data'=>$re?:[],
'goodsModel'=>$storeRes['goodsModel'],
'sellOutTotal'=>$sellOutTotal,
'recycleTotal'=>$recycleTotal,
'isMain'=>$storeRes['isMain'],
);
return $this->result(1,'成功',$new_data,$count);
}
public function actionGetDetail(){
$uniacid=$this->wqData['uniacid'];
$storeId=$this->wqData['storeId'];
$id=axios_request()['id'];
$foodInfo=(new \yii\db\Query())
->select('a.id,a.name,a.icon,a.maxNum,a.minNum,a.isSpecs,a.isAttr,a.isMaterial,b.name labelName,a.aloneType,a.hotsaleType')
->from('{{%ybwm_core_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodLabel=b.id')
->where('a.uniacid=:uniacid AND a.storeId=:storeId AND a.id=:id',[':uniacid'=>$uniacid,'storeId'=>$storeId,'id'=>$id])
->one();
if($foodInfo['isSpecs']==1){
$specesData=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where('uniacid=:uniacid AND storeId=:storeId AND goodId=:goodId',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodId'=>$id])
->all();
}
if($foodInfo['isAttr']==1){
$attrData=(new \yii\db\Query())
->from('{{%ybwm_good_attribute}}')
->where('uniacid=:uniacid AND storeId=:storeId AND goodId=:goodId',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodId'=>$id])
->all();
}
if($foodInfo['isMaterial']==1){
$meterData=(new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where('uniacid=:uniacid AND storeId=:storeId AND goodId=:goodId',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodId'=>$id])
->all();
}
$data=array(
'foodInfo'=>$foodInfo,
'attrData'=>$attrData,
'specesData'=>$specesData,
'meterData'=>$meterData,
);
return $this->result(1,'成功',$data);
}
//商品上下架
public function actionFoodDown(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$this->wqData['storeId'];
if(!$result['id']){
return $this->result(2,'参数有误');
}
//1上架2下架3估清4置满5推荐6取消推荐7删除
switch ($result['type']){
case 1;
$data=['display'=>1];
break;
case 2;
$data=['display'=>2];
break;
case 3;
$data=['stock'=>0];
break;
case 4;
$data=['stock'=>999];
break;
case 5;
$data=['isRecommend'=>1];
break;
case 6;
$data=['isRecommend'=>2];
break;
case 7;
$data=['deleteAt'=>time()];
break;
case 8;
$data=['deleteAt'=>0];
break;
}
if($result['price']){
$data=['price'=>$result['price']];
}
if($result['stock']){
$data=['stock'=>$result['stock']];
}
$res=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,'id'=>$storeId])
->one();
if($res['isMain']==1||$res['goodsModel']==3){
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', $data, ['id'=>$result['id']])->execute();
if($result['price']){
$shopData=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND isMain=2 AND goodsModel=1',[':uniacid'=>$uniacid])
->all();
foreach ($shopData as $v){
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', $data, ['goodsId'=>$result['id'],'storeId'=>$v['id']])->execute();
}
}
}else{
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', $data, ['storeId'=>$storeId,'goodsId'=>$result['id']])->execute();
// if($res['goodsModel']==3){
// YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', $data, ['id'=>$result['id']])->execute();
// }
}
return $this->result(1,'成功');
}
public function actionGetSpes(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$this->wqData['storeId'];
$goodData=(new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where('uniacid=:uniacid AND storeId=:storeId AND goodId=:goodId',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodId'=>$result['id']])
->one();
if($goodData['isSpecs']==2){
return $this->result(2,'该商品是单规格商品');
}else{
$specsData=$goodData['data']['specs'];
}
return $this->result(1,'成功',$specsData);
}
//商品保存
public function actionGoodsSave(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$this->wqData['storeId'];
if($result['isMember']==2){
if(!$result['id']){
echo json_encode(['code'=>2,'msg'=>'请先添加商品基础信息']);die;
}
$goodsNewData=array(
// 'isMember'=>$result['isMember'],//是否参与会员折扣
'maxNum'=>trim($result['maxNum']),//每单限购
'virtualSales'=>trim($result['virtualSales']),//虚拟销量
'printLabel'=>$result['printLabel'],//打印标签
'goodLabel'=>$result['goodLabel'],//商品标签
);
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', $goodsNewData, 'id=:id', ['id' => $result['id']])->execute();
echo json_encode(['code'=>1,'msg'=>'成功','goodsId'=>$result['id']]);die;
}
if(!$result['typeId'][0]){
echo json_encode(['code'=>2,'msg'=>'请添加商品分类']);die;
}
if(!$result['unit']){
echo json_encode(['code'=>2,'msg'=>'请添加商品单位']);die;
}
$isSpecs=2;$attribute=2;$materia=2;
if(!empty($result['spec'])){
$isSpecs=1;
if(array_unique(array_column($result['spec'],'specsName'))){
echo json_encode(['code'=>2,'msg'=>'请删除重复的规格名称']);die;
}
foreach ($result['spec'] as $k=>$v){
$newSpcs=array(
'name'=>$v['specName'],
'display'=>1,
'createdAt'=>time(),
'uniacid'=>$uniacid,
'storeId'=>$storeId,
);
$specsOpers=(new \yii\db\Query())
->from('{{%ybwm_spec}}')
->where(['name' =>$v['specName'],'uniacid'=>$uniacid,'storeId'=>$storeId])
->one();
if(!$specsOpers){
Yii::$app->db->createCommand()->insert('{{%ybwm_spec}}', $newSpcs)->execute();
}
if(!$result['spec'][$k]['specName']){
echo json_encode(['code'=>2,'msg'=>'请填写规格的名称']);die;
}
if(!$result['spec'][$k]['price']){
echo json_encode(['code'=>2,'msg'=>'请填写规格的价格']);die;
}
if(!$result['spec'][$k]['stock']){
echo json_encode(['code'=>2,'msg'=>'请填写规格的库存']);die;
}
}
}
if(!empty($result['attribute'])){
$attribute=1;
if(array_unique(array_column($result['attribute'],'name'))){
echo json_encode(['code'=>2,'msg'=>'请删除重复的属性名称']);die;
}
foreach ($result['attribute'] as $k=>$v){
$attrStr=array_column($v['attrStr'],'name');
$newAttrStr=[];
foreach ($attrStr as $vs){
if($vs){
$newAttrStr[]=['sxz'=>$vs];
}
}
$newSpcs=array(
'name'=>$v['attrName'],
'createdAt'=>time(),
'uniacid'=>$uniacid,
'storeId'=>$storeId,
'attr_str'=>json_encode($newAttrStr,JSON_UNESCAPED_UNICODE)
);
$specsOpers=(new \yii\db\Query())
->from('{{%ybwm_attribute}}')
->where(['name' =>$v['attrName'],'uniacid'=>$uniacid,'storeId'=>$storeId])
->one();
if(!$specsOpers){
Yii::$app->db->createCommand()->insert('{{%ybwm_attribute}}', $newSpcs)->execute();
}
}
}
if(!empty($result['feeding'])){
$materia=1;
if(array_unique(array_column($result['feeding'],'name'))){
echo json_encode(['code'=>2,'msg'=>'请删除重复的加料名称']);die;
}
foreach ($result['feeding'] as $k=>$v){
$newSpcs=array(
'name'=>$v['materialName'],
'createdAt'=>time(),
'uniacid'=>$uniacid,
'storeId'=>$storeId,
'price'=>$v['salesPrice'],
);
$specsOpers=(new \yii\db\Query())
->from('{{%ybwm_material}}')
->where(['name' =>$v['materialName'],'uniacid'=>$uniacid,'storeId'=>$storeId])
->one();
if(!$specsOpers){
Yii::$app->db->createCommand()->insert('{{%ybwm_material}}', $newSpcs)->execute();
}
}
}
//单位
$unitData = (new \yii\db\Query())
->from('{{%ybwm_core_unit}}')
->where(['uniacid' => $uniacid, 'storeId'=>$storeId,'name'=>$result['unit']])
->one();
if(!$unitData){
$unitArr=array(
'name'=>$result['unit'],
'uniacid'=>$uniacid,
'createdAt'=>time(),
'storeId'=>$storeId,
);
Yii::$app->db->createCommand()->insert('{{%ybwm_core_unit}}',$unitArr)->execute();
}
$array=array(
'sort'=>trim($result['sort']),//排序
'goodsType'=>$result['goodsType']?:1,//排序
'typePid'=>$result['typeId'][0],//商品一级分类
'typeId'=>$result['typeId'][1],//商品一级分类
'name'=>trim($result['name']),//商品名称
'body'=>trim($result['body']),//商品简介
'minNum'=>trim($result['minNum']),//起购数量
'unit'=>$result['unit'],//商品单位
'price'=>trim($result['price']),
'stock'=>intval(trim($result['stock']))?:'0',
'boxMoney'=>trim($result['boxMoney']),
'fillType'=>$result['fillType'],
'crossedPrice'=>trim($result['crossedPrice']),//划线价格
'costPrice'=>trim($result['costPrice']),//成本价格
'goodCode'=>trim($result['goodCode'])?:'',//商品编号
'salesType'=>$result['salesType'],//售卖时间类型1全时段售卖2自定义售卖时间
'weekSalesType'=>$result['weekSalesType'],
'weekDay'=>json_encode($result['weekDay'],JSON_UNESCAPED_UNICODE),//星期一到星期日的集合
'hotsaleType'=>$result['hotsaleType'],//是否参与热销排行
'aloneType'=>$result['aloneType'],//单点时不配送
'details'=>$result['details'],//详情
'specialType'=>$result['specialType'],//详情
'uniacid'=>$uniacid,
'storeId'=>$storeId,
'startTime'=>strtotime($result['validity'][0])?strtotime($result['validity'][0]):0,
'endTime'=>strtotime($result['validity'][1])?strtotime($result['validity'][1]):0,
'icon'=>$result['icon'],
'media'=>json_encode($result['media'])?:'',
'isSpecs'=>$isSpecs,
'isAttr'=>$attribute,
'isMaterial'=>$materia,
);
$transaction = Yii::$app->db->beginTransaction(); //开始事务
$storeRes=Store::getStoreInfo($uniacid,$storeId);
try{
if ($result['id']) {
$id = $result['id'];
$re = YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', $array, 'id=:id', ['id' => $result['id']])->execute();
$upData=array(
'price'=>trim($result['price']),
'stock'=>trim($result['stock']),
'isSpecs'=>$isSpecs,
);
//修改多规格
$specsToData=[];
if (!empty($result['spec'])){
//$specsIds=array_column($result['spec'],'id');
Yii::$app->db->createCommand()->delete('{{%ybwm_good_specs}}',['goodId' => $id])->execute();
$priceRes=[];$stockRes=[];
foreach ($result['spec'] as $v){
$specsInsert=[];
$priceRes[]=$v['price'];
$stockRes[]=$v['stock'];
$specsInsert=array(
'specsName'=>$v['specName'],
'boxMoney'=>$v['boxMoney'],
'crossedPrice'=>$v['crossedPrice'],
'costPrice'=>$v['costPrice'],
'fillType'=>$v['fillType'],
'goodCode'=>$v['goodCode'],
'goodId'=>$result['id'],
'SalesPrice'=>$v['price'],
'SalesStock'=>$v['stock'],
'storeId'=>$storeId,
'uniacid'=>$uniacid,
);
$specsToData[]=$specsInsert;
Yii::$app->db->createCommand()->insert('{{%ybwm_good_specs}}',$specsInsert)->execute();
}
$upData['stock']=max($stockRes);
$upData['price']=min($priceRes);
$upData['maxPrice']=max($priceRes);
Yii::$app->db->createCommand()->update('{{%ybwm_core_goods}}', ['stock'=>max($stockRes),'price' => min($priceRes), 'maxPrice' => max($priceRes), 'isSpecs' => 1],['id'=>$id])->execute();
}
else{
$upData['price']=trim($result['price']);
$upData['stock']=trim($result['stock']);
Yii::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['isSpecs'=>2],['id'=>$id])->execute();
Yii::$app->db->createCommand()->delete('{{%ybwm_good_specs}}',['goodId'=>$id])->execute();
//修改子门店规格
if($storeRes['isMain']==1){
//同步插入到子门店商品列表中
$shopDatas = (new \yii\db\Query())
->select('id')
->from('{{%ybwm_store}}')
->where(['uniacid' => $uniacid, 'isMain' =>2])
->andWhere(['in','goodsModel',[1,2]])
->all();
$shopIds=array_column($shopDatas,'id');
$condition = ['and',
['goodsId'=>$id],
['in', 'storeId', $shopIds],
];
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',['isSpecs'=>2],$condition)->execute();
}
}
//商品属性
if (count($result['attribute'])) {
$attr_re = (new \yii\db\Query())
->from('{{%ybwm_good_attribute}}')
->where(['goodId' =>$id,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
if ($attr_re) {
Yii::$app->db->createCommand()->delete('{{%ybwm_good_attribute}}', ['goodId' => $id])->execute();
}
foreach ($result['attribute'] as $v) {
$v['attrStr']=array_values($v['attrStr']);
$attr_arr= array(
'goodId' => $id,
'attrName' => $v['attrName'],
'attrStr' => json_encode($v['attrStr'],JSON_UNESCAPED_UNICODE),
'createdAt' => time(),
'uniacid'=>$uniacid,
'storeId'=>$storeId,
);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_attribute}}', $attr_arr)->execute();
}
}else {
YII::$app->db->createCommand()->delete('{{%ybwm_good_attribute}}', ['goodId' => $id, 'uniacid' => $uniacid, 'storeId' => $storeId])->execute();
}
//商品加料
if (count($result['feeding'])){
$meteria_re = (new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where(['goodId' => $id, 'uniacid' => $uniacid, 'storeId' => $storeId])
->all();
//var_dump($meteria_re);die;
if ($meteria_re) {
Yii::$app->db->createCommand()->delete('{{%ybwm_good_materia}}', ['goodId' => $id])->execute();
}
foreach ($result['feeding'] as $v) {
$materia_arr = [];
$materia_arr= array(
'goodId' => $id,
'materialName' => $v['materialName'],
'SalesPrice' => $v['salesPrice'],
'SalesStock' => $v['salesStock'],
'fillType' => $v['fillType'],
'createdAt' => time(),
'uniacid'=>$uniacid,
'storeId'=>$storeId,
);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_materia}}', $materia_arr)->execute();
}
}else {
YII::$app->db->createCommand()->delete('{{%ybwm_good_materia}}', ['goodId' => $id, 'uniacid' => $uniacid, 'storeId' => $storeId])->execute();
}
//售卖时间自定义时间 售卖时间段自定义时间
if($result['weekSalesType']==2&&$result['salesType']==2){
$time_arr=(new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_hours}}')
->where(['goodId' => $id, 'uniacid' =>$uniacid, 'storeId' => $storeId])
->all();
if($time_arr){
Yii::$app->db->createCommand()->delete('{{%ybwm_good_hours}}', ['goodId' => $id])->execute();
}
foreach ($result['salesTimeStr'] as $v){
$time_insert_arr=[];
$time_insert_arr=array(
'uniacid'=>$uniacid,
'storeId'=>$storeId,
'startTime'=>$v['startTime'],
'endTime'=>$v['endTime'],
'goodId'=>$id,
);
if($v['startTime']&&$v['endTime']){
Yii::$app->db->createCommand()->insert('{{%ybwm_good_hours}}', $time_insert_arr)->execute();
}
}
}
//如果是子门店模式三修改商品信息,还要修改模式三store_goods表价格 库存 多规格 多规格data
// if($result['storeId']&&$storeRes['goodsModel']==3){
// YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', $upData,['goodsId' => $result['id']])->execute();
// }
////如果是主门店修改商品信息 商品一模式同步价格和库存 还有是否是多规格
if($storeRes['isMain']==1){
$upData['isSpecs']=$isSpecs;
$upData['data']=json_encode(array(
'specs'=>$specsToData,
'attr'=>$result['attribute'],
'materia'=>$result['feeding'],
));
$shopDatas= (new \yii\db\Query())
->select('id')
->from('{{%ybwm_store}}')
->where(['uniacid' => $uniacid])
->andWhere(['in','goodsModel',[1,2]])
->all();
//查询模式一这个商品之前的状态是不是单规格,如果是单规格库存不改动
if(empty($result['spec'])){
$sonFromSpecs=(new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where(['storeId' => $shopDatas[0]['id'], 'goodsId' =>$result['id']])
->one();
if($sonFromSpecs['isSpecs']==2){
unset($upData['stock']);
}
}
foreach ($shopDatas as &$v){
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',$upData, ['goodsId' => $id,'storeId'=>$v['id']])->execute();
}
}
}
else{
$array['createdAt'] = time();
$re = Yii::$app->db->createCommand()->insert('{{%ybwm_core_goods}}', $array)->execute();
$id = Yii::$app->db->getLastInsertID();
//自定义规格
$sonSpecsData='';
if (!empty($result['spec'])){
$sonSpecsData=[];
foreach ($result['spec'] as $v) {
$priceRes[]=$v['price'];
$stockRes[]=$v['stock'];
$priceData= array(
'goodId' => $id,
'specsName' => $v['specName'],
'SalesPrice' => $v['price'],
'SalesStock' =>$v['stock'],
'createdAt'=>time(),
'fillType'=>$v['fillType']?:2,
'crossedPrice'=>$v['crossedPrice'],
'goodCode'=>$v['goodCode'],
'costPrice'=>$v['costPrice'],
'boxMoney'=>$v['boxMoney'],
'uniacid'=>$uniacid,
'storeId'=>$storeId,
);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_specs}}', $priceData)->execute();
$specsId= Yii::$app->db->getLastInsertID();
$sonSpecsData[]=array(
'id'=>$specsId,
'goodsId' => $id,
'specsName' => $v['specName'],
'fillType'=>$v['fillType']?:2,
'crossedPrice'=>$v['crossedPrice'],
'costPrice'=>$v['costPrice'],
'boxMoney'=>$v['boxMoney'],
'SalesPrice'=>$v['price'],
'SalesStock'=>$v['stock'],
);
}
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', ['stock'=>max($stockRes),'price' => min($priceRes), 'maxPrice' => max($priceRes), 'isSpecs' => 1], ['id' => $id, 'uniacid' => $uniacid, 'storeId' => $storeId])->execute();
}
//商品属性
if (count($result['attribute'])) {
$attribute=1;
$attr_re = (new \yii\db\Query())
->from('{{%ybwm_good_attribute}}')
->where(['goodId' =>$id,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
if ($attr_re) {
Yii::$app->db->createCommand()->delete('{{%ybwm_good_attribute}}', ['goodId' => $id, 'uniacid' => $this->wqData['uniacid'], 'storeId' => $storeId])->execute();
}
foreach ($result['attribute'] as $v) {
$v['attrStr']=array_values($v['attrStr']);
$attr_arr= array(
'goodId' => $id,
'attrName' => $v['attrName'],
'attrStr' => json_encode($v['attrStr'],JSON_UNESCAPED_UNICODE),
'createdAt' => time(),
'uniacid'=>$uniacid,
'storeId'=>$storeId,
);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_attribute}}', $attr_arr)->execute();
}
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['isAttr'=>1],['id' =>$id, 'uniacid'=>$uniacid,'storeId'=>$storeId])->execute();
}else {
$attribute=2;
YII::$app->db->createCommand()->delete('{{%ybwm_good_attribute}}', ['goodId' => $id, 'uniacid' => $uniacid, 'storeId' => $storeId])->execute();
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['isAttr'=>2],['id' =>$id, 'uniacid'=>$uniacid,'storeId'=>$storeId])->execute();
}
//商品加料
if (count($result['feeding'])){
$materia=1;
$meteria_re = (new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where(['goodId' => $id, 'uniacid' => $uniacid, 'storeId' => $storeId])
->all();
//var_dump($meteria_re);die;
if ($meteria_re) {
Yii::$app->db->createCommand()->delete('{{%ybwm_good_materia}}', ['goodId' => $id, 'uniacid' => $uniacid, 'storeId' => $storeId])->execute();
}
foreach ($result['feeding'] as $v) {
$materia_arr = [];
$materia_arr= array(
'goodId' => $id,
'materialName' => $v['materialName'],
'SalesPrice' => $v['salesPrice'],
'SalesStock' => $v['salesStock'],
'fillType' => $v['fillType'],
'createdAt' => time(),
'uniacid'=>$uniacid,
'storeId'=>$storeId,
);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_materia}}', $materia_arr)->execute();
}
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['isMaterial'=>1],['id' =>$id, 'uniacid'=>$uniacid,'storeId'=>$storeId])->execute();
}else {
YII::$app->db->createCommand()->delete('{{%ybwm_good_materia}}', ['goodId' => $id, 'uniacid' => $uniacid, 'storeId' => $storeId])->execute();
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['isMaterial'=>2],['id' =>$id, 'uniacid'=>$uniacid,'storeId'=>$storeId])->execute();
}
//售卖时间自定义时间 售卖时间段自定义时间
if($result['weekSalesType']==2&&$result['salesType']==2){
$hours=1;
$time_arr=(new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_hours}}')
->where(['goodId' => $id, 'uniacid' =>$uniacid, 'storeId' => $storeId])
->all();
if($time_arr){
Yii::$app->db->createCommand()->delete('{{%ybwm_good_hours}}', ['goodId' => $id, 'uniacid' => $uniacid, 'storeId' =>$storeId])->execute();
}
foreach ($result['salesTimeStr'] as $v){
$time_insert_arr=[];
$time_insert_arr=array(
'uniacid'=>$uniacid,
'storeId'=>$storeId,
'startTime'=>$v['startTime'],
'endTime'=>$v['endTime'],
'goodId'=>$id,
);
if($v['startTime']&&$v['endTime']){
Yii::$app->db->createCommand()->insert('{{%ybwm_good_hours}}', $time_insert_arr)->execute();
}
}
}
//如果是主门店 子门店都插入一条数据
if($storeRes['isMain']==1){
//同步插入到子门店商品列表中
$shopDatas = (new \yii\db\Query())
->select('id')
->from('{{%ybwm_store}}')
->where(['uniacid' => $uniacid, 'isMain' => 2])
->andWhere(['in','goodsModel',[1,2]])
->all();
if ($shopDatas){
foreach ($shopDatas as $v){
$subArray=[];
$subArray=array(
'goodsId'=>$id,
'display'=>1,
'storeId'=>$v['id'],
'uniacid'=>$uniacid,
'createdAt'=>time(),
'isSpecs'=>$isSpecs,
'boxMoney'=>trim($result['boxMoney']),
'data'=>json_encode(array('specs'=>$sonSpecsData))
);
if(empty($result['spec'])){
$subArray['stock']=trim($result['stock']);
$subArray['price']=trim($result['price']);
$subArray['maxPrice']=trim($result['price']);
}else{
$subArray['stock']=max(array_column($sonSpecsData,'SalesStock'));
$subArray['price']=min(array_column($sonSpecsData,'SalesPrice'));
$subArray['maxPrice']=max(array_column($sonSpecsData,'SalesPrice'));
}
$subArray['data']=json_encode(array(
'specs'=>$result['spec'],
'attr'=>$result['attribute'],
'materia'=>$result['feeding'],
));
Yii::$app->db->createCommand()->insert('{{%ybwm_store_goods}}', $subArray)->execute();
}
}
}
}
$transaction->commit();//提交事务
}catch(\Exception $e){// 如果有一条查询失败,则会抛出异常
$transaction->rollBack();//事务回滚
}
if($re!==false){
echo json_encode(['code'=>1,'msg'=>'成功','goodsId'=>$id]);die;
// return $this->result(1,'操作成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
}
public function getTrees($arr,$id){
$list =array();
foreach ($arr as $k=>$v){
if ($v['pid'] == $id){
if($this -> getTrees($arr,$v['value'])){
$v['children'] = $this -> getTree($arr,$v['value']);
}
$list[] = $v;
}
}
return $list;
}
public function actionGoodRender(){
$request = Yii::$app->request;
$result=axios_request();
$type=$result['type']?:1;
$item=6;
if($type==2){
$item=9;
}
if ($request->isGet){
$uniacid=$this->wqData['uniacid'];
$storeId=$this->wqData['storeId'];
$storeRes=Store::getStoreInfo($uniacid,$storeId);
$storeInfo=(new \yii\db\Query())->from('{{%ybwm_store}}')
->where(['uniacid'=>$uniacid,'isMain'=>1])
->one();
$query= (new \yii\db\Query())
->select('id,id as value,name as label,pid')
->from('{{%ybwm_core_category}}')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId,'item'=>$item,'pid'=>0,'display'=>1]);
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$storeId=$storeInfo['id'];
$query->andWhere(['storeId'=>$storeId]);
}else{
$query->andWhere(['storeId'=>$storeId]);
}
$categoryData=$query->orderBy('id desc')->all();
//dd($re);die;
//获取商品的分类
//$categoryData = $this->getTrees($categoryRe, 0);
//获取商品的单位库
$unitData= (new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_core_unit}}')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
//获取商品的规格
$specsData= (new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_spec}}')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
//获取商品属性库
$attrData= (new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_attribute}}')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
//商品加料获取
$materData=(new \yii\db\Query())
->select('id,name,price')
->from('{{%ybwm_material}}')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
$data=array(
'categoryData'=>$categoryData,
'unitData'=>$unitData,
'specsData'=>$specsData,
'attrData'=>$attrData,
'materData'=>$materData,
);
// dd($data);die;
return $this->result(1, '成功', $data);
}
}
//商品详情
public function actionGoodDetail(){
$request = Yii::$app->request;
if ($request->isGet){
$result=axios_request();
$storeId=$this->wqData['storeId'];
$storeRes=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$this->wqData['uniacid'],'id'=>$storeId])
->one();
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$re= (new \yii\db\Query())
->select('a.*,b.*')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['goodsId'=>$result['id'],'a.uniacid'=>$this->wqData['uniacid'],'a.storeId'=>$storeId])
->one();
}else{
$re= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_core_goods}}')
->where(['id'=>$result['id'],'uniacid'=>$this->wqData['uniacid'],'storeId'=>$storeId])
->one();
}
$re['startTime']=date('Y-m-d',$re['startTime']);
$re['endTime']=date('Y-m-d',$re['endTime']);
$re['weekDay']=json_decode($re['weekDay'],true);
if($re['typeId']>0){
$re['typeId']=[$re['typePid'],$re['typeId']];
}else{
$re['typeId']=[$re['typePid']];
}
$re['goodLabel']=intval($re['goodLabel'])?:0;
$re['media']=json_decode($re['media'],true);
unset($re['typePid']);
//规格
$specsData= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$result['id'],'storeId'=>$storeId])
->all();
//属性
$attrData= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_attribute}}')
->where(['goodId'=>$result['id'],'storeId'=>$storeId])
->all();
foreach ($attrData as $k=>$v){
$attrData[$k]['attrStr']=json_decode($v['attrStr']);
}
//加料
$meterData= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_materia}}')
->where(['goodId'=>$result['id'],'storeId'=>$storeId])
->all();
$hoursData= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_hours}}')
->where(['goodId'=>$result['id'],'storeId'=>$storeId])
->all();
$re['weekSalesType']=$re['weekSalesType']?:1;
$data=array(
'detail'=>$re,
'specsData'=>$specsData,
'attrData'=>$attrData,
'meterData'=>$meterData,
'hoursData'=>$hoursData
);
return $this->result(1,'成功',$data);
}
}
public function actionSelectGood(){
$result=axios_request();
$storeId=$this->wqData['storeId'];
$keyword=$result['keyword']?:'';
$uniacid=$this->wqData['uniacid'];
$page=$result['page']?:1;
$size=$result['size']?:10;
$res=Good::getSelectGoods($uniacid,$storeId,$keyword,$page,$size);
return $this->result(1,'成功',$res);
}
//获取堂食商品列表
public function actionInFoodList(){
$uniacid=$this->wqData['uniacid'];
$storeId=$this->wqData['storeId'];
$result=axios_request();
if(!$uniacid||!$storeId){
return $this->result(2,'门店不存在');
}
$page=$result['page']?$result['page']:1;
$num=($page-1)*10;
$limit=10;
$data=Good::getGoodSData($uniacid,$storeId,'',2,null,2);
return $this->result(1,'成功',$data);
}
}