/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.business.notify.service.dto;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.jeeplus.business.editor.service.dto.EditorFilesDTO;
import com.jeeplus.business.notify.domain.JyNotifyLog;
import com.jeeplus.common.constant.CommonConstants;
import com.jeeplus.core.query.Query;
import com.jeeplus.core.query.QueryType;
import com.jeeplus.core.service.dto.BaseDTO;


import com.jeeplus.sys.service.dto.UserDTO;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;
import java.util.PriorityQueue;
import java.util.stream.Collectors;

/**
 * 通知通告Entity
 *
 * @author jeeplus
 * @version 2021-05-16
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class JyNotifyDTO extends BaseDTO {

    private static final long serialVersionUID = 1L;
    /**
     * 类型
     */
    @Query(type = QueryType.EQ)
    @Size(min = 0, max = 1, message = "类型长度必须介于 0 和 1 之间")
    private String type;
    /**
     * 标题
     */
    @Query
    @Size(min = 0, max = 200, message ="标题长度必须介于 0 和 200 之间")
    private String title;
    /**
     * 内容
     */
    private String content;
    /**
     * 附件
     */
    @Size(min = 0, max = 2000, message = "附件长度必须介于 0 和 2000 之间")
    private String files;
    /**
     * 状态
     */
    @Query(type = QueryType.EQ)
    @Size(min = 0, max = 1, message = "状态长度必须介于 0 和 1 之间")
    private String status;
    /**
     * 已读
     */
    private String readNum;
    /**
     * 未读
     */
    private String unReadNum;
    /**
     * 是否只查询自己的通知
     */
    private boolean isSelf;
    /**
     * 备注
     */
    private String remarks;
    /**
     * 通告状态
     */
    private String notifyType;
    /**
     * 文号
     */
    private String no;
    /**
     * 开始时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date beginDate;
    /**
     * 结束时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endDate;
    /**
     * 所属部门id
     */
    private String officeId;

    /**
     * 本人阅读状态
     */
    @Query(type = QueryType.EQ)
    private String readFlag;

    /**
     * 关闭状态
     */
    @Query(type = QueryType.EQ)
    private String isClose;

    /**
     * 筛选条件：发布者
     */
    private String createName;
    private String createById;

    /**
     * 筛选条件：创建时间
     */
    private String[] createDates;

    /**
     * 流程信息
     */
    private String taskId;

    /**
     * 附件
     */
    private List<WorkAttachmentDto> workAttachmentDtoList;
    /**
     * 接收部门集合
     */
    private List<PluginNotifyOfficesDTO> pluginNotifyOfficesDTOList;
    /**
     * 接收用户集合
     */
    private List<PluginNotifyUserDTO> pluginNotifyUserDTOList;
    /**
     * 富文本图片集合
     */
    private List<EditorFilesDTO> editorFilesDTOList;

    private List<JyNotifyRecordDTO> notifyRecordDTOList = Lists.newArrayList();

    /**
     * 访问日志
     */
    private List<JyNotifyLog> notifyLogList;
    //日志访问总数
    private Integer logCount;

    /**
     * 评论
     */
    private List<PluginNotifyCommentsDTO> comments;

    /**
     * 获取通知发送记录用户ID
     *
     * @return
     */
    public String getNotifyRecordIds() {
        return notifyRecordDTOList.stream ().map ( notifyRecordDTO -> {
            return notifyRecordDTO.getUserDTO ().getId ();
        } ).collect( Collectors.joining(","));
    }

    /**
     * 设置通知发送记录用户ID
     *
     * @return
     */
    public void setNotifyRecordIds(String notifyRecordIds) {
        this.notifyRecordDTOList = Lists.newArrayList();
        for (String id : StrUtil.split(notifyRecordIds, ",")) {
            JyNotifyRecordDTO dto = new JyNotifyRecordDTO();
            dto.setNotifyDTO ( this );
            dto.setUserDTO ( new UserDTO( id ) );
            dto.setReadFlag ( CommonConstants.NO );
            this.notifyRecordDTOList.add(dto);
        }
    }

    /**
     * 获取通知发送记录用户Name
     *
     * @return
     */
    public String getNotifyRecordNames() {
        return notifyRecordDTOList.stream ().map ( notifyRecordDTO -> {
            return notifyRecordDTO.getUserDTO ().getName ();
        } ).collect( Collectors.joining(","));
    }
}
