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

2196 lines
105 KiB
PHP

<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\web\Controller;
use yii\web\UploadedFile;
use app\models\common\Config;
use app\models\common\Store;
use app\models\common\Good;
use app\models\common\Ali;
use yii\db\Expression;
class GoodController extends CommonController{
public $enableCsrfValidation = false;
private $shop_id;
public function init(){
parent::init();
$this->shop_id=$this->storeId?:Store::getMainStore($this->wqData['uniacid'])['id'];
$this->shop_id=$this->shop_id?:0;
$request = Yii::$app->request;
if($request->isPost){
if(!$this->shop_id){
return $this->result(3,'无效的门店,请先添加商店');die;
}
}
}
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();
$item=6;
if($result['type']==2){
$item=9;
}
if ($request->isGet){
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$query= (new \yii\db\Query())
->select('id as value,name as label,pid')
->from('{{%ybwm_core_category}}')
->orderBy('sort asc,id asc')
->where(['deleteAt' => 0,'uniacid'=>$uniacid,'item'=>$item]);
$query->andWhere(['storeId'=>$storeId]);
$categoryRe=$query->orderBy('sort asc,id asc')->all();
//dd($re);die;
//获取商品的分类
$categoryData = $this->getTrees($categoryRe, 0);
//获取商品的单位库
$unitData= (new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_core_unit}}')->orderBy('sort asc,id asc')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
//获取商品的规格
$specsData= (new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_spec}}')->orderBy('sort asc,id asc')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
//获取商品属性库
$attrData= (new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_attribute}}')->orderBy('sort asc,id asc')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
//商品加料获取
$materData=(new \yii\db\Query())
->select('id,name,price')
->from('{{%ybwm_material}}')->orderBy('sort asc,id asc')
->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 actionAttrShow(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$re= (new \yii\db\Query())
->select('id,name,attr_str')
->from('{{%ybwm_attribute}}')
->where(['id'=>$result['id'],'deleteAt'=>0])
->one();
$re['attr_str']=json_decode($re['attr_str'],true);
if($re){
return $this->result(1,'成功',$re);
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//商品加料选中渲染
public function actionMaterShow(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$re= (new \yii\db\Query())
->select('id,name,price,stock')
->from('{{%ybwm_material}}')
->where(['id'=>$result['id'],'deleteAt'=>0,'uniacid'=>$this->wqData['uniacid'],'storeId'=>$storeId])
->one();
if($re){
return $this->result(1,'成功',$re);
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
public function actionGetGoodList(){
$result=axios_request();
$type=$result['goodsType']?:1;
$result['type']=$result['type']?:1;
if($type==1){
$item=6;
}else{
$item=9;
}
//if ($request->isPost){
//isMain==1 goodsModel==3
$shop_id=Store::getMainStore($this->wqData['uniacid'])['id']?:0;
if(!$shop_id){
echo json_encode(['code'=>1,'msg'=>'无效的门店']);die;
}
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$shop_id;
$storeRes=Store::getStoreInfo($uniacid,$storeId);
if($storeRes['isMain']==1||$storeRes['goodsModel']==3){
$categoryData=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->orderBy('sort asc,id asc')
->where(['deleteAt' => 0,'storeId'=>$storeId,'uniacid'=>$uniacid,'display'=>1,'pid'=>0,'item'=>$item])
->all();
}else{
$categoryData=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->orderBy('sort asc,id asc')
->where(['deleteAt' => 0,'storeId'=>$shop_id,'uniacid'=>$uniacid,'display'=>1,'pid'=>0,'item'=>$item])
->all();
}
// if(!$categoryData){
// echo json_encode(['code'=>1,'msg'=>'请添加商品分类']);die;
// }
// $unitData=(new \yii\db\Query())
// ->from('{{%ybwm_core_unit}}')
// ->where(['deleteAt' => 0,'uniacid'=>$uniacid,'display'=>1])
// ->all();
// if(!$unitData){
// echo json_encode(['code'=>1,'msg'=>'请添加商品单位']);die;
// }
$page=$result['page']?$result['page']:1;
$num=($page-1)*($result['size']?$result['size']:10);
// $num=($page-1)*$num;
$limit=$result['size']?$result['size']:10;
if($storeRes['isMain']==1||$storeRes['goodsModel']==3){
$query= (new \yii\db\Query())
->select('a.sort,discountOpen,id,name,icon,salesNum,typePid,typeId,price,stock,isRecommend,display,maxPrice,isSpecs,storeId,comboGoodsArr')
->from('{{%ybwm_core_goods}} a')
->where(['uniacid'=>$uniacid,'goodsType'=>$type,'storeId'=>$storeId]);
}else{
//门店商品模式是模式一或者模式二
$query= (new \yii\db\Query())
->select('b.sort,b.display mainDisplay,b.id,b.name,b.icon,a.salesNum,typePid,typeId,a.price,a.stock,a.isRecommend,a.display,a.maxPrice,a.isSpecs,goodsPid,a.data,b.comboGoodsArr')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'b.goodsType'=>$type,'a.storeId'=>$storeId]);
}
if($result['type']==1){
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
$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['goodsModel']<3&&$storeRes['isMain']==2){
$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['goodsModel']<3&&$storeRes['isMain']==2){
$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['goodsModel']<3&&$storeRes['isMain']==2){
$query->andWhere(['b.deleteAt'=>0]);
}else{
$query->andWhere(['a.deleteAt'=>0]);
}
$query->andWhere(['a.display'=>2]);
}
if($result['type']==5){
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
$query->andWhere(['b.deleteAt'=>0]);
}else{
$query->andWhere(['a.deleteAt'=>0]);
}
$query->andWhere(['a.display'=>3]);
}
if($result['type']==6){
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
$query->andWhere(['>','b.deleteAt',0]);
}else{
$query->andWhere(['>','a.deleteAt',0]);
}
}
if($result['name']){
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
$query->andWhere(['or',['like','b.name',$result['name']],['like','b.pinYin',$result['name']]]);
}else{
$query->andWhere(['or',['like','a.name',$result['name']],['like','a.pinYin',$result['name']]]);
}
}
if($result['typePid']){
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
$query->andWhere(['b.typePid'=>$result['typePid']]);
}else{
$query->andWhere(['a.typePid'=>$result['typePid']]);
}
}
//var_dump(ddSql($query));die;
$count=$query->count();
$re=$query->offset($num)
->limit($limit)
->orderBy('sort asc,id desc')
->all();
if($storeRes['isMain']==1||$storeRes['goodsModel']==3){
foreach($re as $k=>$v){
if($v['isSpecs']==1){
$specsRow=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$v['id']])
->all();
if(!$specsRow){
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', ['isSpecs'=>2], 'id=:id', ['id' => $v['id']])->execute();
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', ['isSpecs'=>2], 'goodsId=:goodsId', ['goodsId' => $v['id']])->execute();
}
}
if(!$v['salesNum']){
$re[$k]['salesNum']=0;
}
}
}
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
//剔除异常的数据 子门店有,总店没有的数据
// $errSql="select goodsId FROM ims_ybwm_store_goods
//LEFT JOIN (select id as i from ims_ybwm_core_goods where uniacid=".$uniacid." and storeId=".$shop_id.") as t1
//ON ims_ybwm_store_goods.goodsId=t1.i where ims_ybwm_store_goods.uniacid=$uniacid and ims_ybwm_store_goods.storeId=$storeId and t1.i IS NULL";
// $errArr= Yii::$app->db->createCommand($errSql)->queryAll();
// if($errArr){
// foreach ($errArr as $v){
// YII::$app->db->createCommand()->delete('{{%ybwm_store_goods}}', ['goodsId'=>$v['goodsId']])->execute();
// }
// }
foreach($re as $k=>$v){
if($v['isSpecs']!==2){
$specsData=json_decode($v['data'],true)['specs'];
if(!$specsData){
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', ['isSpecs'=>2], ['storeId'=>$result['storeId'],'goodsId' => $v['id']])->execute();
}
$idArr=array_column($specsData,'id');
if(empty($idArr)){
$specsRow=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$v['id']])
->all();
if(empty($specsRow)){
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}', ['isSpecs' =>2],['goodsId'=>$v['id'],'storeId'=>$result['storeId']])->execute();
$v['isSpecs']=2;
}else{
$sonSpecsData=[];
foreach ($specsRow as $vo){
$stockRes[]=$vo['SalesStock'];
$priceRes[]=$vo['SalesPrice'];
$sonSpecsData[]=array(
'id'=>$vo['id'],
'goodsId' =>$vo['goodId'],
'specsName' => $vo['specsName'],
'SalesPrice'=>$vo['SalesPrice'],
'SalesStock'=>$vo['SalesStock'],
);
}
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}', ['data'=>json_encode(array('specs'=>$sonSpecsData)),'price' => min($priceRes), 'maxPrice' => max($priceRes),'isSpecs' => 1,'stock'=>max($stockRes),],['goodsId'=>$v['id'],'storeId'=>$result['storeId']])->execute();
}
}
}
}
//同步总门店的未上架的商品
// $connection = Yii::$app->db;
// $sql="SELECT * FROM {{%ybwm_core_goods}} a WHERE uniacid =".$uniacid." AND storeId =$shop_id AND NOT EXISTS (SELECT 1 FROM {{%ybwm_store_goods}} b WHERE a.id = b.goodsid AND uniacid =$uniacid AND storeId =$storeId)";
// $command = $connection->createCommand($sql);
// $noResult = $command->queryAll();
// if($noResult){
// foreach ($noResult as $v){
// $subArray=array(
// 'goodsId'=>$v['id'],
// 'display'=>3,
// 'stock'=>$v['stock'],
// 'price'=>$v['price'],
// 'storeId'=>$result['storeId'],
// 'uniacid'=>$uniacid,
// 'createdAt'=>time(),
// );
// Yii::$app->db->createCommand()->insert('{{%ybwm_store_goods}}', $subArray)->execute();
// }
// }
}
foreach ($re as $k=>$v){
if($v['storeId']==$shop_id){
$re[$k]['isMain']=1?:2;
}
$ids=[$v['typePid'],$v['typeId']];
$res=(new \yii\db\Query())
->select('name')
->from('{{%ybwm_core_category}}')
->where(['in','id',$ids])
->all();
$re[$k]['category_name']=implode(",", array_column($res,'name'));
$re[$k]['media']=json_decode($re[$k]['media'],true);
}
//统计----------------------------------------
$where=[];
$where['a.uniacid']=$uniacid;
if($storeRes['goodsModel']==3){
$where['a.storeId']=array('in','a.storeId',[$result['storeId'],$shop_id]);
}else{
$where['a.storeId']=$storeId;
}
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
$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'=>$result['storeId'],'a.display'=>1,'b.deleteAt'=>0])
->andWhere(['>','a.stock',0])
->count();
$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'=>$result['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'=>$result['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'=>$result['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'=>$result['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,
'categoryData'=>$categoryData
);
return $this->result(1,'成功',$new_data,$count);
}
//}
//添加商品保存
public function actionGoodsSave(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
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;
}
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$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,
'comboGoodsArr'=>$result['comboGoodsArr']?json_encode($result['comboGoodsArr']):'',
);
$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();
if($storeRes['goodsModel']<3&&$result['storeId']){
$array['storeId']=$this->shop_id;
}
$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){// 如果有一条查询失败,则会抛出异常
file_put_contents('./error.txt',$e->getMessage());
return $this->result(2,$e->getMessage());
$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 actionGoodSearch(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
$type=$result['goodsType']?:1;
$where=array(
'name'=>$result['name'],
'typePid'=>$result['typePid'],
'typeId'=>$result['typeId'],
'goodsType'=>$type,
);
$where=filter_array($where);
$array= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_core_goods}}')
->where($where)
->orderBy('createdAt desc sort asc')
->all();
$count=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where($where)
->count();
if($array){
echo json_encode(['code'=>1,'msg'=>'商品分类列表获取成功','data'=>$array,'count'=>$count]);die;
}else{
echo json_encode(['code'=>2,'msg'=>'没有更多了']);die;
}
}
}
//商品置满/估清
public function actionSetGoodNum(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
if(is_array($result['id'])){
$ids=$result['id'];
}
if(is_string($result['id'])){
$ids[]=$result['id'];
}
$data=[];
if($result['type']){
if($result['type']==1){$data['stock']=0;}
if($result['type']==2){$data['stock']=999;}
}
if($result['num']){
$data['stock']=trim($result['num']);
}
$storeId=$result['storeId']?:$this->shop_id;
$uniacid=$this->wqData['uniacid'];
$storeRes=Store::getStoreInfo($uniacid,$storeId);
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$condition= ['and',['uniacid'=>$uniacid,'storeId'=>$storeId],['in', 'goodsId', $ids]];
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}',$data,$condition)->execute();
}else{
$condition= ['and',['uniacid'=>$uniacid,'storeId'=>$storeId],['in', 'id', $ids]];
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',$data,$condition)->execute();
if($storeRes['isMain']==1){
$shopDatas= (new \yii\db\Query())
->select('id')
->from('{{%ybwm_store}}')
->where(['uniacid' => $uniacid,'goodsModel'=>1])
->all();
$condition= ['and',['uniacid'=>$uniacid],['in', 'goodsId', $ids],['in', 'storeId', array_column($shopDatas,'id')]];
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}',$data,$condition)->execute();
}
if($storeRes['goodsModel']==3){
$condition= ['and',['uniacid'=>$uniacid,'storeId'=>$storeId],['in', 'goodsId', $ids]];
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}',$data,$condition)->execute();
}
}
$dition=['in', 'goodId', $ids];
YII::$app->db->createCommand()->update('{{%ybwm_good_specs}}',['SalesStock'=>$data['stock']],$dition)->execute();
echo json_encode(['code'=>1,'msg'=>'商品信息已修改']);die;
}
}
//单个或批量修改包装费
public function actionChangeBoxMoney(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
if($result['boxMoney']){
if(is_array($result['id'])){
$re = YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', ['boxMoney'=>$result['boxMoney']], ['in','id',$result['id']])->execute();
}else{
$re = YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', ['boxMoney'=>$result['boxMoney']], 'id=:id', ['id'=>$result['id']])->execute();
}
}
if($re!==false){
echo json_encode(['code'=>1,'msg'=>'商品包装费已修改']);die;
}else{
echo json_encode(['code'=>2,'msg'=>'没有更多了']);die;
}
}
}
//商品上下架
public function actionGoodDown(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
if(is_array($result['id'])){
$ids=$result['id'];
}
if(is_string($result['id'])){
$ids[]=$result['id'];
}
$data=[];
//下架
if($result['display']==1){
$data['display']=2;
//清除购物车的商品
$condition= ['and',['in', 'goodsId', $ids],['storeId'=>$result['storeId']]];
YII::$app->db->createCommand()->delete('{{%ybwm_shop_car}}',$condition)->execute();
YII::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}',$condition)->execute();
}
if($result['display']==2){
$data['display']=1;
}
if($result['isRecommend']==1){$data['isRecommend']=2;}//推荐
if($result['isRecommend']==2){$data['isRecommend']=1;}
$storeRes=Store::getStoreInfo($uniacid,$storeId);
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
$condition= ['and',['in', 'goodsId', $ids],['storeId'=>$result['storeId']]];
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}',$data,$condition)->execute();
}else{
$condition=['in', 'id', $ids];
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',$data,$condition)->execute();
if($storeRes['isMain']==1){
if($result['isRecommend']||$data['display']==1){
$condition= ['in', 'goodsId', $ids];
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}',$data,$condition)->execute();
}
}
}
echo json_encode(['code'=>1,'msg'=>'商品信息已修改']);die;
}
}
//商品回收站恢复商品
public function actionGoodsRecovery(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
if(is_array($result['id'])){
$ids=$result['id'];
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['deleteAt'=>0],['in', 'id', $ids])->execute();
}
if(is_string($result['id'])){
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['deleteAt'=>0],['id'=>$result['id']])->execute();
}
//var_dump($condition);die;
echo json_encode(['code'=>1,'msg'=>'商品已恢复']);die;
}
//商品excel导入功能
public function actionGoodExcelPull(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$file = UploadedFile::getInstanceByName('file'); //获取上传的文件实例
if($file){
if(in_array($file->extension,array('xls','xlsx'))){
$filename = './web/uploads/'.date('Y-m-d',time()).'_'.rand(1,9999).".". $file->extension;
$bool=$file->saveAs($filename); //保存文件
$fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
$excelReader = \PHPExcel_IOFactory::createReader($fileType);
$phpexcel = $excelReader->load($filename)->getSheet(0);//载入文件并获取第一个sheet
$total_line = $phpexcel->getHighestRow();//总行数
$total_column= $phpexcel->getHighestColumn();//总列数
$total_array = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA'];
if($total_line > 1){
for($row = 2;$row <= $total_line; $row++){
$data = array();
// for($column = 'A'; $column <= $total_column; $column++){
// echo $column;
// }
foreach ($total_array as $column) {
if($column=='Q'||$column=='R'){
$value= trim($phpexcel->getCell($column.$row)->getValue());
$data[] =strtotime(date('Y-m-d', ($value-25569)* 24*60*60));
}else{
$data[] = trim($phpexcel->getCell($column.$row)->getValue());
}
}
// if(!$data[1]||!preg_match("/^[1-9][0-9]*$/",$data[1])){
// return $this->result(1,'请填写正确的商品分类');die;
// }
if(!$data[3]){
return $this->result(1,'请填写商品名称');die;
}
if(!$data[8]){
return $this->result(1,'请填写商品价格');die;
}
if(!$data[10]){
return $this->result(1,'请填写商品库存');die;
}
if($data[18]){
if(!strpos($data[18],',') !==false){
return $this->result(1,'请填写正确的星期结构');die;
}
}
$arr=[];
$arr=array(
'sort'=>$data[0],
'typePid'=>$data[1],
'typeId'=>$data[2],
'name'=>$data[3],
'body'=>$data[4],
'icon'=>$data[5],
'minNum'=>$data[6],
'unit'=>$data[7],
'price'=>$data[8],
'boxMoney'=>$data[9],
'stock'=>$data[10],
'fillType'=>$data[11],
'crossedPrice'=>$data[12],
'costPrice'=>$data[13],
'goodCode'=>$data[14],
'salesType'=>$data[15],
'startTime'=>$data[16],
'endTime'=>$data[17],
'weekDay'=>json_encode(explode(",", $data[18]),JSON_UNESCAPED_UNICODE),
'hotsaleType'=>$data[19],
'aloneType'=>$data[20],
'details'=>$data[21],
'isMember'=>$data[22],
'maxNum'=>$data[23],
'salesNum'=>$data[24],
'goodLabel'=>$data[25],
'createdAt'=>time(),
'storeId'=>$storeId,
'uniacid'=>$this->wqData['uniacid'],
'sourceType'=>2,
'goodsType'=>$data[26],
);
//一行行的插入数据库操作
$info=Yii::$app->db->createCommand()->insert('{{%ybwm_core_goods}}', $arr)->execute();
if ($info) {
$ok = 1;
}else{
$ok = 0;
}
}
if($ok){
echo json_encode(['code'=>1,'msg'=>'商品添加成功']);die;
}else{
echo json_encode(['code'=>2,'msg'=>'网络异常,请稍后再试']);die;
}
}
}else{
echo json_encode(['code'=>2,'msg'=>'无效的文件类型']);die;
}
}
}
}
//下载上传示例
public function actionExcelDown(){
header('Content-Disposition: attachment; filename=商品导入模板.xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: '.filesize('./web/2020-08-24_925.xls'));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('./web/2020-08-24_925.xls');
}
//门店设置
public function actionGoodChangeSet(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
//门店可以修改价格 //门店可以修改库存 //门店添加商品 //门店添加分类
$data=array(
'goodSet'=>$result['goodSet'],
'shopIn'=>$result['shopIn'],
'showDistance'=>$result['showDistance'],
'distance'=>$result['distance']
);
$result=Config::saveSystemSet($data,'goodset');
if ($result) {
return $this->result(1,'成功');
} else {
return $this->result(2,'网络异常,请稍后再试');
}
}
}
public function actionGoodDel(){
$request = Yii::$app->request;
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
if($request->isPost){
$result=axios_request();
$sessionData=Yii::$app->session->get('userInfo');
if(is_array($result['id'])){
$ids=$result['id'];
}
if(is_string($result['id'])){
$ids[]=$result['id'];
}
if($result['type']==1){
if($ids){
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['deleteAt'=>time()],['in','id',$ids])->execute();
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}',['deleteAt'=>time()],['in','goodsId',$ids])->execute();
}
}else{
if($result['id']=='all'){
file_put_contents('deleteGoods.log',$sessionData['id'].'操作'.$result['id'].PHP_EOL, FILE_APPEND);
if($storeId){
YII::$app->db->createCommand()->delete('{{%ybwm_core_goods}}',['storeId'=>$storeId])->execute();
YII::$app->db->createCommand()->delete('{{%ybwm_store_goods}}',['storeId'=>$storeId])->execute();
}
}else{
file_put_contents('deleteGoods.log',$sessionData['id'].'操作'.json_encode($ids).PHP_EOL, FILE_APPEND);
if($ids){
YII::$app->db->createCommand()->delete('{{%ybwm_core_goods}}',['in','id',$ids])->execute();
YII::$app->db->createCommand()->delete('{{%ybwm_store_goods}}',['in','goodsId',$ids])->execute();
}
}
}
return $this->result(1,'成功');
}
}
public function actionGoodUsave(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
if($result['name']){
$data['name']=$result['name'];
}
if($result['price']){
$data['price']=$result['price'];
}
if($result['sort']){
$data['sort']=$result['sort'];
}
if($result['stock']){
$data['stock']=intval(trim($result['stock']));
}
$uniacid=$this->wqData['uniacid'];
$storeRes=Store::getStoreInfo($uniacid,$result['storeId']);
if($storeRes['goodsModel']<3&&$storeRes['isMain']==2){
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', $data, ['uniacid'=>$uniacid,'storeId'=>$result['storeId'],'goodsId'=>$result['id']])->execute();
}else{
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', $data, ['id'=>$result['id']])->execute();
if($result['price']) {
//同步修改子门店模式一的价格和库存
$subStoreData = array(
'price' => $data['price']
);
$shopDatas = (new \yii\db\Query())->select('id')
->from('{{%ybwm_store}}')
->where(['uniacid' => $uniacid, 'isMain' => 2, 'goodsModel' => 1])
->all();
if ($shopDatas&&$subStoreData) {
$shopIds = array_column($shopDatas, 'id');
if($subStoreData){
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', $subStoreData, ['and', ['in', 'storeId', $shopIds], ['goodsId' => $result['id']]])->execute();
}
}
}
}
return $this->result(1,'成功');
}
}
//商品详情
public function actionGoodDetail(){
$request = Yii::$app->request;
if ($request->isGet){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$storeRes=Store::getStoreInfo($uniacid,$storeId);
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=[];
if($re['isSpecs']==1){
//规格
$specsData= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$result['id']])
->all();
}
//属性
$attrData= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_attribute}}')
->where(['goodId'=>$result['id']])
->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']])
->all();
$hoursData= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_good_hours}}')
->where(['goodId'=>$result['id']])
->all();
$data=array(
'detail'=>$re,
'specsData'=>$specsData,
'attrData'=>$attrData,
'meterData'=>$meterData,
'hoursData'=>$hoursData
);
return $this->result(1,'成功',$data);
}
}
public function actionSpecsList(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
//获取商品的规格
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$storeRes=Store::getStoreInfo($uniacid,$storeId);
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$specsData= (new \yii\db\Query())
->select('data')
->from('{{%ybwm_store_goods}}')
->where(['goodsId'=>$result['id'],'uniacid'=>$uniacid,'storeId'=>$result['storeId']])
->one();
$specsData=json_decode($specsData['data'],true)['specs'];
}else{
$specsData= (new \yii\db\Query())
->select('id,specsName,SalesPrice,SalesStock')
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$result['id']])
->all();
}
return $this->result(1,'成功',$specsData);
}
}
public function actionSpecsSave(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId'];
$data=$result['price']?:$result['stock'];
$storeRes=Store::getStoreInfo($uniacid,$storeId);
if($storeRes['isMain']==2&&$storeRes['goodsModel']<3){
$SalesStock=[];$Salesprice=[];
foreach ($data as $v){
$SalesStock[]=$v['SalesStock'];
$Salesprice[]=$v['SalesPrice'];
}
$goodRes= (new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where(['goodsId'=>$result['id'],'uniacid'=>$uniacid,'storeId'=>$result['storeId']])
->one();
$specsData=json_decode($goodRes['data'],true);
$specsData['specs']=[];
$specsData['specs']=$data;
$price=min($Salesprice);
$stock=max($SalesStock);
$maxPrice=max($Salesprice);
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', ['data'=>json_encode($specsData),'price'=>$price,'maxPrice'=>$maxPrice,'stock'=>$stock], ['goodsId'=>$result['id'],'storeId'=>$result['storeId']])->execute();
return $this->result(1,'成功');die;
}else{
$priceData=[];
$stockData=[];
foreach ($data as $v){
$priceData[]=$v['SalesPrice'];
$stockData[]=$v['SalesStock'];
YII::$app->db->createCommand()->update('{{%ybwm_good_specs}}', ['SalesPrice'=>$v['SalesPrice'], 'SalesStock'=>$v['SalesStock']],['id'=>$v['id']])->execute();
}
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['price'=>min($priceData),'maxPrice'=>max($priceData),'stock'=>max($stockData)],['id' =>$result['id']])->execute();
//如果修改价格,同步门店一模式价格
if($result['price']){
$shopDatas= (new \yii\db\Query())
->select('id')
->from('{{%ybwm_store}}')
->where(['uniacid' => $uniacid,'goodsModel'=>1])
->all();
foreach($shopDatas as &$vo){
$goodRes= (new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where(['goodsId'=>$result['id'],'storeId'=>$result['storeId']])
->one();
$specsData=json_decode($goodRes['data'],true);
$specsData['specs']=[];
$specsData['specs']=$data;
$upData['price']=min($priceData);
$upData['maxPrice']=max($priceData);
$upData['stock']=max($stockData);
$upData['data']=json_encode($specsData);
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',$upData, ['goodsId' =>$result['id'],'storeId'=>$vo['id']])->execute();
}
}
}
return $this->result(1,'成功');
}
}
//商品单个或批量同步到子门店
public function actionSetGoodSync(){
$request = Yii::$app->request;
if($request->isPost){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$this->shop_id;
$where=['deleteAt' => 0,'uniacid'=>$uniacid,'storeId'=>$storeId];
if(!is_array($result['id'])){
$result['id']=[$result['id']];
}
$where[]=['in','id',$result['id']];
$data= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_core_goods}}')
->where($where)
->all();
//单规格商品
foreach ($data as $v){
$v['storeId']=$result['storeId'];
$v['pid']=$v['id'];
unset($v['id']);
YII::$app->db->createCommand()->insert('{{%ybwm_core_goods}}', $v)->execute();
//如果是多规格的商品
if($v['isSpecs']==2){
$id=Yii::$app->db->getLastInsertID();
$result= (new \yii\db\Query())
->select('*')
->from('{{%ybwm_goods_specs}}')
->where(['goodId'=>$v['pid'],'deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId])
->all();
foreach ($result as $vo){
$vo['goodId']=$id;
$vo['pid']=$v['pid'];
YII::$app->db->createCommand()->insert('{{%ybwm_goods_specs}}', $vo)->execute();
}
}
}
return $this->result(1,'成功');
}
}
public function actionGetGoodDetail(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$this->shop_id;
$re=(new \yii\db\Query())
->select('name,icon,price,isSpecs,display,createdAt')
->from('{{%ybwm_core_goods}}')
->where(['id'=>$result['id'],'uniacid'=>$uniacid,'storeId'=>$storeId])
->one();
$re['media']=json_decode($re['media'],true);
$re['createdAt']=date('Y-m-d H:i:s',$re['createdAt']);
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
//今日浏览量
$todayVisitCount=Good::getGoodVisit($uniacid,$storeId,$result['id'],$beginToday,$endToday);
$yesVisitCount=Good::getGoodVisit($uniacid,$storeId,$result['id'],$beginYesterday,$endYesterday);
$newVisitCount=bcdiv(bcsub($todayVisitCount-$yesVisitCount),$todayVisitCount);
$totalVisitCount=Good::getGoodVisit($uniacid,$storeId,$result['id']);
//今日下单数
$payCount=Good::getGoodCount($uniacid,$storeId,$result['id'],$beginToday,$endToday);
$yesPayCount=Good::getGoodVisit($uniacid,$storeId,$result['id'],$beginYesterday,$endYesterday);
$newPayCount=bcdiv(bcsub($payCount-$yesPayCount),$payCount);
$totalPayCount=Good::getGoodVisit($uniacid,$storeId,$result['id']);
//今日销售量
$paySum=Good::getGoodSum($uniacid,$storeId,$result['id'],$beginToday,$endToday);
$yesPaySum=Good::getGoodVisit($uniacid,$storeId,$result['id'],$beginYesterday,$endYesterday);
$newPaySum=bcdiv(bcsub($paySum-$yesPaySum),$paySum);
$totalPaySum=Good::getGoodVisit($uniacid,$storeId,$result['id']);
if($result['timeType']==1&&$result['time']){
//自然日
$startTime=strtotime(date('Y-m-d 00:00:00',strtotime($result['time'])));
$endTime=strtotime(date('Y-m-d 23:59:59',strtotime($result['time'])));
}elseif($result['timeType']==2){
//自然月
$startTime=strtotime(date('Y-m-01 00:00:00',strtotime($result['time'])));
$day=date('t',strtotime($result['time']));
$endTime=strtotime(date('Y-m-'.$day. ' 23:59:59',strtotime($result['time'])));
}else{
$startTime=strtotime(date('Y-m-d 00:00:00',time()));
$endTime=strtotime(date('Y-m-d 23:59:59',time()));
}
$queryData=[];
$queryData=(new \yii\db\Query())
->select('FROM_UNIXTIME(a.createdAt)hours,sum(a.money)money')
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} b','a.orderId=b.id')
->where('a.item=1 AND a.uniacid=:uniacid AND a.storeId=:storeId AND goodsId=:goodsId AND b.state in (2,3,4,5,6)',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodsId'=>$result['id']])
->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])
->groupBy('hours')
->all()?:[];
//15天内商品销量折线图
$saleArr=CreatQuery($result['time_type'],$queryData,$result['time']);
if($result['time_type']==2){
$saleArr=CreatQuery(3,$queryData,$result['time']);
}
//15天内商品支付金额折线图
$queryData=[];
$queryData=(new \yii\db\Query())
->select('FROM_UNIXTIME(a.createdAt)hours,count(*)money')
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} b','a.orderId=b.id')
->where('a.item=1 AND a.uniacid=:uniacid AND a.storeId=:storeId AND goodsId=:goodsId AND b.state in (2,3,4,5,6)',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodsId'=>$result['id']])
->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])
->groupBy('hours')
->all()?:[];
$payArr=CreatQuery($result['time_type'],$queryData,$result['time']);
if($result['time_type']==2){
$payArr=CreatQuery(3,$queryData,$result['time']);
}
$data=array(
'result'=>$re,
'todayVisitCount'=>$todayVisitCount,
'newVisitCount'=>$newVisitCount,
'totalVisitCount'=>$totalVisitCount,
'payCount'=>$payCount,
'newPayCount'=>$newPayCount,
'totalPayCount'=>$totalPayCount,
'paySum'=>$paySum,
'newPaySum'=>$newPaySum,
'totalPaySum'=>$totalPaySum,
'saleArr'=>$saleArr,
'payArr'=>$payArr,
);
return $this->result(1,'成功',$data);
}
//添加商品change选择加料的时候把价格赋值
public function actionChangeMateria(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$this->shop_id;
$re=(new \yii\db\Query())
->select('name,icon,price,isSpecs,display')
->from('{{%ybwm_material}}')
->where(['id'=>$result['id'],'deleteAt'=>0,'uniacid'=>$uniacid,'storeId'=>$storeId,'display'=>1])
->one();
return $this->result(1,'成功',$re);
}
//商品数据分析
public function actionGoodsAnalysis(){
$result=axios_request();
$goodsType=1;
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$startTime=strtotime(date('Y-m-d 00:00:00',strtotime($result['startTime'])));
$endTime=strtotime(date('Y-m-d 23:59:59',strtotime($result['endTime'])));
$storeInfo=(new \yii\db\Query())
->from('{{%ybwm_store}}')->where(['id'=>$storeId])->one();
//已上架
if($storeInfo['isMain']==1||$storeInfo['goodsModel']==3){
$upTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_core_goods}} a')
->where(['a.display'=>1,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'deleteAt'=>0])
->andWhere(['>','a.stock',0])->count();
}else{
$upTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.display'=>1,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'b.deleteAt'=>0])
->andWhere(['>=','a.stock',Yii::$app->params['stock']])->count();
}
//库存不足
if($storeInfo['isMain']==1||$storeInfo['goodsModel']==3){
$noTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_core_goods}} a')
->where(['a.display'=>1,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'deleteAt'=>0])
->andWhere(['<=','a.stock',YII::$app->params['stock']])->andWhere(['>','a.stock',0])->count();
}else{
$noTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.display'=>1,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'b.deleteAt'=>0])
->andWhere(['<=','a.stock',Yii::$app->params['stock']])->count();
}
//已售罄
if($storeInfo['isMain']==1||$storeInfo['goodsModel']==3){
$noneTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_core_goods}} a')
->where(['a.display'=>1,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'deleteAt'=>0])
->andWhere('a.stock=0 or a.stock is null')->count();
}else{
$noneTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.display'=>1,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'b.deleteAt'=>0])
->andWhere('a.stock=0 or a.stock is null')->count();
}
//已下架
if($storeInfo['isMain']==1||$storeInfo['goodsModel']==3){
$downTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_core_goods}} a')
->where(['a.display'=>2,'a.uniacid'=>$uniacid,'deleteAt'=>0])
->count();
}else{
$downTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.display'=>2,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'b.deleteAt'=>0])
->count();
}
//待上架
if($storeInfo['isMain']==1||$storeInfo['goodsModel']==3){
$shelfTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_core_goods}} a')
->where(['a.display'=>3,'a.uniacid'=>$uniacid,'deleteAt'=>0])->count();
}else{
$shelfTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.display'=>3,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'b.deleteAt'=>0])
->count();
}
//回收站
if($storeInfo['isMain']==1||$storeInfo['goodsModel']==3){
$recycleTotal=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_core_goods}} a')
->where(['a.uniacid'=>$uniacid,'a.storeId'=>$storeId])
->andWhere(['>','a.deleteAt',0])->count();
}else{
$recycleTotal=$goodQuery=(new \yii\db\Query())
->select('count(a.*)count')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'a.storeId'=>$storeId])
->andWhere(['>','b.deleteAt',0])
->count();
}
$beforeStartTime=strtotime(date('Y-m-d 00:00:00',$endTime-14*24*60*60));
//商品总销量
$goodTotal=(new \yii\db\Query())
->select(new Expression("from_unixtime(a.createdAt,'%Y-%m-%d') as hours,sum(a.num)money"))
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} b','a.orderId=b.id')
->where(['a.uniacid'=>$uniacid,'a.storeId'=>$storeId])
->andWhere(['>=','a.createdAt',$beforeStartTime])
->andWhere(['in','b.state',[2,3,4,5,6,9]])
->groupBy(new Expression("from_unixtime(a.createdAt,'%%Y-%m-%d')"))->all();
$goodTotal=dataByTime($goodTotal,$result['endTime']);
//商品总金额
$goodMoney=(new \yii\db\Query())
->select(new Expression("from_unixtime(a.createdAt,'%Y-%m-%d') as hours,sum(a.money*a.num)money"))
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} b','a.orderId=b.id')
->where(['a.uniacid'=>$uniacid,'a.storeId'=>$storeId])
->andWhere(['>=','a.createdAt',$beforeStartTime])
->andWhere(['in','b.state',[2,3,4,5,6,9]])
->groupBy(new Expression("from_unixtime(a.createdAt,'%%Y-%m-%d')"))->all();
$goodMoney=dataByTime($goodMoney,$result['endTime']);
//总支付金额
$totalMoney=(new \yii\db\Query())
->select('sum(a.money)money')
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} b','a.orderId=b.id')
->where(['a.uniacid'=>$uniacid,'a.storeId'=>$storeId])
->andWhere(['in','b.state',[2,3,4,5,6,9]])->one()['money']?:'0.00';
$goodFx=(new \yii\db\Query())
->select('a.orderId,a.goodsId,name,sum(a.num)count,sum(a.money*a.num)money')
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} c','a.orderId=c.id')
->where(['a.item'=>$goodsType,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId])
->andWhere(['>=','a.createdAt',$startTime])
->andWhere(['<=','a.createdAt',$endTime])
->andWhere(['in','c.state',[2,3,4,5,6,9]])
->groupBy('a.goodsId')->orderBy('count desc,money desc')
->limit(5)
->all();
foreach ($goodFx as $k=>$v){
$goodFx[$k]['id']=$k+1;
}
//支付金额榜
$payFx=(new \yii\db\Query())
->select('name,sum(a.money*a.num)money')
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} c','a.orderId=c.id')
->where(['a.item'=>$goodsType,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'c.uniacid'=>$uniacid,'c.storeId'=>$storeId])
->andWhere(['>=','a.createdAt',$startTime])
->andWhere(['<=','a.createdAt',$endTime])
->andWhere(['in','c.state',[2,3,4,5,6,9]])->groupBy('a.goodsId')
->orderBy('money desc')->limit(5)->all();
foreach ($payFx as $k=>$v){
$payFx[$k]['id']=$k+1;
}
//访客榜
$userFx=(new \yii\db\Query())
->select('name,count(*)count')
->from('{{%ybwm_goods_visit}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.uniacid'=>$uniacid,'a.storeId'=>$storeId])
->andWhere(['>=','a.createdAt',$startTime])
->andWhere(['<=','a.createdAt',$endTime])
->groupBy('a.goodsId')->orderBy('count desc')->limit(5)->all();
foreach ($userFx as $k=>$v){
$userFx[$k]['id']=$k+1;
}
$data=array(
'upTotal'=>$upTotal,
'noTotal'=>$noTotal,
'noneTotal'=>$noneTotal,
'downTotal'=>$downTotal,
'shelfTotal'=>$shelfTotal,
'recycleTotal'=>$recycleTotal,
'goodTotal'=>$goodTotal,
'goodMoney'=>$goodMoney,
'payFx'=>$payFx,
'userFx'=>$userFx,
'totalMoney'=>$totalMoney,
'goodFx'=>$goodFx
);
return $this->result(1,'成功',$data);
}
//查看全部商品排行
public function actionGoodsRank(){
$result=axios_request();
$goodsType=1;
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$startTime=strtotime(date('Y-m-d 00:00:00',strtotime($result['startTime'])));
$endTime=strtotime(date('Y-m-d 23:59:59',strtotime($result['endTime'])));
$page=$result['page']?$result['page']:1;
$num=($page-1)*10;
$limit=10;
$goodQuery=(new \yii\db\Query())
->select('a.orderId,b.name,sum(a.num)count,sum(a.money*a.num)money')
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->leftJoin('{{%ybwm_takeout_order}} c','a.orderId=c.id')
->where(['a.item'=>$goodsType,'a.uniacid'=>$uniacid,'a.storeId'=>$storeId,'c.uniacid'=>$uniacid,'c.storeId'=>$storeId])
->andWhere(['>=','a.createdAt',$startTime])
->andWhere(['<=','a.createdAt',$endTime])
->andWhere(['in','c.state',[2,3,4,5,6,9]])
->groupBy('a.goodsId')->orderBy('count desc,money desc');
$count=count($goodQuery->all());
$goodFx=$goodQuery->offset($num)
->limit($limit)
->all();
foreach ($goodFx as $k=>$v){
$goodFx[$k]['id']=$k+1;
}
return $this->result(1,'成功',$goodFx,$count);
}
//同步商品
public function actionSynchroGoods(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
Good::synchroGoods($storeId,$result['typeId']);
return $this->result(1,'成功');
}
//查看商品会员价
public function actionGoodsVip(){
//array_intersect
$result=axios_request();
$goodsId=$result['goodsId'];
$uniacid=$this->wqData['uniacid'];
$res=(new \yii\db\Query())
->select('level')
->from('{{%ybwm_user_card}}')
->where(['uniacid'=>$uniacid])
->orderBy('level asc')->all();
if(!$res){
return $this->result(2,'请先配置会员等级');
}
$goodsInfo=(new \yii\db\Query())
->select('discountOpen,discountArr')
->from('{{%ybwm_core_goods}}')
->where(['id'=>$goodsId])
->one();
$discountArr=json_decode($goodsInfo['discountArr'],true)?:[];
for($i=0;$i<count($res);$i++){
$res[$i]['open']=2;
$res[$i]['discount']=10;
if($discountArr){
$res[$i]['open']=$discountArr[$i]['open']?:2;
$res[$i]['discount']=$discountArr[$i]['discount']?:10;
}
}
return $this->result(1,'成功',$res);
}
//修改商品会员
public function actionSaveGoodsVip(){
$result=axios_request();
$goodsId=$result['goodsId'];
$res=Yii::$app->db->createCommand()->update('{{%ybwm_core_goods}}', ['discountArr'=>json_encode($result['discountArr']),'discountOpen'=>$result['discountOpen']], 'id=:id', ['id' => $goodsId])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'失败');
}
}
public function actionTest(){
$res=Ali::saveStore($this->wqData['uniacid']);
print_R($res);die;
}
//同步价格库存到子门店
public function actionChangeGoods(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeIds=$result['storeIds'];
$goodIds=$result['goodIds'];
$goodsType=$result['goodsType']?:1;
if($goodIds=='all'){
$goodInfo=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')->where(['uniacid'=>$uniacid,'storeId'=>$this->shop_id,'goodsType'=>$goodsType])
->all();
}else{
$goodInfo=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')->where(['uniacid'=>$uniacid,'storeId'=>$this->shop_id,'goodsType'=>$goodsType])
->where(['in','id',$goodIds])
->all();
}
foreach ($goodInfo as &$v){
if($v){
$data=[];
$data=[
'price'=>$v['price'],
'maxPrice'=>$v['maxPrice'],
'createdAt'=>time(),
'isSpecs'=>$v['isSpecs'],
'stock'=>$v['stock'],
'display'=>$v['display'],
];
$speData = (new \yii\db\Query())
->select('fillType,id,specsName,SalesPrice,SalesStock,status,crossedPrice,boxMoney,goodCode')
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$v['id']])
->all()?:[];
$materiaData = (new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where(['goodId'=>$v['id']])
->all()?:[];
$attrData = (new \yii\db\Query())
->from('{{%ybwm_good_attribute}}')
->where(['goodId'=>$v['id']])
->all()?:[];
$data['data']=json_encode(array(
'specs'=>$speData,
'materia'=>$materiaData,
'attr'=>$attrData,
));
$goodData=[];$ids=[];
foreach ($storeIds as &$vo){
try{
$storeGoods=(new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where(['goodsId'=>$v['id']])->andWhere(['storeId'=>$vo])
->one();
$transaction = Yii::$app->db->beginTransaction(); //开始事务
if($storeGoods){
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',$data, ['goodsId'=>$v['id'],'storeId'=>$vo])->execute();
}else{
$data['uniacid']=$this->wqData['uniacid'];
$data['storeId']=$vo;
$data['goodsId']=$v['id'];
$data['createdAt']=time();
$data['isRecommend']=$v['isRecommend'];
Yii::$app->db->createCommand()->insert('{{%ybwm_store_goods}}',$data)->execute();
}
$transaction->commit();//提交事务
}catch(Exception $e){// 如果有一条查询失败,则会抛出异常
$transaction->rollBack();//事务回滚
}
}
}
}
return $this->result(1,'成功');
}
//同步外卖商品到店内
public function actionMergeGoods(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$this->shop_id;
$goodIds=$result['goodIds'];
$transaction = Yii::$app->db->beginTransaction(); //开始事务
try{
//判断分类店内分类有没有
if($goodIds=='all'){
$goodCategory=(new \yii\db\Query())->from('{{%ybwm_core_category}}')
->where(['item'=>6,'uniacid'=>$uniacid,'storeId'=>$storeId,'pid'=>0])->all();
}else{
$goodData=(new \yii\db\Query())->from('{{%ybwm_core_goods}}')
->where(['in','id',$goodIds])->all();
$categoryIds=array_column($goodData,'typePid');
$goodCategory=(new \yii\db\Query())->from('{{%ybwm_core_category}}')
->where(['in','id',$categoryIds])->all();
}
if(!$goodCategory){
return false;
}
foreach ($goodCategory as &$v){
$categoryInfo=(new \yii\db\Query())->from('{{%ybwm_core_category}}')
->where(['name'=>$v['name'],'storeId'=>$v['storeId'],'item'=>9,'deleteAt'=>0])
->one();
if($categoryInfo){
$categoryPid=$v['id'];
$typePid=$categoryInfo['id'];
}else{
//如果有二级分类
$categoryPid=$v['id'];
unset($v['id']);$v['item']=9;
$v['oldCategoryId']=$categoryPid;
Yii::$app->db->createCommand()->insert('{{%ybwm_core_category}}',$v)->execute();
$typePid= Yii::$app->db->getLastInsertID();
}
if($goodIds=='all'){
$goodData=(new \yii\db\Query())->from('{{%ybwm_core_goods}}')
->where(['deleteAt'=>0,'goodsType'=>1,'uniacid'=>$uniacid,'storeId'=>$storeId,'typePid'=>$categoryPid])
->all();
}else{
$goodData=(new \yii\db\Query())->from('{{%ybwm_core_goods}}')
->where(['deleteAt'=>0,'goodsType'=>1,'uniacid'=>$uniacid,'storeId'=>$storeId,'typePid'=>$categoryPid])
->andWhere(['in','id',$goodIds])->all();
}
if($goodData){
foreach ($goodData as &$vo){
$oldId=$vo['id'];
//查询之前老商品有没有店内商品
$goodRes=(new \yii\db\Query())->from('{{%ybwm_core_goods}}')
->where(['goodsType'=>2,'pid'=>$oldId,'storeId'=>$storeId])
->one();
if(!$goodRes){
$vo['pid']=$oldId;
unset($vo['id']);
$vo['goodsType']=2;$vo['typePid']=$typePid;
$vo['salesNum']=0;
Yii::$app->db->createCommand()->insert('{{%ybwm_core_goods}}',$vo)->execute();
$id = Yii::$app->db->getLastInsertID();
if($vo['isSpecs']==1){
$speData = (new \yii\db\Query())
->select('fillType,id,specsName,SalesPrice,SalesStock,status,crossedPrice,boxMoney,goodCode')
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$oldId])
->all();
foreach ($speData as &$vs){
$vs['goodId']=$id;
unset($vs['id']);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_specs}}',$vs)->execute();
}
}
if($vo['isMaterial']==1){
$materiaData = (new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where(['goodId'=>$oldId])
->all();
if($materiaData){
foreach ($materiaData as &$va){
$va['goodId']=$id;
unset($va['id']);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_materia}}',$va)->execute();
}
}
}
if($vo['isAttr']==1){
$attrData = (new \yii\db\Query())
->from('{{%ybwm_good_attribute}}')
->where(['goodId'=>$oldId])
->all();
if($attrData){
foreach ($attrData as &$vb){
$vb['goodId']=$id;
unset($vb['id']);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_attribute}}',$vb)->execute();
}
}
}
if($vo['weekSalesType']==2){
$hoursData = (new \yii\db\Query())
->from('{{%ybwm_good_hours}}')
->where(['goodId'=>$oldId])
->all();
if($hoursData) {
foreach ($hoursData as &$vc) {
$vc['goodId'] = $id;
unset($vc['id']);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_hours}}', $vc)->execute();
}
}
}
}
}
}
}
$transaction->commit();//提交事务
}catch(Exception $e){// 如果有一条查询失败,则会抛出异常
$transaction->rollBack();//事务回滚
}
return $this->result(1,'成功');
}
//全部下架
/**
* @return bool
*/
public function actionAllDown(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$this->shop_id;
$goodIds=$result['id'];
$hoursData = (new \yii\db\Query())->select('id')
->from('{{%ybwm_store}}')
->where(['uniacid'=>$uniacid])->andWhere(['in','goodsModel',[1,2]])
->all();
YII::$app->db->createCommand()->update('{{%ybwm_core_goods}}', ['display'=>2], ['id' =>$goodIds,'storeId'=>$storeId])->execute();
$condition= ['and',['in', 'storeId', array_column($hoursData,'id')],['goodsId'=>$goodIds]];
YII::$app->db->createCommand()->update('{{%ybwm_store_goods}}', ['display'=>2], $condition)->execute();
return $this->result(1,'成功');
}
}