<?php

namespace App\Services;

use App\Traits\ServiceAppAop;
use App\Demand;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Auth;
use Intervention\Image\Facades\Image;
use Ramsey\Uuid\Uuid;

class DemandService
{
    use ServiceAppAop;

    protected $modelClass = Demand::class;

    /**
     * saveUPLoadFile
     * @param Demand $demand
     * @param UploadedFile $file
     * @return array
     */
    public function saveUPLoadFile(Demand $demand, UploadedFile $file): array
    {
        $tmpFile = $file->getRealPath();

        if (!$demand->uploadFile()) return ['success' => false, 'message' => '该需求已有描述图片'];
        if (!$file) return ['success' => false, 'message' => '上传图片不得为空'];
        if (!$file->isValid()) return ['success' => false, 'message' => '找不到上传图片'];
        if (!is_uploaded_file($tmpFile)) return ['success' => false, 'message' => '文件错误'];
        if ($file->getSize() > 5 * 1024 * 1024) return ['success' => false, 'message' => '文件不能大于5MB'];
        $fileSuffix = $file->getClientOriginalExtension();

        $dirPath = storage_path('app\public\files\issue');
        if (!file_exists($dirPath)) {
            mkdir($dirPath);
        }

        $fileName = date('ymd') . '-' . Uuid::uuid1();

        $thumbnailName = storage_path('app\public\files\issue\\' . $fileName . '-thumbnail.' . $fileSuffix);
        $commonName = storage_path('app\public\files\issue\\' . $fileName . '-common.' . $fileSuffix);
        $bulkyName = storage_path('app\public\files\issue\\' . $fileName . '-bulky.' . $fileSuffix);

        $result = move_uploaded_file($tmpFile, $bulkyName);

        if ($result == false) return ['success' => false, 'data' => '文件上传失败'];

        $img = Image::make($bulkyName);

        if ($img->height() > $img->width()) {
            $img->heighten(250)->save($commonName);
        } else {
            $img->widen(250)->save($commonName);
        }
        $img->heighten(28)->save($thumbnailName);

        $upLoadFile = $demand->saveFile($fileName, $fileSuffix);

        if (!$upLoadFile) return ['success' => false, 'message' => '文件上传失败'];

        return ['success' => true, 'data' => $demand];
    }

    /**
     * 完结问题
     *
     * @param Demand $demand
     * @return array
     */
    public function finishDemand(Demand $demand): array
    {
        $currentId = Auth::user()['id'];
        if ($currentId != $demand['handler']) {
            return ['success' => false, 'message' => '非当前问题处理人不能完结'];
        }
        if ($demand->update(['status' => 2])){
            return ['success' => true, 'data' => $demand];
        }
        else
            return ['success' => false, 'message' => '修改失败'];
    }

    /**
     * 认领问题
     *
     * @param Demand $demand
     * @param $handler
     * @return array
     */
    public function claimDemand(Demand $demand, $handler): array
    {
        if ($demand['status'] != 0)
            return ['success' => false, 'message' => '任务已被认领'];

        $bool = $demand->update(['handler' => $handler, 'status' => 1]);
        if ($bool) {
            $demand->loadMissing('initiator', 'handle', 'uploadFile', 'processes');
            return ['success' => true, 'data' => $demand];
        }
        return ['success' => false, 'message' => '任务认领失败'];
    }

    /**
     * 获取问题中未认领和 未完成 的比例
     * 未完成 = 未认领 + 未完成
     * @return array
     */
    public function getUnClaimDemandRatio(): array
    {
        $unClaimDemandCount = Demand::query()->where('status',0)->count();
        $unDoneDemandCount = Demand::query()->whereIn('status',[0,1])->count();
        return ['unClaimCount' => $unClaimDemandCount,'ClaimCount' => $unDoneDemandCount];
    }
}
