StoreExtra.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_extra".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城id
  11. * @property int $store_id 门店id
  12. * @property string|null $give_setting 赠送设置
  13. * @property string|null $deduct_setting 抵扣设置
  14. * @property int $status 状态[1正常 -1删除]
  15. * @property int $created_at 创建时间
  16. * @property int $updated_at 修改时间
  17. */
  18. class StoreExtra extends BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_store_extra}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'store_id', 'status', 'created_at', 'updated_at'], 'integer'],
  34. [['give_setting', 'deduct_setting'], '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. 'give_setting' => 'Give Setting',
  47. 'deduct_setting' => 'Deduct Setting',
  48. 'status' => 'Status',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. ];
  52. }
  53. public static function setData($params)
  54. {
  55. try{
  56. // 根据商品id获取商品信息
  57. $model = self::findOne(['store_id' => $params['store_id'],'status'=>StatusEnum::ENABLED]);
  58. if (empty($model)) {
  59. // 初始化商品额外参数
  60. $model = new self();
  61. $model->loadDefaultValues();
  62. }
  63. $model->store_id = $params['store_id'];
  64. $params['give_setting'] = json_encode($params['give_setting']);
  65. $params['deduct_setting'] = json_encode($params['deduct_setting']);
  66. if(!$model->load($params,'') || !$model->save(false)) throw new \Exception($model->getErrorMessage());
  67. return true;
  68. }catch(\Exception $e){
  69. self::$static_error = $e->getMessage();
  70. return false;
  71. }
  72. }
  73. }