canyin-project/ybcy/models/common/Good.php
2024-11-01 16:07:54 +08:00

974 lines
46 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\models\common;
use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
use app\models\common\Member;
class Good extends ActiveRecord{
//获取门店的商品
public function getGoodSData($uniacid,$storeId,$userId,$goodsType=1,$isRequire=2,$isShow=1){
$item=[6,12];
if($goodsType==2){
$item=[9,13];
}
$storeRe=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,':id'=>$storeId])
->one();
//20210525 代码将上面模式三的商品写入到core_goods表
if($storeRe['isMain']==2&&$storeRe['goodsModel']==3) {
$oldGoods=(new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where('uniacid=:uniacid AND storeId=:storeId',[':uniacid'=>$uniacid,':storeId'=>$storeId])
->all();
foreach ($oldGoods as &$v){
$goodOne=[];
$goodOne=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where('id=:id',[':id'=>$v['goodsId']])
->one();
//导入分类
$categoryOne=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where('id=:id',[':id'=>$goodOne['typePid']])
->one();
if($categoryOne){
$categoryOne['storeId']=$v['storeId'];
$oldCategoryId=$categoryOne['id'];
$categoryOne['oldCategoryId']=$oldCategoryId;
unset($categoryOne['id']);
$categoryOldOne=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where(['oldCategoryId'=>$oldCategoryId,'storeId'=>$storeId])
->one();
if($categoryOldOne){
$categoryId=$categoryOldOne['id'];
}else{
Yii::$app->db->createCommand()->insert('{{%ybwm_core_category}}', $categoryOne)->execute();
$categoryId = Yii::$app->db->getLastInsertID();
}
$goodOne['isSpecs']=$v['isSpecs'];
$goodOne['price']=$v['price'];
$goodOne['maxPrice']=$v['maxPrice'];
$goodOne['stock']=$v['stock'];
$goodOne['boxMoney']=$v['boxMoney']?:$goodOne['boxMoney'];
$goodOne['isAttr']=$v['isAttr'];
$goodOne['isMaterial']=$v['isMaterial'];
$goodOne['deleteAt']=$v['deleteAt'];
$goodOne['salesNum']=$v['salesNum'];
$goodOne['isRecommend']=$v['isRecommend'];
$goodOne['display']=$v['display'];
$goodOne['createdAt']=$v['createdAt'];
$goodOne['storeId']=$v['storeId'];
$goodOne['typePid']=$categoryId;
unset($goodOne['id']);
Yii::$app->db->createCommand()->insert('{{%ybwm_core_goods}}', $goodOne)->execute();
$goodNewId = Yii::$app->db->getLastInsertID();
$storeData=json_decode($v['data'],true);
if($v['isSpecs']==1&&$storeData['specs']){
foreach ($storeData['specs'] as $vo){
$specsOne=array(
'goodId'=>$goodNewId,
'specsName'=>$vo['specsName'],
'SalesPrice'=>$vo['SalesPrice'],
'SalesStock'=>$vo['SalesStock'],
'createdAt'=>$vo['createdAt']?:time(),
'fillType'=>$vo['fillType'],
'crossedPrice'=>$vo['crossedPrice'],
'goodCode'=>$vo['goodCode'],
'costPrice'=>$vo['costPrice'],
'uniacid'=>$v['uniacid'],
'storeId'=>$v['storeId'],
'boxMoney'=>$vo['boxMoney']?:0,
);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_specs}}', $specsOne)->execute();
}
}else{
if($goodOne['isSpecs']==1){
Yii::$app->db->createCommand()->update('{{%ybwm_core_goods}}', ['isSpecs'=>2],['id'=>$goodNewId])->execute();
}
}
//属性
if($v['isAttr']==1){
if($storeData['attr']){
foreach ($storeData['attr'] as $vo){
$attrOne=array(
'goodId'=>$goodNewId,
'attrName'=>$vo['attrName'],
'attrStr'=>$vo['attrStr'],
'createdAt'=>$vo['createdAt']?:time(),
'uniacid'=>$v['uniacid'],
'storeId'=>$v['storeId'],
);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_attribute}}', $attrOne)->execute();
}
}
}
if($v['isMaterial']==1){
if($storeData['materia']) {
foreach ($storeData['materia'] as $vo) {
$materiaOne = array(
'goodId' => $goodNewId,
'materialName' => $vo['materialName'],
'SalesPrice' => $vo['SalesPrice'],
'SalesStock' => $vo['SalesStock'],
'createdAt' => $vo['createdAt'] ?: time(),
'fillType' => $vo['fillType'],
'uniacid' => $v['uniacid'],
'storeId' => $v['storeId'],
);
Yii::$app->db->createCommand()->insert('{{%ybwm_good_materia}}', $materiaOne)->execute();
}
}
}
Yii::$app->db->createCommand()->delete('{{%ybwm_store_goods}}', ['id'=>$v['id']])->execute();
}
}
}
$mainStoreId=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND isMain=1',[':uniacid'=>$uniacid])
->one()['id'];
$query=(new \yii\db\Query())
->select('name,id,pid,icon,isRequire,customName,timeType,weekStr,startTime,endTime,item')
->from('{{%ybwm_core_category}}')
->where(['deleteAt' => 0,'uniacid'=>$uniacid,'display'=>1,'pid'=>0])
->andWhere(['in','item',$item]);
if($isRequire==1){
$query->andWhere(['isRequire'=>$isRequire]);
}
//如果是总门店
if($storeRe['isMain']==2&&$storeRe['goodsModel']==3){
$query->andWhere(['storeId'=>$storeId]);
}else{
$query->andWhere(['storeId'=>$mainStoreId]);
}
$res=$query->orderBy('item desc,sort asc,id desc')
->all();
//获取门店商品设置
$storeConfig=Config::getSystemSet('currency', $uniacid);
//saleShow 销量显示 1开启2关闭 stockShow 库存显示 1开启2关闭 sellOutShow 售罄显示 1开启2关闭
//dd($res);die;
//商品分类取总门店的分类加当前门店分类
foreach ($res as $k=>$v){
//如果是自定义显示分类
if($isShow==1){
if($v['timeType']==2){
//判断当天在周几
if(in_array(getWeek(),json_decode($v['weekStr'],true))==false){
unset($res[$k]);
continue; // 当 $value为b时跳出本次循环
}
if($v['startTime']&&$v['endTime']){
if(time()<strtotime($v['startTime'])||time()>strtotime($v['endTime'])){
unset($res[$k]);
continue; // 当 $value为b时跳出本次循环
}
}
}
}
if($storeRe['isMain']==1||$storeRe['goodsModel']==3) {
$goodQuery= (new \yii\db\Query())
->select('a.typePid,a.aloneType,a.id,a.name,icon,0 + CAST(price AS CHAR)price,(salesNum+virtualSales)salesNum,stock,minNum,maxNum,num,a.body,b.name labelName,b.color as labelColor,a.isSpecs,a.isAttr,a.isMaterial,a.salesType,a.startTime,a.endTime,a.weekDay,a.weekSalesType')
->leftJoin('{{%ybwm_core_label}} b', 'a.goodLabel=b.id')
->from('{{%ybwm_core_goods}} a')
->where(['a.goodsType'=>$goodsType, 'a.deleteAt' => 0, 'a.uniacid' => $uniacid, 'a.storeId' => $storeId, 'a.display' => 1]);
if($v['item']==12 || $v['item']==13){
if($v['customName']=='新品'){
$goodQuery->andWhere(['a.specialType' => 1]);
}
if($v['customName']=='热卖'){
$goodQuery->andWhere(['a.specialType' => 2]);
}
if($v['customName']=='推荐'){
$goodQuery->andWhere(['a.specialType' => 3]);
}
}else{
$goodQuery->andWhere(['a.typePid' => $v['id']]);
}
if($storeConfig['sellOutShow']==2){
$goodQuery->andWhere(['>','a.stock',0]);
}
//var_dump(ddSql($goodQuery));die;
$res[$k]['goods']=$goodQuery->orderBy('a.sort asc,a.id desc')
->all();
}else{
$goodQuery=(new \yii\db\Query())
->select('b.typePid,a.data,a.isSpecs,b.isSpecs goodSpecs,b.aloneType,a.goodsId id,b.name,icon,0 + CAST(a.price AS CHAR)price,(a.salesNum+b.virtualSales)salesNum,a.stock,b.minNum,b.maxNum,b.num,b.body,c.name labelName,c.color as labelColor,a.isSpecs,b.isAttr,b.isMaterial,b.salesType,b.startTime,b.endTime,b.weekDay,b.weekSalesType')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b', 'a.goodsId=b.id')
->leftJoin('{{%ybwm_core_label}} c', 'b.goodLabel=c.id')
->where(['b.goodsType'=>$goodsType, 'b.deleteAt' => 0, 'a.uniacid' => $uniacid, 'a.storeId' => $storeId, 'a.display' => 1]);
if($v['item']==12 || $v['item']==13){
if($v['customName']=='新品'){
$goodQuery->andWhere(['b.specialType' => 1]);
}
if($v['customName']=='热卖'){
$goodQuery->andWhere(['b.specialType' => 2]);
}
if($v['customName']=='推荐'){
$goodQuery->andWhere(['b.specialType' => 3]);
}
}else{
$goodQuery->andWhere(['b.typePid' => $v['id']]);
}
if($storeConfig['sellOutShow']==2){
$goodQuery->andWhere(['>','a.stock',0]);
}
$res[$k]['goods']=$goodQuery->orderBy('b.sort asc,b.id desc')
->all();
}
// dd($res[$k]['goods']);die;
foreach ($res[$k]['goods'] as $ko=>$vo){
$activityGoodData=self::getGoodActivity($uniacid,$storeId,$vo['price'],$vo['id'],$goodsType);
$res[$k]['goods'][$ko]['activityGoodData']=$activityGoodData;
$res[$k]['goods'][$ko]['vipPrice']=Member::getDiscountByGood($userId,$vo['id'],$vo['price'])['money'];
if($activityGoodData['type']==1 || $activityGoodData['type']==2 || $activityGoodData['type']==3){
$res[$k]['goods'][$ko]['vipPrice']=Member::getDiscountByGood($userId,$vo['id'],$activityGoodData['activityMoney'])['money'];
}
if($storeRe['isMain']==2&&$storeRe['goodsModel']<3){
$res[$k]['goods'][$ko]['isSpecs']=$res[$k]['goods'][$ko]['goodSpecs'];
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',['isSpecs'=>$res[$k]['goods'][$ko]['goodSpecs']], ['goodsId'=>$res[$k]['goods'][$ko]['id'],'storeId'=>$storeId])->execute();
}
if($vo['salesType']==2){
if($vo['startTime']>time()||$vo['endTime']<time()) {
unset($res[$k]['goods'][$ko]);
}
if(in_array(getWeek(),json_decode($vo['weekDay'],true))==false){
unset($res[$k]['goods'][$ko]);
}
if($vo['weekSalesType']==2){
if($storeRe['isMain']==1||$storeRe['goodsModel']==3){
$goodTimeArr=(new \yii\db\Query())
->select('startTime,endTime')
->from('{{%ybwm_good_hours}}')
->where(['uniacid' => $uniacid, 'goodId' =>$vo['id']])->andWhere(['storeId' => $storeId])->all();
}else{
$goodTimeArr=(new \yii\db\Query())
->select('startTime,endTime')
->from('{{%ybwm_good_hours}}')
->where(['uniacid' => $uniacid, 'goodId' =>$vo['id']])->andWhere(['storeId' => $mainStoreId])->all();
}
if($goodTimeArr){
$bool=0;
foreach ($goodTimeArr as $vs){
$strBool=getCurrentTimeSection($vs['startTime'],$vs['endTime']);
if($strBool){
$bool++;
}
}
if(!$bool){
unset($res[$k]['goods'][$ko]);
}
}else{
unset($res[$k]['goods'][$ko]);
}
}
}
}
$res[$k]['goods']=array_values($res[$k]['goods']);
if(!$res[$k]['goods']){
unset($res[$k]);
}
}
$data=array_values($res);
return $data;
}
//获取指定分类下的商品
public function getCategoryGoods($uniacid,$storeId,$cid){
$re= (new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where(['id'=>$cid,'deleteAt'=>0,'uniacid'=>$uniacid,'item'=>6])
->one();
$storeRes=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,':id'=>$storeId])
->one();
$storeConfig=Config::getSystemSet('currency', $uniacid);
if($storeRes['isMain']==1||$storeRes['goodsModel']==3){
$goodsQuery=(new \yii\db\Query())
->select('a.aloneType,a.id,a.name,icon,0 + CAST(price AS CHAR)price,salesNum,stock,minNum,maxNum,num,a.body,b.name labelName,b.color as labelColor,a.isSpecs,a.isAttr,a.isMaterial,a.salesType,a.startTime,a.endTime,a.weekDay,a.weekSalesType')
->leftJoin('{{%ybwm_core_label}} b', 'a.goodLabel=b.id')
->from('{{%ybwm_core_goods}} a')
->where('a.uniacid=:uniacid AND a.storeId=:storeId and a.deleteAt=0 and a.display=1',[':uniacid'=>$uniacid,':storeId'=>$storeId]);
}else {
$goodsQuery=(new \yii\db\Query())
->select('b.aloneType,a.goodsId id,b.name,icon,0 + CAST(a.price AS CHAR)price,b.salesNum,a.stock,b.minNum,b.maxNum,b.num,b.body,c.name labelName,c.color as labelColor,b.isSpecs,b.isAttr,b.isMaterial,b.salesType,b.startTime,b.endTime,b.weekDay,b.weekSalesType')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b', 'a.goodsId=b.id')
->leftJoin('{{%ybwm_core_label}} c', 'b.goodLabel=c.id')
->where('a.uniacid=:uniacid AND a.storeId=:storeId and b.deleteAt=0 and a.display=1',[':uniacid'=>$uniacid,':storeId'=>$storeId]);
}
if($re['pid']==0){
$goodsQuery->andWhere(['typePid'=>$cid]);
}else{
$goodsQuery->andWhere(['typeId'=>$cid]);
}
if($storeConfig['sellOutShow']==2){
$goodsQuery->andWhere(['>','a.stock',0]);
}
if($storeRes['isMain']==1||$storeRes['goodsModel']==1) {
$goodsData = $goodsQuery->orderBy('a.sort asc,a.id desc')->all();
}else{
$goodsData = $goodsQuery->orderBy('b.sort asc,a.id desc')->all();
}
foreach ($goodsData as $ko=>$vo){
if($vo['salesType']==2){
if($vo['startTime']>time()||$vo['endTime']<time()) {
unset($goodsData[$ko]);
}
if(in_array(getWeek(),json_decode($vo['weekDay'],true))==false){
unset($goodsData[$ko]);
}
if($vo['weekSalesType']==2){
$goodTimeArr=(new \yii\db\Query())
->select('startTime,endTime')
->from('{{%ybwm_good_hours}}')
->where(['uniacid' => $uniacid, 'storeId' => $storeId, 'goodId' =>$vo['id']])
->all();
$bool=0;
foreach ($goodTimeArr as $vs){
$strBool=getCurrentTimeSection($vs['startTime'],$vs['endTime']);
if($strBool){
$bool++;
}
}
if(!$bool){
unset($goodsData[$ko]);
}
}
}
}
$goodsData=array_values($goodsData);
return $goodsData;
}
//搜索的商品
public function getSelectGoods($uniacid,$storeId,$keyword,$page,$size,$userId=null,$goodsType=1){
$storeRes=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,':id'=>$storeId])
->one();
$storeConfig=Config::getSystemSet('currency', $uniacid);
if($storeRes['isMain']==1||$storeRes['goodsModel']==3){
$goodsQuery=(new \yii\db\Query())
->select('a.display,a.aloneType,a.id,a.name,icon,0 + CAST(price AS CHAR)price,salesNum,stock,minNum,maxNum,num,a.body,b.name labelName,b.color as labelColor,a.isSpecs,a.isAttr,a.isMaterial,a.salesType,a.startTime,a.endTime,a.weekDay,a.weekSalesType')
->leftJoin('{{%ybwm_core_label}} b', 'a.goodLabel=b.id')
->from('{{%ybwm_core_goods}} a')
->where('a.uniacid=:uniacid AND a.storeId=:storeId and a.deleteAt=0 and a.display=1 and a.goodsType='.$goodsType,[':uniacid'=>$uniacid,':storeId'=>$storeId]);
if($keyword){
$goodsQuery->andWhere(['like','a.name',$keyword]);
}
}else {
$goodsQuery=(new \yii\db\Query())
->select('a.display,b.aloneType,a.goodsId id,b.name,icon,0 + CAST(a.price AS CHAR)price,b.salesNum,a.stock,b.minNum,b.maxNum,b.num,b.body,c.name labelName,c.color as labelColor,b.isSpecs,b.isAttr,b.isMaterial,b.salesType,b.startTime,b.endTime,b.weekDay,b.weekSalesType')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b', 'a.goodsId=b.id')
->leftJoin('{{%ybwm_core_label}} c', 'b.goodLabel=c.id')
->where('a.uniacid=:uniacid AND a.storeId=:storeId and b.deleteAt=0 and a.display=1 and b.goodsType='.$goodsType,[':uniacid'=>$uniacid,':storeId'=>$storeId]);
if($keyword){
$goodsQuery->andWhere(['like','b.name',$keyword]);
}
}
if($storeConfig['sellOutShow']==2){
$goodsQuery->andWhere(['>','a.stock',0]);
}
if($storeRes['isMain']==1||$storeRes['goodsModel']==3) {
$goodsData = $goodsQuery->offset(($page - 1) * $size)->limit($size)->orderBy('a.sort asc,a.id desc')->all();
}else{
$goodsData = $goodsQuery->offset(($page - 1) * $size)->limit($size)->orderBy('b.sort asc,a.id desc')->all();
}
foreach ($goodsData as $ko=>$vo){
$activityGoodData=self::getGoodActivity($uniacid,$storeId,$vo['price'],$vo['id'],$goodsType);
$goodsData[$ko]['activityGoodData']=$activityGoodData;
$goodsData[$ko]['vipPrice']=Member::getDiscountByGood($userId,$vo['id'],$vo['price'])['money'];
if($activityGoodData['type']==1 || $activityGoodData['type']==2 || $activityGoodData['type']==3){
$goodsData[$ko]['vipPrice']=Member::getDiscountByGood($userId,$vo['id'],$activityGoodData['activityMoney'])['money'];
}
if($vo['salesType']==2){
if($vo['startTime']>time()||$vo['endTime']<time()) {
unset($goodsData[$ko]);
}
if(in_array(getWeek(),json_decode($vo['weekDay'],true))==false){
unset($goodsData[$ko]);
}
if($vo['weekSalesType']==2){
$goodTimeArr=(new \yii\db\Query())
->select('startTime,endTime')
->from('{{%ybwm_good_hours}}')
->where(['uniacid' => $uniacid, 'storeId' => $storeId, 'goodId' =>$vo['id']])
->all();
$bool=0;
foreach ($goodTimeArr as $vs){
$strBool=getCurrentTimeSection($vs['startTime'],$vs['endTime']);
if($strBool){
$bool++;
}
}
if(!$bool){
unset($goodsData[$ko]);
}
}
}
}
$goodsData=array_values($goodsData);
return $goodsData;
}
//获取指定门店的商品分类
public function getCategoryList($uniacid){
$storeId=(new \yii\db\Query())
->select('id')
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND isMain=1',[':uniacid'=>$uniacid])
->one()['id'];
$re= (new \yii\db\Query())
->select('id,icon,name,pid,isRequire')
->from('{{%ybwm_core_category}}')
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'item'=>6,'storeId'=>$storeId])
->orderBy('sort asc,id desc')
->all();
$categoryData=self::getTree($re,0);
return $categoryData;
}
public function getTree($arr,$id){
$list =array();
foreach ($arr as $k=>$v){
if ($v['pid'] == $id){
if(self::getTree($arr,$v['id'])){
$v['children'] =self::getTree($arr,$v['id']);
}
$list[] = $v;
}
}
return $list;
}
//计算商品的浏览量
public function getGoodVisit($uniacid,$storeId,$id,$startTime=null,$endTime=null){
$goodQuery=(new \yii\db\Query())
->from('{{%ybwm_goods_visit}}')
->where('uniacid=:uniacid AND storeId=:storeId AND goodsId=:goodsId',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodsId'=>$id]);
if($startTime&&$endTime){
$goodQuery->andWhere('createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]);
}
$count=$goodQuery->count();
return $count?:0;
}
//计算商品的下单数
public function getGoodCount($uniacid,$storeId,$id,$startTime,$endTime){
$goodQuery=(new \yii\db\Query())
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} b','a.orderId=b.id')
->where('a.uniacid=:uniacid AND a.storeId=:storeId AND goodsId=:goodsId AND b.state in (2,3,4,5,6)',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodsId'=>$id]);
if($startTime&&$endTime){
$goodQuery->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]);
}
$count=$goodQuery->count();
return $count?:0;
}
//计算商品的销售量
public function getGoodSum($uniacid,$storeId,$id,$startTime,$endTime){
$goodQuery=(new \yii\db\Query())
->from('{{%ybwm_order_goods}} a')
->leftJoin('{{%ybwm_takeout_order}} b','a.orderId=b.id')
->where('a.uniacid=:uniacid AND a.storeId=:storeId AND goodsId=:goodsId AND b.state in (2,3,4,5,6)',[':uniacid'=>$uniacid,'storeId'=>$storeId,'goodsId'=>$id]);
if($startTime&&$endTime){
$goodQuery->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]);
}
$count=$goodQuery->sum('a.num');
return $count?:0;
}
//同步分类下的商品
public function synchroGoods($storeId,$typeId){
$goodType=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where('id=:id',[':id'=>$typeId])->one();
unset($goodType['id']);
$goodType['item']=9;
// $category=(new \yii\db\Query())
// ->from('{{%ybwm_core_category}}')
// ->where($goodType)->one();
// print_R($category);die;
Yii::$app->db->createCommand()->insert('{{%ybwm_core_category}}', $goodType)->execute();
$newTypeId = Yii::$app->db->getLastInsertID();
$goodArr=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where('typePid=:typePid AND deleteAt=0',[':typePid'=>$typeId])->all();
for($i=0;$i<count($goodArr);$i++){
$attribute=(new \yii\db\Query())
->from('{{%ybwm_good_attribute}}')
->where('goodId=:goodId',[':goodId'=>$goodArr[$i]['id']])->all();
$hours=(new \yii\db\Query())
->from('{{%ybwm_good_hours}}')
->where('goodId=:goodId',[':goodId'=>$goodArr[$i]['id']])->all();
$materia=(new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where('goodId=:goodId',[':goodId'=>$goodArr[$i]['id']])->all();
$specs=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where('goodId=:goodId',[':goodId'=>$goodArr[$i]['id']])->all();
$goodArr[$i]['goodsType']=2;
$goodArr[$i]['typePid']=$newTypeId;
$goodArr[$i]['typeId']=0;
$goodArr[$i]['storeId']=$storeId;
unset($goodArr[$i]['id']);
Yii::$app->db->createCommand()->insert('{{%ybwm_core_goods}}', $goodArr[$i])->execute();
$goodsId = Yii::$app->db->getLastInsertID();
//如果是主门店 模式一的子门店都插入一条数据
//同步插入到子门店商品列表中
$shopDatas = (new \yii\db\Query())
->select('id')
->from('{{%ybwm_store}}')
->where(['uniacid' => $goodType['uniacid'], 'isMain' => 2,'deleteAt'=>0])
->andWhere(['<','goodsModel',3])
->all();
if ($shopDatas){
foreach ($shopDatas as $v){
$subArray=array(
'goodsId'=>$goodsId,
'display'=>3,
'stock'=>intval(trim($goodArr[$i]['stock']))?:'0',
'price'=>trim($goodArr[$i]['price']),
'storeId'=>$v['id'],
'uniacid'=>$goodType['uniacid'],
'createdAt'=>time(),
'isSpecs'=>$goodArr[$i]['isSpecs'],
'data'=>json_encode(['specs'=>$specs,'attr'=>$attribute,'materia'=>$materia])
);
Yii::$app->db->createCommand()->insert('{{%ybwm_store_goods}}', $subArray)->execute();
}
}
if($attribute){
for($a=0;$a<count($attribute);$a++){
unset($attribute[$a]['id']);
$attribute[$a]['goodId']=$goodsId;
$attribute[$a]['storeId']=$storeId;
Yii::$app->db->createCommand()->insert('{{%ybwm_good_attribute}}', $attribute[$a])->execute();
}
}
if($hours){
for($h=0;$h<count($hours);$h++) {
unset($hours[$h]['id']);
$hours[$h]['goodId'] = $goodsId;
$hours[$h]['storeId'] = $storeId;
Yii::$app->db->createCommand()->insert('{{%ybwm_good_hours}}', $hours[$h])->execute();
}
}
if($materia){
for($m=0;$m<count($materia);$m++) {
unset($materia[$m]['id']);
$materia[$m]['goodId'] = $goodsId;
$materia[$m]['storeId'] = $storeId;
Yii::$app->db->createCommand()->insert('{{%ybwm_good_materia}}', $materia[$m])->execute();
}
}
if($specs){
for($s=0;$s<count($specs);$s++) {
unset($specs[$s]['id']);
$specs[$s]['goodId'] = $goodsId;
$specs[$s]['storeId'] = $storeId;
Yii::$app->db->createCommand()->insert('{{%ybwm_good_specs}}', $specs[$s])->execute();
}
}
}
return true;
}
//查看商品活动后的价格
public function getGoodActivity($uniacid,$storeId,$money,$goodsId,$goodsType=1){
$startTime=strtotime(date("Y-m-d")." 00:00:00");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
$query=(new \yii\db\Query())
->from('{{%ybwm_good_activity}}')
->where('uniacid=:uniacid AND abolishAt=0 AND display=1 AND deleteAt=0 AND startTime<:startTime AND endTime>:endTime',
[':endTime'=>time(),':startTime'=>time(),':uniacid'=>$uniacid])
->andWhere(['or',['storeId'=>$storeId,'storeType'=>1],['storeType'=>2],['and',['storeType'=>3],['like','storeArr',$storeId]]])
->orderBy('id desc');
if($goodsType==1){
$query->andWhere(['like','goodsArr','"id":"'.$goodsId.'"']);
}else{
$query->andWhere(['like','goodsArr2','"id":"'.$goodsId.'"']);
}
//var_dump(ddSql($query));die;
$info=$query->one();
if($goodsType==1){
$orderNum=(new \yii\db\Query())
->from('{{%ybwm_order_goods}} as a')
->leftJoin('{{%ybwm_takeout_order}} b', 'a.orderId=b.id')
->where('a.storeId=:storeId AND a.goodsId=:goodsId AND a.item=1 AND a.isActivity>0 AND b.state in (1,2,3,4,5,6,9) AND a.createdAt>=:startTime AND a.createdAt<=:endTime',
[':startTime'=>$startTime,':endTime'=>$endTime,':storeId'=>$storeId,':goodsId'=>$goodsId])
->sum('a.num')?:0;
}else{
$orderNum=(new \yii\db\Query())
->from('{{%ybwm_order_goods}} as a')
->leftJoin('{{%ybwm_instore_order}} b', 'a.orderId=b.id')
->where('a.storeId=:storeId AND a.goodsId=:goodsId AND a.item>1 AND a.createdAt>=:startTime AND a.createdAt<=:endTime',
[':startTime'=>$startTime,':endTime'=>$endTime,':storeId'=>$storeId,':goodsId'=>$goodsId])
->andWhere(['in','b.state',[2,3]])
->sum('a.num')?:0;
}
// if($goodsId==10){
// var_dump($orderNum);
// var_dump($info['stock']);die;
// }
if($orderNum>=$info['stock']){
return [
'id'=>0,
'type'=>0,
'money'=>0,
'activityMoney'=>$money,
'limitNum'=>0,
'limitType'=>$info['limitType'],
'stock'=>$info['stock'],
];
}
$limitType=$info['limitType'];
if($limitType==1){
$limitNum=0;
}else{
$limitNum=$info['limitNum']?:1;
}
if($info['type']==1){
return [
'id'=>$info['id'],
'type'=>$info['type'],
'discount'=>$info['goodsMoney'],
'activityMoney'=>$info['goodsMoney'],
'limitNum'=>$limitNum,
'limitType'=>$info['limitType'],
'stock'=>$info['stock'],
];
}
if($info['type']==2){
return [
'id'=>$info['id'],
'type'=>$info['type'],
'discount'=>$info['goodsMoney'],
'activityMoney'=>bcmul($money,$info['goodsMoney']/10,2),
'limitNum'=>$limitNum,
'limitType'=>$info['limitType'],
'stock'=>$info['stock'],
];
}
if($info['type']==3){
return [
'id'=>$info['id'],
'type'=>$info['type'],
'discount'=>$info['goodsMoney'],
'activityMoney'=>bcsub($money,$info['goodsMoney'],2),
'limitNum'=>$limitNum,
'limitType'=>$info['limitType'],
'stock'=>$info['stock'],
];
}
if($info['type']==4){
return [
'id'=>$info['id'],
'type'=>$info['type'],
'discount'=>$info['goodsMoney'],
'activityMoney'=>bcmul($money,$info['goodsMoney']/10,2),
'limitNum'=>$limitNum,
'limitType'=>$info['limitType'],
'stock'=>$info['stock'],
];
}
if($info['type']==5){
return [
'id'=>$info['id'],
'type'=>$info['type'],
'discount'=>0,
'activityMoney'=>0,
'limitNum'=>$limitNum,
'limitType'=>$info['limitType'],
'stock'=>$info['stock'],
];
}
return [
'id'=>0,
'type'=>0,
'money'=>0,
'activityMoney'=>$money,
'limitNum'=>0,
'limitType'=>$info['limitType'],
'stock'=>$info['stock'],
];
}
//查看商品价格
public function getGoodMoneyByCar($carId){
$shopInfo = (new \yii\db\Query())
->from('{{%ybwm_shop_car}}')
->where(['id' => $carId])
->one();
$goodInfo = (new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['id' => $shopInfo['goodsId'], 'uniacid' => $shopInfo['uniacid']])
->one();
$storeRe=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$shopInfo['uniacid'],':id'=>$shopInfo['storeId']])
->one();
if($storeRe['isMain']==2&&$storeRe['goodsModel']<3){
$storeGood=(new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where(['goodsId' => $shopInfo['goodsId'], 'storeId' => $shopInfo['storeId'], 'uniacid' => $shopInfo['uniacid']])
->one();
$goodInfo['price']=$storeGood['price'];
}
$money=$goodInfo['price'];
if ($shopInfo['groupId']) {
$specsInfo = (new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where(['id' => $shopInfo['groupId'],'goodId' => $shopInfo['goodsId']])
->one();
$money = $specsInfo['SalesPrice'];
}
return $money;
}
function getGoodInfo($uniacid,$storeId,$goodsId,$groupId=0){
$goodsInfo=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['id'=>$goodsId])
->one();
$storeRes = (new \yii\db\Query())
->from('{{%ybwm_store}}')
->where(['id' =>$storeId,'uniacid' =>$uniacid])
->one();
if($storeRes['isMain']==1||$storeRes['goodsModel']==3){
if($groupId){
$specsInfo=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where(['id'=>$groupId])
->one();
$goodsInfo['price']=$specsInfo['SalesPrice'];
$goodsInfo['stock']=$specsInfo['SalesStock'];
$goodsInfo['name']=$goodsInfo['name'].'('.$specsInfo['specsName'].')';
}
}else{
$goodsInfo=(new \yii\db\Query())
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['goodsId'=>$goodsId,'a.storeId'=>$storeId])
->one();
if($groupId){
$goodsData=json_decode($goodsInfo['data'],true)['specs'];
foreach ($goodsData as $v){
if($groupId==$v['id']){
$goodsInfo['price']=$v['SalesPrice'];
$goodsInfo['stock']=$v['SalesStock'];
$goodsInfo['name']=$goodsInfo['name'].'('.$v['specsName'].')';
}
}
}
}
return $goodsInfo;
}
//异常多规格数据处理
public function saveGoods($id,$goodsType=1){
if($goodsType==1){
$goodsData=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['id'=>$id])
->one();
if($goodsData['isSpecs']==1){
$speData=(new \yii\db\Query())
->select('fillType,id,specsName,SalesPrice,SalesStock,status,crossedPrice,boxMoney,goodCode')
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$id])
->all();
if(!$speData){
Yii::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['isSpecs'=>2], ['id'=>$id])->execute();
}
}
}else{
$goodsInfo=(new \yii\db\Query())->from('{{%ybwm_store_goods}} a')
->select('a.uniacid,a.storeId,b.goodsPid,a.id,a.storeId,b.name,a.stock,a.price,a.deleteAt,a.display,a.data,a.isSpecs,b.isSpecs goodSpecs,a.goodsId')
->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id')
->where(['a.id'=>$id])->one();
$storeRe=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$goodsInfo['uniacid'],':id'=>$goodsInfo['storeId']])
->one();
if($storeRe['isMain']==2&&$storeRe['goodsModel']<3){
if($goodsInfo['isSpecs']==1){
$specsData=json_decode($goodsInfo['data'],true)['specs'];
if(!$specsData){
$goodsData=(new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['id'=>$goodsInfo['goodsId']])
->one();
if($goodsData){
$speData=(new \yii\db\Query())
->select('fillType,id,specsName,SalesPrice,SalesStock,status,crossedPrice,boxMoney,goodCode')
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$goodsInfo['goodsId'],'storeId'=>$goodsInfo['storeId']])
->all();
if($speData){
$insetSpec=json_decode($goodsInfo)['data'];
$insetSpec['specs']=[];
$insetSpec['specs']=$speData;
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',['data'=>json_encode($insetSpec),'isSpecs'=>1], ['id'=>$id])->execute();
}else{
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}', ['data'=>'','isSpecs'=>2],['id'=>$id])->execute();
}
}else{
Yii::$app->db->createCommand()->delete('{{%ybwm_store_goods}}', ['id'=>$id])->execute();
}
}
}else{
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',['data'=>'','isSpecs'=>2], ['id'=>$id])->execute();
}
}
}
}
//检测购物车商品价格变化
public static function checkPriceChange($carId){
$now=time();
$shopInfo = (new \yii\db\Query())
->from('{{%ybwm_shop_car}}')
->where(['id' => $carId])
->one();
$goodInfo = (new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['id' => $shopInfo['goodsId'], 'uniacid' => $shopInfo['uniacid']])
->one();
$storeRe=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$shopInfo['uniacid'],':id'=>$shopInfo['storeId']])
->one();
$money = $goodInfo['price'];
if($storeRe['isMain']==2&&$storeRe['goodsModel']<3){
$storeGood=(new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where(['goodsId' => $shopInfo['goodsId'], 'storeId' => $shopInfo['storeId'], 'uniacid' => $shopInfo['uniacid']])
->one();
$goodInfo['price']=$storeGood['price'];
}
$money=$goodInfo['price'];
if ($shopInfo['groupId']) {
$specsInfo = (new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where(['id' => $shopInfo['groupId'],'goodId' => $shopInfo['goodsId']])
->one();
$money = $specsInfo['SalesPrice'];
}
$user = $specsInfo = (new \yii\db\Query())
->from('{{%ybwm_member}}')
->where(['id' => $shopInfo['userId']])
->one();
//判断商品原始价格是否有变动
if($shopInfo['originalMoney'] != $money){
return['state'=>false,'message'=>$shopInfo['name'].'活动商品有变动,请重新添加'];
}
//判断商品会员价变动
if(!empty($goodInfo['discountArr']) && !empty($goodInfo['discountOpen'])){
foreach (json_decode($goodInfo['discountArr'],true) as $key3 => $level){
if($user['level'] == $level['level'] && $level['open'] == 1 ){
if(bcmul($level['discount'],$shopInfo['originalMoney']/10,2) != $shopInfo['money']){
return['state'=>false,'message'=>$shopInfo['name'].'商品会员价有变动,请重新添加'];
break;
}
}
}
}
//判断商品活动价变动
if($shopInfo['discountId']){
$activity=(new \yii\db\Query())
->from('{{%ybwm_good_activity}}')
->where('abolishAt=0 AND deleteAt=0 AND display=1 AND startTime<:time AND endTime>:time',[':time'=>$now])
->andWhere(['id'=>$shopInfo['discountId']])
->one();
if(!$activity){
return['state'=>false,'message'=>$shopInfo['name'].'活动商品有变动,请重新添加'];
}
if(!empty($activity['goodsArr'])){
foreach (json_decode($activity['goodsArr'],true) as $key2 => $activityGoods){
//特价
if($shopInfo['discountType'] == 1){
if($activityGoods['id'] == $shopInfo['goodsId'] && $activity['goodsMoney'] != $shopInfo['originalDiscountPrice']){
return['state'=>false,'message'=>$shopInfo['name'].'活动商品有变动,请重新添加'];
}
//打折和第二件打折
}else if($shopInfo['discountType'] == 2 || $shopInfo['discountType'] == 4 ){
if($activityGoods['id'] == $shopInfo['goodsId'] && bcmul($activity['goodsMoney'],$money/10,2) != $shopInfo['originalDiscountPrice']){
return['state'=>false,'message'=>$shopInfo['name'].'活动商品有变动,请重新添加'];
}
//满减
}else if($shopInfo['discountType'] == 3){
if($activityGoods['id'] == $shopInfo['goodsId'] && bcsub($money,$activity['goodsMoney'],2) != $shopInfo['originalDiscountPrice']){
return['state'=>false,'message'=>$shopInfo['name'].'活动商品有变动,请重新添加'];
}
}
}
}
}else{
//判断当前无商品否有活动 和价格变动
$activityList=(new \yii\db\Query())
->from('{{%ybwm_good_activity}}')
->where('abolishAt=0 AND deleteAt=0 AND display=1 AND startTime<:time AND endTime>:time',[':time'=>$now])
->andWhere(['storeId'=>$shopInfo['storeId']])
->andWhere(['like','goodsArr','"id":"'.$goodInfo['id'].'"'])
->all();
foreach ($activityList as $key1 => $activity){
foreach (json_decode($activity['goodsArr'],true) as $key2 => $activityGoods){
if($activityGoods['id'] == $shopInfo['goodsId']){
return['state'=>false,'message'=>$shopInfo['name'].'活动商品有变动,请重新添加'];
break;
}
}
}
}
return['state'=>true,'message'=>''];
}
}