| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace addons\StarChain\common\models;
- use common\events\goods\GoodsCreateEvent;
- use common\events\goods\SupplyChainGoodsChangeEvent;
- use common\models\goods\Goods;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- class MallGoods extends Goods
- {
- public static $static_error;
- public static $mall_id;
- /**
- * 商品数据
- *
- * @param integer $goods_id
- * @return void
- */
- public static function getDetail($goods_id)
- {
- $detail = self::getOne([['id' => $goods_id]], true, ['cats', 'attr', 'extra']);
- $detail['freight_type'] = !empty($detail['freight_type']) ? explode(',', $detail['freight_type']) : [];
- $detail['services'] = !empty($detail['services']) ? explode(',', $detail['services']) : [];
- $detail['labels'] = !empty($detail['labels']) ? explode(',', $detail['labels']) : [];
- $detail['attr_groups'] = Json::decode($detail['attr_groups'], true) ?? [];
- $detail['bannar_pic'] = Json::decode($detail['bannar_pic'], true) ?? [];
- $detail['attr_groups_format'] = Json::decode($detail['attr_groups_format'], true) ?? [];
- $detail['area_limit'] = Json::decode($detail['area_limit'], true) ?? [];
- $detail['cats'] = self::getCatsArr($detail['cats']);
- return $detail;
- }
- /**
- * 获取分类ID
- *
- * @param array $cats
- * @return array
- */
- private static function getCatsArr($cats)
- {
- if (empty($cats)) return [];
- return array_column($cats, 'id');
- }
-
- /**
- * 保存商品
- *
- * @return boolean|MallGoods
- */
- public static function saveGoods($params)
- {
- self::$mall_id = $params['mall_id'];
- // 获取商品额外属性
- $params['extra'] = self::getGoodsExtra($params);
- // 去添加
- $goods_model = MallGoods::setData($params/* , QiJuheEnum::PLUGIN_SIGN, function ($attr, $model, $goods_id) {
- return self::addJuheGoodsAttr($attr, $model->id, $goods_id);
- } */);
- if ($goods_model === false) {
- throw new Exception(MallGoods::getStaticError());
- }
- // 添加规格
- self::matchingSku($goods_model, $params['attr']);
- $data['goods_id'] = $goods_model->id;
- $data['mall_id'] = $params['mall_id'];
- // 新增商品出发新增事件
- if (empty($params['id'])) {
- $event = new GoodsCreateEvent($params['mall_id']);
- Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- }
- $event = new SupplyChainGoodsChangeEvent();
- Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- return $goods_model;
- }
-
- /**
- * 储存商城sku和聚合sku关系
- *
- * @param array $attr
- * @param int $sku_id
- * @return boolean
- */
- private static function addJuheGoodsAttr($attr, $attr_id, $goods_id)
- {
- $mall_id = self::$mall_id;
- $sku_id = $attr['sku_id'];
- // 删除之前的sku
- self::deleteAttr($mall_id, $goods_id, $sku_id);
- $params = [
- 'mall_id' => $mall_id,
- 'mall_goods_id' => $goods_id,
- 'attr_id' => $attr_id,
- ];
- $model = GoodsAttr::find()->where($params)->one();
- if (empty($model)) {
- $model = new GoodsAttr();
- $model->mall_id = $mall_id;
- $model->mall_goods_id = $goods_id;
- $model->attr_id = $attr_id;
- $model->chain_goods_id = $attr['chain_goods_id'];
- }
- $model->sku_id = $sku_id;
- return $model->save() ? true : false;
- }
- /**
- * 匹配商城sku
- *
- * @param array $attr_models
- * @param array $attr
- * @return boolean
- */
- private static function matchingSku($goods_model, $attrs)
- {
- if (count($attrs) > 1) {
- // 多规格添加
- foreach ($goods_model->attr as $model) {
- // 商城attr
- $sign_arr = explode(':', $model->sign_id);
- foreach ($attrs as $attr) {
- // 聚合attr
- $attr_id_arr = array_column($attr['attr_list'], 'attr_id');
- sort($attr_id_arr);
- sort($sign_arr);
- // 两个数组相等
- if ($attr_id_arr == $sign_arr) {
- $res = self::addJuheGoodsAttr($attr, $model->id, $goods_model->id);
- if ($res === false) {
- throw new Exception("goods_id:{$goods_model->id},sku关系添加失败");
- }
- }
- }
- }
- } else {
- // 单规格添加
- foreach ($goods_model->attr as $model) {
- foreach ($attrs as $attr) {
- $res = self::addJuheGoodsAttr($attr, $model->id, $goods_model->id);
- if ($res === false) {
- throw new Exception("goods_id:{$goods_model->id},sku关系添加失败");
- }
- }
- }
- }
- }
- /**
- * 删除sku
- *
- * @param integer $mall_id
- * @param integer $goods_id
- * @param integer $sku_id
- * @return void
- */
- private static function deleteAttr($mall_id, $goods_id, $sku_id)
- {
- GoodsAttr::deleteAll([
- 'mall_id' => $mall_id,
- 'mall_goods_id' => $goods_id,
- 'sku_id' => $sku_id,
- ]);
- /* GoodsAttr::updateAll(['is_delete' => 1], [
- 'mall_id' => $mall_id,
- 'mall_goods_id' => $goods_id,
- 'sku_id' => $sku_id,
- ]); */
- }
- /**
- * 获取商品额外属性
- *
- * @return void
- */
- private static function getGoodsExtra($params)
- {
- if (isset($params['extra']) && $params['extra']) {
- isset($params['extra']['score_give_setting']) && $params['extra']['score_give_setting'] = Json::decode($params['extra']['score_give_setting']);
- isset($params['extra']['member_buy_limit_setting']) && $params['extra']['member_buy_limit_setting'] = Json::decode($params['extra']['member_buy_limit_setting']);
- return $params['extra'];
- }
- return [
- // 营销配置
- 'full_reduce_price_limit' => 0,
- 'score_give_setting' => [
- 'code' => 'score',
- 'name' => '积分',
- 'give_num' => 0,
- 'give_type' => 1,
- ],
- // 会员权益
- 'is_member_price' => 0,
- 'member_price_setting' => [],
- 'member_buy_limit_setting' => [
- 'member_level' => [],
- 'every_num' => 0,
- 'every_day_num' => 0,
- 'every_week_num' => 0,
- 'every_month_num' => 0,
- 'nums' => 0,
- 'total_num' => 0,
- ],
- ];
- }
- }
|