WriteOffSetting.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace addons\WriteOff\common\models;
  3. use addons\WriteOff\common\enums\WriteOffEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_write_off_setting}}".
  8. *
  9. * @property int $id ID
  10. * @property int $mall_id 商城ID
  11. * @property int $is_enable 启用状态[0禁用; 1启用]
  12. * @property int $goods_id 商品ID
  13. * @property float $give_num 奖励数量
  14. * @property int $settlement_method 计算方式[0订单完成, 1订单支付]
  15. * @property int $status 状态[-1删除; 0禁用; 1正常]
  16. * @property int $created_at 创建时间
  17. * @property int $updated_at 更新时间
  18. * @property int $is_enable_promote_reward_type 奖励类型
  19. * @property int $is_enable_promote_reward_prize 奖励基数
  20. */
  21. class WriteOffSetting extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_write_off_setting}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'is_enable', 'goods_id', 'settlement_method', 'status', 'created_at', 'updated_at','is_enable_promote_reward','is_enable_promote_reward_type'], 'integer'],
  37. [['goods_id'], 'required'],
  38. [['give_num','promote_reward_prize','is_enable_promote_reward_prize'], 'number'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'is_enable' => 'Is Enable',
  50. 'goods_id' => 'Goods ID',
  51. 'give_num' => 'Give Num',
  52. 'settlement_method' => 'Settlement Method',
  53. 'status' => 'Status',
  54. 'created_at' => 'Created At',
  55. 'is_enable_promote_reward_type' => 'Updated At',
  56. 'is_enable_promote_reward_prize' => 'Updated At',
  57. ];
  58. }
  59. public static function getGoodSetting($params)
  60. {
  61. // 后台页面组件展示
  62. if ($params['type'] == 'show_component') {
  63. return [
  64. 'component' => 'com-write-off',
  65. 'path' => Yii::$app->basePath . '/../addons/WriteOff/backend/views/goods',
  66. ];
  67. } else {
  68. try {
  69. if (empty($params['setting'][WriteOffEnum::ADDONS_NAME])) return true;
  70. $setting = $params['setting'][WriteOffEnum::ADDONS_NAME];
  71. $setting['mall_id'] = $params['mall_id'];
  72. $setting['goods_id'] = $params['goods_id'];
  73. // 数据保存
  74. $res = self::setData($setting);
  75. if ($res === false) throw new \Exception(self::getStaticError());
  76. return true;
  77. } catch (\Exception $e) {
  78. self::setStaticError($e->getMessage());
  79. return false;
  80. }
  81. }
  82. }
  83. public static function setData(array $params)
  84. {
  85. try {
  86. $model = self::findOne(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  87. if (empty($model)) {
  88. $model = new self();
  89. $model->loadDefaultValues();
  90. }
  91. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  92. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  93. return $model;
  94. } catch (\Exception $e) {
  95. self::$static_error = $e->getMessage();
  96. return false;
  97. }
  98. }
  99. }