package com.mortals.xhx.module.attendance.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.attendance.dao.AttendanceClassDao;
import com.mortals.xhx.module.attendance.model.AttendanceClassEntity;
import com.mortals.xhx.module.attendance.service.AttendanceClassService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.attendance.model.AttendanceClassDetailEntity;
import com.mortals.xhx.module.attendance.model.AttendanceClassDetailQuery;
import com.mortals.xhx.module.attendance.service.AttendanceClassDetailService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
/**
* AttendanceClassService
* 考勤班次信息 service实现
*
* @author zxfei
* @date 2023-04-08
*/
@Service("attendanceClassService")
public class AttendanceClassServiceImpl extends AbstractCRUDServiceImpl<AttendanceClassDao, AttendanceClassEntity, Long> implements AttendanceClassService {
    @Autowired
    private AttendanceClassDetailService attendanceClassDetailService;
    
    @Override
    protected void saveAfter(AttendanceClassEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceClassDetailList())){
            entity.getAttendanceClassDetailList().stream().peek(item->{
                item.setClassId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
            }).count();
            attendanceClassDetailService.save(entity.getAttendanceClassDetailList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(AttendanceClassEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceClassDetailList())){
            Long[] attendanceClassDetailIds = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(entity.getId())).stream().map(AttendanceClassDetailEntity::getId).toArray(Long[]::new);
            attendanceClassDetailService.remove(attendanceClassDetailIds,context);
            entity.getAttendanceClassDetailList().stream().peek(item ->{
                item.setClassId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
                item.setUpdateUser(context.getUser().getLoginName());
                item.setUpdateUserId(context.getUser().getId());
                item.setUpdateTime(new Date());
            }).count();
            attendanceClassDetailService.save(entity.getAttendanceClassDetailList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<AttendanceClassDetailEntity> attendanceClassDetaillist = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classIdList(Arrays.asList(ids)));
        attendanceClassDetailService.removeList(attendanceClassDetaillist,context);
        super.removeAfter(ids, context, result);
    }
}