CouponGiveItemSettingDetail.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_detail".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $coupon_give_item_setting_id 设置id
  12. * @property int $coupon_id 优惠券id
  13. * @property int $num 赠送数量
  14. * @property int $status 状态[-1:删除;0:禁用;1启用]
  15. * @property int $created_at
  16. * @property int $updated_at
  17. */
  18. class CouponGiveItemSettingDetail extends BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_coupon_give_item_setting_detail}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id'], 'required'],
  34. [['mall_id', 'coupon_give_item_setting_id', 'coupon_id', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => '商城ID',
  45. 'coupon_give_item_setting_id' => '设置id',
  46. 'coupon_id' => '优惠券id',
  47. 'num' => '赠送数量',
  48. 'status' => '状态[-1:删除;0:禁用;1启用]',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. ];
  52. }
  53. public function getCoupon()
  54. {
  55. return $this->hasOne(AddonsCoupon::class, ['id' => 'coupon_id'])->where(['status' => StatusEnum::ENABLED]);
  56. }
  57. public static function setData($params, $data)
  58. {
  59. try{
  60. $coupon_id_arr = array_column($data,'coupon_id');
  61. self::updateAll(['status'=>StatusEnum::DELETE],[
  62. 'and',
  63. ['coupon_give_item_setting_id'=>$params['coupon_give_item_setting_id']],
  64. ['not in','coupon_id',$coupon_id_arr],
  65. ]);
  66. foreach ($data as $item) {
  67. $model = self::findOne([
  68. 'mall_id' => $params['mall_id'],
  69. 'coupon_id' => $item['coupon_id'],
  70. 'coupon_give_item_setting_id' => $params['coupon_give_item_setting_id'],
  71. 'status' => [StatusEnum::ENABLED]
  72. ]);
  73. if (empty($model)) {
  74. $model = new self();
  75. $model->mall_id = $params['mall_id'];
  76. $model->coupon_give_item_setting_id = $params['coupon_give_item_setting_id'];
  77. $model->loadDefaultValues();
  78. }
  79. if(!$model->load($item,'') || !$model->save()){
  80. throw new \Exception($model->getErrorMessage());
  81. }
  82. }
  83. return true;
  84. }catch(\Exception $e){
  85. self::$static_error = $e->getMessage();
  86. return false;
  87. }
  88. }
  89. }