<?php

namespace App\Admin\Repositories;

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

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


    /*
     * 更新发布版本
     */
    public static function publishVersion($appearanceId,$distId)
    {
        $model = new Model();
        $row = $model->where('appearance_id',$appearanceId)->where('dist_id',$distId)->first();
        if ($row) {
            $row->template_update_code = generateVersionNumber();
            $row->save();
        } else {
            $model->appearance_id = $appearanceId;
            $model->dist_id = $distId;
            $model->template_version = '1.0';
            $model->template_update_code = generateVersionNumber();
            $model->template_local_code = '';
            $model->save();
        }
    }

    /*
     * 发布模版与变量
     */
    public static function publish($appearanceId,$distId) {
        //同步模版到正式表
        DistAppearanceTemplate::syncAppearanceTemplates($appearanceId,$distId);
        //同步变量到正式表
        DistAppearanceVariable::syncAppearanceVariables($appearanceId, $distId);
        //发布版本号
        DistAppearancePublishList::publishVersion($appearanceId,$distId);
        return true;
    }
}
