package com.mortals.xhx.module.attendance.service.impl;

import com.mortals.framework.model.PageInfo;
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;
import java.util.stream.Collectors;

/**
 * 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 findAfter(AttendanceRecordEntity params, PageInfo pageInfo, Context context, List<AttendanceRecordEntity> list) throws AppException {
        list.stream().map(item -> {
            List<AttendanceRecordDetailEntity> attendanceRecordDetailEntities = attendanceRecordDetailService.find(new AttendanceRecordDetailQuery().recordId(item.getId()));
            item.setAttendanceRecordDetailList(attendanceRecordDetailEntities);
            return item;
        }).collect(Collectors.toList());


        super.findAfter(params, pageInfo, context, list);
    }

    @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.setCreateUserId(this.getContextUserId(context));
                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.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                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);
    }
}
