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

    @Override
    protected void updateAfter(FeedbackEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getFeedbackQuestionList())){
            Long[] feedbackQuestionIds = feedbackQuestionService.find(new FeedbackQuestionQuery().feedbackId(entity.getId())).stream().map(FeedbackQuestionEntity::getId).toArray(Long[]::new);
            feedbackQuestionService.remove(feedbackQuestionIds,context);
            entity.getFeedbackQuestionList().stream().peek(item ->{
                item.setFeedbackId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            feedbackQuestionService.save(entity.getFeedbackQuestionList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<FeedbackQuestionEntity> feedbackQuestionlist = feedbackQuestionService.find(new FeedbackQuestionQuery().feedbackIdList(Arrays.asList(ids)));
        feedbackQuestionService.removeList(feedbackQuestionlist,context);
        super.removeAfter(ids, context, result);
    }
}