GoodsFullMinus.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%goods_full_minus}}".
  11. *
  12. * @property integer $id
  13. * @property integer $store_id
  14. * @property integer $goods_id
  15. * @property string $colonel
  16. * @property string $group_num
  17. * @property integer $group_time
  18. * @property string $attr
  19. * @property string $is_delete
  20. * @property int $is_level
  21. */
  22. class GoodsFullMinus extends \yii\db\ActiveRecord
  23. {
  24. /**
  25. * @inheritdoc
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%goods_full_minus}}';
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['store_id'], 'required'],
  38. [['store_id', 'goods_id', 'full_minus_num', 'is_delete'], 'integer'],
  39. [['attr'], 'string'],
  40. ];
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'store_id' => 'Store ID',
  50. 'goods_id' => 'Goods ID',
  51. 'full_minus_num' => '商品满减数',
  52. 'attr' => '规格的库存及价格',
  53. ];
  54. }
  55. public function getCheckedAttrData()
  56. {
  57. if ($this->isNewRecord) {
  58. return [];
  59. }
  60. if (!$this->use_attr) {
  61. return [];
  62. }
  63. if (!$this->attr) {
  64. return [];
  65. }
  66. $attr_data = json_decode($this->attr, true);
  67. foreach ($attr_data as $i => $attr_data_item) {
  68. if (!isset($attr_data[$i]['no'])) {
  69. $attr_data[$i]['no'] = '';
  70. }
  71. if (!isset($attr_data[$i]['pic'])) {
  72. $attr_data[$i]['pic'] = '';
  73. }
  74. foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
  75. $attr_group = $this->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
  76. $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
  77. }
  78. }
  79. return $attr_data;
  80. }
  81. private function getAttrGroupByAttId($att_id)
  82. {
  83. $cache_key = 'get_attr_group_by_attr_id_' . $att_id;
  84. $attr_group = Yii::$app->cache->get($cache_key);
  85. if ($attr_group) {
  86. return $attr_group;
  87. }
  88. $attr_group = AttrGroup::find()->alias('ag')
  89. ->where(['ag.id' => Attr::find()->select('attr_group_id')->distinct()->where(['id' => $att_id])])
  90. ->limit(1)->one();
  91. if (!$attr_group) {
  92. return $attr_group;
  93. }
  94. Yii::$app->cache->set($cache_key, $attr_group, 10);
  95. return $attr_group;
  96. }
  97. }