ChannelController.java 10 KB
package com.sincere.wechatbusiness.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.sincere.wechatbusiness.dto.BaseDto;
import com.sincere.wechatbusiness.dto.ChannelDto;
import com.sincere.wechatbusiness.model.*;
import com.sincere.wechatbusiness.service.*;
import com.sincere.wechatbusiness.utils.Page;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

@RestController
public class ChannelController {
    @Autowired
    ChannelService channelService;

    @Autowired
    ChannelProductService channelProductService;

    @Autowired
    BannerService bannerService;

    @Autowired
    AttentionService attentionService;

    @Autowired
    CatalogService catalogService;

    @Autowired
    CatalogContentService catalogContentService;

    @RequestMapping(value = "GetChannelList",method = RequestMethod.POST)
    @ApiOperation(value = "获取渠道商列表")
    public BaseDto<Page<Channel>> GetChannelList(@RequestBody ChannelDto channelDto){
        BaseDto<Page<Channel>> result=new BaseDto<>();
        Channel channel=new Channel();
        channel.setName(channelDto.getName());
        channel.setState(1);
        result.setData(channelService.getList(channel,channelDto.getPage(),channelDto.getPageSize()));
        return result;
    }

    @RequestMapping(value = "GetPackageList",method = RequestMethod.POST)
    @ApiOperation(value = "获取代理商品列表")
    public BaseDto<List<DiscountPackage>> GetPackageList(){
        BaseDto<List<DiscountPackage>> result=new BaseDto<>();
        String r=Get("https://mytest.myjxt.com:51314/University/getPackageList","");
        JSONObject jsonObject= JSONObject.parseObject(r);
        String data = jsonObject.getString("data");
        List<DiscountPackage> list=JSON.parseObject(data,new TypeReference<List<DiscountPackage>>(){});
        result.setData(list);
        return result;
    }

    @RequestMapping(value = "AddChannel",method = RequestMethod.POST)
    @ApiOperation(value = "新增渠道商")
    public BaseDto AddChannel(@RequestBody Channel channel){
        channelService.insert(channel);
        if(channel.getChannelProductList()!=null&&channel.getChannelProductList().size()>0) {
            for(ChannelProduct channelProduct:channel.getChannelProductList()){
                channelProduct.setChannelId(channel.getId());
                channelProductService.insert(channelProduct);
            }
        }
        return new BaseDto();
    }

    @RequestMapping(value = "GetChannelDetail",method = RequestMethod.GET)
    @ApiOperation(value = "获取渠道商详情")
    public BaseDto<Channel> GetChannelDetail(int id){
        BaseDto<Channel> result=new BaseDto<>();
        Channel channel=channelService.getDetail(id);
        channel.setChannelProductList(channelProductService.getList(id));
        result.setData(channel);
        return result;
    }

    @RequestMapping(value = "UpdateChannel",method = RequestMethod.POST)
    @ApiOperation(value = "编辑渠道商")
    public BaseDto UpdateChannel(@RequestBody Channel channel){
        channelService.update(channel);
        if(channel.getChannelProductList()!=null&&channel.getChannelProductList().size()>0){
            channelProductService.deleteChannelProduct(channel.getId());
            for (ChannelProduct channelProduct:channel.getChannelProductList()){
                channelProduct.setChannelId(channel.getId());
                channelProductService.insert(channelProduct);
            }
        }
        return new BaseDto();
    }


    @RequestMapping(value = "DeleteChannel",method = RequestMethod.GET)
    @ApiOperation(value = "删除渠道商")
    public BaseDto DeleteChannel(int id){
        channelService.deleteChannel(id);
        return new BaseDto();
    }

    @RequestMapping(value = "UpdateChannelProduct",method = RequestMethod.POST)
    @ApiOperation(value = "编辑代理商品")
    public BaseDto UpdateChannelProduct(@RequestBody ChannelProduct channelProduct){
        channelProductService.update(channelProduct);
        return new BaseDto();
    }

