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


import com.red.circle.framework.dto.PageResult;
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.DynamicReportDAO;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicReport;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicReportService;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDynamicReportCmd;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDynamicReportQryCmd;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 投诉动态 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2022-04-18
 */
@Service
public class DynamicReportServiceImpl extends
    BaseServiceImpl<DynamicReportDAO, DynamicReport> implements DynamicReportService {

  @Override
  public Boolean exist(Long dynamicContentId, Long userId) {

    return Optional.ofNullable(query()
            .eq(DynamicReport::getReportUserId, userId)
            .eq(DynamicReport::getApprovalStatus, 0)
            .eq(DynamicReport::getDynamicContentId, dynamicContentId)
            .last(PageConstant.LIMIT_ONE)
            .getOne()).map(obj -> Boolean.TRUE)
        .orElse(Boolean.FALSE);

  }

  @Override
  public PageResult<DynamicReport> pageData(ApprovalDynamicReportQryCmd query) {
    return query()
        .eq(Objects.nonNull(query.getSysOrigin()), DynamicReport::getSysOrigin,
            query.getSysOrigin())
        .eq(Objects.nonNull(query.getReportUserId()), DynamicReport::getReportUserId,
            query.getReportUserId())
        .eq(Objects.nonNull(query.getReportedUserId()), DynamicReport::getReportedUserId,
            query.getReportedUserId())
        .eq(Objects.nonNull(query.getApprovalStatus()), DynamicReport::getApprovalStatus,
            query.getApprovalStatus())
        .eq(Objects.nonNull(query.getReportType()), DynamicReport::getReportType,
            query.getReportType())
        .ge(Objects.nonNull(query.getStartTime()), DynamicReport::getCreateTime,
            query.getStartTime())
        .le(Objects.nonNull(query.getEndTime()), DynamicReport::getCreateTime,
            query.getEndTime())
        .orderByDesc(DynamicReport::getCreateTime)
        .page(query.getPageQuery());
  }

  @Override
  public List<DynamicReport> getByContentId(Long contentId) {
    return query()
        .eq(DynamicReport::getDynamicContentId, contentId)
        .eq(DynamicReport::getApprovalStatus, 0)
        .list();
  }

  @Override
  public void updateDynamicReport(ApprovalDynamicReportCmd cmd, List<DynamicReport> complaints) {
    update()
        .set(DynamicReport::getApprovalStatus, cmd.getApprovalStatus())
        .set(DynamicReport::getUpdateTime, TimestampUtils.now())
        .in(DynamicReport::getId, complaints.stream().map(DynamicReport::getId)
            .collect(Collectors.toSet()))
        .execute();
  }
}
