DemandService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Services;
  3. use App\Traits\ServiceAppAop;
  4. use App\Demand;
  5. use Illuminate\Http\UploadedFile;
  6. use Illuminate\Support\Facades\Auth;
  7. use Intervention\Image\Facades\Image;
  8. use Ramsey\Uuid\Uuid;
  9. class DemandService
  10. {
  11. use ServiceAppAop;
  12. protected $modelClass = Demand::class;
  13. /**
  14. * saveUPLoadFile
  15. * @param Demand $demand
  16. * @param UploadedFile $file
  17. * @return array
  18. */
  19. public function saveUPLoadFile(Demand $demand, UploadedFile $file): array
  20. {
  21. $tmpFile = $file->getRealPath();
  22. if (!$demand->uploadFile()) return ['success' => false, 'message' => '该需求已有描述图片'];
  23. if (!$file) return ['success' => false, 'message' => '上传图片不得为空'];
  24. if (!$file->isValid()) return ['success' => false, 'message' => '找不到上传图片'];
  25. if (!is_uploaded_file($tmpFile)) return ['success' => false, 'message' => '文件错误'];
  26. if ($file->getSize() > 5 * 1024 * 1024) return ['success' => false, 'message' => '文件不能大于5MB'];
  27. $fileSuffix = $file->getClientOriginalExtension();
  28. if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg']))
  29. return ['success' => false , 'message' => '文件格式应该为gif,image,jpeg,jpg,png,svg格式'];
  30. $path = ['app','public','files'];
  31. $path = join(DIRECTORY_SEPARATOR,$path);
  32. $dirPath = storage_path($path);
  33. if (!file_exists($dirPath)) {
  34. mkdir($dirPath);
  35. }
  36. $fileName = date('ymd') . '-' . Uuid::uuid1();
  37. $thumbnailName = storage_path('app/public/files/' . $fileName . '-thumbnail.' . $fileSuffix);
  38. $commonName = storage_path('app/public/files/' . $fileName . '-common.' . $fileSuffix);
  39. $bulkyName = storage_path('app/public/files/' . $fileName . '-bulky.' . $fileSuffix);
  40. $result = move_uploaded_file($tmpFile, $bulkyName);
  41. if ($result == false) return ['success' => false, 'data' => '文件上传失败'];
  42. $img = Image::make($bulkyName);
  43. if ($img->height() > $img->width()) {
  44. $img->heighten(250)->save($commonName);
  45. } else {
  46. $img->widen(250)->save($commonName);
  47. }
  48. $img->heighten(28)->save($thumbnailName);
  49. $upLoadFile = $demand->saveFile($fileName, $fileSuffix);
  50. if (!$upLoadFile) return ['success' => false, 'message' => '文件上传失败'];
  51. return ['success' => true, 'data' => $demand];
  52. }
  53. /**
  54. * 完结问题
  55. *
  56. * @param Demand $demand
  57. * @return array
  58. */
  59. public function finishDemand(Demand $demand): array
  60. {
  61. $currentId = Auth::user()['id'];
  62. if ($currentId != $demand['handler']) {
  63. return ['success' => false, 'message' => '非当前问题处理人不能完结'];
  64. }
  65. if ($demand->update(['status' => 2])){
  66. return ['success' => true, 'data' => $demand];
  67. }
  68. else
  69. return ['success' => false, 'message' => '修改失败'];
  70. }
  71. /**
  72. * 认领问题
  73. *
  74. * @param Demand $demand
  75. * @param $handler
  76. * @return array
  77. */
  78. public function claimDemand(Demand $demand, $handler): array
  79. {
  80. if ($demand['status'] != 0)
  81. return ['success' => false, 'message' => '任务已被认领'];
  82. $bool = $demand->update(['handler' => $handler, 'status' => 1]);
  83. if ($bool) {
  84. $demand->loadMissing('initiator', 'handle', 'uploadFile', 'processes');
  85. return ['success' => true, 'data' => $demand];
  86. }
  87. return ['success' => false, 'message' => '任务认领失败'];
  88. }
  89. /**
  90. * 获取问题中未认领和 未完成 的比例
  91. * 未完成 = 未认领 + 未完成
  92. * @return array
  93. */
  94. public function getUnClaimDemandRatio(): array
  95. {
  96. $unClaimDemandCount = Demand::query()->where('status',0)->count();
  97. $unDoneDemandCount = Demand::query()->whereIn('status',[0,1])->count();
  98. return ['unClaimCount' => $unClaimDemandCount,'ClaimCount' => $unDoneDemandCount];
  99. }
  100. }