package com.mortals.xhx.module.perform.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.perform.dao.PerformAttendAppealDao;
import com.mortals.xhx.module.perform.model.PerformAttendAppealEntity;
import com.mortals.xhx.module.perform.service.PerformAttendAppealService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.perform.model.PerformAttendAppealFilesEntity;
import com.mortals.xhx.module.perform.model.PerformAttendAppealFilesQuery;
import com.mortals.xhx.module.perform.service.PerformAttendAppealFilesService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
/**
* PerformAttendAppealService
* 绩效记录申诉信息 service实现
*
* @author zxfei
* @date 2023-07-05
*/
@Service("performAttendAppealService")
@Slf4j
public class PerformAttendAppealServiceImpl extends AbstractCRUDServiceImpl<PerformAttendAppealDao, PerformAttendAppealEntity, Long> implements PerformAttendAppealService {
    @Autowired
    private PerformAttendAppealFilesService performAttendAppealFilesService;
    
    @Override
    protected void saveAfter(PerformAttendAppealEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getPerformAttendAppealFilesList())){
            entity.getPerformAttendAppealFilesList().stream().peek(item->{
                item.setAppealId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            performAttendAppealFilesService.save(entity.getPerformAttendAppealFilesList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(PerformAttendAppealEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getPerformAttendAppealFilesList())){
            Long[] performAttendAppealFilesIds = performAttendAppealFilesService.find(new PerformAttendAppealFilesQuery().appealId(entity.getId())).stream().map(PerformAttendAppealFilesEntity::getId).toArray(Long[]::new);
            performAttendAppealFilesService.remove(performAttendAppealFilesIds,context);
            entity.getPerformAttendAppealFilesList().stream().peek(item ->{
                item.setAppealId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            performAttendAppealFilesService.save(entity.getPerformAttendAppealFilesList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<PerformAttendAppealFilesEntity> performAttendAppealFileslist = performAttendAppealFilesService.find(new PerformAttendAppealFilesQuery().appealIdList(Arrays.asList(ids)));
        performAttendAppealFilesService.removeList(performAttendAppealFileslist,context);
        super.removeAfter(ids, context, result);
    }
}