package com.mortals.xhx.module.ent.service.impl;
import com.mortals.framework.model.PageInfo;
import org.springframework.beans.BeanUtils;

import java.util.Map;
import java.util.function.Function;
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.ent.dao.EntLifeMatterDao;
import com.mortals.xhx.module.ent.model.EntLifeMatterEntity;
import com.mortals.xhx.module.ent.service.EntLifeMatterService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.ent.model.EntLifeMatterSubEntity;
import com.mortals.xhx.module.ent.model.EntLifeMatterSubQuery;
import com.mortals.xhx.module.ent.service.EntLifeMatterSubService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
/**
* EntLifeMatterService
* 企业周期事项主 service实现
*
* @author zxfei
* @date 2025-03-15
*/
@Service("entLifeMatterService")
@Slf4j
public class EntLifeMatterServiceImpl extends AbstractCRUDServiceImpl<EntLifeMatterDao, EntLifeMatterEntity, Long> implements EntLifeMatterService {
    @Autowired
    private EntLifeMatterSubService entLifeMatterSubService;

    @Override
    protected void findAfter(EntLifeMatterEntity params,PageInfo pageInfo, Context context, List<EntLifeMatterEntity> list) throws AppException {
        fillSubData(list);
        super.findAfter(params,pageInfo, context, list);
    }
    @Override
    protected void findAfter(EntLifeMatterEntity params, Context context, List<EntLifeMatterEntity> list) throws AppException {
        fillSubData(list);
        super.findAfter(params, context, list);
    }

    private void fillSubData(List<EntLifeMatterEntity> list) {
        List<Long> idList = list.stream().map(i -> i.getId()).collect(Collectors.toList());
        if(ObjectUtils.isEmpty(idList))return;
        Map<Long, List<EntLifeMatterSubEntity>> entLifeMatterSubListMap = entLifeMatterSubService
                .find(new EntLifeMatterSubQuery().masterRecordIdList(idList)).parallelStream()
                .collect(Collectors.groupingBy(EntLifeMatterSubEntity::getMasterRecordId));
        list.forEach(item -> item.setEntLifeMatterSubList(entLifeMatterSubListMap.get(item.getId())));
    }

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

    @Override
    protected void updateAfter(EntLifeMatterEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getEntLifeMatterSubList())){
            Long[] entLifeMatterSubIds = entLifeMatterSubService.find(new EntLifeMatterSubQuery().masterRecordId(entity.getId())).stream().map(EntLifeMatterSubEntity::getId).toArray(Long[]::new);
            entLifeMatterSubService.remove(entLifeMatterSubIds,context);
            entity.getEntLifeMatterSubList().stream().peek(item ->{
                item.setMasterRecordId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            entLifeMatterSubService.save(entity.getEntLifeMatterSubList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<EntLifeMatterSubEntity> entLifeMatterSublist = entLifeMatterSubService.find(new EntLifeMatterSubQuery().masterRecordIdList(Arrays.asList(ids)));
        entLifeMatterSubService.removeList(entLifeMatterSublist,context);
        super.removeAfter(ids, context, result);
    }
}