package com.red.circle.other.app.inner.service.material.props.impl;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.service.material.props.RoomThemeUserCustomizeClientService;
import com.red.circle.other.infra.convertor.room.RoomThemeUserInfraConvertor;
import com.red.circle.other.infra.database.rds.entity.props.RoomThemeUserCustomize;
import com.red.circle.other.infra.database.rds.service.props.RoomThemeUserCustomizeService;
import com.red.circle.other.inner.model.cmd.material.RoomThemeUserCustomizeApprovalCmd;
import com.red.circle.other.inner.model.cmd.material.RoomThemeUserCustomizeOpsPageQryCmd;
import com.red.circle.other.inner.model.dto.material.RoomThemeUserCustomizeDTO;
import com.red.circle.tool.core.text.StringUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户房间主题 服务类.
 *
 * @author pengliang on 2023/12/4
 */
@Service
@RequiredArgsConstructor
public class RoomThemeUserCustomizeClientServiceImpl implements
    RoomThemeUserCustomizeClientService {

  private final RoomThemeUserCustomizeService roomThemeUserCustomizeService;
  private final RoomThemeUserInfraConvertor roomThemeUserInfraConvertor;

  @Override
  public PageResult<RoomThemeUserCustomizeDTO> pageOps(
      RoomThemeUserCustomizeOpsPageQryCmd pageQryCmd) {
    return roomThemeUserCustomizeService.query()
        .eq(StringUtils.isNotBlank(pageQryCmd.getSysOrigin()), RoomThemeUserCustomize::getSysOrigin,
            pageQryCmd.getSysOrigin())
        .eq(StringUtils.isNotBlank(pageQryCmd.getThemeStatus()),
            RoomThemeUserCustomize::getThemeStatus,
            pageQryCmd.getThemeStatus())
        .eq(Objects.nonNull(pageQryCmd.getUserId()), RoomThemeUserCustomize::getUserId,
            pageQryCmd.getUserId())
        .ge(Objects.nonNull(pageQryCmd.getStartDateTime()), RoomThemeUserCustomize::getCreateTime,
            pageQryCmd.getStartDateTime())
        .le(Objects.nonNull(pageQryCmd.getEndDateTime()), RoomThemeUserCustomize::getCreateTime,
            pageQryCmd.getEndDateTime())
        .orderByDesc(RoomThemeUserCustomize::getCreateTime)
        .page(pageQryCmd.getPageQuery())
        .convert(roomThemeUserInfraConvertor::toRoomThemeUserCustomize);
  }

  @Override
  public void updateApprovalStatus(RoomThemeUserCustomizeApprovalCmd cmd) {
    roomThemeUserCustomizeService.update()
        .set(RoomThemeUserCustomize::getThemeStatus, cmd.getApprovalStatus())
        .set(RoomThemeUserCustomize::getUpdateUser, cmd.getApprovalUserId())
        .in(RoomThemeUserCustomize::getId, cmd.getContentIds())
        .execute();
  }

}
