package com.bjtdba.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjtdba.system.service.SystemGroupDataService;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system_service/nologin")
public class NewHomeController {

    @Autowired
    public SystemGroupDataService systemGroupDataService;

    @GetMapping("/banner/{key}")
    public Object banner(@PathVariable String key) throws UnsupportedEncodingException {
        if(key.equals("")){
            ResponseUtil.fail("参数错误");
        }
        List<Object> data=new ArrayList<Object>();
        List<String> value=systemGroupDataService.getsystemGroupDataListbyKey(key);
        for (String str: value ) {
            JSONObject object=JSON.parseObject(str);
            String name=object.get("name").toString();
            data.add(object);
        }
//        JSONObject object=JSON.parseObject(value);
        return ResponseUtil.ok(data);
    }

    /**
     * 热门搜索
     * @param key
     * @return
     * @throws Exception
     */
    @GetMapping("/common/{key}")
    public Object common(@PathVariable String key) throws Exception {
        if(key.equals("")){
            ResponseUtil.fail("参数错误");
        }
        List<Object> data=new ArrayList<Object>();
        List<String> value=systemGroupDataService.getsystemGroupDataListbyKey(key);
        for (String str: value ) {
            JSONObject object=JSON.parseObject(str);
            data.add(object);
        }
        return ResponseUtil.ok(data);
    }
}
