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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.model.MatterQuery;
import com.mortals.xhx.module.matter.service.MatterService;
import com.mortals.xhx.module.site.dao.SiteMatterDao;
import com.mortals.xhx.module.site.model.SiteMatterEntity;
import com.mortals.xhx.module.site.service.SiteMatterService;
import com.mortals.xhx.module.site.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * SiteMatterService
 * 站点事项 service实现
 *
 * @author zxfei
 * @date 2022-01-12
 */
@Service("siteMatterService")
@Slf4j
public class SiteMatterServiceImpl extends AbstractCRUDServiceImpl<SiteMatterDao, SiteMatterEntity, Long> implements SiteMatterService {

    @Autowired
    private MatterService matterService;
    @Autowired
    private SiteService siteService;

    @Override
    protected void findAfter(SiteMatterEntity params, PageInfo pageInfo, Context context, List<SiteMatterEntity> list) throws AppException {

        if (!ObjectUtils.isEmpty(list)) {
            List<Long> matterIdlist = list.stream().map(item -> item.getMatterId()).collect(Collectors.toList());
            Map<Long, MatterEntity> matterEntityMap = matterService.find(new MatterQuery().idList(matterIdlist)).parallelStream().collect(Collectors.toMap(x -> x.getId(), y -> y, (o, n) -> n));
            list.forEach(item -> {
                if (!ObjectUtils.isEmpty(item.getMatterId())) {
                    MatterEntity matterEntity = matterEntityMap.get(item.getMatterId());
                    if (!ObjectUtils.isEmpty(matterEntity)) {
                        item.setBelongDept(matterEntity.getBelongDept());
                        item.setWindowToTheSceneNum(matterEntity.getWindowToTheSceneNum());
                        item.setOnlineToTheSceneNum(matterEntity.getOnlineToTheSceneNum());
                        item.setDeptCode(matterEntity.getDeptCode());
                        item.setAreaCode(matterEntity.getAreaCode());
                    }
                }
            });
        }
        super.findAfter(params, pageInfo, context, list);
    }

    /**
     * @param entity
     * @param context
     * @throws AppException
     */
    @Override
    protected void updateBefore(SiteMatterEntity entity, Context context) throws AppException {

    }

    /**
     * @param entity
     * @param context
     * @throws AppException
     */
    @Override
    protected void saveBefore(SiteMatterEntity entity, Context context) throws AppException {
        super.saveBefore(entity, context);
    }


}