<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class DistAppearanceTemplate extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'dist_appearance_template';

    /*
     * 把原始模板复制给分销商
     */
    public static function copyTemplateToDist($appearanceId,$distId) {
        $appearanceId = intval($appearanceId);
        $distId = intval($distId);
        $count = self::where('dist_id', $distId)->where('appearance_id', $appearanceId)->count();
        if ($count > 0) {
            return;
        }
        $baseDistId = config('dictionary.base_dist_id');
        //复制
        DB::statement("
INSERT INTO `dist_appearance_template` (`dist_id`, `appearance_id`, `file_name`, `file_path`, `content`, `created_at`, `updated_at`,  `template_code`)
SELECT {$distId}, `appearance_id`, `file_name`, `file_path`, `content`, NOW(), NOW(), `template_code`
FROM `dist_appearance_template`
WHERE `dist_id` = {$baseDistId} AND `appearance_id` = {$appearanceId};
        ");
    }

    public static function deleteTemplates($appearanceId,$distId) {
        self::where('dist_id',$distId)->where('appearance_id',$appearanceId)->delete();
        return true;
    }

}
