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

import com.mortals.xhx.module.care.model.CareConfigQuery;
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.CareConfigDao;
import com.mortals.xhx.module.care.model.CareConfigEntity;
import com.mortals.xhx.module.care.service.CareConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.care.model.CareConfigTimesEntity;
import com.mortals.xhx.module.care.model.CareConfigTimesQuery;
import com.mortals.xhx.module.care.service.CareConfigTimesService;
import org.springframework.util.ObjectUtils;

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

/**
 * CareConfigService
 * 主动关怀配置 service实现
 *
 * @author zxfei
 * @date 2023-04-09
 */
@Service("careConfigService")
public class CareConfigServiceImpl extends AbstractCRUDServiceImpl<CareConfigDao, CareConfigEntity, Long> implements CareConfigService {
    @Autowired
    private CareConfigTimesService careConfigTimesService;


    @Override
    protected void saveAfter(CareConfigEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getCareConfigTimesList())) {
            careConfigTimesService.remove(new CareConfigTimesQuery().careCfgId(entity.getId()),context);
            entity.getCareConfigTimesList().stream().peek(item -> {
                item.setCareCfgId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            careConfigTimesService.save(entity.getCareConfigTimesList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(CareConfigEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getCareConfigTimesList())) {
            careConfigTimesService.remove(new CareConfigTimesQuery().careCfgId(entity.getId()),context);
            entity.getCareConfigTimesList().stream().peek(item -> {
                item.setCareCfgId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            careConfigTimesService.save(entity.getCareConfigTimesList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<CareConfigTimesEntity> careConfigTimeslist = careConfigTimesService.find(new CareConfigTimesQuery().careCfgIdList(Arrays.asList(ids)));
        careConfigTimesService.removeList(careConfigTimeslist, context);
        super.removeAfter(ids, context, result);
    }
}