CommentsServiceImpl.java 6.69 KB
package com.cnlive.shenhe.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShyComments;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.mapper.ShyCommentsMapper;
import com.cnlive.shenhe.mapper.ShySitesMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.utils.AuditPass;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Auther: chenhao
 * @Date: 2018/12/4 17:30
 * @Description:
 */
@Service
public class CommentsServiceImpl {

    private static Logger logger = LoggerFactory.getLogger(CommentsServiceImpl.class);
    @Autowired
    ShyCommentsMapper shyCommentsMapper;
    @Autowired
    ShySitesMapper shySitesMapper;
    @Autowired
    ShyUsersMapper shyUsersMapper;
    @Autowired
    AuditPass Pass;

    public List<ShySites> getBusiness(ShySites shySites) {
        List<ShySites> sitesList = shySitesMapper.select(shySites);
        return sitesList;
    }

    //**************************************
    public List<ShyComments> findshyComments(ShyComments shyComments) {
        List<ShyComments> commentsList = shyCommentsMapper.select(shyComments);
        return commentsList;
    }

    public boolean updateshyComments1(ShyComments shyComments) {
        int result = shyCommentsMapper.updateByPrimaryKeySelective(shyComments);
        if (result == 0) {
            return false;
        }
        return true;
    }

    public boolean impotComments(ShyComments shyComments) {
        int result = shyCommentsMapper.insert(shyComments);
        if (result == 0) {
            return false;
        }
        return true;
    }

    public int updateComment(Integer id, Integer state, String msg) {
        ShyComments shyComments = shyCommentsMapper.selectByPrimaryKey(id);
        shyComments.setState(state);
        shyComments.setMsg(msg);
        return shyCommentsMapper.updateByPrimaryKeySelective(shyComments);
    }

    public PageInfo<ShyComments> getPage(String activity_id, String content, Integer is_hot, Date start_date, Date end_date, String states, String auditor, Integer page, Integer pageSize, String orderByClause, Integer spid) {
        Example example = new Example(ShyComments.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(orderByClause)) {
            example.setOrderByClause(orderByClause);
        } else {
            example.setOrderByClause("comment_time desc");
        }
        if (CommonUtils.isNotNull(start_date) && CommonUtils.isNotNull(end_date)) {
            criteria.andGreaterThanOrEqualTo("comment_time", start_date);
            criteria.andLessThanOrEqualTo("comment_time", end_date);
        }
        if (CommonUtils.isNotEmpty(content)) {
            criteria.andLike("comment_content", "%" + content + "%");
        }
        if (CommonUtils.isNotEmpty(auditor)) {
            criteria.andEqualTo("auditor", auditor);
        }
        if (CommonUtils.isNotNull(spid)) {
            criteria.andEqualTo("spid", spid);
        }
        if (CommonUtils.isNotNull(is_hot)) {
            criteria.andEqualTo("is_hot", is_hot);
        }
        if (CommonUtils.isNotEmpty(activity_id)) {
            criteria.andLike("activity_id", "%" + activity_id + "%");
        }
        if (CommonUtils.isNotEmpty(states)) {
            Example.Criteria criteria1 = example.createCriteria();
            String[] stas = states.split(",");
            for (String s : stas) {
                criteria1.orEqualTo("state", Integer.parseInt(s));
            }
            example.and(criteria1);
        }
        PageHelper.startPage(page, pageSize, true);
        List<ShyComments> commentsList = shyCommentsMapper.selectByExample(example);
        PageInfo<ShyComments> pageInfo = new PageInfo<>(commentsList);
        return pageInfo;
    }

    public ShyComments getDetailsComment(Integer id) {
        ShyComments comment = shyCommentsMapper.selectByPrimaryKey(id);
        if (comment.getState() != CommonConst.AUDIT_INTT) {
            String auditorName = "白名单";
            String auditor_id = comment.getAuditor_id();
            if (CommonUtils.isNotEmpty(auditor_id)) {
                ShyUsers user = new ShyUsers();
                user.setUser_id(auditor_id);
                user = shyUsersMapper.selectOne(user);
                String userName = CommonUtils.isEmpty(user.getUsername())?"":user.getUsername();
                String email = CommonUtils.isEmpty(user.getEmail())?"":user.getEmail();
                auditorName = userName + email;
            }
            comment.setAuditor(auditorName);
        }
        return comment;
    }

    public boolean updateState(String ids, Integer state, String userId) {
        boolean success = true;
        String[] commentIds = ids.split(",");
        for (String commentId : commentIds) {
            ShyComments comment = shyCommentsMapper.selectByPrimaryKey(Integer.parseInt(commentId));
            JSONObject json = new JSONObject();
            json.put("activity_id", comment.getActivity_id());
            json.put("state", state + 2);
            json.put("attr_tags", "");
            json.put("msg", "");
            JSONObject result = new JSONObject();
            try {
                result = Pass.commentPass(json, comment.getCallback());
            } catch (Exception e) {
                logger.error("评论回调失败,activity_id:{}", comment.getActivity_id());
                success = false;
                break;
            }
            if (result == null) {
                logger.error("评论回调错误,且无返回信息。activity_id:{}", comment.getActivity_id());
                success = false;
                break;
            } else {
                Integer code = result.getInteger("errorCode");
                if (code != 0) {
                    logger.error("评论回调错误,activity_id:{},errorCode:{},errorMessage:{}", comment.getActivity_id(), code, result.getString("errorMessage"));
                    success = false;
                    break;
                }
                comment.setState(state);
                comment.setAuditor_id(userId);
                int i = shyCommentsMapper.updateByPrimaryKeySelective(comment);
                if (i != 1) {
                    success = false;
                }
            }

        }
        return success;
    }
}