package com.red.circle.other.infra.database.mongo.entity.team.team;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.other.inner.enums.team.TeamMemberTargetIndex;
import com.red.circle.other.inner.enums.team.TeamMemberTargetSettlementResult;
import com.red.circle.other.inner.enums.team.TeamMemberTargetStatus;
import java.io.Serial;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * 团队成员目标.
 *
 * @author pengliang on 2022/12/1
 */
@Data
@Accessors(chain = true)
@Document("team_member_target")
public class TeamMemberTarget implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * ID.
   */
  @Id
  private String id;

  /**
   * 时序id.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long timeId;

  /**
   * 团队系统.
   */
  private String sysOrigin;

  /**
   * 团队id.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long teamId;

    /**
     * 是否团队长
     */
  private Boolean isOwn;

  /**
   * 账单归属.
   */
  private Integer billBelong;

  /**
   * 账单标题.
   */
  private String billTitle;

  /**
   * 成员用户.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long userId;

  /**
   * 每天目标.
   */
  private List<TeamMemberTargetIndex> dailyTargets;

  /**
   * TeamMemberTargetStatus=CREDITED 该字段生效, 结算结果.
   */
  private TeamMemberTargetSettlementResult settlementResult;

  /**
   * 状态.
   */
  private TeamMemberTargetStatus status;

  /**
   * 备注.
   */
  private List<String> remarks;

  /**
   * 是否为历史账单 true: 是， false: 不是. 历史账单: 存档，不会再有任何档编辑处理(!!当月一个用户只会存在一条history为false的记录).
   */
  private Boolean history;

  /**
   * 创建时间.
   */
  private Timestamp createTime;

  /**
   * 修改时间.
   */
  private Timestamp updateTime;

  /**
   * 所有在线时间.
   */
  public Long sumAllTargetTime() {
    return dailyTargets.stream()
        .mapToLong(target -> {
          Long ownOnlineTime =
              Objects.isNull(target.getOwnOnlineTime()) ? 0L : target.getOwnOnlineTime();
          Long otherOnlineTime =
              Objects.isNull(target.getOtherOnlineTime()) ? 0L : target.getOtherOnlineTime();
          return ownOnlineTime + otherOnlineTime;
        }).sum();
  }

  /**
   * 自己房间时间.
   */
  public Long sumOwnOnlineTime() {
    return dailyTargets.stream()
        .mapToLong(target ->
            Objects.isNull(target.getOwnOnlineTime()) ? 0L : target.getOwnOnlineTime()).sum();
  }

  /**
   * 其他房间时间.
   */
  public Long sumOtherOnlineTime() {
    return dailyTargets.stream()
        .mapToLong(target ->
            Objects.isNull(target.getOtherOnlineTime()) ? 0L : target.getOtherOnlineTime()).sum();
  }

  /**
   * 有效天统计.
   */
  public Integer sumEffectiveDay() {
    return dailyTargets.stream()
            .mapToInt(target ->
                    Objects.isNull(target.getEffectiveDay()) ? 0 : target.getEffectiveDay()).sum();
  }

  /**
   * 接收礼物.
   */
  public Long sumAcceptGiftValue() {
    return dailyTargets.stream()
        .mapToLong(target ->
            Objects.isNull(target.getAcceptGiftValue()) ? 0L : target.getAcceptGiftValue()).sum();
  }

  /**
   * 发送礼物.
   */
  public Long sumGiveGiftValue() {
    return dailyTargets.stream()
        .mapToLong(target ->
            Objects.isNull(target.getGiveGiftValue()) ? 0L : target.getGiveGiftValue()).sum();
  }

  /**
   * 房间流水.
   */
  public Long sumRoomValue() {
    return dailyTargets.stream()
        .mapToLong(target ->
            Objects.isNull(target.getRoomValue()) ? 0L : target.getRoomValue()).sum();
  }

}
