ChannelServiceImpl.java 8.58 KB
package com.cnlive.shenhe.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShyChannel;
import com.cnlive.shenhe.entity.ShyChannel;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.entity.ShyUsersfree;
import com.cnlive.shenhe.entity.ShyVideos;
import com.cnlive.shenhe.mapper.ShyChannelMapper;
import com.cnlive.shenhe.mapper.ShyChannelMapper;
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.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

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

    private static Logger logger = LoggerFactory.getLogger(ChannelServiceImpl.class);
    @Autowired
    ShyChannelMapper shyChannelMapper;
    @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<ShyChannel> findshyChannel(ShyChannel shyChannel) {
        List<ShyChannel> channelList = shyChannelMapper.selectByRowBounds(shyChannel, new RowBounds(0, 100));
        return channelList;
    }

    public boolean updateshyChannel(ShyChannel shyChannel) {
        int result = shyChannelMapper.updateByPrimaryKey(shyChannel);
        if (result == 0) {
            return false;
        }
        return true;
    }

    public boolean impotChannel(ShyChannel shyChannel) {
        int result = shyChannelMapper.insertSelective(shyChannel);
        if (result == 0) {
            return false;
        }
        return true;
    }

    public int updateChannel(Integer id, Integer state, String msg) {
        ShyChannel shyChannel = shyChannelMapper.selectByPrimaryKey(id);
        shyChannel.setShenhe_status(state);
        shyChannel.setShenhe_msg(msg);
        return shyChannelMapper.updateByPrimaryKeySelective(shyChannel);
    }

    public PageInfo<ShyChannel> getPage(String channelId, String channelName, Integer receive, Date start_date, Date end_date, Integer shenhe_status, String updater, Integer page, Integer pageSize, String orderByClause, Integer spid, String userId) {
        Example example = new Example(ShyChannel.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(orderByClause)) {
            example.setOrderByClause(orderByClause);
        } else {
            example.setOrderByClause("created_at desc");
        }
        if (CommonUtils.isNotNull(start_date) ) {
            criteria.andGreaterThanOrEqualTo("created_at", start_date);
        }
        if (CommonUtils.isNotNull(end_date)) {
        	criteria.andLessThanOrEqualTo("created_at", end_date);
        }
        if (CommonUtils.isNotEmpty(channelName)) {
            criteria.andLike("channel_name", "%" + channelName + "%");
        }
        if (CommonUtils.isNotEmpty(updater)) {
            criteria.andEqualTo("updater", updater);
        }
        if (CommonUtils.isNotNull(spid)) {
            criteria.andEqualTo("spid", spid);
        }
        
        if (CommonUtils.isNotEmpty(channelId)) {
            criteria.andLike("channel_id", "%" + channelId + "%");
        }
        
        if (CommonUtils.isNotNull(receive)&& receive==1) {//已认领 加上用户id
        	criteria.andEqualTo("auditor", userId);
        }
        if (CommonUtils.isNotNull(shenhe_status) && shenhe_status != 3) {//不是   我的已审 进来的,只查一种状态
            criteria.andEqualTo("shenhe_status", shenhe_status);
        } else if (CommonUtils.isNotNull(shenhe_status) && shenhe_status == 3) {//是 我的已审,查通过和拒绝的两种状态
        	Example.Criteria criteria1 = example.createCriteria();
        	criteria1.orEqualTo("shenhe_status", 1);
        	criteria1.orEqualTo("shenhe_status", 2);
        	example.and(criteria1);
        }

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

    public ShyChannel getDetailsComment(Integer id) {
        ShyChannel comment = shyChannelMapper.selectByPrimaryKey(id);
        if (comment.getShenhe_status() != CommonConst.AUDIT_INTT) {
        	String updater = CommonUtils.isEmpty(comment.getUpdater()) || "null".equals(comment.getUpdater())? "白名单":comment.getUpdater();
            String auditor_id = comment.getAuditor();
            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[] 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 = new JSONObject();
            try {
                result = Pass.commentPass(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;
    }

    public ShyChannel selectByPrimaryKey(Integer id) {
        ShyChannel shyChannel = shyChannelMapper.selectByPrimaryKey(id);
        return shyChannel;
    }
    public int updateByPrimaryKeySelective(ShyChannel channel) {
        return shyChannelMapper.updateByPrimaryKeySelective(channel);
    }
    
    /**
     * 获得审核类型
     * @param shyChannel
     */
    public void getShenheType(ShyChannel shyChannel){
		
		//获取直播频道免审sp
        List<Integer> sites = sitesService.getBusinessList(CommonConst.BUSINESS_CHANNEL);
        //初始化一个审核状态
        Integer shenheType=3;//审核类型,1:免审,2:机审,3:人工审
        //如果当前sp是免审sp
        if(sites.contains(shyChannel.getSp_id())){
        	shenheType=1;//免审
        }
        shyChannel.setShenhe_type(shenheType);
	}

}