CouponGiveItemSetting.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace addons\Coupon\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_coupon_give_item_setting".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $is_enable 是否独立设置:0:否;1是
  12. * @property int $item_id 项目id
  13. * @property string $item_type 项目类型:商品goods
  14. * @property int $status 状态[-1:删除;0:禁用;1启用]
  15. * @property int $created_at
  16. * @property int $updated_at
  17. * @property int $send_type
  18. */
  19. class CouponGiveItemSetting extends BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_coupon_give_item_setting}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id'], 'required'],
  35. [['mall_id', 'is_enable', 'item_id', 'status', 'created_at', 'updated_at', 'send_type'], 'integer'],
  36. [['item_type'], 'string', 'max' => 255],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => '商城ID',
  47. 'is_enable' => '是否独立设置:0:否;1是',
  48. 'item_id' => '项目id',
  49. 'item_type' => '项目类型:商品goods',
  50. 'status' => '状态[-1:删除;0:禁用;1启用]',
  51. 'created_at' => 'Created At',
  52. 'updated_at' => 'Updated At',
  53. ];
  54. }
  55. public function getDetail()
  56. {
  57. return $this->hasMany(CouponGiveItemSettingDetail::class, ['coupon_give_item_setting_id' => 'id']);
  58. }
  59. public static function setData($params, $data)
  60. {
  61. try {
  62. $model = self::findOne([
  63. 'mall_id' => $params['mall_id'],
  64. 'item_type' => $data['item_type'],
  65. 'item_id' => $params['item_id'],
  66. 'status' => [StatusEnum::ENABLED]
  67. ]);
  68. if (empty($model)) {
  69. $model = new self();
  70. $model->mall_id = $params['mall_id'];
  71. $model->item_id = $params['item_id'];
  72. $model->loadDefaultValues();
  73. }
  74. $data['mall_id'] = $params['mall_id'];
  75. $data['item_id'] = $params['item_id'];
  76. $data['send_type'] = $params['send_type'];
  77. isset($params['is_enable']) && $data['is_enable'] = $params['is_enable'];
  78. if (!$model->load($data, '') || !$model->save()) {
  79. throw new \Exception($model->getErrorMessage());
  80. }
  81. // 插入配置详情
  82. $detail = ['mall_id' => $data['mall_id'], 'coupon_give_item_setting_id' => $model->id];
  83. $res = CouponGiveItemSettingDetail::setData($detail, $data['detail']);
  84. if ($res == false) throw new \Exception(CouponGiveItemSettingDetail::getStaticError());
  85. return true;
  86. } catch (\Exception $e) {
  87. self::$static_error = $e->getMessage();
  88. return false;
  89. }
  90. }
  91. /**
  92. * 商品设置优惠券
  93. * @Author: lun
  94. * @DateTime: 2022/3/9 0009 10:55
  95. * @Copyright: copyright (c) 2021 广东七件事集团
  96. * @param $mall_id
  97. * @param $goods_id
  98. * @param $coupons
  99. * @return bool
  100. */
  101. public static function setGoodsSetting($mall_id, $goods_id, $coupons)
  102. {
  103. try {
  104. $base_params = ['mall_id' => $mall_id, 'item_id' => $goods_id, 'is_enable' => $coupons['is_enable'], 'send_type' => $coupons['send_type']];
  105. // 获取有效的优惠券
  106. $coupon_arr = [];
  107. foreach ($coupons['multiple_selection'] as $key => $val) {
  108. if ($val['num'] > 0) {
  109. $coupon_arr[] = [
  110. 'coupon_id' => $val['relevant_id'],
  111. 'num' => $val['num'],
  112. ];
  113. }
  114. }
  115. $coupon_data = ['detail' => $coupon_arr, 'item_id' => $goods_id, 'item_type' => 'goods'];
  116. $res = CouponGiveItemSetting::setData($base_params, $coupon_data);
  117. if ($res == false) throw new \Exception(CouponGiveItemSetting::getStaticError());
  118. return true;
  119. } catch (\Exception $e) {
  120. self::setStaticError($e->getMessage());
  121. return false;
  122. }
  123. }
  124. }