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

import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.activity.ActivityHallFameInnerConvertor;
import com.red.circle.other.app.inner.service.activity.ActivityHallFameClientService;
import com.red.circle.other.infra.database.rds.entity.activity.ActivityHallFame;
import com.red.circle.other.infra.database.rds.service.activity.ActivityHallFameService;
import com.red.circle.other.inner.model.cmd.activity.ActivityHallFameQryCmd;
import com.red.circle.other.inner.model.dto.activity.ActivityHallFameDTO;
import com.red.circle.tool.core.text.StringUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 名人堂.
 *
 * @author pengliang on 2023/10/9
 */
@Service
@RequiredArgsConstructor
public class ActivityHallFameClientServiceImpl implements ActivityHallFameClientService {

  private final ActivityHallFameService activityHallFameService;
  private final ActivityHallFameInnerConvertor activityHallFameInnerConvertor;

  @Override
  public void saveOrUpdate(ActivityHallFameDTO dto) {
    ActivityHallFame activityHallFame = activityHallFameInnerConvertor.toActivityHallFame(dto);
    if (Objects.isNull(dto.getId())) {
      activityHallFameService.save(activityHallFame);
      return;
    }
    activityHallFameService.updateSelectiveById(activityHallFame);
  }

  @Override
  public void deleteById(Long id) {
    activityHallFameService.removeById(id);
  }

  @Override
  public PageResult<ActivityHallFameDTO> pageQuery(ActivityHallFameQryCmd qryCmd) {
    return activityHallFameService.query()
        .eq(StringUtils.isNotBlank(qryCmd.getSysOrigin()), ActivityHallFame::getSysOrigin,
            qryCmd.getSysOrigin())
        .eq(Objects.nonNull(qryCmd.getDel()), ActivityHallFame::getDel,
            qryCmd.getDel())
        .eq(Objects.nonNull(qryCmd.getDel()), ActivityHallFame::getDel, qryCmd.getDel())
        .orderByDesc(ActivityHallFame::getSort, ActivityHallFame::getCreateTime)
        .page(qryCmd.getPageQuery())
        .convert(activityHallFameInnerConvertor::toActivityHallFameDTO);
  }

}
