CommonGoodsAttr.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\models\common;
  8. use app\models\Attr;
  9. use app\models\AttrGroup;
  10. use app\models\Goods;
  11. use yii\helpers\Json;
  12. class CommonGoodsAttr
  13. {
  14. /**
  15. * 给规格列表添加个 规格组名称字段(商品编辑时需要用到)
  16. * @param array $goods
  17. * @return array
  18. */
  19. public static function getCheckedAttr($goods)
  20. {
  21. $storeId = get_store_id();
  22. $attr = is_string($goods['attr']) ? Json::decode($goods['attr']) : $goods['attr'];
  23. $newAttr = [];
  24. foreach ($attr as $k => $item) {
  25. $newAttr[$k] = $item;
  26. }
  27. foreach ($newAttr as $k => $item) {
  28. foreach ($item['attr_list'] as $k2 => $item2) {
  29. $attr = Attr::find()->where(['id' => $item2['attr_id'], 'is_delete' => 0])->asArray()->one();
  30. $cache_key = 'attr_group_by_attr_' . $attr['attr_group_id'];
  31. $attrGroup = \Yii::$app->cache->get($cache_key);
  32. if (!$attrGroup) {
  33. $attrGroup = AttrGroup::find()->where(['id' => $attr['attr_group_id'], 'store_id' => $storeId])->asArray()->one();
  34. \Yii::$app->cache->set($cache_key, $attrGroup, 1);
  35. }
  36. $newAttr[$k]['attr_list'][$k2]['attr_group_name'] = $attrGroup['attr_group_name'];
  37. }
  38. }
  39. return $newAttr;
  40. }
  41. /**
  42. * 商品库存减少
  43. * @param $attrIdList
  44. * @param $num
  45. * @param array $otherData
  46. * @return array
  47. */
  48. public static function num($attrIdList, $num, $otherData = [])
  49. {
  50. try {
  51. if (!isset($otherData['action_type']) || !in_array($otherData['action_type'], ['add', 'sub'])) {
  52. return [
  53. 'code' => 1,
  54. 'msg' => '请传入操作类型action_type, 必须[add||sub]'
  55. ];
  56. }
  57. $goodId = $otherData['good_id'];
  58. switch ($otherData['good_type']) {
  59. case 'STORE':
  60. $good = Goods::findOne($goodId);
  61. break;
  62. default:
  63. return [
  64. 'code' => 1,
  65. 'msg' => '无此商品类型'
  66. ];
  67. break;
  68. }
  69. sort($attrIdList);
  70. $attrs = json_decode($good->attr);
  71. $subAttrNum = false;
  72. foreach ($attrs as $i => $attr) {
  73. $goodAttrIdList = [];
  74. foreach ($attr->attr_list as $item) {
  75. array_push($goodAttrIdList, $item->attr_id);
  76. }
  77. sort($goodAttrIdList);
  78. if (!array_diff($attrIdList, $goodAttrIdList)) {
  79. if ($num > intval($attrs[$i]->num)) {
  80. return [
  81. 'code' => 1,
  82. 'msg' => '商品库存不足,无法购买',
  83. ];
  84. }
  85. if ($otherData['action_type'] === 'add') {
  86. $attrs[$i]->num = intval($attrs[$i]->num) + $num;
  87. }
  88. if ($otherData['action_type'] === 'sub') {
  89. $attrs[$i]->num = intval($attrs[$i]->num) - $num;
  90. }
  91. $subAttrNum = true;
  92. break;
  93. }
  94. }
  95. if (!$subAttrNum) {
  96. return [
  97. 'code' => 1,
  98. 'msg' => '商品规格信息有误,请检查商品'
  99. ];
  100. }
  101. $good->attr = Json::encode($attrs, JSON_UNESCAPED_UNICODE);
  102. $isTrue = true;
  103. if (!$good->save()) {
  104. $isTrue = false;
  105. }
  106. if ($isTrue) {
  107. return [
  108. 'code' => 0,
  109. 'msg' => '库存充足,可以购买'
  110. ];
  111. }
  112. return [
  113. 'code' => 1,
  114. 'msg' => '服务器出错啦!'
  115. ];
  116. } catch (\Exception $e) {
  117. return [
  118. 'code' => 1,
  119. 'msg' => $e->getMessage(),
  120. ];
  121. }
  122. }
  123. }