CouponStoreGoodsRelate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace addons\Coupon\common\models;
  3. use common\enums\StatusEnum;
  4. use PHPUnit\Util\Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_coupon_store_goods_relate".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $status 状态[-1:删除;0:禁用;1启用]
  12. * @property int $coupon_id 优惠券id
  13. * @property int $goods_id 门店商品id
  14. * @property int $created_at 创建时间
  15. * @property int $updated_at 修改时间
  16. */
  17. class CouponStoreGoodsRelate extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return 'qimall_addons_coupon_store_goods_relate';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id', 'status', 'coupon_id', 'goods_id', 'created_at', 'updated_at'], 'integer'],
  33. [['coupon_id', 'goods_id'], 'required'],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => 'ID',
  43. 'mall_id' => 'Mall ID',
  44. 'status' => 'Status',
  45. 'coupon_id' => 'Coupon ID',
  46. 'goods_id' => 'Goods ID',
  47. 'created_at' => 'Created At',
  48. 'updated_at' => 'Updated At',
  49. ];
  50. }
  51. /**
  52. * 保存数据
  53. *
  54. * @param $coupon_id
  55. * @param $mall_id
  56. * @param $goods_id
  57. *
  58. * @return bool
  59. * @throws \yii\db\Exception
  60. */
  61. public static function setData($coupon_id, $mall_id, $goods_id)
  62. {
  63. try {
  64. if ($coupon_id && $goods_id) {
  65. self::deleteAll([
  66. 'and',
  67. [
  68. 'coupon_id' => $coupon_id,
  69. 'status' => StatusEnum::ENABLED,
  70. 'mall_id' => $mall_id
  71. ],
  72. ['not in', 'goods_id', $goods_id]
  73. ]);
  74. $exist_goods_ids = self::find()
  75. ->select('goods_id')
  76. ->where([
  77. 'mall_id' => $mall_id,
  78. 'goods_id' => $goods_id,
  79. 'coupon_id' => $coupon_id,
  80. 'status' => StatusEnum::ENABLED
  81. ])
  82. ->column();
  83. $insert = [];
  84. $now = time();
  85. foreach ($goods_id as $id) {
  86. if (in_array($id, $exist_goods_ids)) continue;
  87. $insert[] = [$mall_id, $coupon_id, $id, $now, $now];
  88. }
  89. $field = ['mall_id', 'coupon_id', 'goods_id', 'created_at', 'updated_at'];
  90. // 批量插入数据
  91. Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  92. }
  93. return true;
  94. } catch (Exception $e) {
  95. self::$static_error = $e->getMessage();
  96. return false;
  97. }
  98. }
  99. }