HigoStoreGoodsMode.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace addons\HiGo\common\models;
  3. use common\enums\AddonsEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use Exception;
  7. /**
  8. * This is the model class for table "{{%addons_higo_store_goods_mode}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $source_id 来源id
  13. * @property string $source 来源,插件名称
  14. * @property int $goods_id 商品id
  15. * @property int $is_enable 是否开启嗨购模式:0:否,1是
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class HigoStoreGoodsMode extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_higo_store_goods_mode}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id'], 'required'],
  36. [['mall_id', 'source_id', 'goods_id', 'is_enable', 'status', 'created_at', 'updated_at'], 'integer'],
  37. [['source'], 'string'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => 'Mall ID',
  48. 'source' => 'Source',
  49. 'source_id' => 'Source Id',
  50. 'goods_id' => 'Goods ID',
  51. 'is_enable' => 'Is Enable',
  52. 'status' => 'Status',
  53. 'created_at' => 'Created At',
  54. 'updated_at' => 'Updated At',
  55. ];
  56. }
  57. /**
  58. * 保存设置
  59. * @Author: lun
  60. * @DateTime: 2022/3/23 0023 14:26
  61. * @Copyright: copyright (c) 2021 广东七件事集团
  62. * @param $mall_id
  63. * @param $goods_id
  64. * @param $item
  65. * @return bool
  66. */
  67. public static function setGoodsSetting($mall_id, $goods_id, $item)
  68. {
  69. try {
  70. $model = self::findOne(['mall_id' => $mall_id, 'goods_id' => $goods_id, 'source' => $item['source'], 'source_id' => $item['source_id'], 'status' => StatusEnum::ENABLED]);
  71. if (empty($model)) {
  72. $model = new self();
  73. $model->mall_id = $mall_id;
  74. $model->goods_id = $goods_id;
  75. $model->loadDefaultValues();
  76. }
  77. if (!$model->load($item, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  78. return $model;
  79. } catch (\Exception $e) {
  80. self::setStaticError('商品嗨购设置失败:' . $e->getMessage());
  81. return false;
  82. }
  83. }
  84. }