package com.zcloud.safetyDutyList.command.tasklist;

import com.alibaba.cola.exception.BizException;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.FeedbackGateway;
import com.zcloud.safetyDutyList.domain.model.tasklist.FeedbackE;
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.FeedbackCO;
import com.zcloud.safetyDutyList.dto.tasklist.FeedbackEditCmd;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.FeedbackDO;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.FeedbackRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class FeedbackEditExe {
    private final FeedbackGateway feedbackGateway;
    private final FeedbackRepository feedbackRepository;

    public FeedbackCO execute(FeedbackEditCmd cmd) {
        FeedbackE feedbackE = feedbackGateway.getByFeedbackId(cmd.getFeedbackId());
        if (feedbackE == null) {
            throw new BizException("反馈记录不存在");
        }
        feedbackE.setFeedbackContent(cmd.getFeedbackContent());
        boolean res = feedbackGateway.update(feedbackE);
        if (!res) {
            throw new BizException("反馈编辑失败");
        }

        FeedbackDO feedbackDO = feedbackRepository.getByFeedbackId(cmd.getFeedbackId());
        FeedbackCO co = new FeedbackCO();
        BeanUtils.copyProperties(feedbackDO, co);
        return co;
    }
}
