package com.red.circle.other.inner.model.cmd.sys;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.red.circle.common.business.enums.PushPlatformEnum;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.tool.core.parse.DataTypeUtils;
import com.red.circle.tool.core.regex.RegexConstant;
import com.red.circle.tool.core.text.StringUtils;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 推送记录日志.
 * </p>
 *
 * @author pengliang
 * @since 2020-03-08
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
public class CreateRecordCmd extends CommonCommand {

  private static final long serialVersionUID = 1L;

  /**
   * 主键标识.
   */
  private Long id;

  /**
   * 标题.
   */
  private String title;

  /**
   * 消息内容.
   */
  private String content;

  /**
   * 手机类型.
   */
  private String platform;

  /**
   * 系统平台.
   */
  private String sysOrigin;

  /**
   * 语言
   */
  private String language;

  /**
   * 业务场景.
   */
  private String businessScene;

  /**
   * 设备平台(UMENG,GOOGLE).
   */
  private String deviceType;

  /**
   * 定投用户id.
   */
  private String fixedUserIds;

  /**
   * 用户ids.
   */
  private List<Long> userIds;

  /**
   * 推送链接.
   */
  private String link;

  /**
   * 发送状态.
   */
  private String pushStatus;

  /**
   * 创建用户.
   */
  private Long createUser;

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

  /**
   * 失败数量.
   */
  private Integer failQuantity;

  /**
   * 成功数量.
   */
  private Integer successQuantity;

  @JsonIgnore
  public boolean isFixedUserIdSend() {
    return StringUtils.isNotBlank(fixedUserIds);
  }

  @JsonIgnore
  public List<Long> getQualifiedFixedUserIdList() {
    return Optional.ofNullable(fixedUserIds)
        .filter(StringUtils::isNotBlank)
        .map(fixedUserIdStr ->
            Arrays.stream(fixedUserIdStr.split(","))
                .filter(userIdStr -> userIdStr.matches(RegexConstant.NUMBER))
                .map(DataTypeUtils::toLong)
                .distinct()
                .collect(Collectors.toList())
        )
        .orElse(null);

  }

  @JsonIgnore
  public boolean isPushAll() {
    return StringUtils.isBlank(platform);
  }

  public boolean isGoogleDevice() {
    return Objects.equals(deviceType, PushPlatformEnum.GOOGLE.name());
  }


}
