package com.mortals.xhx.module.feedback.service.impl;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.feedback.dao.FeedbackAnswerDao;
import com.mortals.xhx.module.feedback.model.FeedbackAnswerEntity;
import com.mortals.xhx.module.feedback.service.FeedbackAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.feedback.model.FeedbackAnswerOptionEntity;
import com.mortals.xhx.module.feedback.model.FeedbackAnswerOptionQuery;
import com.mortals.xhx.module.feedback.service.FeedbackAnswerOptionService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
/**
* FeedbackAnswerService
* 绩效问题回答 service实现
*
* @author zxfei
* @date 2023-07-05
*/
@Service("feedbackAnswerService")
@Slf4j
public class FeedbackAnswerServiceImpl extends AbstractCRUDServiceImpl<FeedbackAnswerDao, FeedbackAnswerEntity, Long> implements FeedbackAnswerService {
    @Autowired
    private FeedbackAnswerOptionService feedbackAnswerOptionService;
    
    @Override
    protected void saveAfter(FeedbackAnswerEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getFeedbackAnswerOptionList())){
            entity.getFeedbackAnswerOptionList().stream().peek(item->{
                item.setAnswerId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            feedbackAnswerOptionService.save(entity.getFeedbackAnswerOptionList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(FeedbackAnswerEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getFeedbackAnswerOptionList())){
            Long[] feedbackAnswerOptionIds = feedbackAnswerOptionService.find(new FeedbackAnswerOptionQuery().answerId(entity.getId())).stream().map(FeedbackAnswerOptionEntity::getId).toArray(Long[]::new);
            feedbackAnswerOptionService.remove(feedbackAnswerOptionIds,context);
            entity.getFeedbackAnswerOptionList().stream().peek(item ->{
                item.setAnswerId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            feedbackAnswerOptionService.save(entity.getFeedbackAnswerOptionList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<FeedbackAnswerOptionEntity> feedbackAnswerOptionlist = feedbackAnswerOptionService.find(new FeedbackAnswerOptionQuery().answerIdList(Arrays.asList(ids)));
        feedbackAnswerOptionService.removeList(feedbackAnswerOptionlist,context);
        super.removeAfter(ids, context, result);
    }
}