ChannelControllerApi.java 3.39 KB
package com.cnlive.shenhe.api;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.entity.ShyChannel;
import com.cnlive.shenhe.service.ChannelService;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonConstStates;
import com.cnlive.shenhe.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @author Administrator
 */
@Controller
@RequestMapping("/api/channel")
public class ChannelControllerApi {
    private static Logger logger = LoggerFactory.getLogger(ChannelControllerApi.class);
    @Autowired
    ChannelService channelService;

    /**
     * @Auther: liwei
     * @Date: 2019/08/17 11:17
     * @Description: 频道送审接口
     */
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @PostMapping("/import")
    @ResponseBody
    public ResponseBean impotChannel(String param) {
        logger.info("频道入库,param=====" + param);
        JSONObject json = JSONObject.parseObject(param);
        boolean yorN;
        //判断是否存在
        ShyChannel shyChannel = new ShyChannel();
        shyChannel.setChannel_id(json.getString("channelId"));
        List<ShyChannel> channelList = channelService.findshyChannel(shyChannel);
        Integer spId;
        try {
            spId = json.getInteger("spId");
        } catch (Exception e) {
            spId = null;
        }
        if (channelList.size() > 0) {
            //  存在则更新
            ShyChannel shyChannel1 = channelList.get(0);
            yorN = saveOrUpdateChannel(shyChannel1, json, spId);
            logger.info("频道修改操作");
        } else {
            // 创建频道时间
            shyChannel.setCreated_at(CommonUtils.getCurrentTime());
            //  不存在则保存
            yorN = saveOrUpdateChannel(shyChannel, json, spId);
            logger.info("频道保存操作");
        }
        if (yorN) {
            return new ResponseBean(ErrorEnum.SUCCESS);
        } else {
            return new ResponseBean(ErrorEnum.ERROR);
        }
    }

    /**
     * 保存和修改频道
     *
     * @param shyChannel
     * @param json
     * @param spId
     * @return
     */
    private boolean saveOrUpdateChannel(ShyChannel shyChannel, JSONObject json, Integer spId) {
        // 如果不存在直接插入  存在就直接修改
        shyChannel.setShenhe_status(CommonConst.AUDIT_INTT);
        shyChannel.setChannel_name(json.getString("channelName"));
        shyChannel.setChannel_img(json.getString("face"));
        shyChannel.setCallback(json.getString("callback"));
        shyChannel.setSp_id(spId);
        //是否下线 0表示正常 1表示下线
        shyChannel.setOffline(CommonConstStates.ZHENGCHANG);
        shyChannel.setUpdated_at(CommonUtils.getCurrentTime());
        channelService.getShenheType(shyChannel);
        if (shyChannel.getId() == null) {
            return channelService.impotChannel(shyChannel);
        } else {
            return channelService.updateshyChannel(shyChannel);
        }
    }
}