StoreCouponGiveItemSetting.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_coupon_give_item_setting".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $store_id 门店
  12. * @property int $is_enable 是否独立设置:0:否;1是
  13. * @property int $item_id 项目id
  14. * @property string $item_type 项目类型:商品goods
  15. * @property int $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int $created_at
  17. * @property int $updated_at
  18. */
  19. class StoreCouponGiveItemSetting extends BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_store_coupon_give_item_setting}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'store_id'], 'required'],
  35. [['mall_id', 'store_id', 'is_enable', 'item_id', 'status', 'created_at', 'updated_at'], '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' => 'Mall ID',
  47. 'store_id' => 'Store ID',
  48. 'is_enable' => 'Is Enable',
  49. 'item_id' => 'Item ID',
  50. 'item_type' => 'Item Type',
  51. 'status' => 'Status',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. public function getDetail()
  57. {
  58. return $this->hasMany(StoreCouponGiveItemSettingDetail::class, ['coupon_give_item_setting_id' => 'id']);
  59. }
  60. public static function setData($params, $data)
  61. {
  62. try {
  63. $model = self::findOne([
  64. 'mall_id' => $params['mall_id'],
  65. 'store_id' => $params['store_id'],
  66. 'item_type' => $data['item_type'],
  67. 'item_id' => $params['item_id'],
  68. 'status' => [StatusEnum::ENABLED]
  69. ]);
  70. if (empty($model)) {
  71. $model = new self();
  72. $model->mall_id = $params['mall_id'];
  73. $model->store_id = $params['store_id'];
  74. $model->item_id = $params['item_id'];
  75. $model->loadDefaultValues();
  76. }
  77. $data['mall_id'] = $params['mall_id'];
  78. $data['store_id'] = $params['store_id'];
  79. $data['item_id'] = $params['item_id'];
  80. isset($params['is_enable']) && $data['is_enable'] = $params['is_enable'];
  81. if (!$model->load($data, '') || !$model->save()) {
  82. throw new \Exception($model->getErrorMessage());
  83. }
  84. // 插入配置详情
  85. if($data['detail']){
  86. foreach ($data['detail'] as $from_type=>$value){
  87. $detail = ['mall_id' => $data['mall_id'],'from_type'=>$from_type, 'store_id' => $data['store_id'], 'coupon_give_item_setting_id' => $model->id];
  88. $res = StoreCouponGiveItemSettingDetail::setData($detail,$value);
  89. if ($res == false) throw new \Exception(StoreCouponGiveItemSettingDetail::getStaticError());
  90. }
  91. }else{
  92. //删除优惠券
  93. StoreCouponGiveItemSettingDetail::updateAll(['status'=>StatusEnum::DELETE],[
  94. 'and',
  95. ['coupon_give_item_setting_id'=> $model->id],
  96. ['store_id'=>$params['store_id']],
  97. ]);
  98. }
  99. return true;
  100. } catch (\Exception $e) {
  101. self::$static_error = $e->getMessage();
  102. return false;
  103. }
  104. }
  105. /**
  106. * 商品设置优惠券
  107. * @Author: lun
  108. * @DateTime: 2022/3/9 0009 10:55
  109. * @Copyright: copyright (c) 2021 广东七件事集团
  110. * @param $mall_id
  111. * @param $goods_id
  112. * @param $store_id
  113. * @param $params
  114. * @return bool
  115. */
  116. public static function setGoodsSetting($mall_id,$store_id,$goods_id, $params)
  117. {
  118. try {
  119. $base_params = ['mall_id' => $mall_id, 'item_id' => $goods_id,'store_id' => $store_id, 'is_enable' => $params['coupon_is_enable']];
  120. // 获取有效的优惠券
  121. $coupon_arr = [];
  122. if (!empty($params['coupon_selection'])) {
  123. foreach ($params['coupon_selection'] as $key => $val) {
  124. if ($val['num'] > 0) {
  125. $coupon_arr[$val['from_type']][] = [
  126. 'coupon_id' => $val['coupon_id'],
  127. 'num' => $val['num'],
  128. 'from_type' => $val['from_type'],
  129. ];
  130. }
  131. }
  132. }
  133. $coupon_data = ['detail' => $coupon_arr, 'store_id'=>$store_id,'item_id' => $goods_id, 'item_type' => 'goods'];
  134. $res = StoreCouponGiveItemSetting::setData($base_params, $coupon_data);
  135. if ($res == false) throw new \Exception(StoreCouponGiveItemSetting::getStaticError());
  136. return true;
  137. } catch (\Exception $e) {
  138. self::setStaticError($e->getMessage());
  139. return false;
  140. }
  141. }
  142. }