import { AutoWrite, HpsfirIoCContext, Service } from 'hpsfir-ioc';
import { Log, Logger } from 'hpsfir-log';
import { Transaction, HpsfirOrmQuery, HPSFIR_ORM_ORDER_BY_E } from 'hpsfir-orm';
import { TestPo } from '@model/po';
import { PageQueryVo, PageVo, TestAdminVo } from '@model/vo';
import { BaseSnowflakeService } from 'hpsfir-managebase';
import { typeCast } from 'hpsfir-ts-extends';
import { HpsfirControllerError } from 'hpsfir-controller';
import { TestDao } from '@dao';

@Service()
export class TestService {

  @Log(Symbol(TestService.name))
  private logger!: Logger;

  @AutoWrite()
  private baseSnowflakeService!: BaseSnowflakeService;

  @AutoWrite()
  private testDao!: TestDao;

  /**
   * 获取test
   */
  @Transaction()
  public async findTest(
    conditions: Partial<TestAdminVo>,
    ctx?: HpsfirIoCContext,
  ) {
    const query = new HpsfirOrmQuery<TestPo>();
    query.everyEqOrLike(typeCast<TestPo>(conditions));
    query.orderBy(HPSFIR_ORM_ORDER_BY_E.ASC, 'createTime');

    const testItem = await this.testDao.findQuery(query, ctx);
    return testItem;
  }

  /**
   * 获取test列表
   */
  @Transaction()
  public async listTest(
    conditions: Partial<TestAdminVo>,
    ctx?: HpsfirIoCContext,
  ) {
    const query = new HpsfirOrmQuery<TestPo>();
    query.everyEqOrLike(typeCast<TestPo>(conditions));
    query.orderBy(HPSFIR_ORM_ORDER_BY_E.ASC, 'createTime');

    const tests = (await this.testDao.listQuery(query, ctx))?.map((item) => {
      const testAdminVo = typeCast<TestAdminVo>(item);
      return testAdminVo;
    }) || [];

    return tests;
  }

  /**
  * 获取test列表(分页)
  */
  @Transaction()
  public async pageTest(
    conditions: Partial<TestAdminVo>,
    pageQuery: PageQueryVo,
    ctx?: HpsfirIoCContext,
  ) {
    const res = new PageVo<TestAdminVo>();

    // 创建查询条件
    const query = new HpsfirOrmQuery<TestPo>();
    query.everyEqOrLike(typeCast<TestPo>(conditions));
    query.orderBy(HPSFIR_ORM_ORDER_BY_E.ASC, 'createTime');

    // 查询总记录数
    const total = await this.testDao.countQuery(query, ctx);
    res.total = total || 0;

    // 使用ORM的分页查询
    const page = {
      page: pageQuery.page,
      pageSize: pageQuery.pageSize,
    };
    const testPos = await this.testDao.pageQuery(query, page, ctx);

    // 转换为VO对象
    res.data = [];
    if (testPos && testPos.length > 0) {
      for (const item of testPos) {
        res.data.push(typeCast<TestAdminVo>(item));
      }
    }

    return res;
  }

  /**
   * 添加test
   */
  @Transaction()
  public async addTest(
    testAdminVo: TestAdminVo,
    ctx?: HpsfirIoCContext,
  ) {
    const testPo = typeCast<TestPo>(testAdminVo);
    testPo.id = this.baseSnowflakeService.generateId();
    testPo.createTime = new Date();
    await this.testDao.save(testPo, ctx);

    return testPo;
  }

  /**
  * 删除test
  */
  @Transaction()
  public async delTest(
    testId: number,
    ctx?: HpsfirIoCContext,
  ) {
    const testPo = await this.testDao.findByPrimaryKey(testId, ctx);
    if (!testPo) {
      this.logger.errorX('删除test，test不存在', 'test', testId);
      throw new HpsfirControllerError(400);
    }
    await this.testDao.deleteByPrimaryKey(testId, ctx);
  }

  /**
  * 更新test
  */
  @Transaction()
  public async updateTest(testAdminVo: Partial<TestAdminVo>, ctx?: HpsfirIoCContext) {
    if (!testAdminVo.id) {
      this.logger.errorX('更新test，id不存在', 'test', testAdminVo);
      throw new HpsfirControllerError(400);
    }
    const testPo = await this.testDao.findByPrimaryKey(testAdminVo.id, ctx);
    if (!testPo) {
      this.logger.errorX('更新test，test不存在', 'test', testAdminVo);
      throw new HpsfirControllerError(400);
    }

    if (testAdminVo.name) {
      testPo.name = testAdminVo.name;
    }
    if (testAdminVo.password) {
      testPo.password = testAdminVo.password;
    }


    await this.testDao.updateByPrimaryKey(testPo, ctx);
  }

}
