package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.MarketExtensionLinkRepository;
import com.crm.manager.service.MarketExtensionLinkService;
import com.crm.rely.backend.core.dto.market.extension.link.MarketExtensionLinkDto;
import com.crm.rely.backend.core.entity.market.extension.link.MarketExtensionLinkSaveEntity;
import com.crm.rely.backend.core.pojo.table.MarketExtensionLinkTable;
import com.crm.rely.backend.core.pojo.table.MarketExtensionTable;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MarketExtensionLinkServiceImpl implements MarketExtensionLinkService {

    @Autowired
    private MarketExtensionLinkRepository repository;

    @Override
    public List<MarketExtensionLinkDto> getByExtensionId(Long informationId) throws ServiceException {
        List<MarketExtensionLinkTable> tables = repository.getAllByExtensionId(informationId);
        if (tables != null && tables.size() > 0) {
            List<MarketExtensionLinkDto> tagDtos = new ArrayList<>(tables.size());
            for (MarketExtensionLinkTable webInformationTagTable : tables) {
                MarketExtensionLinkDto linkDto = new MarketExtensionLinkDto();
                linkDto.setExtensionId(webInformationTagTable.getExtensionId());
                linkDto.setLink(webInformationTagTable.getLink());
                linkDto.setSourceCode(webInformationTagTable.getSourceCode());
                linkDto.setSourceName(webInformationTagTable.getSourceName());
                linkDto.setSourceNameEn(webInformationTagTable.getSourceNameEn());
                linkDto.setId(webInformationTagTable.getId());
                tagDtos.add(linkDto);
            }
            return tagDtos;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(List<MarketExtensionLinkSaveEntity> entities, MarketExtensionTable table) throws ServiceException {
        if (entities == null || entities.size() <= 0) {
            List<MarketExtensionLinkTable> webInformationTagRepositoryAllByExtensionId =
                    repository.findAllByExtensionId(table.getId());
            if (webInformationTagRepositoryAllByExtensionId != null && webInformationTagRepositoryAllByExtensionId.size() > 0) {
                repository.delete(webInformationTagRepositoryAllByExtensionId);
            }
            return;
        }

        List<MarketExtensionLinkTable> saveTables = new ArrayList<>(entities.size());
        List<MarketExtensionLinkTable> tables = repository.findAllByExtensionId(table.getId());
        List<MarketExtensionLinkTable> deleteTables;
        //判断是否存在 如果存在 进行判断是否修改和删除 如果不存在则直接进入添加环节
        if (tables != null || tables.size() > 0) {
            deleteTables = new ArrayList<>(tables.size());
            for (MarketExtensionLinkTable linkTable : tables) {
                //标识数据库数据是否需要删除
                boolean isDelete = true;
                int size = entities.size();
                for (int i = 0; i < size; i++) {

                    MarketExtensionLinkSaveEntity entity = entities.get(i);

                    if (entity != null && linkTable.getId().equals(entity.getId())) {
                        BeanUtils.copyProperties(entity, linkTable);
                        linkTable.setModifyIp(entity.getIp());
                        linkTable.setModifyTime(entity.getTime());
                        linkTable.setModifyUser(entity.getUserId());
                        saveTables.add(linkTable);
                        isDelete = false;
                        //移除需要修改的实体 并结束循环
                        entities.remove(i);
                        if (table.getLink().contains("?")) {
                            linkTable.setLink(table.getLink() + "&ex=" + entity.getSourceCode());
                        } else {
                            linkTable.setLink(table.getLink() + "?ex=" + entity.getSourceCode());
                        }

                        break;
                    }
                }
                //循环判断中 原数据库数据不在这次修改或者添加范围 则这个记录是需要删除 添加到删除记录中
                if (isDelete) {
                    deleteTables.add(linkTable);
                }
            }
        } else {
            deleteTables = null;
        }
        //剩下的这些为需要添加的实体
        for (MarketExtensionLinkSaveEntity entity : entities) {
            entity.setId(null);
            MarketExtensionLinkTable marketExtensionLinkTable = new MarketExtensionLinkTable();
            BeanUtils.copyProperties(entity, marketExtensionLinkTable);
            marketExtensionLinkTable.setExtensionId(table.getId());
            if (table.getLink().contains("?")) {
                marketExtensionLinkTable.setLink(table.getLink() + "&ex=" + entity.getSourceCode());
            } else {
                marketExtensionLinkTable.setLink(table.getLink() + "?ex=" + entity.getSourceCode());
            }
            marketExtensionLinkTable.setAddIp(entity.getIp());
            marketExtensionLinkTable.setAddTime(entity.getTime());
            marketExtensionLinkTable.setAddUser(entity.getUserId());
            saveTables.add(marketExtensionLinkTable);
        }

        if (saveTables != null && saveTables.size() > 0) {
            repository.save(saveTables);
        }

        if (deleteTables != null && deleteTables.size() > 0) {
            repository.delete(deleteTables);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> extensionIds) throws ServiceException {
        if (extensionIds != null && extensionIds.size() > 0) {
            List<MarketExtensionLinkTable> webInformationTagRepositoryAllByExtensionIdIn = repository.findAllByExtensionIdIn(extensionIds);
            if (webInformationTagRepositoryAllByExtensionIdIn != null && webInformationTagRepositoryAllByExtensionIdIn.size() > 0) {
                repository.delete(webInformationTagRepositoryAllByExtensionIdIn);
            }
        }
    }

    @Override
    public List<MarketExtensionLinkTable> getByExtensionIdIn(List<Long> extensionIds) throws ServiceException {
        List<MarketExtensionLinkTable> tables = repository.getAllByExtensionIdIn(extensionIds);

        if (tables == null || tables.size() <= 0) {
            return null;
        }
        return tables;
    }
}
