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

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.dto.CommentsDTO;
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.apache.ibatis.session.RowBounds;
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.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;

    @Autowired
    SitesServiceImpl sitesService;

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

    //**************************************
    public List<ShyComments> findshyComments(ShyComments shyComments) {
        List<ShyComments> commentsList = shyCommentsMapper.selectByRowBounds(shyComments, new RowBounds(0, 100));
        return commentsList;
    }

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

    public boolean impotComments(ShyComments shyComments) {
        int result = shyCommentsMapper.insertSelective(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(CommentsDTO commentsDTO) {
        logger.info("接受参数对象为,CommentsDTO:{}",JSONObject.toJSON(commentsDTO));
        Example example = new Example(ShyComments.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(commentsDTO.getOrderByClause())) {
            example.setOrderByClause(commentsDTO.getOrderByClause());
        } else {
            example.setOrderByClause("comment_time desc");
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getStart_date()) && CommonUtils.isNotEmpty(commentsDTO.getEnd_date())) {
            criteria.andGreaterThanOrEqualTo("comment_time", commentsDTO.getStart_date());
            criteria.andLessThanOrEqualTo("comment_time", commentsDTO.getEnd_date());
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getContent())) {
            criteria.andLike("comment_content", "%" + commentsDTO.getContent() + "%");
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getUpdater())) {
            criteria.andEqualTo("updater", commentsDTO.getUpdater());
        }
        if (CommonUtils.isNotNull(commentsDTO.getSpid())) {
            criteria.andEqualTo("spid", commentsDTO.getSpid());
        }
        if (CommonUtils.isNotNull(commentsDTO.getIs_hot())) {
            criteria.andEqualTo("is_hot", commentsDTO.getIs_hot());
        }
        if (CommonUtils.isNotNull(commentsDTO.getComment_user_id())) {
            criteria.andEqualTo("comment_user_id", commentsDTO.getComment_user_id());
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getType()) && commentsDTO.getType() == "0") {
            criteria.andEqualTo("type", CommonConst.CATEGORY_TOPIC);
        } else if (CommonUtils.isNotEmpty(commentsDTO.getType()) && commentsDTO.getType() == "1") {
            criteria.andEqualTo("type", CommonConst.CATEGORY_ELSE);
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getActivity_id())) {
            criteria.andLike("activity_id", "%" + commentsDTO.getActivity_id() + "%");
        }
        if (CommonUtils.isNotNull(commentsDTO.getReceive())) {
            criteria.andEqualTo("receive", commentsDTO.getReceive());
            if (commentsDTO.getReceive().equals(CommonConst.RECEIVE_AFTER)) {
                criteria.andEqualTo("receive_user_id", commentsDTO.getUserId());
            }
        }

        //if (CommonUtils.isNotNull(commentsDTO.getState())) {
        //    Example.Criteria criteria1 = example.createCriteria();
        //    String[] stas = String.valueOf(commentsDTO.getState()).split(",");
        //    for (String s : stas) {
        //        criteria1.orEqualTo("state", s);
        //    }
        //    example.and(criteria1);
        //}

        if (CommonUtils.isNotNull(commentsDTO.getState()) && commentsDTO.getState() != 3) {
            criteria.andEqualTo("state", commentsDTO.getState());
        } else if (CommonUtils.isNotNull(commentsDTO.getState()) && commentsDTO.getState() == 3) {
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.orEqualTo("state", 1);
            criteria1.orEqualTo("state", 2);
            example.and(criteria1);
        }

        PageHelper.startPage(commentsDTO.getPage(), commentsDTO.getPageSize(), true);
        List<ShyComments> commentsList = shyCommentsMapper.selectByExample(example);
        PageInfo<ShyComments> pageInfo = new PageInfo<>(commentsList);
        return pageInfo;
    }

    public PageInfo<ShyComments> getPageExcel(CommentsDTO commentsDTO) {
        Example example = new Example(ShyComments.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(commentsDTO.getOrderByClause())) {
            example.setOrderByClause(commentsDTO.getOrderByClause());
        } else {
            example.setOrderByClause("comment_time desc");
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getStart_date()) && CommonUtils.isNotEmpty(commentsDTO.getEnd_date())) {
            criteria.andGreaterThanOrEqualTo("comment_time", commentsDTO.getStart_date());
            criteria.andLessThanOrEqualTo("comment_time", commentsDTO.getEnd_date());
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getContent())) {
            criteria.andLike("comment_content", "%" + commentsDTO.getContent() + "%");
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getUpdater())) {
            criteria.andEqualTo("updater", commentsDTO.getUpdater());
        }
        if (CommonUtils.isNotNull(commentsDTO.getSpid())) {
            criteria.andEqualTo("spid", commentsDTO.getSpid());
        }
        if (CommonUtils.isNotNull(commentsDTO.getIs_hot())) {
            criteria.andEqualTo("is_hot", commentsDTO.getIs_hot());
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getType()) && commentsDTO.getType() == "0") {
            criteria.andEqualTo("type", CommonConst.CATEGORY_TOPIC);
        } else if (CommonUtils.isNotEmpty(commentsDTO.getType()) && commentsDTO.getType() == "1") {
            criteria.andIsNull("type");
        }
        if (CommonUtils.isNotEmpty(commentsDTO.getActivity_id())) {
            criteria.andLike("activity_id", "%" + commentsDTO.getActivity_id() + "%");
        }
        if (CommonUtils.isNotNull(commentsDTO.getReceive())) {
            criteria.andEqualTo("receive", commentsDTO.getReceive());
            if (commentsDTO.getReceive().equals(CommonConst.RECEIVE_AFTER)) {
                criteria.andEqualTo("receive_user_id", commentsDTO.getUserId());
            }
        }

        if (CommonUtils.isNotEmpty(String.valueOf(commentsDTO.getState()))) {
            Example.Criteria criteria1 = example.createCriteria();
            String[] stas = String.valueOf(commentsDTO.getState()).split(",");
            for (String s : stas) {
                criteria1.orEqualTo("state", Integer.parseInt(s));
            }
            example.and(criteria1);
        }

        if (CommonUtils.isNotNull(commentsDTO.getState()) && commentsDTO.getState() != 3) {
            criteria.andEqualTo("state", commentsDTO.getState());
        } else if (CommonUtils.isNotNull(commentsDTO.getState()) && commentsDTO.getState() == 3) {
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.orEqualTo("state", 1);
            criteria1.orEqualTo("state", 2);
            example.and(criteria1);
        }

        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().equals(CommonConst.AUDIT_INTT)) {
            String updater = CommonUtils.isEmpty(comment.getUpdater()) || "null".equals(comment.getUpdater()) ? "白名单" : comment.getUpdater();
            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 email = CommonUtils.isEmpty(user.getEmail()) || "null".equals(user.getEmail()) ? "" : user.getEmail();
                String userName = CommonUtils.isEmpty(user.getUsername()) ? email : user.getUsername();
                updater = userName;
            }
            comment.setUpdater(updater);
        }
        return comment;
    }

    public boolean updateState(String ids, Integer state, String userId, String updater, String msg) {
        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", 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.setMsg(msg);
                if (CommonUtils.isEmpty(userId)) {
                    comment.setUpdater(updater);
                } else {
                    comment.setAuditor_id(userId);
                    comment.setReceive(1);//已领取
                    comment.setReceive_user_id(userId);
                }
                int i = shyCommentsMapper.updateByPrimaryKeySelective(comment);
                if (i != 1) {
                    success = false;
                }
            }

        }
        return success;
    }

    public Integer receive(String ids, Integer num, String userId, Integer spid) {
        Integer n = 0;
        if (CommonUtils.isNotEmpty(ids)) {
            String[] cids = ids.split(",");
            for (String cid : cids) {
                ShyComments shyComments = shyCommentsMapper.selectByPrimaryKey(Integer.parseInt(cid));
                if (CommonUtils.isNotNull(shyComments) && shyComments.getState().equals(CommonConst.AUDIT_INTT) && shyComments.getReceive().equals(CommonConst.RECEIVE_BEFORE)) {
                    shyComments.setReceive_user_id(userId);
                    shyComments.setReceive(CommonConst.RECEIVE_AFTER);
                    shyCommentsMapper.updateByPrimaryKeySelective(shyComments);
                    n++;
                }
            }
        } else if (CommonUtils.isNotNull(num) && num > 0) {
            Example example = new Example(ShyComments.class);
            example.setOrderByClause("created_at desc");
            example.and().andEqualTo("state", CommonConst.AUDIT_INTT);
            example.and().andEqualTo("receive", CommonConst.RECEIVE_AFTER);
            if (CommonUtils.isNotNull(spid) && spid != 0) {
                example.and().andEqualTo("spid", spid);
            }
            List<ShyComments> shyCommentsList = shyCommentsMapper.selectByExampleAndRowBounds(example, new RowBounds(0, num));
            if (!shyCommentsList.isEmpty()) {
                for (ShyComments shyComments : shyCommentsList) {
                    shyComments.setReceive_user_id(userId);
                    shyComments.setReceive(CommonConst.RECEIVE_AFTER);
                    shyCommentsMapper.updateByPrimaryKeySelective(shyComments);
                    n++;
                }
            }
        }
        return n;
    }

    public Integer retReceive(String ids, String userId) {
        Integer n = 0;
        String[] cids = ids.split(",");
        for (String cid : cids) {
            ShyComments shyComments = shyCommentsMapper.selectByPrimaryKey(Integer.parseInt(cid));
            if (shyComments.getState().equals(CommonConst.AUDIT_INTT) && shyComments.getReceive().equals(CommonConst.RECEIVE_AFTER) && userId.equals(shyComments.getReceive_user_id())) {
                shyComments.setReceive(CommonConst.RECEIVE_BEFORE);
                shyComments.setReceive_user_id(null);
                int i = shyCommentsMapper.updateByPrimaryKeySelective(shyComments);
                if (i == 1) {
                    n++;
                }
            }
        }
        return n;
    }

    public ShyComments selectByPrimaryKey(Integer id) {
        ShyComments shyComments = shyCommentsMapper.selectByPrimaryKey(id);
        return shyComments;
    }

    public int updateByPrimaryKeySelective(ShyComments comments) {
        return shyCommentsMapper.updateByPrimaryKeySelective(comments);
    }

    /**
     * 获得审核类型
     *
     * @param shyComments
     */
    public void getShenheType(ShyComments shyComments) {

        //获取评论免审sp
        List<Integer> sites = sitesService.getBusinessList(CommonConst.BUSINESS_COMMENT);
        //初始化一个审核状态
        Integer shenheType = 3;//审核类型,1:免审,2:机审,3:人工审
        //如果当前sp是免审sp
        if (sites.contains(shyComments.getSpid())) {
            shenheType = 1;//免审
        }
        shyComments.setShenhe_type(shenheType);
    }

}