RedpackGoodsSetting.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace addons\Redpack\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%addons_redpack_goods_setting}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $goods_id 商品id
  12. * @property int|null $redpack_wallet_type 红包类型[frozen_redpack:冻结红包,redpack:激活红包]
  13. * @property int $is_enable 是否开启:0:否,1是
  14. * @property int $is_percent 是否百分比:0:否(固定金额),1是
  15. * @property float $give 赠送红包金额
  16. * @property int $deduct_type 商品抵扣类型[1:单件抵扣,2:多件抵扣]
  17. * @property float $deduct_money 商品抵扣金额
  18. * @property float $is_deduct_percent 抵扣是否百分比[0=否,1=是]
  19. * @property int $status 状态[-1:删除;0:禁用;1启用]
  20. * @property int $created_at
  21. * @property int $updated_at
  22. */
  23. class RedpackGoodsSetting extends \common\models\base\BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%addons_redpack_goods_setting}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id'], 'required'],
  39. [['mall_id', 'goods_id', 'is_enable', 'is_percent', 'deduct_type', 'is_deduct_percent', 'status', 'created_at', 'updated_at'], 'integer'],
  40. [['give', 'deduct_money'], 'number'],
  41. [['redpack_wallet_type'], 'string', 'max' => 20],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城ID',
  52. 'goods_id' => '商品id',
  53. 'redpack_wallet_type' => '红包类型[frozen_redpack:冻结红包,redpack:激活红包]',
  54. 'is_enable' => '是否开启:0:否,1是',
  55. 'is_percent' => '是否百分比:0:否(固定金额),1是',
  56. 'give' => '赠送红包金额',
  57. 'deduct_type' => '商品抵扣类型[1:单件抵扣,2:多件抵扣]',
  58. 'deduct_money' => '商品抵扣金额',
  59. 'is_deduct_percent' => '抵扣是否百分比[0=否,1=是]',
  60. 'status' => '状态[-1:删除;0:禁用;1启用]',
  61. 'created_at' => 'Created At',
  62. 'updated_at' => 'Updated At',
  63. ];
  64. }
  65. /**
  66. * 保存设置
  67. * @Author: lun
  68. * @DateTime: 2022/3/23 0023 14:26
  69. * @Copyright: copyright (c) 2021 广东七件事集团
  70. * @param $mall_id
  71. * @param $goods_id
  72. * @param $list
  73. * @return bool
  74. */
  75. public static function setGoodsSetting($mall_id, $goods_id, $list)
  76. {
  77. try {
  78. foreach ($list as $item) {
  79. $model = self::findOne(['mall_id' => $mall_id, 'goods_id' => $goods_id, 'redpack_wallet_type' => $item['redpack_wallet_type'], 'status' => StatusEnum::ENABLED]);
  80. if (empty($model)) {
  81. $model = new self();
  82. $model->mall_id = $mall_id;
  83. $model->goods_id = $goods_id;
  84. $model->loadDefaultValues();
  85. }
  86. if (!$model->load($item, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  87. }
  88. return true;
  89. } catch (\Exception $e) {
  90. self::setStaticError('商品红包设置失败:' . $e->getMessage());
  91. return false;
  92. }
  93. }
  94. }