package com.taais.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.taais.biz.domain.DataAmplificationTask;
import com.taais.biz.domain.bo.DataAmplificationTaskBo;
import com.taais.biz.domain.vo.DataAmplificationTaskVo;
import com.taais.biz.mapper.DataAmplificationTaskMapper;
import com.taais.biz.service.IDataAmplificationTaskService;
import com.taais.common.core.core.page.PageResult;
import com.taais.common.core.utils.MapstructUtils;
import com.taais.common.orm.core.page.PageQuery;
import com.taais.common.orm.core.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import static com.taais.biz.domain.table.DataAmplificationTaskTableDef.DATA_AMPLIFICATION_TASK;

/**
 * 数据扩增任务 Service 业务层处理
 */
@Service
@Slf4j
public class DataAmplificationTaskServiceImpl extends BaseServiceImpl<DataAmplificationTaskMapper, DataAmplificationTask> implements IDataAmplificationTaskService {

    @Resource
    private DataAmplificationTaskMapper dataAmplificationTaskMapper;

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

    private QueryWrapper buildQueryWrapper(DataAmplificationTaskBo taskBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(DATA_AMPLIFICATION_TASK.SUB_TASK_ID.eq(taskBo.getSubTaskId()));
        queryWrapper.and(DATA_AMPLIFICATION_TASK.NAME.like(taskBo.getName()));
        queryWrapper.and(DATA_AMPLIFICATION_TASK.STATUS.eq(taskBo.getStatus()));
        queryWrapper.and(DATA_AMPLIFICATION_TASK.REMARKS.like(taskBo.getRemarks()));
        return queryWrapper;
    }

    @Override
    public DataAmplificationTaskVo selectById(Long id) {
        return this.getOneAs(query().where(DATA_AMPLIFICATION_TASK.ID.eq(id)), DataAmplificationTaskVo.class);
    }

    @Override
    public List<DataAmplificationTaskVo> selectList(DataAmplificationTaskBo taskBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(taskBo);
        return this.listAs(queryWrapper, DataAmplificationTaskVo.class);
    }

    @Override
    public PageResult<DataAmplificationTaskVo> selectPage(DataAmplificationTaskBo taskBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(taskBo);
        Page<DataAmplificationTaskVo> page = this.pageAs(PageQuery.build(), queryWrapper, DataAmplificationTaskVo.class);
        return PageResult.build(page);
    }

    @Override
    public boolean insert(DataAmplificationTaskBo taskBo) {
        DataAmplificationTask task = MapstructUtils.convert(taskBo, DataAmplificationTask.class);
        return this.save(task);
    }

    @Override
    public DataAmplificationTask insertTask(DataAmplificationTaskBo taskBo) {
        DataAmplificationTask task = MapstructUtils.convert(taskBo, DataAmplificationTask.class);
        this.save(task);
        return task;
    }

    @Override
    public boolean update(DataAmplificationTaskBo taskBo) {
        DataAmplificationTask task = MapstructUtils.convert(taskBo, DataAmplificationTask.class);
        if (ObjectUtil.isNotNull(task) && ObjectUtil.isNotNull(task.getId())) {
            return this.updateById(task);
        }
        return false;
    }

    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
