<?php
namespace App\Services\Apk;

use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Models\ApkModel;
use App\Models\OptionLogModel;
use App\Services\Apk\IApkService;
use Illuminate\Support\Facades\Auth;

class ApkService implements IApkService
{
    private $time;

    public function __construct()
    {
        $this->time = Carbon::now()->format('Y-m-d H:i:s');
    }

    /**
     * apk列表
     * @return array
     * @date 2020/11/25
     * @author live
     */
    public function getApkList($name)
    {
        $pageNum = env('PAGE_NUM', 20);
        $paginateAppends = [];
        $lists = ApkModel::when($name != '', function ($query) use ($name) {
            return $query->where('name', 'like', $name . '%');
        })->select('id', 'name', 'version', 'local_url', 'size', 'origin_name','down','status','remark','created_at')->paginate($pageNum);

        if ($name != '') $paginateAppends['name'] = $name;
        if (!empty($paginateAppends)) $lists->appends($paginateAppends);
        return $lists;
    }

    /**
     * 上传apk
     * @return array
     * @date 2020/11/25
     * @author live
     */
    public function getApkAdd($postData)
    {
        $row = new ApkModel();
        $row->name = $postData['name'];
        $row->version = $postData['version'];
        $row->file_url = '';
        $row->local_url = env('UPLOAD_PATH').$postData['apk'];

        $md5 = MD5($this->time.$row->file_url.$row->local_url.$this->time);
        $row->down_url = '';
        $row->md5 = $md5;
        $row->size = $postData['apk_size'];
        $row->origin_name = $postData['apk_origin_name'];
        $row->remark = $postData['remark'];
        if($row->save()){
            unset($postData['_token']);
            OptionLogModel::getAddLog('apk','add',$postData,Auth::id(),$row->id,'上传apk');
            return true;
        }
        throw new Exception(config('msg.apk.3002'),3002);
    }

    /**
     * 设置apk状态
     * @return array
     * @date 2020/11/26
     * @author live
     */
    public function getApkSetStatus($postData)
    {
        $apk = ApkModel::find($postData['id']);
        if(!$apk) throw new Exception(config('msg.common.1003'),1003);
        $status = 0;
        if($postData['status'] == 1) $status = 1;
        $apk->status = $status;
        if($apk->save()){
            unset($postData['_token']);
            OptionLogModel::getAddLog('apk','update',$postData,Auth::id(),$apk->id,'设置apk状态');
            return $apk->id;
        }
        throw new Exception(config('msg.common.1006'),1006);
    }

    /**
     * 删除apk包
     * @return array
     * @date 2020/11/26
     * @author live
     */
    public function getApkDel($id)
    {
        $apk = ApkModel::where('id',$id)->first();
        if(!$apk) throw new Exception(config('msg.common.1003'),1003);

        if($apk->delete()){
            OptionLogModel::getAddLog('apk','del',['id'=>$id],Auth::id(),$apk->id,'删除apk');
            return true;
        }
        throw new Exception(config('msg.common.1005'),1005);
    }

    /**
     * 编辑apk包
     * @return array
     * @date 2020/11/26
     * @author live
     */
    public function getApkEdit($postData)
    {
        $apk = ApkModel::where('id',$postData['up_id'])->first();
        if(!$apk) throw new Exception(config('msg.common.1003'),1003);

        $apk->name = $postData['name'];
        $apk->version = $postData['version'];
        $apk->remark = $postData['remark'];
        if($apk->save()){
            unset($postData['_token']);
            OptionLogModel::getAddLog('apk','update',$postData,Auth::id(),$apk->id,'编辑apk包');
            return true;
        }
        throw new Exception(config('msg.apk.3002'),3002);
    }

    /**
     * 编辑apk包
     * @return array
     * @date 2020/11/26
     * @author live
     */
    public function getApkDown($md5)
    {
        $apk = ApkModel::where('md5',$md5)->select('id','file_url','local_url','down')->first();
        if(!$apk) return false;
        if($apk->file_url == '') return false;
        if(!file_exists($apk->local_url)) return false;
        $apk->down = $apk->down + 1;
        $apk->save();
        return $apk->file_url;
    }

}
