package com.taais.generator.service;

import com.taais.common.core.core.page.PageResult;
import com.taais.common.core.core.text.Convert;
import com.taais.common.orm.core.page.PageQuery;
import com.taais.common.orm.core.service.impl.BaseServiceImpl;
import com.taais.common.security.utils.LoginHelper;
import com.taais.generator.domain.GenTableColumn;
import com.taais.generator.mapper.GenTableColumnMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.taais.generator.domain.table.GenTableColumnTableDef.GEN_TABLE_COLUMN;

/**
 * 业务字段 服务层实现
 *
 * @author wgk
 */
@Service
public class GenTableColumnServiceImpl extends BaseServiceImpl<GenTableColumnMapper, GenTableColumn> implements IGenTableColumnService {
    @Resource
    private GenTableColumnMapper genTableColumnMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(GEN_TABLE_COLUMN);
    }

    /**
     * 查询业务字段列表
     *
     * @param tableId 业务字段编号
     * @return 业务字段集合
     */
    @Override
    public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
            .from(GEN_TABLE_COLUMN)
            .where(GEN_TABLE_COLUMN.TABLE_ID.eq(tableId))
            .orderBy(GEN_TABLE_COLUMN.SORT.asc());
        return this.list(queryWrapper);
    }

    /**
     * 查询业务字段列表
     *
     * @param tableId 业务字段编号
     * @return 业务字段集合
     */
    @Override
    public PageResult<GenTableColumn> selectPageGenTableColumnListByTableId(Long tableId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
            .from(GEN_TABLE_COLUMN)
            .where(GEN_TABLE_COLUMN.TABLE_ID.eq(tableId))
            .orderBy(GEN_TABLE_COLUMN.SORT.asc());
        Page<GenTableColumn> page = genTableColumnMapper.paginateWithRelationsAs(PageQuery.build(), queryWrapper, GenTableColumn.class);
        return PageResult.build(page);
    }

    /**
     * 新增业务字段
     *
     * @param genTableColumn 业务字段信息
     * @return 结果
     */
    @Override
    public int insertGenTableColumn(GenTableColumn genTableColumn) {
        Long loginUserId = LoginHelper.getUserId();
        Date createTime = new Date();
        genTableColumn.setCreateBy(loginUserId);
        genTableColumn.setCreateTime(createTime);
        genTableColumn.setUpdateBy(loginUserId);
        genTableColumn.setUpdateTime(createTime);

        return genTableColumnMapper.insertSelective(genTableColumn);
    }

    /**
     * 修改业务字段
     *
     * @param genTableColumn 业务字段信息
     * @return 结果
     */
    @Override
    public int updateGenTableColumn(GenTableColumn genTableColumn) {
        Long loginUserId = LoginHelper.getUserId();
        Date createTime = new Date();
        genTableColumn.setUpdateBy(loginUserId);
        genTableColumn.setUpdateTime(createTime);

        return genTableColumnMapper.update(genTableColumn);
    }

    /**
     * 删除业务字段对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGenTableColumnByIds(String ids) {
        return genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids));
    }
}
