package com.zcloud.safetyDutyList.gatewayimpl.tasklist;

import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.FeedbackGateway;
import com.zcloud.safetyDutyList.domain.model.tasklist.FeedbackE;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.FeedbackDO;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.FeedbackRepository;
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;

/**
 * 反馈网关实现类
 * <p>
 * 实现反馈网关接口，负责领域模型(FeedbackE)与数据对象(FeedbackDO)之间的转换，
 * 并委托Repository层完成实际的数据库操作。
 */
@Service
@AllArgsConstructor
public class FeedbackGatewayImpl implements FeedbackGateway {
    private final FeedbackRepository feedbackRepository;

    @Override
    public Boolean add(FeedbackE feedbackE) {
        if (StringUtils.isEmpty(feedbackE.getFeedbackId())) {
            feedbackE.setFeedbackId(Tools.get32UUID());
        }
        FeedbackDO d = new FeedbackDO();
        BeanUtils.copyProperties(feedbackE, d);
        feedbackRepository.save(d);
        feedbackE.setId(d.getId());
        return true;
    }

    @Override
    public Boolean update(FeedbackE feedbackE) {
        FeedbackDO d = new FeedbackDO();
        BeanUtils.copyProperties(feedbackE, d);
        feedbackRepository.updateById(d);
        return true;
    }

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

    @Override
    public FeedbackE getByFeedbackId(String feedbackId) {
        FeedbackDO d = feedbackRepository.getByFeedbackId(feedbackId);
        if (d == null) return null;
        FeedbackE e = new FeedbackE();
        BeanUtils.copyProperties(d, e);
        return e;
    }

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

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

    @Override
    public List<FeedbackE> listByTaskExecutionIdAndPeriodFlag(String taskExecutionId, String feedbackPeriodFlag) {
        List<FeedbackDO> doList = feedbackRepository.listByTaskExecutionIdAndPeriodFlag(taskExecutionId, feedbackPeriodFlag);
        List<FeedbackE> eList = new ArrayList<>();
        if (doList != null) {
            for (FeedbackDO d : doList) {
                FeedbackE e = new FeedbackE();
                BeanUtils.copyProperties(d, e);
                eList.add(e);
            }
        }
        return eList;
    }
}
