StoreCouponGiveItemSettingDetail.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_detail".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $store_id 商城ID
  12. * @property int $coupon_give_item_setting_id 设置id
  13. * @property int $coupon_id 优惠券id
  14. * @property int $from_type 类型:1:平台优惠券;2:门店优惠券
  15. * @property int $num 赠送数量
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class StoreCouponGiveItemSettingDetail extends BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_store_coupon_give_item_setting_detail}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'store_id', 'from_type'], 'required'],
  36. [['mall_id', 'store_id', 'coupon_give_item_setting_id', 'coupon_id', 'from_type', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
  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. 'coupon_give_item_setting_id' => 'Coupon Give Item Setting ID',
  49. 'coupon_id' => 'Coupon ID',
  50. 'from_type' => 'From Type',
  51. 'num' => 'Num',
  52. 'status' => 'Status',
  53. 'created_at' => 'Created At',
  54. 'updated_at' => 'Updated At',
  55. ];
  56. }
  57. public function getCoupon()
  58. {
  59. return $this->hasOne(StoreCoupon::class, ['id' => 'coupon_id'])->where(['status' => StatusEnum::ENABLED]);
  60. }
  61. public static function setData($params, $data)
  62. {
  63. try{
  64. $coupon_id_arr = array_column($data,'coupon_id');
  65. self::updateAll(['status'=>StatusEnum::DELETE],[
  66. 'and',
  67. ['coupon_give_item_setting_id'=>$params['coupon_give_item_setting_id']],
  68. ['from_type'=>$params['from_type']],
  69. ['not in','coupon_id',$coupon_id_arr],
  70. ]);
  71. foreach ($data as $item) {
  72. $where = [
  73. 'mall_id' => $params['mall_id'],
  74. 'coupon_id' => $item['coupon_id'],
  75. 'store_id' => $params['store_id'],
  76. 'from_type' => $params['from_type'],
  77. 'coupon_give_item_setting_id' => $params['coupon_give_item_setting_id'],
  78. 'status' => [StatusEnum::ENABLED]];
  79. $model = self::findOne($where);
  80. if (empty($model)) {
  81. $model = new self();
  82. $model->mall_id = $params['mall_id'];
  83. $model->store_id = $params['store_id'];
  84. $model->from_type = $params['from_type'];
  85. $model->coupon_give_item_setting_id = $params['coupon_give_item_setting_id'];
  86. $model->loadDefaultValues();
  87. }
  88. if(!$model->load($item,'') || !$model->save()){
  89. throw new \Exception($model->getErrorMessage());
  90. }
  91. }
  92. return true;
  93. }catch(\Exception $e){
  94. self::$static_error = $e->getMessage();
  95. return false;
  96. }
  97. }
  98. }