package com.hwrj.cloud.admin.controller;

import com.hwrj.cloud.admin.service.BigScreamService;
import com.hwrj.cloud.admin.service.UmsCompanyService;
import com.hwrj.cloud.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Api(tags = "BigScreamController",description = "大屏数据展示")
@RequestMapping("/big")
@Slf4j
public class BigScreamController  {
    @Autowired
    private BigScreamService bigScreamService;

    @Autowired
    private UmsCompanyService companyService;

    @ApiOperation("林业态势-获取平台信息")
    @GetMapping("/1/data1")
    public CommonResult data11(@RequestParam(value = "id",required = false,defaultValue = "1")Long id){
        Map<String, Object> map = bigScreamService.data1(id);
        return CommonResult.success(map);
    }

    @ApiOperation("林业态势-获取供应商分布信息")
    @GetMapping("/1/data2")
    public CommonResult data12(@RequestParam(value = "compType",required = false)String compType){
        List<Map<String, Object>> map = bigScreamService.date2(compType);
        List<Map<String, Object>> maps = new ArrayList<>();
        for (Map<String,Object> m:map) {
            if (m.containsKey("lng")&&m.containsKey("lat")){
                String lng = m.get("lng").toString();
                String lat = m.get("lat").toString();
                List<String> str = new LinkedList<>();
                str.add(lng);
                str.add(lat);
                m.put("lnglat",str);
            }
            maps.add(m);
        }
        return CommonResult.success(maps);
    }

    @ApiOperation("产品现状-获取产品分布信息")
    @GetMapping("/1/data3")
    public CommonResult data13(){
        List<Map<String, Object>> map = bigScreamService.data4();
        List<Map<String,Object>>  list = new ArrayList<>();
        return CommonResult.success(map);
    }

    @ApiOperation("林业态势-获取产品分布信息")
    @GetMapping("/1/data6")
    public CommonResult data16(){

        String title = "供应信息";
        String[] titles = {"供应商名称","供应产品","规格","参考价格"};
        List<String> strings = bigScreamService.data6();
        Map<String, Object> listData = getListData(title, titles, strings);

        return CommonResult.success(listData);
    }

    @ApiOperation("林业态势-需求信息")
    @GetMapping("/1/data7")
    public CommonResult data17(){

        String title = "需求信息";
        String[] titles = {"需求主体","需求产品"};
        List<String> strings = bigScreamService.data7();
        Map<String, Object> listData = getListData(title, titles, strings);

        return CommonResult.success(listData);
    }

    @ApiOperation("林业态势-左下角")
    @GetMapping("/1/data8")
    public CommonResult data18(){


        String[] titles = {"id","name","num"};
        List<String> strings = bigScreamService.data8();
        List<Map<String, Object>> title = getTitle(strings, titles, "name", "企业分布");
        return CommonResult.success(title);
    }

    @ApiOperation("林业态势-幽魂")
    @GetMapping("/1/data9")
    public CommonResult data19(){


        String[] titles = {"id","name"};
        String title1 = "黔林优选企业名录";

        Map<String,Object> maps = new HashMap<>();
        maps.put("title",title1);

        List<String> strings = bigScreamService.data9();
        List<Map<String, Object>> title = getTitle(strings, titles, null, null);
        maps.put("detail",title);
        return CommonResult.success(maps);
    }

    /**
     * 分装返回大屏数据
     * @param title
     * @param titles
     * @param strings 必须用---分割成和titles一样长度的数组的字符串
     * @return
     */
    public Map<String,Object> getListData(String title,String[] titles,List<String> strings){
        Map<String,Object> has = new HashMap<>();
        has.put("title",title);
        List<Map<String,Object>> tabHeader = new LinkedList<>();
        for (int i = 0 ;i<titles.length;i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("name",titles[i]);
            tabHeader.add(map);
        }
        has.put("tabHeader",tabHeader);
        List<List<Map<String,Object>>> list = new ArrayList<>();
        for (String str:strings) {
            if (!StringUtils.isEmpty(str)){
                String[] split = str.split("---");
                List<Map<String,Object>> maps = new LinkedList<>();
                for (int i = 0;i<split.length;i++){
                    Map<String,Object> map = new HashMap<>();
                    map.put("value",split[i]);
                    maps.add(map);
                }
                list.add(maps);
            }

        }
        has.put("list",list);
        return has;
    }

    /**
     *
     * @param strings 数据
     * @param params
     * @return
     */
    public List<Map<String,Object>> getTitle(List<String> strings,String[] params,String title,String addStr){
        if (strings == null|| strings.size()<1){
            return null;
        }
        List<Map<String,Object>> list = new ArrayList<>();
        for (String str:strings) {
            Map<String,Object> map = new HashMap<>();
            String[] split = str.split("---");
            for (int i = 0;i<split.length;i++){
                String param = params[i];
                String s = split[i];
                if ((!StringUtils.isEmpty(title))&&title.equals(param)){
                    s=s+addStr;
                }
                map.put(param,s);
            }
            list.add(map);
        }
        return list;
    }



}
