package cn.com.poc.ai_data_audit.rest.impl;

import java.util.List;
import javax.annotation.Resource;

import cn.com.poc.common.utils.BlContext;
import org.springframework.stereotype.Component;
import cn.com.poc.ai_data_audit.rest.BizAiDataAuditTaskRest;
import cn.com.poc.ai_data_audit.service.BizAiDataAuditTaskService;
import cn.com.poc.ai_data_audit.dto.BizAiDataAuditTaskDto;
import cn.com.poc.ai_data_audit.entity.BizAiDataAuditTaskEntity;
import cn.com.poc.ai_data_audit.convert.BizAiDataAuditTaskConvert;

import java.util.stream.Collectors;

import org.springframework.util.Assert;

@Component
public class BizAiDataAuditTaskRestImpl implements BizAiDataAuditTaskRest {

    @Resource
    private BizAiDataAuditTaskService bizAiDataAuditTaskService;

    @Override
    public List<BizAiDataAuditTaskDto> getTaskList() throws Exception {
        BizAiDataAuditTaskEntity bizAiDataAuditTaskEntity = new BizAiDataAuditTaskEntity();

        Integer memberId = BlContext.getCurrentUser().getUserId().intValue();
        if (memberId == null) {
            return null;
        }
        bizAiDataAuditTaskEntity.setMemberId(memberId);
        List<BizAiDataAuditTaskEntity> bizAiDataAuditTaskEntityList = bizAiDataAuditTaskService.findByExample(bizAiDataAuditTaskEntity, null);
        for (BizAiDataAuditTaskEntity aiDataAuditTaskEntity : bizAiDataAuditTaskEntityList) {
            aiDataAuditTaskEntity.setAuditResult(null);
        }
        return bizAiDataAuditTaskEntityList.stream().map(BizAiDataAuditTaskConvert::entityToDto).collect(Collectors.toList());
    }

    @Override
    public BizAiDataAuditTaskDto getByTaskId(Integer taskId) throws Exception {
        Assert.notNull(taskId, "taskId can not be null");
        BizAiDataAuditTaskEntity bizAiDataAuditTaskEntity = bizAiDataAuditTaskService.get(taskId);
        if (bizAiDataAuditTaskEntity == null) {
            throw new Exception("the task can not be found");
        }
        return BizAiDataAuditTaskConvert.entityToDto(bizAiDataAuditTaskEntity);
    }
}