WorkerGoods.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. /**
  12. * This is the model class for table "{{%worker_goods}}".
  13. *
  14. * @property integer $id
  15. * @property integer $worker_id
  16. * @property integer $goods_id
  17. * @property integer $status
  18. * @property integer $created_at
  19. * @property integer $updated_at
  20. * @property integer $type
  21. * @property integer $is_delete
  22. */
  23. class WorkerGoods extends \yii\db\ActiveRecord
  24. {
  25. /**
  26. * @inheritdoc
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%worker_goods}}';
  31. }
  32. public function behaviors()
  33. {
  34. return [
  35. [
  36. 'class' => TimestampBehavior::class,
  37. ]
  38. ];
  39. }
  40. //增加或者全部替换
  41. public static function saveAll($worker_id, $goodsIds = [], $delAll = 0) {
  42. if($delAll){
  43. self::updateAll(['status' => 0], ['worker_id' => $worker_id]);
  44. }
  45. self::updateAll(['status' => 1], ['worker_id' => $worker_id, 'goods_id' => $goodsIds]);
  46. foreach($goodsIds as $goodsId){
  47. $has = self::findOne(['status' => 1, 'worker_id' => $worker_id, 'goods_id' => $goodsId]);
  48. if($has){
  49. continue;
  50. }
  51. $self = new self();
  52. $self->worker_id = $worker_id;
  53. $self->goods_id = $goodsId;
  54. $self->save();
  55. }
  56. return true;
  57. }
  58. public static function del($worker_id, $ids = []) {
  59. return self::updateAll(['status' => 0], ['worker_id' => $worker_id, 'goods_id' => $ids]);
  60. }
  61. }