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

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.other.infra.database.rds.entity.user.user.UserConfessionRecord;
import com.red.circle.other.infra.database.rds.service.user.user.UserConfessionRecordService;
import com.red.circle.other.app.inner.convertor.user.UserConfessionRecordConvertor;
import com.red.circle.other.inner.model.cmd.user.UserConfessionRecordQryCmd;
import com.red.circle.other.inner.model.dto.user.UserConfessionDTO;
import com.red.circle.other.inner.model.dto.user.UserConfessionRecordDTO;
import com.red.circle.other.app.inner.service.user.user.UserConfessionRecordClientService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author zongpubin on 2024/1/30
 */
@Service
@RequiredArgsConstructor
public class UserConfessionRecordClientServiceImpl implements UserConfessionRecordClientService {

  private final UserConfessionRecordService userConfessionRecordService;
  private final UserConfessionRecordConvertor userConfessionRecordConvertor;

  @Override
  public PageResult<UserConfessionRecordDTO> pageListQuery(UserConfessionRecordQryCmd query) {
    PageResult<UserConfessionRecord> pageResult = userConfessionRecordService.pageListQuery(query);
    return pageResult.convert(userConfessionRecordConvertor::toUserConfessionRecordDTO);
  }

  @Override
  public void auditUserConfession(UserConfessionDTO dto) {
    ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
        updateInfo(userConfessionRecordConvertor.toUserConfessionRecord(dto)));
  }

  private boolean updateInfo(UserConfessionRecord record) {
    return userConfessionRecordService.update()
        .set(UserConfessionRecord::getStatus, record.getStatus())
        .eq(UserConfessionRecord::getId, record.getId())
        .set(UserConfessionRecord::getUpdateUser, record.getUpdateUser())
        .set(UserConfessionRecord::getUpdateTime, record.getUpdateTime())
        .last(PageConstant.LIMIT_ONE)
        .execute();
  }
}
