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.AttendanceGroupFixedworkDao;
import com.mortals.xhx.module.attendance.model.AttendanceGroupFixedworkEntity;
import com.mortals.xhx.module.attendance.service.AttendanceGroupFixedworkService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.attendance.model.AttendanceGroupFixedworkSpecialEntity;
import com.mortals.xhx.module.attendance.model.AttendanceGroupFixedworkSpecialQuery;
import com.mortals.xhx.module.attendance.service.AttendanceGroupFixedworkSpecialService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;

/**
* AttendanceGroupFixedworkService
* 考勤组考勤固定工时 service实现
*
* @author zxfei
* @date 2023-04-08
*/
@Service("attendanceGroupFixedworkService")
public class AttendanceGroupFixedworkServiceImpl extends AbstractCRUDServiceImpl<AttendanceGroupFixedworkDao, AttendanceGroupFixedworkEntity, Long> implements AttendanceGroupFixedworkService {
    @Autowired
    private AttendanceGroupFixedworkSpecialService attendanceGroupFixedworkSpecialService;
    
    @Override
    protected void saveAfter(AttendanceGroupFixedworkEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceGroupFixedworkSpecialList())){
            entity.getAttendanceGroupFixedworkSpecialList().stream().peek(item->{
                item.setFixedWorkId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
            }).count();
            attendanceGroupFixedworkSpecialService.save(entity.getAttendanceGroupFixedworkSpecialList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(AttendanceGroupFixedworkEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceGroupFixedworkSpecialList())){
            Long[] attendanceGroupFixedworkSpecialIds = attendanceGroupFixedworkSpecialService.find(new AttendanceGroupFixedworkSpecialQuery().fixedWorkId(entity.getId())).stream().map(AttendanceGroupFixedworkSpecialEntity::getId).toArray(Long[]::new);
            attendanceGroupFixedworkSpecialService.remove(attendanceGroupFixedworkSpecialIds,context);
            entity.getAttendanceGroupFixedworkSpecialList().stream().peek(item ->{
                item.setFixedWorkId(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();
            attendanceGroupFixedworkSpecialService.save(entity.getAttendanceGroupFixedworkSpecialList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<AttendanceGroupFixedworkSpecialEntity> attendanceGroupFixedworkSpeciallist = attendanceGroupFixedworkSpecialService.find(new AttendanceGroupFixedworkSpecialQuery().fixedWorkIdList(Arrays.asList(ids)));
        attendanceGroupFixedworkSpecialService.removeList(attendanceGroupFixedworkSpeciallist,context);
        super.removeAfter(ids, context, result);
    }
}