DemandService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $dirPath = storage_path('app\public\files\issue');
  29. if (!file_exists($dirPath)) {
  30. mkdir($dirPath);
  31. }
  32. $fileName = date('ymd') . '-' . Uuid::uuid1();
  33. $thumbnailName = storage_path('app\public\files\issue\\' . $fileName . '-thumbnail.' . $fileSuffix);
  34. $commonName = storage_path('app\public\files\issue\\' . $fileName . '-common.' . $fileSuffix);
  35. $bulkyName = storage_path('app\public\files\issue\\' . $fileName . '-bulky.' . $fileSuffix);
  36. $result = move_uploaded_file($tmpFile, $bulkyName);
  37. if ($result == false) return ['success' => false, 'data' => '文件上传失败'];
  38. $img = Image::make($bulkyName);
  39. if ($img->height() > $img->width()) {
  40. $img->heighten(250)->save($commonName);
  41. } else {
  42. $img->widen(250)->save($commonName);
  43. }
  44. $img->heighten(28)->save($thumbnailName);
  45. $upLoadFile = $demand->saveFile($fileName, $fileSuffix);
  46. if (!$upLoadFile) return ['success' => false, 'message' => '文件上传失败'];
  47. return ['success' => true, 'data' => $demand];
  48. }
  49. /**
  50. * 完结问题
  51. *
  52. * @param Demand $demand
  53. * @return array
  54. */
  55. public function finishDemand(Demand $demand): array
  56. {
  57. $currentId = Auth::user()['id'];
  58. if ($currentId != $demand['handler']) {
  59. return ['success' => false, 'message' => '非当前问题处理人不能完结'];
  60. }
  61. if ($demand->update(['status' => 2])){
  62. return ['success' => true, 'data' => $demand];
  63. }
  64. else
  65. return ['success' => false, 'message' => '修改失败'];
  66. }
  67. /**
  68. * 认领问题
  69. *
  70. * @param Demand $demand
  71. * @param $handler
  72. * @return array
  73. */
  74. public function claimDemand(Demand $demand, $handler): array
  75. {
  76. if ($demand['status'] != 0)
  77. return ['success' => false, 'message' => '任务已被认领'];
  78. $bool = $demand->update(['handler' => $handler, 'status' => 1]);
  79. if ($bool) {
  80. $demand->loadMissing('initiator', 'handle', 'uploadFile', 'processes');
  81. return ['success' => true, 'data' => $demand];
  82. }
  83. return ['success' => false, 'message' => '任务认领失败'];
  84. }
  85. /**
  86. * 获取问题中未认领和 未完成 的比例
  87. * 未完成 = 未认领 + 未完成
  88. * @return array
  89. */
  90. public function getUnClaimDemandRatio(): array
  91. {
  92. $unClaimDemandCount = Demand::query()->where('status',0)->count();
  93. $unDoneDemandCount = Demand::query()->whereIn('status',[0,1])->count();
  94. return ['unClaimCount' => $unClaimDemandCount,'ClaimCount' => $unDoneDemandCount];
  95. }
  96. }