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

import com.mortals.xhx.module.appointment.model.AppointmentConstraintEntity;
import com.mortals.xhx.module.appointment.model.AppointmentConstraintQuery;
import com.mortals.xhx.module.care.model.CareConstraintQuery;
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.care.dao.CareConstraintDao;
import com.mortals.xhx.module.care.model.CareConstraintEntity;
import com.mortals.xhx.module.care.service.CareConstraintService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.care.model.CareConstraintTrackEntity;
import com.mortals.xhx.module.care.model.CareConstraintTrackQuery;
import com.mortals.xhx.module.care.service.CareConstraintTrackService;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.Arrays;
import java.util.List;

/**
 * CareConstraintService
 * 主动关怀服务约束 service实现
 *
 * @author zxfei
 * @date 2023-04-09
 */
@Service("careConstraintService")
public class CareConstraintServiceImpl extends AbstractCRUDServiceImpl<CareConstraintDao, CareConstraintEntity, Long> implements CareConstraintService {
    @Autowired
    private CareConstraintTrackService careConstraintTrackService;


    @Override
    protected void saveBefore(CareConstraintEntity entity, Context context) throws AppException {
        int count = this.count(new CareConstraintQuery().idCard(entity.getIdCard()), context);
        if (count > 0) {
            throw new AppException("添加人员已经存在，idCard:" + entity.getIdCard());
        }
        entity.setOperator(context.getUser() == null ? "" : context.getUser().getRealName());
        super.saveBefore(entity, context);
    }

    @Override
    protected void updateBefore(CareConstraintEntity entity, Context context) throws AppException {
        CareConstraintEntity appointmentConstraintEntity = this.selectOne(new CareConstraintQuery().idCard(entity.getIdCard()));
        if (ObjectUtils.isEmpty(appointmentConstraintEntity)) {
            entity.setId(appointmentConstraintEntity.getId());
        }
        entity.setOperator(context.getUser() == null ? "" : context.getUser().getRealName());
        super.updateBefore(entity, context);
    }

    @Override
    protected void saveAfter(CareConstraintEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getCareConstraintTrackList())) {
            entity.getCareConstraintTrackList().stream().peek(item -> {
                item.setCareId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            careConstraintTrackService.save(entity.getCareConstraintTrackList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(CareConstraintEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getCareConstraintTrackList())) {
            Long[] careConstraintTrackIds = careConstraintTrackService.find(new CareConstraintTrackQuery().careId(entity.getId())).stream().map(CareConstraintTrackEntity::getId).toArray(Long[]::new);
            careConstraintTrackService.remove(careConstraintTrackIds, context);
            entity.getCareConstraintTrackList().stream().peek(item -> {
                item.setCareId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            careConstraintTrackService.save(entity.getCareConstraintTrackList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<CareConstraintTrackEntity> careConstraintTracklist = careConstraintTrackService.find(new CareConstraintTrackQuery().careIdList(Arrays.asList(ids)));
        careConstraintTrackService.removeList(careConstraintTracklist, context);
        super.removeAfter(ids, context, result);
    }
}