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.AttendanceGroupDao;
import com.mortals.xhx.module.attendance.model.AttendanceGroupEntity;
import com.mortals.xhx.module.attendance.service.AttendanceGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.attendance.model.AttendanceGroupStaffEntity;
import com.mortals.xhx.module.attendance.model.AttendanceGroupStaffQuery;
import com.mortals.xhx.module.attendance.service.AttendanceGroupStaffService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;

/**
* AttendanceGroupService
* 考勤组信息 service实现
*
* @author zxfei
* @date 2023-04-08
*/
@Service("attendanceGroupService")
public class AttendanceGroupServiceImpl extends AbstractCRUDServiceImpl<AttendanceGroupDao, AttendanceGroupEntity, Long> implements AttendanceGroupService {
    @Autowired
    private AttendanceGroupStaffService attendanceGroupStaffService;
    
    @Override
    protected void saveAfter(AttendanceGroupEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceGroupStaffList())){
            entity.getAttendanceGroupStaffList().stream().peek(item->{
                item.setGroupId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
            }).count();
            attendanceGroupStaffService.save(entity.getAttendanceGroupStaffList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(AttendanceGroupEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceGroupStaffList())){
            Long[] attendanceGroupStaffIds = attendanceGroupStaffService.find(new AttendanceGroupStaffQuery().groupId(entity.getId())).stream().map(AttendanceGroupStaffEntity::getId).toArray(Long[]::new);
            attendanceGroupStaffService.remove(attendanceGroupStaffIds,context);
            entity.getAttendanceGroupStaffList().stream().peek(item ->{
                item.setGroupId(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();
            attendanceGroupStaffService.save(entity.getAttendanceGroupStaffList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<AttendanceGroupStaffEntity> attendanceGroupStafflist = attendanceGroupStaffService.find(new AttendanceGroupStaffQuery().groupIdList(Arrays.asList(ids)));
        attendanceGroupStaffService.removeList(attendanceGroupStafflist,context);
        super.removeAfter(ids, context, result);
    }
}