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.FeedbackQuestionDao;
import com.mortals.xhx.module.feedback.model.FeedbackQuestionEntity;
import com.mortals.xhx.module.feedback.service.FeedbackQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.feedback.model.FeedbackOptionEntity;
import com.mortals.xhx.module.feedback.model.FeedbackOptionQuery;
import com.mortals.xhx.module.feedback.service.FeedbackOptionService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
/**
* FeedbackQuestionService
* 绩效反馈问题 service实现
*
* @author zxfei
* @date 2023-07-05
*/
@Service("feedbackQuestionService")
@Slf4j
public class FeedbackQuestionServiceImpl extends AbstractCRUDServiceImpl<FeedbackQuestionDao, FeedbackQuestionEntity, Long> implements FeedbackQuestionService {
    @Autowired
    private FeedbackOptionService feedbackOptionService;
    
    @Override
    protected void saveAfter(FeedbackQuestionEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getFeedbackOptionList())){
            entity.getFeedbackOptionList().stream().peek(item->{
                item.setQuestionId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                feedbackOptionService.save(item);
            }).count();
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(FeedbackQuestionEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getFeedbackOptionList())){
            Long[] feedbackOptionIds = feedbackOptionService.find(new FeedbackOptionQuery().questionId(entity.getId())).stream().map(FeedbackOptionEntity::getId).toArray(Long[]::new);
            feedbackOptionService.remove(feedbackOptionIds,context);
            entity.getFeedbackOptionList().stream().peek(item ->{
                item.setQuestionId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            feedbackOptionService.save(entity.getFeedbackOptionList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<FeedbackOptionEntity> feedbackOptionlist = feedbackOptionService.find(new FeedbackOptionQuery().questionIdList(Arrays.asList(ids)));
        feedbackOptionService.removeList(feedbackOptionlist,context);
        super.removeAfter(ids, context, result);
    }
}