package com.slodon.b2b2c.vo.goods;

import com.slodon.b2b2c.core.util.FileUrlUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.goods.pojo.GoodsComment;
import com.slodon.b2b2c.member.pojo.Member;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品评价信息vo
 */
@Data
public class GoodsCommentsInfoVO {

    @ApiModelProperty("平均评分，0-5，保留1位小数")
    private String avgScore = "0.0";
    @ApiModelProperty("好评率")
    private String highPercent = "0%";
    @ApiModelProperty("评价总数")
    private Integer commentsCount;
    @ApiModelProperty("有图数")
    private Integer hasPicCount;
    @ApiModelProperty("好评数")
    private Integer highCount;
    @ApiModelProperty("中评数")
    private Integer middleCount;
    @ApiModelProperty("差评数")
    private Integer lowCount;
    @ApiModelProperty("评价列表")
    private List<CommentsVO> list = new ArrayList<>();
    @ApiModelProperty("分页")
    private Map<String, Object> pagination;

    /**
     * 评价vo
     */
    @Data
    public static class CommentsVO {

        @ApiModelProperty("评价id")
        private Integer commentId;
        @ApiModelProperty("评价人ID")
        private Integer memberId;
        @ApiModelProperty("评价人账号")
        private String memberName;
        @ApiModelProperty("用户头像")
        private String memberAvatar;
        @ApiModelProperty("评分(1到5)")
        private Integer score;
        @ApiModelProperty("评价内容")
        private String content;
        @ApiModelProperty("评价时间")
        private Date createTime;
        @ApiModelProperty("规格值")
        private String specValues;
        @ApiModelProperty("评价图片")
        private List<String> images = new ArrayList<>();
        @ApiModelProperty("商家回复内容")
        private String replyContent;

        public CommentsVO(GoodsComment goodsComment, Member member) {
            commentId = goodsComment.getCommentId();
            memberId = goodsComment.getMemberId();
            memberName = goodsComment.getMemberName();
            memberAvatar = member.getMemberAvatar().startsWith("http") ? member.getMemberAvatar() : FileUrlUtil.getFileUrl(member.getMemberAvatar(), null);
            score = goodsComment.getScore();
            content = StringUtil.isEmpty(goodsComment.getContent()) ? "此用户未填写评价" : goodsComment.getContent();
            createTime = goodsComment.getCreateTime();
            specValues = goodsComment.getSpecValues();
            if (!StringUtils.isEmpty(goodsComment.getImage())) {
                for (String imagePath : goodsComment.getImage().split(",")) {
                    images.add(FileUrlUtil.getFileUrl(imagePath, null));
                }
            }
            replyContent = goodsComment.getReplyContent();
        }
    }
}
