package com.xueyi.system.digitalmans.manager.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xueyi.common.web.entity.manager.impl.BaseManagerImpl;
import com.xueyi.system.digitalmans.domain.dto.DmBroadcastDto;
import com.xueyi.system.digitalmans.domain.merge.DmBroadcastResourceMerge;
import com.xueyi.system.digitalmans.domain.model.DmBroadcastConverter;
import com.xueyi.system.digitalmans.domain.po.DmBroadcastPo;
import com.xueyi.system.digitalmans.domain.query.DmBroadcastQuery;
import com.xueyi.system.digitalmans.manager.IDmBroadcastManager;
import com.xueyi.system.digitalmans.mapper.DmBroadcastMapper;
import com.xueyi.system.digitalmans.mapper.merge.DmBroadcastResourceMergeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 播报管理 数据封装层处理
 *
 * @author xueyi
 */
@Component
public class DmBroadcastManager extends BaseManagerImpl<DmBroadcastQuery, DmBroadcastDto, DmBroadcastPo, DmBroadcastMapper, DmBroadcastConverter> implements IDmBroadcastManager {

    private static final Logger log = LoggerFactory.getLogger(DmBroadcastManager.class);
    @Autowired
    private DmBroadcastResourceMergeMapper broadcastResourceMergeMapper;


    @Override
    public void addBroadcastResourceMerge(Long broadcastId, List<DmBroadcastResourceMerge> merges) {
        List<DmBroadcastResourceMerge> broadcastResourceMerges = new ArrayList<DmBroadcastResourceMerge>();
        if (merges != null && merges.size() > 0) {
            for (DmBroadcastResourceMerge merge : merges) {
                merge.setBroadcastId(broadcastId);
                broadcastResourceMerges.add(merge);
            }
        }

        if (broadcastResourceMerges.size() > 0) {
            broadcastResourceMergeMapper.insertBatch(broadcastResourceMerges);
        }
    }

    @Override
    public void editBroadcastResourceMerge(Long broadcastId, List<DmBroadcastResourceMerge> merges) {
        List<DmBroadcastResourceMerge> broadcastResourceMerges = new ArrayList<DmBroadcastResourceMerge>();
        List<DmBroadcastResourceMerge> mergeDtos = selectBroadcastResourceMerge(broadcastId);
        if (merges != null && merges.size() > 0) {
            for (DmBroadcastResourceMerge merge : merges) {
                merge.setBroadcastId(broadcastId);
                broadcastResourceMerges.add(merge);
            }
        }
        List<Long> idList = mergeDtos.stream()
                .map(DmBroadcastResourceMerge::getId)
                .collect(Collectors.toList());

        log.info("editBroadcastResourceMerge idList:{}",idList);
        List<Long> newList = broadcastResourceMerges.stream()
                .map(DmBroadcastResourceMerge::getId)
                .collect(Collectors.toList());

        log.info("editBroadcastResourceMerge newList:{}",newList);

//        idList.removeAll(newList);//从原图片列表中取新列表的差集进行删除
        //批量删除
        if (idList.size() > 0) {
            broadcastResourceMergeMapper.deleteBatchIds(idList);
        }
        //批量新增
        if (broadcastResourceMerges.size() > 0) {
            broadcastResourceMergeMapper.insertBatch(broadcastResourceMerges);
        }
    }

    @Override
    public List<DmBroadcastResourceMerge> selectBroadcastResourceMerge(Long broadcastId) {
        List<DmBroadcastResourceMerge> merges = broadcastResourceMergeMapper.selectList(Wrappers.<DmBroadcastResourceMerge>query().lambda()
                .eq(DmBroadcastResourceMerge::getBroadcastId, broadcastId)
                .orderByAsc(DmBroadcastResourceMerge::getId));

        return merges;
    }

}