    @RequestMapping(value = "GetBannerList",method = RequestMethod.GET)
    @ApiOperation(value = "获取Banner轮播图列表")
    public BaseDto<List<Banner>> GetBannerList(int id){
        BaseDto<List<Banner>> result=new BaseDto<>();
        result.setData(bannerService.getList(id));
        return result;
    }

    @RequestMapping(value = "AddBanner",method = RequestMethod.POST)
    @ApiOperation(value = "添加Banner轮播图")
    public BaseDto AddBanner(@RequestBody Banner banner){
        bannerService.insert(banner);
        return new BaseDto();
    }

    @RequestMapping(value = "UpdateBanner",method = RequestMethod.POST)
    @ApiOperation(value = "编辑Banner轮播图")
    public BaseDto UpdateBanner(@RequestBody Banner banner){
        bannerService.update(banner);
        return new BaseDto();
    }

    @RequestMapping(value = "DeleteBanner",method = RequestMethod.GET)
    @ApiOperation(value = "删除Banner轮播图")
    public BaseDto DeleteBanner(int id){
        bannerService.deleteBanner(id);
        return new BaseDto();
    }

    @RequestMapping(value = "GetAttention",method = RequestMethod.GET)
    @ApiOperation(value = "获取关注配置")
    public BaseDto<Attention> GetAttention(int id){
        BaseDto<Attention> result=new BaseDto<>();
        result.setData(attentionService.getDetail(id));
        return result;
    }

    @RequestMapping(value = "AddAttention",method = RequestMethod.POST)
    @ApiOperation(value = "添加关注配置")
    public BaseDto AddAttention(@RequestBody Attention attention){
        attentionService.insert(attention);
        return new BaseDto();
    }

    @RequestMapping(value = "UpdateAttention",method = RequestMethod.POST)
    @ApiOperation(value = "编辑关注配置")
    public BaseDto UpdateAttention(@RequestBody Attention attention){
        attentionService.update(attention);
        return new BaseDto();
    }

    @RequestMapping(value = "GetCatalog",method = RequestMethod.GET)
    @ApiOperation(value = "获取栏目")
    public BaseDto<Catalog> GetCatalog(int channelId,int sort){
        BaseDto<Catalog> result=new BaseDto<>();
        Catalog catalog=new Catalog();
        catalog.setChannelId(channelId);
        catalog.setSort(sort);
        catalog=catalogService.getDetail(catalog);
        catalog.setCatalogContentList(catalogContentService.getList(catalog.getId()));
        result.setData(catalog);
        return result;
    }

    @RequestMapping(value = "AddCatalog",method = RequestMethod.POST)
    @ApiOperation(value = "新增栏目")
    public BaseDto AddCatalog(@RequestBody Catalog catalog){
        catalogService.insert(catalog);
        return new BaseDto();
    }

    @RequestMapping(value = "UpdateCatalog",method = RequestMethod.POST)
    @ApiOperation(value = "编辑栏目")
    public BaseDto UpdateCatalog(@RequestBody Catalog catalog){
        catalogService.update(catalog);
        return new BaseDto();
    }

    @RequestMapping(value = "AddCatalogContent",method = RequestMethod.POST)
    @ApiOperation(value = "新增栏目内容")
    public BaseDto AddCatalogContent(@RequestBody CatalogContent catalogContent){
        catalogContentService.insert(catalogContent);
        return new BaseDto();
    }

    @RequestMapping(value = "UpdateCatalogContent",method = RequestMethod.POST)
    @ApiOperation(value = "编辑栏目内容")
    public BaseDto UpdateCatalogContent(@RequestBody CatalogContent catalogContent){
        catalogContentService.update(catalogContent);
        return new BaseDto();
    }

    @RequestMapping(value = "DeleteCatalogContent",method = RequestMethod.GET)
    @ApiOperation(value = "删除栏目内容")
    public BaseDto DeleteCatalogContent(int id){
        catalogContentService.deleteCatalogContent(id);
        return new BaseDto();
    }

    public String Get(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
}