StoreGoodsModel.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_store_goods_model".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $store_id
  12. * @property int $goods_id 商品id
  13. * @property string|null $content 内容
  14. * @property int $status 状态[0禁用 1启用 -1删除]
  15. * @property int $created_at 创建时间
  16. * @property int $updated_at 修改时间
  17. */
  18. class StoreGoodsModel extends BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_store_goods_model}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'store_id','goods_id', 'status', 'created_at', 'updated_at'], 'integer'],
  34. [['content'], 'string'],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => 'Mall ID',
  45. 'store_id' => 'Store ID',
  46. 'goods_id' => '商品id',
  47. 'content' => '内容',
  48. 'status' => '状态[0禁用 1启用 -1删除]',
  49. 'created_at' => '创建时间',
  50. 'updated_at' => '修改时间',
  51. ];
  52. }
  53. public static function setData(array $params)
  54. {
  55. try {
  56. if (!empty($params['mall_id'])&&!empty($params['store_id'])&&!empty($params['goods_id'])) {
  57. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED]]);
  58. if (empty($model)) {
  59. $model = new self();
  60. $model->loadDefaultValues();
  61. }
  62. } else {
  63. $model = new self();
  64. $model->loadDefaultValues();
  65. }
  66. if (!$model->load($params, '') || !$model->save()) {
  67. throw new \Exception($model->getErrorMessage());
  68. }
  69. return $model;
  70. } catch (\Exception $e) {
  71. self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
  72. return false;
  73. }
  74. }
  75. //获取门店商品独立设置模式
  76. public static function getGoodsModel(int $goods_id, string $addons_name, string $type){
  77. if($goods_id && $type){
  78. $goods_model = self::find()->where(['goods_id' => $goods_id])->select('content')->one();
  79. $setting = [];
  80. if (!empty($goods_model['content'])) {
  81. $content = json_decode($goods_model['content'], true);
  82. foreach ($content as $key => $item) {
  83. $setting[$key] = json_decode($item, true);
  84. }
  85. }
  86. return $setting[$addons_name][$type];
  87. }
  88. }
  89. }