package com.mortals.xhx.module.feedback.service.impl;
import com.mortals.xhx.module.feedback.model.*;
import com.mortals.xhx.module.feedback.service.FeedbackStaffService;
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.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.feedback.service.FeedbackQuestionService;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
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;
    @Autowired
    private FeedbackStaffService feedbackStaffService;

    @Override
    protected void saveBefore(FeedbackEntity entity, Context context) throws AppException {
        super.saveBefore(entity,context);
        if(!ObjectUtils.isEmpty(entity.getStaffList())){
            entity.setInviteNum(entity.getStaffList().size());
        }
    }

    @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());
        }
        if(!ObjectUtils.isEmpty(entity.getStaffList())){
            List<FeedbackStaffEntity> staffEntityList = new ArrayList<>();
            for(Long staffId:entity.getStaffList()){
                FeedbackStaffEntity feedbackStaffEntity = new FeedbackStaffEntity();
                feedbackStaffEntity.setFeedbackId(entity.getId());
                feedbackStaffEntity.setStaffId(staffId);
                feedbackStaffEntity.setCreateTime(new Date());
                feedbackStaffEntity.setCreateUserId(this.getContextUserId(context));
                staffEntityList.add(feedbackStaffEntity);
            }
            feedbackStaffService.save(staffEntityList,context);
        }
        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());
        }
        if(!ObjectUtils.isEmpty(entity.getStaffList())){
            Long[] feedbackStaffIds = feedbackStaffService.find(new FeedbackStaffQuery().feedbackId(entity.getId())).stream().map(FeedbackStaffEntity::getId).toArray(Long[]::new);
            feedbackStaffService.remove(feedbackStaffIds,context);
            List<FeedbackStaffEntity> staffEntityList = new ArrayList<>();
            for(Long staffId:entity.getStaffList()){
                FeedbackStaffEntity feedbackStaffEntity = new FeedbackStaffEntity();
                feedbackStaffEntity.setFeedbackId(entity.getId());
                feedbackStaffEntity.setStaffId(staffId);
                feedbackStaffEntity.setCreateTime(new Date());
                feedbackStaffEntity.setCreateUserId(this.getContextUserId(context));
                staffEntityList.add(feedbackStaffEntity);
            }
            feedbackStaffService.save(staffEntityList,context);
        }
        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);
        List<FeedbackStaffEntity> staffEntityList = feedbackStaffService.find(new FeedbackStaffQuery().feedbackIdList(Arrays.asList(ids)));
        feedbackStaffService.removeList(staffEntityList,context);
        super.removeAfter(ids, context, result);
    }
}