AttachmentService.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace services\common;
  3. use common\components\Service;
  4. use common\enums\StatusEnum;
  5. use common\models\common\Attachment;
  6. /**
  7. * Class AttachmentService
  8. * @package services\common
  9. */
  10. class AttachmentService extends Service
  11. {
  12. /**
  13. * @param $data
  14. * @return Attachment
  15. * @throws \yii\web\UnprocessableEntityHttpException
  16. */
  17. public function create($data)
  18. {
  19. $model = new Attachment();
  20. $model->attributes = $data;
  21. !$model->save() && $this->error($model);
  22. return $model;
  23. }
  24. /**
  25. * 查询校验
  26. *
  27. * @param $md5
  28. * @param string $drive
  29. * @param int $cate_id
  30. * @param string $upload_type
  31. * @return array|false
  32. */
  33. public function findByMd5($md5, $drive = '', $cate_id = 0, $upload_type = '')
  34. {
  35. $model = Attachment::find()
  36. ->where(['md5' => $md5])
  37. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  38. ->andFilterWhere(['drive' => $drive])
  39. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  40. ->one();
  41. if ($model) {
  42. !empty($cate_id) && $model->cate_id = $cate_id;
  43. $model->upload_type = $upload_type;
  44. $model->updated_at = time();
  45. $model->save();
  46. return $model->toArray();
  47. }
  48. return false;
  49. }
  50. /**
  51. * 获取百度编辑器查询数据
  52. *
  53. * @param $uploadType
  54. * @param $offset
  55. * @param $limit
  56. * @return array
  57. */
  58. public function baiduListPage($uploadType, $offset, $limit)
  59. {
  60. $data = Attachment::find()
  61. ->where(['status' => StatusEnum::ENABLED])
  62. ->andWhere(['upload_type' => $uploadType])
  63. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  64. ->orderBy('id desc');
  65. $countModel = clone $data;
  66. $models = $data->offset($offset)
  67. ->limit($limit)
  68. ->asArray()
  69. ->all();
  70. $files = [];
  71. foreach ($models as $model) {
  72. $files[] = [
  73. 'url' => $model['url'],
  74. 'mtime' => $model['created_at'],
  75. ];
  76. }
  77. return [$files, $countModel->count()];
  78. }
  79. /**
  80. * @param $id
  81. * @return array|\yii\db\ActiveRecord|null
  82. */
  83. public function findById($id)
  84. {
  85. return Attachment::find()
  86. ->where(['id' => $id])
  87. ->one();
  88. }
  89. }
粤ICP备19079148号