package com.mortals.xhx.module.care.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.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 {
        entity.setOperator(context.getUser()==null?"":context.getUser().getRealName());
        super.saveBefore(entity, context);
    }

    @Override
    protected void updateBefore(CareConstraintEntity entity, Context context) throws AppException {
        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.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                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.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();
            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);
    }
}