CommentsServiceImpl.java 15.3 KB
package com.cnlive.shenhe.service.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.dto.CommentsDTO;
import com.cnlive.shenhe.entity.ShyComments;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.mapper.ShyCommentsMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.service.CommentsService;
import com.cnlive.shenhe.service.SitesService;
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 implements CommentsService {

    private static Logger logger = LoggerFactory.getLogger(CommentsServiceImpl.class);
    @Autowired
    ShyCommentsMapper shyCommentsMapper;
    @Autowired
    ShyUsersMapper shyUsersMapper;
    @Autowired
    AuditPass pass;
    @Autowired
    SitesService sitesService;


    /**
     * 根据评论对象获取评论集合
     *
     * @param shyComments
     * @return
     */
    @Override
    public List<ShyComments> findShyComments(ShyComments shyComments) {
        return shyCommentsMapper.selectByRowBounds(shyComments, new RowBounds(0, 100));
    }

    /**
     * 修改评论 返回布尔类型
     *
     * @param shyComments
     * @return
     */
    @Override
    public boolean updateShyComments1(ShyComments shyComments) {
        int result = shyCommentsMapper.updateByPrimaryKey(shyComments);
        return result != 0;
    }

    /**
     * 导入评论  返回布尔类型
     *
     * @param shyComments
     * @return
     */
    @Override
    public boolean impotComments(ShyComments shyComments) {
        int result = shyCommentsMapper.insertSelective(shyComments);
        return result != 0;
    }

    /**
     * 评论分页、条件查询
     *
     * @param commentsDTO
     * @return
     */
    @Override
    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()) && Integer.parseInt(commentsDTO.getType()) == 0) {
            criteria.andEqualTo("type", CommonConst.CATEGORY_TOPIC);
        } else if (CommonUtils.isNotEmpty(commentsDTO.getType()) && Integer.parseInt(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()) && commentsDTO.getState() != 3) {
            criteria.andEqualTo("state", commentsDTO.getState());
        } else if (CommonUtils.isNotNull(commentsDTO.getState()) && commentsDTO.getState() == 3) {
            criteria.andBetween("state", CommonConst.AUDIT_SUCCESS, CommonConst.AUDIT_REFUSE);
        }

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

    /**
     * 导出Excel表格
     *
     * @param commentsDTO
     * @return
     */
    @Override
    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()) && Integer.parseInt(commentsDTO.getType()) == 0) {
            criteria.andEqualTo("type", CommonConst.CATEGORY_TOPIC);
        } else if (CommonUtils.isNotEmpty(commentsDTO.getType()) && Integer.parseInt(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.isNotNull(commentsDTO.getState()) && commentsDTO.getState() != 3) {
            criteria.andEqualTo("state", commentsDTO.getState());
            //是 我的已审,查通过和拒绝的两种状态
        } else if (CommonUtils.isNotNull(commentsDTO.getState()) && commentsDTO.getState() == 3) {
            criteria.andBetween("state", CommonConst.AUDIT_SUCCESS, CommonConst.AUDIT_REFUSE);
        }

        List<ShyComments> commentsList = shyCommentsMapper.selectByExample(example);
        PageInfo<ShyComments> pageInfo = new PageInfo<>(commentsList);
        return pageInfo;
    }

    /**
     * 获取评论详情
     *
     * @param id
     * @return
     */
    @Override
    public ShyComments getDetailsComment(Integer id, Integer spId) {
        ShyComments comment = shyCommentsMapper.selectBySpId(id, spId);
        //ShyComments comment = shyCommentsMapper.selectByPrimaryKey(id);
        if (CommonUtils.isNotNull(comment)) {
            if (!comment.getState().equals(CommonConst.AUDIT_INTT)) {
                String updater = CommonUtils.isEmpty(comment.getUpdater()) || "null".equals(comment.getUpdater()) ? "白名单" : comment.getUpdater();
                String auditorId = comment.getAuditor_id();
                if (CommonUtils.isNotEmpty(auditorId)) {
                    ShyUsers user = new ShyUsers();
                    user.setUser_id(auditorId);
                    user = shyUsersMapper.selectOne(user);
                    String email = CommonUtils.isEmpty(user.getEmail()) || "null".equals(user.getEmail()) ? "" : user.getEmail();
                    updater = CommonUtils.isEmpty(user.getUsername()) ? email : user.getUsername();
                }
                comment.setUpdater(updater);
            }
        }
        return comment;
    }

    /**
     * 修改状态 回调 返回布尔类型
     *
     * @param ids
     * @param state
     * @param userId
     * @param updater
     * @param msg
     * @return
     */
    @Override
    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;
    }

    /**
     * 认领
     *
     * @param ids
     * @param num
     * @param userId
     * @param spid
     * @return
     */
    @Override
    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;
    }

    /**
     * 退领
     *
     * @param ids
     * @param userId
     * @return
     */
    @Override
    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;
    }

    /**
     * 根据主键id查询评论对象
     *
     * @param id
     * @return
     */
    @Override
    public ShyComments selectByPrimaryKey(Integer id) {
        return shyCommentsMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据主键id修改对象
     *
     * @param comments
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(ShyComments comments) {
        return shyCommentsMapper.updateByPrimaryKeySelective(comments);
    }

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

}