package com.zcloud.safetyDutyList.gatewayimpl.tasklist;

import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskExecutionGateway;
import com.zcloud.safetyDutyList.domain.model.tasklist.TaskExecutionE;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.TaskExecutionDO;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.TaskExecutionRepository;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class TaskExecutionGatewayImpl implements TaskExecutionGateway {

    private final TaskExecutionRepository taskExecutionRepository;

    @Override
    public Boolean add(TaskExecutionE taskExecutionE) {
        if (StringUtils.isEmpty(taskExecutionE.getTaskExecutionId())) {
            taskExecutionE.setTaskExecutionId(Tools.get32UUID());
        }
        TaskExecutionDO d = new TaskExecutionDO();
        BeanUtils.copyProperties(taskExecutionE, d);
        taskExecutionRepository.save(d);
        taskExecutionE.setId(d.getId());
        return true;
    }

    @Override
    public Boolean batchAdd(List<TaskExecutionE> taskExecutionEList) {
        if (taskExecutionEList == null || taskExecutionEList.isEmpty()) return true;
        for (TaskExecutionE e : taskExecutionEList) {
            add(e);
        }
        return true;
    }

    @Override
    public Boolean update(TaskExecutionE taskExecutionE) {
        TaskExecutionDO d = new TaskExecutionDO();
        BeanUtils.copyProperties(taskExecutionE, d);
        taskExecutionRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deleteById(Long id) {
        return taskExecutionRepository.removeById(id);
    }

    @Override
    public TaskExecutionE getByTaskExecutionId(String taskExecutionId) {
        TaskExecutionDO d = taskExecutionRepository.getByTaskExecutionId(taskExecutionId);
        if (d == null) return null;
        TaskExecutionE e = new TaskExecutionE();
        BeanUtils.copyProperties(d, e);
        return e;
    }

    @Override
    public List<TaskExecutionE> listByTaskIssueId(String taskIssueId) {
        List<TaskExecutionDO> doList = taskExecutionRepository.listByTaskIssueId(taskIssueId);
        List<TaskExecutionE> eList = new ArrayList<>();
        if (doList != null) {
            for (TaskExecutionDO d : doList) {
                TaskExecutionE e = new TaskExecutionE();
                BeanUtils.copyProperties(d, e);
                eList.add(e);
            }
        }
        return eList;
    }

    @Override
    public List<TaskExecutionE> listByTaskListId(String taskListId) {
        List<TaskExecutionDO> doList = taskExecutionRepository.listByTaskListId(taskListId);
        List<TaskExecutionE> eList = new ArrayList<>();
        if (doList != null) {
            for (TaskExecutionDO d : doList) {
                TaskExecutionE e = new TaskExecutionE();
                BeanUtils.copyProperties(d, e);
                eList.add(e);
            }
        }
        return eList;
    }

    @Override
    public List<TaskExecutionE> listByTaskDetailId(String taskDetailId) {
        List<TaskExecutionDO> doList = taskExecutionRepository.listByTaskDetailId(taskDetailId);
        List<TaskExecutionE> eList = new ArrayList<>();
        if (doList != null) {
            for (TaskExecutionDO d : doList) {
                TaskExecutionE e = new TaskExecutionE();
                BeanUtils.copyProperties(d, e);
                eList.add(e);
            }
        }
        return eList;
    }
}
