package com.xjrsoft.xjrsoftboot;

import cn.hutool.core.util.IdUtil;
import cn.hutool.db.Entity;
import com.xjrsoft.XjrSoftApplication;
import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = XjrSoftApplication.class)
public class SqlRunnerAdapterTest {

    @Transactional
    @Test
    public void DynamicUpdateTest() {
        String tableName = "room";
        Entity params = Entity.create(tableName);
        params.set("gender","SB10003");
        params.set("bed_count",10);


        Entity where = Entity.create(tableName);
        where.set("id","1745648843609927682");
        where.set("create_user_id","14954812448325");

        Boolean result = SqlRunnerAdapter.db().dynamicUpdate(tableName,params,where);
        System.out.println(result);
    }

    @Transactional
    @Test
    public void DynamicInsertTest() {
        String tableName = "official_document_received";
        long keyValue = IdUtil.getSnowflakeNextId();
        Entity params = Entity.create(tableName);
        params.set("communication_number","12312");
        params.set("emergency_level","emergency_level2");
        params.set("document_level","document_level1");
        params.set("communication_org","asdfasd");
        params.set("received_date","2024-02-26");
        params.set("checkout_time","2024-02-26 00:06:00");
        params.set("file_id","1761947003676405762");
        params.set("received_title","发生的发生的newnewnewnnewnewnewewewewe");
        params.set("received_type","rt_administrative");
        params.set("received_number","123123");
        params.set("id",keyValue);
        params.set("create_user_id","1000000000000000000");
        params.set("create_date","2024-02-27 17:43:59.140274");
        params.set("delete_mark","0");
        params.set("enabled_mark","1");

        Boolean result = SqlRunnerAdapter.db().dynamicInsert(tableName,params);
        System.out.println(result);
    }

    @Transactional
    @Test
    public void DynamicDeleteTest() {
        String tableName = "room";
        Entity where = Entity.create(tableName);
        where.set("id","1745648843609927682");
        where.set("create_user_id","14954812448325");

        Boolean result = SqlRunnerAdapter.db().dynamicDelete(tableName,where);
        System.out.println(result);
    }

    @Transactional
    @Test
    public void DynamicTest() throws Exception {
        String tableName = "room";
        Entity where = Entity.create(tableName);
        where.set("id","1745648844138409985");
        where.set("create_user_id","14954812448325");

        Boolean result = SqlRunnerAdapter.db().dynamicDelete(tableName,where);
        System.out.println(result);

        Entity params = Entity.create(tableName);
        params.set("gender","SB10003");
        params.set("bed_count",10);


        Entity where2 = Entity.create(tableName);
        where.set("id","1745648843890946050");
        where.set("create_user_id","14954812448325");

        Boolean result2 = SqlRunnerAdapter.db().dynamicUpdate(tableName,params,where2);
        System.out.println(result2);

        throw new Exception("错误回滚");
    }
}
