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.AttendanceRecordDao;
import com.mortals.xhx.module.attendance.model.AttendanceRecordEntity;
import com.mortals.xhx.module.attendance.service.AttendanceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.attendance.model.AttendanceRecordDetailEntity;
import com.mortals.xhx.module.attendance.model.AttendanceRecordDetailQuery;
import com.mortals.xhx.module.attendance.service.AttendanceRecordDetailService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;

/**
* AttendanceRecordService
* 考勤打卡记录信息 service实现
*
* @author zxfei
* @date 2023-04-07
*/
@Service("attendanceRecordService")
public class AttendanceRecordServiceImpl extends AbstractCRUDServiceImpl<AttendanceRecordDao, AttendanceRecordEntity, Long> implements AttendanceRecordService {
    @Autowired
    private AttendanceRecordDetailService attendanceRecordDetailService;
    
    @Override
    protected void saveAfter(AttendanceRecordEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceRecordDetailList())){
            entity.getAttendanceRecordDetailList().stream().peek(item->{
                item.setRecordId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
            }).count();
            attendanceRecordDetailService.save(entity.getAttendanceRecordDetailList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(AttendanceRecordEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceRecordDetailList())){
            Long[] attendanceRecordDetailIds = attendanceRecordDetailService.find(new AttendanceRecordDetailQuery().recordId(entity.getId())).stream().map(AttendanceRecordDetailEntity::getId).toArray(Long[]::new);
            attendanceRecordDetailService.remove(attendanceRecordDetailIds,context);
            entity.getAttendanceRecordDetailList().stream().peek(item ->{
                item.setRecordId(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();
            attendanceRecordDetailService.save(entity.getAttendanceRecordDetailList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<AttendanceRecordDetailEntity> attendanceRecordDetaillist = attendanceRecordDetailService.find(new AttendanceRecordDetailQuery().recordIdList(Arrays.asList(ids)));
        attendanceRecordDetailService.removeList(attendanceRecordDetaillist,context);
        super.removeAfter(ids, context, result);
    }
}