<?php

declare(strict_types=1);

use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Migration\AbstractMigration;

final class CreateChannelFinancialRecord extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {
        $exists = $this->hasTable('channel_financial_record');
        if (!$exists) {
            $table = $this->table('channel_financial_record', ['comment' => '渠道财务记录']);
            $table->addColumn('department_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => true, 'null' => false, 'default' => 0, 'comment' => '部门/渠道id'])
                ->addColumn('player_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => true, 'null' => false, 'default' => 0, 'comment' => '玩家id'])
                ->addColumn('target', 'string', ['limit' => 50, 'null' => false, 'default' => '', 'comment' => '資料表'])
                ->addColumn('target_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => true, 'null' => false, 'default' => 0, 'comment' => '資料id'])
                ->addColumn('action', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => false, 'default' => 0, 'comment' => '操作'])
                ->addColumn('tradeno', 'string', ['limit' => 100, 'null' => false, 'default' => '', 'comment' => '單號'])
                ->addColumn('user_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'null' => false, 'default' => 0, 'comment' => '管理员id'])
                ->addColumn('user_name', 'string', ['limit' => 120, 'null' => false, 'default' => '', 'comment' => '管理员名称'])
                ->addColumn('created_at', 'datetime', ['comment' => '创建时间'])
                ->addColumn('updated_at', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['department_id'], ['name' => 'idx_department_id'])
                ->addIndex(['user_id'], ['name' => 'idx_user_id'])
                ->create();
        }
    }
}
