<?php

namespace app\model;

use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

/**
 * 文档模型字段
 * Class ArchivesModelField
 * @package app\model
 */
class ArchivesModelField extends Base
{
    public const VIRTUAL_YES = 1;//虚拟字段 是
    public const VIRTUAL_NO  = 0;//虚拟字段 否

    /**
     * 模型添加字段列表
     *
     * @param  int  $modelId
     * @param  string  $modelName
     * @throws Exception
     */
    public static function setFieldList(int $modelId, string $modelName)
    {
        if (self::where('model_id', $modelId)->count() <= 0) {
            $rs     = Db::query("SHOW FULL COLUMNS FROM ".Archives::ORIGINAL_TABLE);
            $insert = [];
            foreach ($rs as $val) {
                $arr             = [];
                $arr['model_id'] = $modelId;
                $arr['model']    = $modelName;
                $arr['name']     = $val['Field'];
                $arr['title']    = $val['Comment'];
                $arr['remark']   = $val['Comment'];
                $insert[]        = $arr;
            }

            (new self())->saveAll($insert);
        }
    }

    /**
     * 同步字段
     *
     * @param  int  $modelId
     * @param  string  $modelName
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function syncFieldList(int $modelId, string $modelName)
    {
        $rs           = Db::query("SHOW FULL COLUMNS FROM ".Archives::ORIGINAL_TABLE);
        $oldFieldList = self::where('model_id', $modelId)->where('is_virtual', self::VIRTUAL_NO)->select();
        $oldFields    = $oldFieldList->column('name');


        $newestFields = [];
        foreach ($rs as $val) {
            $newestFields[] = $val['Field'];
        }

        //待删除字段
        $delete = array_diff($oldFields, $newestFields);

        //待新增字段
        $needInsertFields = array_diff($newestFields, $oldFields);

        $insert = [];//新增字段
        foreach ($rs as $val) {
            if (in_array($val['Field'], $needInsertFields)) {
                $arr             = [];
                $arr['model_id'] = $modelId;
                $arr['model']    = $modelName;
                $arr['name']     = $val['Field'];
                $arr['title']    = $val['Comment'];
                $arr['remark']   = $val['Comment'];
                $insert[]        = $arr;
            }
        }

        (new self())->saveAll($insert);
        (new self())->whereIn('name', $delete)->delete();
    }

    /**
     * 各栏目[模型] 可展示字段列表
     *
     * @return array
     */
    public static function showFieldList(): array
    {
        $list = self::alias('amf')
            ->leftJoin('archives_category ac', 'ac.model_id = amf.model_id')
            ->field('amf.*, ac.id as category_id, ac.title as category_title')
            ->where('amf.status', self::COMMON_ON)
            ->where('ac.id', '>', 0)
            ->select();

        $res = [];

        $list = $list->toArray();
        foreach ($list as $item) {
            if (!isset($res[$item['category_id']])) {
                $res[$item['category_id']] = [];
            }

            $res[$item['category_id']][] = $item['name'];
        }

        return $res;
    }
}