<?php

namespace App\Admin\Repositories;

use App\Models\DistAppearanceTemplateLog as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class DistAppearanceTemplateLog extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    /*
     *  添加模版修改日志
     *  $currentContent 修改后的内容
     *  $previousContent 修改前的内容
     */
    public static function insertLog($appearanceId,$distId,$fileName,$filePath,$templateCode,$currentContent,$previousContent)
    {
        $model = new Model();
        $model->dist_id = $distId;
        $model->file_name = $fileName;
        $model->file_path = $filePath;
        $model->appearance_id = $appearanceId;
        $model->template_code = $templateCode;
        $model->current_content = $currentContent;
        $model->previous_content = $previousContent;
        $model->version = generateVersionNumber();
        $model->save();
    }

    /*
     *  获取模版修改日志
     */
    public static function fetchTemplateLogs($appearanceId,$distId,$templateCode)
    {
        $model = new Model();
        $model = $model->where('appearance_id','=',$appearanceId)->where('dist_id','=',$distId)->where('template_code','=',$templateCode)->orderBy('id','desc')->select('id','version','created_at')->get();
        return $model;
    }

    /*
     *  获取单条模版修改日志内容
     */
    public static function fetchTemplateLogContent($logId)
    {
        $model = new Model();
        $model = $model->where('id','=',$logId)->first();
        return $model;
    }
    /*
     * 还原模版
     */
    public static function restoreTemplateLog($logId) {
        $model = new Model();
        $model = $model->where('id', '=', $logId)->first();
        if ($model) {
            $appearanceId = $model->appearance_id;
            $distId = $model->dist_id;
            $templateCode = $model->template_code;
            $content = $model->previous_content;
            DistAppearanceTemplate::saveContent($appearanceId, $distId, $templateCode, $content);
        }
    }

}