package com.red.circle.other.infra.database.rds.service.dynamic.impl;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.other.infra.database.rds.dao.dynamic.TopDynamicDAO;
import com.red.circle.other.infra.database.rds.entity.dynamic.TopDynamic;
import com.red.circle.other.infra.database.rds.service.dynamic.TopDynamicService;
import com.red.circle.other.inner.model.cmd.dynamic.TopDynamicCmd;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统置顶动态 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2022-06-17
 */
@Service
@RequiredArgsConstructor
public class TopDynamicServiceImpl extends BaseServiceImpl<TopDynamicDAO, TopDynamic> implements
    TopDynamicService {


  @Override
  public List<TopDynamic> listBySysOrigin(SysOriginPlatformEnum sysOrigin) {
    return query().eq(TopDynamic::getSysOrigin, sysOrigin.name())
        .orderByDesc(TopDynamic::getWeights)
        .last(PageConstant.formatLimit(300))
        .list();
  }


  @Override
  public void setUpTop(TopDynamicCmd param) {

    delete().eq(TopDynamic::getDynamicId, param.getDynamicId()).last(PageConstant.LIMIT_ONE)
        .execute();

    TopDynamic topDynamic = new TopDynamic();
    topDynamic.setDynamicId(param.getDynamicId());
    topDynamic.setSysOrigin(param.getSysOrigin());
    topDynamic.setWeights(param.getWeights());
    topDynamic.setCreateTime(TimestampUtils.now());
    topDynamic.setCreateUser(param.getOperatorId());
    topDynamic.setUpdateTime(TimestampUtils.now());
    save(topDynamic);

  }

  @Override
  public void closeTop(Long dynamicId) {

    if (Objects.isNull(dynamicId)) {
      return;
    }
    delete().eq(TopDynamic::getDynamicId, dynamicId).last(PageConstant.LIMIT_ONE).execute();
  }

  @Override
  public void deleteByDynamicIds(Set<Long> ids) {

    if (CollectionUtils.isEmpty(ids)) {
      return;
    }

    delete().in(TopDynamic::getDynamicId, ids).execute();


  }


}
