package com.mortals.xhx.module.appointment.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.appointment.dao.AppointmentConfigDao;
import com.mortals.xhx.module.appointment.model.AppointmentConfigEntity;
import com.mortals.xhx.module.appointment.service.AppointmentConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.appointment.model.AppointmentConfigTimesEntity;
import com.mortals.xhx.module.appointment.model.AppointmentConfigTimesQuery;
import com.mortals.xhx.module.appointment.service.AppointmentConfigTimesService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;

/**
* AppointmentConfigService
* 预约自动签到配置 service实现
*
* @author zxfei
* @date 2023-04-09
*/
@Service("appointmentConfigService")
public class AppointmentConfigServiceImpl extends AbstractCRUDServiceImpl<AppointmentConfigDao, AppointmentConfigEntity, Long> implements AppointmentConfigService {
    @Autowired
    private AppointmentConfigTimesService appointmentConfigTimesService;
    
    @Override
    protected void saveAfter(AppointmentConfigEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAppointmentConfigTimesList())){
            entity.getAppointmentConfigTimesList().stream().peek(item->{
                item.setAotoCheckCfgId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
            }).count();
            appointmentConfigTimesService.save(entity.getAppointmentConfigTimesList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(AppointmentConfigEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getAppointmentConfigTimesList())){
            Long[] appointmentConfigTimesIds = appointmentConfigTimesService.find(new AppointmentConfigTimesQuery().aotoCheckCfgId(entity.getId())).stream().map(AppointmentConfigTimesEntity::getId).toArray(Long[]::new);
            appointmentConfigTimesService.remove(appointmentConfigTimesIds,context);
            entity.getAppointmentConfigTimesList().stream().peek(item ->{
                item.setAotoCheckCfgId(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();
            appointmentConfigTimesService.save(entity.getAppointmentConfigTimesList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<AppointmentConfigTimesEntity> appointmentConfigTimeslist = appointmentConfigTimesService.find(new AppointmentConfigTimesQuery().aotoCheckCfgIdList(Arrays.asList(ids)));
        appointmentConfigTimesService.removeList(appointmentConfigTimeslist,context);
        super.removeAfter(ids, context, result);
    }
}