<?php
/**
 * 更新数据结构
 */
$prefix = \Phpcmf\Service::M()->prefix;
$m = \Phpcmf\Service::M();
$t = \Phpcmf\Service::M('table');

$table = $prefix.'member_setting';
if (!$m->is_table_exists($table)) {
    $t->create_table(
        $table,
        [
            'name' => 'varchar(50) NOT NULL',
            'value' => 'mediumtext NOT NULL',
        ],
        [
            'PRIMARY KEY (`name`)',
        ],
        '用户属性参数表'
    );
    } else {
    if ($m->is_field_exists($table, 'id')) {
        // 处理旧版 id 字段：重建为 name 主键
        $settingQuery = $m->db->table('member_setting')->get();
        $data = $settingQuery ? $settingQuery->getResultArray() : [];
        $t->drop_table($table, true);
        $t->create_table(
            $table,
            [
                'name' => 'varchar(50) NOT NULL',
                'value' => 'mediumtext NOT NULL',
            ],
            [
                'PRIMARY KEY (`name`)',
            ],
            '用户属性参数表'
        );
        if ($data) {
            foreach ($data as $row) {
                $m->table('member_setting')->replace([
                    'name' => $row['name'],
                    'value' => $row['value'],
                ]);
            }
        }
    }
}
$auth2Query = $m->db->table('member_setting')->where('name', 'auth2')->get();
if (!$auth2Query || !$auth2Query->getRowArray()) {
    $m->table('member_setting')->replace([
        'name' => 'auth2',
        'value' => '{"1":{"public":{"home":{"show":"0","is_category":"0"},"form_public":[],"share_category_public":{"show":"1","add":"1","edit":"1","code":"1","verify":"1","exp":"","score":"","money":"","day_post":"","total_post":""},"category_public":[],"mform_public":"","form":null,"share_category":null,"category":null,"mform":null}}}',
    ]);
}

$table = $prefix.'member_paylog';
if ($m->is_table_exists($table)) {
    if (!$m->is_field_exists($table, 'site')) {
        $t->add_field($table, 'site', 'INT(10)', 'NOT NULL', '站点');
    }
    if ($m->is_field_exists($table, 'username')) {
        $t->drop_field($table, 'username');
    }
    if ($m->is_field_exists($table, 'tousername')) {
        $t->drop_field($table, 'tousername');
    }
}

$table = $prefix.'member_verify';
if (!$m->is_table_exists($table)) {
    $t->create_table(
        $table,
        [
            'id' => 'smallint(5) unsigned NOT NULL AUTO_INCREMENT',
            'uid' => 'mediumint(8) unsigned NOT NULL COMMENT \'会员uid\'',
            'tid' => 'tinyint(1) unsigned NOT NULL COMMENT \'类别\'',
            'status' => 'tinyint(1) unsigned NOT NULL COMMENT \'状态\'',
            'content' => 'mediumtext NOT NULL COMMENT \'自定义字段信息\'',
            'result' => 'text NOT NULL COMMENT \'处理结果\'',
            'updatetime' => 'int(10) unsigned NOT NULL COMMENT \'处理时间\'',
            'inputtime' => 'int(10) unsigned NOT NULL COMMENT \'提交时间\'',
        ],
        [
            'PRIMARY KEY (`id`)',
            'KEY `tid` (`tid`)',
            'KEY `uid` (`uid`)',
            'KEY `status` (`status`)',
            'KEY `inputtime` (`inputtime`)',
        ],
        '会员审核表'
    );
}
if ($m->is_table_exists($table) && !$m->is_field_exists($table, 'tid')) {
    $t->add_field($table, 'tid', 'int(1)', 'DEFAULT NULL', '类型');
}

$table = $prefix.'member_group_verify';
if ($m->is_table_exists($table) && !$m->is_field_exists($table, 'price')) {
    $t->add_field($table, 'price', 'decimal(10,2)', 'DEFAULT NULL', '已费用');
}

$table = $prefix.'member_scorelog';
if ($m->is_table_exists($table) && !$m->is_field_exists($table, 'username')) {
    $t->add_field($table, 'username', 'VARCHAR(100)', 'DEFAULT NULL', '');
}

$table = $prefix.'member_notice';
if ($m->is_table_exists($table) && !$m->is_field_exists($table, 'mark')) {
    $t->add_field($table, 'mark', 'VARCHAR(100)', 'DEFAULT NULL', '');
    $t->edit_field($prefix.'member_notice', 'type', 'tinyint(2) unsigned', 'NOT NULL', '类型');
}

$table = $prefix.'member_explog';
if ($m->is_table_exists($table) && !$m->is_field_exists($table, 'username')) {
    $t->add_field($table, 'username', 'VARCHAR(100)', 'DEFAULT NULL', '');
}

$table = $prefix.'member_oauth';
if ($m->is_table_exists($table) && !$m->is_field_exists($table, 'unionid')) {
    $t->add_field($table, 'unionid', 'VARCHAR(100)', 'DEFAULT NULL', '');
}

$table = $prefix.'member_menu';
if ($m->is_table_exists($table) && !$m->is_field_exists($table, 'site')) {
    $t->add_field($table, 'site', 'TEXT', 'NOT NULL', '');
}
if ($m->is_table_exists($table) && !$m->is_field_exists($table, 'client')) {
    $t->add_field($table, 'client', 'TEXT', 'DEFAULT NULL', '');
}

$table = $prefix.'member_level';
if (!$m->is_table_exists($table)) {
    $rt = $t->create_table(
        $table,
        [
            'id' => 'smallint(5) unsigned NOT NULL AUTO_INCREMENT',
            'gid' => 'smallint(5) unsigned NOT NULL COMMENT \'用户id\'',
            'name' => 'varchar(255) NOT NULL COMMENT \'名称\'',
            'stars' => 'int(10) NOT NULL COMMENT \'图标\'',
            'value' => 'int(10) unsigned NOT NULL COMMENT \'升级值要求\'',
            'note' => 'text NOT NULL COMMENT \'备注\'',
            'apply' => 'tinyint(1) unsigned NOT NULL COMMENT \'是否允许升级\'',
            'setting' => 'text NOT NULL COMMENT \'等级配置\'',
            'displayorder' => 'smallint(5) NOT NULL COMMENT \'排序\'',
        ],
        [
            'PRIMARY KEY (`id`)',
            'KEY `value` (`value`)',
            'KEY `apply` (`apply`)',
            'KEY `gid` (`gid`)',
            'KEY `displayorder` (`displayorder`)',
        ],
        '用户组级别表'
    );
    if (empty($rt['code'])) {
        log_message('error', 'Member Update create member_level failed: '.($rt['msg'] ?? ''));
    }
} else {
    $t->edit_field($table, 'stars', 'int(10) unsigned', 'NOT NULL', '图标');
    if (!$m->is_field_exists($table, 'setting')) {
        $t->add_field($table, 'setting', 'TEXT', 'NOT NULL', '');
    }
    if (!$m->is_field_exists($table, 'displayorder')) {
        $t->add_field($table, 'displayorder', 'INT(10)', 'DEFAULT NULL', '排序');
    }
}
