package com.red.circle.other.app.inner.service.dynamic.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.dynamic.DynamicInnerConvertor;
import com.red.circle.other.app.inner.service.dynamic.DynamicTagClientService;
import com.red.circle.other.infra.database.cache.service.other.DynamicCacheService;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicTag;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicTagService;
import com.red.circle.other.inner.model.cmd.dynamic.DynamicTagCmd;
import com.red.circle.other.inner.model.cmd.dynamic.PageDynamicTagQryCmd;
import com.red.circle.other.inner.model.dto.dynamic.DynamicTagDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 动态-标签 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2022-06-30
 */
@Service
@RequiredArgsConstructor
public class DynamicTagClientServiceImpl implements DynamicTagClientService {

  private final DynamicTagService dynamicTagService;
  private final DynamicCacheService dynamicCacheService;
  private final DynamicInnerConvertor dynamicInnerConvertor;

  @Override
  public void addOrUpdate(DynamicTagCmd dynamicTag) {
    dynamicTagService.addOrUpdate(dynamicInnerConvertor.toDynamicTag(dynamicTag));
    dynamicCacheService.remove();
  }


  @Override
  public PageResult<DynamicTagDTO> pageData(PageDynamicTagQryCmd query) {
    return dynamicTagService.pageData(query).convert(dynamicInnerConvertor::toDynamicTagDTO);
  }

  @Override
  public Map<String, DynamicTagDTO> getAllDynamicTag() {
    LambdaQueryWrapper<DynamicTag> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(DynamicTag::getDel,Boolean.FALSE);
    List<DynamicTag> dynamicTagList = dynamicTagService.list(queryWrapper);
    if(CollectionUtils.isEmpty(dynamicTagList)){
      return Collections.emptyMap();
    }
    return dynamicTagList.stream().collect(Collectors.toMap(DynamicTag::getTag, dynamicInnerConvertor::toDynamicTagDTO));
  }
}

