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.AttendanceGroupFreeworkDao;
import com.mortals.xhx.module.attendance.model.AttendanceGroupFreeworkEntity;
import com.mortals.xhx.module.attendance.service.AttendanceGroupFreeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.attendance.model.AttendanceGroupFreeworkSpecialEntity;
import com.mortals.xhx.module.attendance.model.AttendanceGroupFreeworkSpecialQuery;
import com.mortals.xhx.module.attendance.service.AttendanceGroupFreeworkSpecialService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;

/**
* AttendanceGroupFreeworkService
* 考勤组考勤自由工时配置 service实现
*
* @author zxfei
* @date 2023-04-08
*/
@Service("attendanceGroupFreeworkService")
public class AttendanceGroupFreeworkServiceImpl extends AbstractCRUDServiceImpl<AttendanceGroupFreeworkDao, AttendanceGroupFreeworkEntity, Long> implements AttendanceGroupFreeworkService {
    @Autowired
    private AttendanceGroupFreeworkSpecialService attendanceGroupFreeworkSpecialService;
    
    @Override
    protected void saveAfter(AttendanceGroupFreeworkEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceGroupFreeworkSpecialList())){
            entity.getAttendanceGroupFreeworkSpecialList().stream().peek(item->{
                item.setFreeWorkId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
            }).count();
            attendanceGroupFreeworkSpecialService.save(entity.getAttendanceGroupFreeworkSpecialList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(AttendanceGroupFreeworkEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAttendanceGroupFreeworkSpecialList())){
            Long[] attendanceGroupFreeworkSpecialIds = attendanceGroupFreeworkSpecialService.find(new AttendanceGroupFreeworkSpecialQuery().freeWorkId(entity.getId())).stream().map(AttendanceGroupFreeworkSpecialEntity::getId).toArray(Long[]::new);
            attendanceGroupFreeworkSpecialService.remove(attendanceGroupFreeworkSpecialIds,context);
            entity.getAttendanceGroupFreeworkSpecialList().stream().peek(item ->{
                item.setFreeWorkId(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();
            attendanceGroupFreeworkSpecialService.save(entity.getAttendanceGroupFreeworkSpecialList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<AttendanceGroupFreeworkSpecialEntity> attendanceGroupFreeworkSpeciallist = attendanceGroupFreeworkSpecialService.find(new AttendanceGroupFreeworkSpecialQuery().freeWorkIdList(Arrays.asList(ids)));
        attendanceGroupFreeworkSpecialService.removeList(attendanceGroupFreeworkSpeciallist,context);
        super.removeAfter(ids, context, result);
    }
}