MallGoodsModel.php 7.4 KB

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