MallGoods.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use common\events\goods\GoodsCreateEvent;
  4. use common\events\goods\SupplyChainGoodsChangeEvent;
  5. use common\models\goods\Goods;
  6. use Exception;
  7. use Yii;
  8. use yii\helpers\Json;
  9. class MallGoods extends Goods
  10. {
  11. public static $static_error;
  12. public static $mall_id;
  13. /**
  14. * 商品数据
  15. *
  16. * @param integer $goods_id
  17. * @return void
  18. */
  19. public static function getDetail($goods_id)
  20. {
  21. $detail = self::getOne([['id' => $goods_id]], true, ['cats', 'attr', 'extra']);
  22. $detail['freight_type'] = !empty($detail['freight_type']) ? explode(',', $detail['freight_type']) : [];
  23. $detail['services'] = !empty($detail['services']) ? explode(',', $detail['services']) : [];
  24. $detail['labels'] = !empty($detail['labels']) ? explode(',', $detail['labels']) : [];
  25. $detail['attr_groups'] = Json::decode($detail['attr_groups'], true) ?? [];
  26. $detail['bannar_pic'] = Json::decode($detail['bannar_pic'], true) ?? [];
  27. $detail['attr_groups_format'] = Json::decode($detail['attr_groups_format'], true) ?? [];
  28. $detail['area_limit'] = Json::decode($detail['area_limit'], true) ?? [];
  29. $detail['cats'] = self::getCatsArr($detail['cats']);
  30. return $detail;
  31. }
  32. /**
  33. * 获取分类ID
  34. *
  35. * @param array $cats
  36. * @return array
  37. */
  38. private static function getCatsArr($cats)
  39. {
  40. if (empty($cats)) return [];
  41. return array_column($cats, 'id');
  42. }
  43. /**
  44. * 保存商品
  45. *
  46. * @return boolean|MallGoods
  47. */
  48. public static function saveGoods($params)
  49. {
  50. self::$mall_id = $params['mall_id'];
  51. // 获取商品额外属性
  52. $params['extra'] = self::getGoodsExtra($params);
  53. // 去添加
  54. $goods_model = MallGoods::setData($params/* , QiJuheEnum::PLUGIN_SIGN, function ($attr, $model, $goods_id) {
  55. return self::addJuheGoodsAttr($attr, $model->id, $goods_id);
  56. } */);
  57. if ($goods_model === false) {
  58. throw new Exception(MallGoods::getStaticError());
  59. }
  60. // 添加规格
  61. self::matchingSku($goods_model, $params['attr']);
  62. $data['goods_id'] = $goods_model->id;
  63. $data['mall_id'] = $params['mall_id'];
  64. // 新增商品出发新增事件
  65. if (empty($params['id'])) {
  66. $event = new GoodsCreateEvent($params['mall_id']);
  67. Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  68. }
  69. $event = new SupplyChainGoodsChangeEvent();
  70. Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  71. return $goods_model;
  72. }
  73. /**
  74. * 储存商城sku和聚合sku关系
  75. *
  76. * @param array $attr
  77. * @param int $sku_id
  78. * @return boolean
  79. */
  80. private static function addJuheGoodsAttr($attr, $attr_id, $goods_id)
  81. {
  82. $mall_id = self::$mall_id;
  83. $sku_id = $attr['sku_id'];
  84. // 删除之前的sku
  85. self::deleteAttr($mall_id, $goods_id, $sku_id);
  86. $params = [
  87. 'mall_id' => $mall_id,
  88. 'mall_goods_id' => $goods_id,
  89. 'attr_id' => $attr_id,
  90. ];
  91. $model = GoodsAttr::find()->where($params)->one();
  92. if (empty($model)) {
  93. $model = new GoodsAttr();
  94. $model->mall_id = $mall_id;
  95. $model->mall_goods_id = $goods_id;
  96. $model->attr_id = $attr_id;
  97. $model->chain_goods_id = $attr['chain_goods_id'];
  98. }
  99. $model->sku_id = $sku_id;
  100. return $model->save() ? true : false;
  101. }
  102. /**
  103. * 匹配商城sku
  104. *
  105. * @param array $attr_models
  106. * @param array $attr
  107. * @return boolean
  108. */
  109. private static function matchingSku($goods_model, $attrs)
  110. {
  111. if (count($attrs) > 1) {
  112. // 多规格添加
  113. foreach ($goods_model->attr as $model) {
  114. // 商城attr
  115. $sign_arr = explode(':', $model->sign_id);
  116. foreach ($attrs as $attr) {
  117. // 聚合attr
  118. $attr_id_arr = array_column($attr['attr_list'], 'attr_id');
  119. sort($attr_id_arr);
  120. sort($sign_arr);
  121. // 两个数组相等
  122. if ($attr_id_arr == $sign_arr) {
  123. $res = self::addJuheGoodsAttr($attr, $model->id, $goods_model->id);
  124. if ($res === false) {
  125. throw new Exception("goods_id:{$goods_model->id},sku关系添加失败");
  126. }
  127. }
  128. }
  129. }
  130. } else {
  131. // 单规格添加
  132. foreach ($goods_model->attr as $model) {
  133. foreach ($attrs as $attr) {
  134. $res = self::addJuheGoodsAttr($attr, $model->id, $goods_model->id);
  135. if ($res === false) {
  136. throw new Exception("goods_id:{$goods_model->id},sku关系添加失败");
  137. }
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * 删除sku
  144. *
  145. * @param integer $mall_id
  146. * @param integer $goods_id
  147. * @param integer $sku_id
  148. * @return void
  149. */
  150. private static function deleteAttr($mall_id, $goods_id, $sku_id)
  151. {
  152. GoodsAttr::deleteAll([
  153. 'mall_id' => $mall_id,
  154. 'mall_goods_id' => $goods_id,
  155. 'sku_id' => $sku_id,
  156. ]);
  157. /* GoodsAttr::updateAll(['is_delete' => 1], [
  158. 'mall_id' => $mall_id,
  159. 'mall_goods_id' => $goods_id,
  160. 'sku_id' => $sku_id,
  161. ]); */
  162. }
  163. /**
  164. * 获取商品额外属性
  165. *
  166. * @return void
  167. */
  168. private static function getGoodsExtra($params)
  169. {
  170. if (isset($params['extra']) && $params['extra']) {
  171. isset($params['extra']['score_give_setting']) && $params['extra']['score_give_setting'] = Json::decode($params['extra']['score_give_setting']);
  172. isset($params['extra']['member_buy_limit_setting']) && $params['extra']['member_buy_limit_setting'] = Json::decode($params['extra']['member_buy_limit_setting']);
  173. return $params['extra'];
  174. }
  175. return [
  176. // 营销配置
  177. 'full_reduce_price_limit' => 0,
  178. 'score_give_setting' => [
  179. 'code' => 'score',
  180. 'name' => '积分',
  181. 'give_num' => 0,
  182. 'give_type' => 1,
  183. ],
  184. // 会员权益
  185. 'is_member_price' => 0,
  186. 'member_price_setting' => [],
  187. 'member_buy_limit_setting' => [
  188. 'member_level' => [],
  189. 'every_num' => 0,
  190. 'every_day_num' => 0,
  191. 'every_week_num' => 0,
  192. 'every_month_num' => 0,
  193. 'nums' => 0,
  194. 'total_num' => 0,
  195. ],
  196. ];
  197. }
  198. }