ChannelServiceImpl.java 8.91 KB
package com.cnlive.shenhe.service.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.dto.ChannelDTO;
import com.cnlive.shenhe.entity.ShyChannel;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.mapper.ShyChannelMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.service.ChannelService;
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 ChannelServiceImpl implements ChannelService {

    private static Logger logger = LoggerFactory.getLogger(ChannelServiceImpl.class);
    @Autowired
    ShyChannelMapper shyChannelMapper;
    @Autowired
    ShyUsersMapper shyUsersMapper;
    @Autowired
    AuditPass pass;

    @Autowired
    SitesService sitesService;

    /**
     * 根据条件查询频道集合
     *
     * @param shyChannel
     * @return
     */
    @Override
    public List<ShyChannel> findshyChannel(ShyChannel shyChannel) {
        return shyChannelMapper.selectByRowBounds(shyChannel, new RowBounds(0, 100));
    }

    /**
     * 修改频道 返回布尔类型
     *
     * @param shyChannel
     * @return
     */
    @Override
    public boolean updateshyChannel(ShyChannel shyChannel) {
        int result = shyChannelMapper.updateByPrimaryKey(shyChannel);
        return result != 0;
    }

    /**
     * 导入频道 返回布尔类型
     *
     * @param shyChannel
     * @return
     */
    @Override
    public boolean impotChannel(ShyChannel shyChannel) {
        int result = shyChannelMapper.insertSelective(shyChannel);
        return result != 0;
    }

    /**
     * 获取分页、条件查询结果集
     *
     * @param channelDTO
     * @return
     */
    @Override
    public PageInfo<ShyChannel> getPage(ChannelDTO channelDTO) {
        logger.info("接受参数对象为,ChannelDTO:{}", JSON.toJSONString(channelDTO));
        Example example = new Example(ShyChannel.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(channelDTO.getOrderByClause())) {
            example.setOrderByClause(channelDTO.getOrderByClause());
        } else {
            example.setOrderByClause("created_at desc");
        }
        if (CommonUtils.isNotEmpty(channelDTO.getStart_date())) {
            criteria.andGreaterThanOrEqualTo("created_at", channelDTO.getStart_date());
        }
        if (CommonUtils.isNotEmpty(channelDTO.getEnd_date())) {
            criteria.andLessThanOrEqualTo("created_at", channelDTO.getEnd_date());
        }
        if (CommonUtils.isNotEmpty(channelDTO.getChannel_name())) {
            criteria.andLike("channel_name", "%" + channelDTO.getChannel_name() + "%");
        }
        if (CommonUtils.isNotEmpty(channelDTO.getUpdater())) {
            criteria.andEqualTo("updater", channelDTO.getUpdater());
        }
        if (CommonUtils.isNotNull(channelDTO.getSpid())) {
            criteria.andEqualTo("sp_id", channelDTO.getSpid());
        }

        if (CommonUtils.isNotEmpty(channelDTO.getChannel_id())) {
            criteria.andLike("channel_id", "%" + channelDTO.getChannel_id() + "%");
        }

        //已认领 加上用户id
        if (CommonUtils.isNotNull(channelDTO.getReceive()) && channelDTO.getReceive() == 1) {
            criteria.andEqualTo("auditor", channelDTO.getUserId());
        }
        //不是   我的已审 进来的,只查一种状态
        if (CommonUtils.isNotNull(channelDTO.getShenhe_status()) && channelDTO.getShenhe_status() != 3) {
            criteria.andEqualTo("shenhe_status", channelDTO.getShenhe_status());
            //是 我的已审,查通过和拒绝的两种状态
        } else if (CommonUtils.isNotNull(channelDTO.getShenhe_status()) && channelDTO.getShenhe_status() == 3) {
            criteria.andBetween("shenhe_status", CommonConst.AUDIT_SUCCESS, CommonConst.AUDIT_REFUSE);
        }

        PageHelper.startPage(channelDTO.getPage(), channelDTO.getPageSize(), true);
        List<ShyChannel> channelList = shyChannelMapper.selectByExample(example);
        PageInfo<ShyChannel> pageInfo = new PageInfo<>(channelList);
        return pageInfo;
    }

    /**
     * 获取频道详情
     *
     * @param id
     * @return
     */
    @Override
    public ShyChannel getDetailsChannel(Integer id) {
        ShyChannel comment = shyChannelMapper.selectByPrimaryKey(id);
        if (!comment.getShenhe_status().equals(CommonConst.AUDIT_INTT)) {
            String updater = CommonUtils.isEmpty(comment.getUpdater()) || "null".equals(comment.getUpdater()) ? "白名单" : comment.getUpdater();
            String auditorId = comment.getAuditor();
            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[] channelIds = ids.split(",");
        for (String channelId : channelIds) {
            ShyChannel channel = shyChannelMapper.selectByPrimaryKey(Integer.parseInt(channelId));

            JSONObject json = new JSONObject();
            json.put("state", state + 2);
            json.put("channel_id", channel.getChannel_id());
            json.put("msg", msg);
            JSONObject result;
            try {
                result = pass.channelPass(json, channel.getCallback());
            } catch (Exception e) {
                logger.error("频道回调失败,id:{},channel_id:{}", channel.getId(), channel.getChannel_id());
                success = false;
                break;
            }
            if (result == null) {
                logger.error("频道回调错误,且无返回信息。id:{},channel_id:{}", channel.getId(), channel.getChannel_id());
                success = false;
                break;
            } else {
                Integer code = result.getInteger("errorCode");
                if (code != 0) {
                    logger.error("频道回调错误,id:{},channel_id:{},errorCode:{},errorMessage:{}", channel.getId(), channel.getChannel_id(), code, result.getString("errorMessage"));
                    success = false;
                    break;
                }
                channel.setShenhe_status(state);
                channel.setShenhe_msg(msg);
                if (CommonUtils.isEmpty(userId)) {
                    channel.setUpdater(updater);
                    channel.setAuditor(null);
                } else {
                    channel.setAuditor(userId);
                    channel.setUpdater(null);
                }
                int i = shyChannelMapper.updateByPrimaryKey(channel);
                if (i != 1) {
                    success = false;
                }
            }

        }
        return success;
    }

    /**
     * 根据主键查询频道对象
     *
     * @param id
     * @return
     */
    @Override
    public ShyChannel selectByPrimaryKey(Integer id) {
        return shyChannelMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据主键修改频道对象
     *
     * @param channel
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(ShyChannel channel) {
        return shyChannelMapper.updateByPrimaryKeySelective(channel);
    }

    /**
     * 获得审核类型
     *
     * @param shyChannel
     */
    @Override
    public void getShenheType(ShyChannel shyChannel) {

        //获取直播频道免审sp
        List<Integer> sites = sitesService.getBusinessList(CommonConst.BUSINESS_CHANNEL);
        //初始化一个审核状态
        //审核类型,1:免审,2:机审,3:人工审
        Integer shenheType = CommonConst.AUDIT_TYPE_USER;
        //如果当前sp是免审sp
        if (sites.contains(shyChannel.getSp_id())) {
            //免审
            shenheType = CommonConst.AUDIT_TYPE_FREE;
        }
        shyChannel.setShenhe_type(shenheType);
    }

}