cxfoot/models/common/CommonCartListForm.php
2023-10-27 14:25:12 +08:00

90 lines
2.9 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月15日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\Cart;
use app\models\Goods;
use app\models\GoodsAttr;
use app\models\Model;
use yii\helpers\ArrayHelper;
use app\components\Serializer;
class CommonCartListForm extends Model
{
public $plugin_sign;
public $cx_mch_id;
public $user_id;
public function rules()
{
return [
[['cx_mch_id', 'user_id'], 'integer'],
[['cx_mch_id', 'user_id', 'plugin_sign'], 'required'],
];
}
public function search()
{
if(!$this->validate()){
return $this->getModelError();
}
while (Cart::cacheStatusGet()) {
// 购物车的编辑、删除等操作完成之后,才可以获取购物车列表
usleep(500);
}
$list = Cart::find()->alias('c')->where([
'c.cx_mch_id' => $this->cx_mch_id,
'c.is_delete' => 0,
'c.user_id' => $this->user_id,
'c.plugin_sign' => $this->plugin_sign,
])
->with(['goods.goodsHub'])
->orderBy(['c.created_at' => SORT_DESC])->all();
$newList = [];
foreach ($list as $item) {
if($item->goods->plugin_sign != $this->plugin_sign){
continue;
}
$newItem = ArrayHelper::toArray($item);
$newItem['goods'] = ArrayHelper::toArray($item->goods);
$newItem['attrs'] = $item->attrs ? ArrayHelper::toArray($item->attrs) : $item->attrs;
$newItem['reduce_price'] = 0;
if ($item->attrs) {
// 还存在的商品
$newItem['attrs']['attr'] = (new Goods())->signToAttr($item->attrs->sign_id, $item->goods->attr_groups);
$newItem['attr_str'] = 0;
if ($item->attr_info) {
try {
$attrInfo = (new Serializer())->decode($item->attr_info);
$reducePrice = $attrInfo['price'] - $item->attrs->price;
if ($attrInfo['price'] - $item->attrs->price) {
$newItem['reduce_price'] = sprintf("%.2f",$reducePrice);
}
} catch (\Exception $exception) {
}
}
} else {
$newItem['attr_str'] = 1;
}
$newItem['attrs']['price'] = \Yii::$app->user->identity->touristGuide ? $newItem['attrs']['tourist'] : $newItem['attrs']['price'];
$newItem['goods']['name'] = $item->goods->name;
$newItem['goods']['cover_pic'] = $item->goods->coverPic;
$newList[] = $newItem;
}
return $this->apiReturnSuccess("ok", $newList);
}
}