ChannelGroupRest.java 5.39 KB
package com.yoho.unions.channel.restapi;

import com.yoho.service.model.union.request.ChannelGroupRequestBO;
import com.yoho.service.model.union.request.ChannelMessageRequestBO;
import com.yoho.service.model.union.request.ChannelUserRequest;
import com.yoho.service.model.union.request.UserInfoBO;
import com.yoho.service.model.union.response.PageChannelGroupRspBO;
import com.yoho.unions.channel.service.IChannelGroupService;
import com.yoho.unions.common.ApiResponse;
import org.apache.commons.lang3.StringUtils;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

import static com.yoho.unions.common.utils.YHPreconditions.checkNotNull;

/**
 * Created by yoho on 2017/2/15.
 */
@Controller
@RequestMapping("/channel")
public class ChannelGroupRest {
    static Logger logger = LoggerFactory.getLogger(ChannelGroupRest.class);

    @Autowired
    private IChannelGroupService channelGroupService;

    @Autowired
    private HttpSession session;


    @RequestMapping("/queryChannelGroupList")
    @ResponseBody
    public ApiResponse queryChannelGroupList( ChannelGroupRequestBO channelGroupRequestBO) {
        logger.info("enter queryChannelGroupList. param channelGroupRequestBO={}", channelGroupRequestBO);
        try {
            PageChannelGroupRspBO pageChannelGroupRspBO = channelGroupService.queryChannelGroupList(channelGroupRequestBO);
            return new ApiResponse.ApiResponseBuilder().code(200).message("成功").data(pageChannelGroupRspBO).build();
        } catch (Exception e) {
            logger.warn("queryChannelGroupList occurs Exception e {}",e.getMessage());
            return new ApiResponse.ApiResponseBuilder().code(500).message("失败").build();
        }
    }

    @RequestMapping("/sendMessage")
    @ResponseBody
    public ApiResponse sendMessage(ChannelMessageRequestBO channelGroupRequestBO,HttpSession session){
        logger.info("enter sendMessage. param channelGroupRequestBO={},session={}", channelGroupRequestBO,session);
        if(StringUtils.isEmpty(channelGroupRequestBO.getContent()) || channelGroupRequestBO.getGroupId()==0){
            return new ApiResponse.ApiResponseBuilder().code(500).message("缺少参数").build();
        }
        UserInfoBO userInfoBO= (UserInfoBO) session.getAttribute("userSession");
        logger.info("ChannelGroupRest.sendMessage:useinfo is {}", userInfoBO);

        if(userInfoBO==null){
            return new ApiResponse.ApiResponseBuilder().code(500).message("获取发送人不成功,请刷新页面!").build();
        }
        int result = channelGroupService.sendMessage(channelGroupRequestBO,userInfoBO.getPid());
//        int result = channelGroupService.sendMessage(channelGroupRequestBO,14);
        if(result==1){
            return new ApiResponse.ApiResponseBuilder().code(200).message("成功").build();
        }else if(result==2){
            return new ApiResponse.ApiResponseBuilder().code(501).message("再次发送时间不能是当天!").build();
        }
        else{
            return new ApiResponse.ApiResponseBuilder().code(500).message("失败").build();
        }
    }


    /**
     * 批量添加黑名单,逗号分隔手机号
     * @param mobiles
     * @return
     */
    @RequestMapping("/addBlackList")
    @ResponseBody
    public  ApiResponse addBlackList(String mobiles){
        logger.info("enter addBlackList. param mobiles={}", mobiles);
        if(StringUtils.isEmpty(mobiles)){
            return new ApiResponse.ApiResponseBuilder().code(500).message("缺少参数").build();
        }
        channelGroupService.addBlackList(mobiles);
        return new ApiResponse.ApiResponseBuilder().code(200).message("成功").build();
    }

    @RequestMapping("/deleteGroup")
    @ResponseBody
    public ApiResponse deleteGroup(int groupId){
        logger.info("enter deleteGroup. param groupId={}", groupId);
        if(groupId<1){
            return new ApiResponse.ApiResponseBuilder().code(500).message("缺少参数").build();
        }
        int count = channelGroupService.deleteGroup(groupId);
        if (count>0){
            return new ApiResponse.ApiResponseBuilder().code(200).message("成功").build();
        }else{
            return new ApiResponse.ApiResponseBuilder().code(500).message("删除失败").build();
        }

    }

    @RequestMapping("/getCondition")
    @ResponseBody
    public ApiResponse getCondition(String ids){
        logger.info("enter getCondition. param ids={}", ids);
        Map<Integer,String> result = channelGroupService.getCondition(ids);
        return new ApiResponse.ApiResponseBuilder().code(200).message("成功").data(result).build();
    }

    /**
     * 增加分组
     */
    @ResponseBody
    @RequestMapping("addGroup")
    public ApiResponse addGroup(@RequestBody ChannelUserRequest request) {

        // 设置系统管理员 ID
        UserInfoBO userInfo = (UserInfoBO) session.getAttribute("userSession");
        checkNotNull(userInfo, "获取当前用户信息失败,请刷新当前页面");

        channelGroupService.saveGroup(request, userInfo);
        return new ApiResponse.ApiResponseBuilder().build();
    }

}