package com.zrlog.service;

import com.hibegin.common.util.StringUtils;
import com.zrlog.common.Constants;
import com.zrlog.common.request.CreateCommentRequest;
import com.zrlog.common.request.PageableRequest;
import com.zrlog.common.request.ReadCommentRequest;
import com.zrlog.common.response.CreateCommentResponse;
import com.zrlog.common.response.StandardResponse;
import com.zrlog.common.response.UpdateRecordResponse;
import com.zrlog.model.Comment;
import com.zrlog.model.Log;
import com.zrlog.util.ParseUtil;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import java.util.Date;
import java.util.Map;

public class CommentService {

    public boolean isAllowComment() {
        return !Constants.getBooleanByFromWebSite("disable_comment_status");
    }

    private boolean isAllowComment(int articleId) {
        Log log = new Log().findByIdOrAlias(articleId);
        return (log != null && log.getBoolean("canComment")) && isAllowComment();
    }

    private static boolean isValidEmailAddress(String email) {
        String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
        java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
        java.util.regex.Matcher m = p.matcher(email);
        return m.matches();
    }

    public CreateCommentResponse save(CreateCommentRequest createCommentRequest) {
        CreateCommentResponse createCommentResponse = new CreateCommentResponse();
        if (createCommentRequest.getLogId() != null && createCommentRequest.getComment() != null) {
            if (isAllowComment(Integer.parseInt(createCommentRequest.getLogId()))) {
                String comment = Jsoup.clean(createCommentRequest.getComment(), Whitelist.basic());
                String email = createCommentRequest.getMail();
                if (StringUtils.isNotEmpty(email) || !isValidEmailAddress(email)) {
                    throw new IllegalArgumentException(email + "not email address");
                }
                String nickname = Jsoup.clean(createCommentRequest.getUserName(), Whitelist.basic());
                String userHome = Jsoup.clean(createCommentRequest.getUserHome(), Whitelist.basic());
                if (comment.length() > 0 && !ParseUtil.isGarbageComment(comment)) {
                    new Comment().set("userHome", userHome)
                            .set("userMail", email)
                            .set("userIp", createCommentRequest.getIp())
                            .set("userName", nickname)
                            .set("logId", createCommentRequest.getLogId())
                            .set("userComment", comment)
                            .set("user_agent", createCommentRequest.getUserAgent())
                            .set("reply_id", createCommentRequest.getReplyId())
                            .set("commTime", new Date()).set("hide", 1).save();
                } else {
                    createCommentResponse.setError(1);
                    createCommentResponse.setMessage("");
                }
            } else {
                createCommentResponse.setError(1);
                createCommentResponse.setMessage("");
            }
        } else {
            createCommentResponse.setError(1);
            createCommentResponse.setMessage("");
        }
        Log log = new Log().findByIdOrAlias(createCommentRequest.getLogId());
        if (log != null) {
            createCommentResponse.setAlias(log.getStr("alias"));
        }
        return createCommentResponse;
    }

    public StandardResponse delete(String[] ids) {
        for (String id : ids) {
            new Comment().deleteById(id);
        }
        return new StandardResponse();
    }

    public UpdateRecordResponse read(ReadCommentRequest commentRequest) {
        new Comment().doRead(commentRequest.getId());
        return new UpdateRecordResponse();
    }

    public Map page(PageableRequest pageable) {
        return new Comment().find(pageable);
    }
}

@@ -651,327 +651,8 @@
  %7B%0A%0A
-    public boolean isAllowComment() %7B%0A        return !Constants.getBooleanByFromWebSite(%22disable_comment_status%22);%0A    %7D%0A%0A    private boolean isAllowComment(int articleId) %7B%0A        Log log = new Log().findByIdOrAlias(articleId);%0A        return (log != null && log.getBoolean(%22canComment%22)) && isAllowComment();%0A    %7D%0A%0A
     
@@ -1030,32 +1030,351 @@
 tches();%0A    %7D%0A%0A
+    public boolean isAllowComment() %7B%0A        return !Constants.getBooleanByFromWebSite(%22disable_comment_status%22);%0A    %7D%0A%0A    private boolean isAllowComment(int articleId) %7B%0A        Log log = new Log().findByIdOrAlias(articleId);%0A        return (log != null && log.getBoolean(%22canComment%22)) && isAllowComment();%0A    %7D%0A%0A
     public Creat
@@ -2096,98 +2096,433 @@
 e = 
-Jsoup.clean(createCommentRequest.getUserName(), Whitelist.basic());%0A                String
+createCommentRequest.getUserName();%0A                if (StringUtils.isEmpty(nickname)) %7B%0A                    throw new IllegalArgumentException(%22nickname not block%22);%0A                %7D%0A                nickname = Jsoup.clean(createCommentRequest.getUserName(), Whitelist.basic());%0A                String userHome = createCommentRequest.getUserHome();%0A                if (StringUtils.isNotEmpty(userHome)) %7B%0A                   
  use
@@ -2589,32 +2589,50 @@
 elist.basic());%0A
+                %7D%0A
                 

Optimize npe check