StoreCouponGoodsRelate.php 2.5 KB

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