package cn.stylefeng.team.portal.modular.management.entity;

import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.stat.api.callback.ClickCountCallback;
import cn.stylefeng.roses.kernel.stat.api.callback.ClickStatusCallback;
import cn.stylefeng.team.portal.api.constants.PortalConstants;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@TableName("portal_news")
@Data
@EqualsAndHashCode(callSuper = true)
public class PortalNews extends BaseEntity implements ClickStatusCallback, ClickCountCallback {

    @TableId(value = "news_id", type = IdType.ASSIGN_ID)
    @ChineseDescription("新闻id")
    private Long newsId;

    @TableField("design_id")
    @ChineseDescription("门户设计id")
    private Long designId;

    @TableField("news_category_id")
    @ChineseDescription("新闻所属的分类id")
    private Long newsCategoryId;

    @TableField("news_title")
    @ChineseDescription("新闻标题")
    private String newsTitle;

    @TableField("news_summary")
    @ChineseDescription("新闻摘要")
    private String newsSummary;

    @TableField("news_content")
    @ChineseDescription("新闻内容")
    private String newsContent;

    @TableField("show_publish_time")
    @ChineseDescription("显示的发布时间")
    private Date showPublishTime;

    @TableField("show_publish_user")
    @ChineseDescription("显示的发布人")
    private String showPublishUser;

    @TableField("show_photo_user")
    @ChineseDescription("显示的摄影人")
    private String showPhotoUser;

    @TableField("cover_file_id")
    @ChineseDescription("缩略图封面的文件id")
    private Long coverFileId;

    @TableField("news_status")
    @ChineseDescription("新闻状态：1-草稿，2-发布")
    private Integer newsStatus;

    @TableField("news_top_flag")
    @ChineseDescription("是否置顶：Y-置顶，N-不置顶")
    private String newsTopFlag;

    @TableField("news_sort")
    @ChineseDescription("新闻排序")
    private BigDecimal newsSort;

    @TableField("news_type")
    @ChineseDescription("文章类型：10-文本文章，20-外部链接")
    private Integer newsType;

    @TableField("link_url")
    @ChineseDescription("外部链接地址")
    private String linkUrl;

    @TableField(value = "version_flag", fill = FieldFill.INSERT)
    @ChineseDescription("乐观锁")
    @Version
    private Long versionFlag;

    @TableField(value = "del_flag", fill = FieldFill.INSERT)
    @ChineseDescription("是否删除：Y-删除，N-未删除")
    @TableLogic
    private String delFlag;

    @TableField(value = "tenant_id", fill = FieldFill.INSERT)
    @ChineseDescription("租户id")
    private Long tenantId;

    @TableField(exist = false)
    @ChineseDescription("点击状态")
    private String clickStatusFlag = YesOrNotEnum.N.getCode();

    @TableField(exist = false)
    @ChineseDescription("点击次数")
    private Long clickCount = 0L;

    @TableField(exist = false)
    @ChineseDescription("文件信息列表")
    private List<SysFileInfoResponse> businessFileInfoList;

    @Override
    public void addClickCount(Long count) {
        this.clickCount = count;
    }

    @Override
    public void addClickStatus() {
        this.clickStatusFlag = YesOrNotEnum.Y.getCode();
    }

    @Override
    public Long getUserId() {
        LoginUser loginUserNullable = LoginContext.me().getLoginUserNullable();
        if (loginUserNullable == null) {
            return -1L;
        } else {
            return loginUserNullable.getUserId();
        }
    }

    @Override
    public Long getBusinessId() {
        return this.newsId;
    }

    @Override
    public String getBusinessTypeCode() {
        return PortalConstants.CALC_MODULAR_NEWS_CODE;
    }

}