package com.hwrj.cloud.admin.controller;

import com.hwrj.cloud.admin.dto.UmsCompanyDto;
import com.hwrj.cloud.admin.dto.UmsMemberOriginParam;
import com.hwrj.cloud.admin.dto.param.BigDataCompanyInfo;
import com.hwrj.cloud.admin.service.BigCompanyService;
import com.hwrj.cloud.admin.service.UmsCompanyService;
import com.hwrj.cloud.admin.service.UmsMemberOriginService;
import com.hwrj.cloud.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/big/company")
@Api(tags = "BigCompanyController",description ="涉林大屏数据")
public class BigCompanyController {

    @Autowired
    private BigCompanyService bigCompanyService;

    @Autowired
    private UmsCompanyService umsCompanyService;

    @Autowired
    private UmsMemberOriginService umsMemberOriginService;

    @GetMapping("/list")
    @ApiOperation("涉林分布-供应商信息获取")
    public CommonResult compList(@RequestParam(value = "id",required = false)Long id, @RequestParam(value = "key",required = false)String key){
        List<Map<String, Object>> companyList = bigCompanyService.getCompanyList(id, key);
        List<Map<String,Object>> sh = new ArrayList<>();
        for (Map<String,Object> map:companyList){
            List<String> strings = new LinkedList<>();
            if (map.containsKey("lng")){
                strings.add(map.get("lng").toString());
            }
            if (map.containsKey("latitude")){
                strings.add(map.get("latitude").toString());
            }


            map.put("laglat",strings);
            map.put("legend","1");
            List<Map<String,Object>> list = new LinkedList<>();

            if (map.containsKey("address")){
                Map<String,Object> map2 = new HashMap<>();
                map2.put("value","联系地址："+map.get("address"));
                list.add(map2);
            }
            if (map.containsKey("compConPerson")){
                Map<String,Object> map1 = new HashMap<>();
//                map1.put("value","联系人："+map.get("compConPerson"));
                map1.put("value","");
                list.add(map1);
            }
            map.put("info",list);
            sh.add(map);
        }
        return CommonResult.success(companyList);
    }

    @GetMapping("/detail")
    @ApiOperation("涉林分布-供应商详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "供应商id",value = "分类id",required = false,paramType = "Long"),
    })
    public CommonResult compDetail(@RequestParam(value = "id",required = true)Long id){
        UmsCompanyDto detailCompany = umsCompanyService.getDetailCompany(id);
        UmsMemberOriginParam param = new UmsMemberOriginParam();
        param.setCompId(detailCompany.getId());
        List<UmsMemberOriginParam> list = umsMemberOriginService.list(param);
        List<BigDataCompanyInfo> listBate = new ArrayList<>();
        BigDataCompanyInfo big1 = new BigDataCompanyInfo();
        big1.setId("1");
        big1.setTitle("企业名称");
        big1.setData(detailCompany.getCompName());
        listBate.add(big1);
        BigDataCompanyInfo big2 = new BigDataCompanyInfo();
        big2.setId("2");
        big2.setTitle("联系人");
        big2.setData(detailCompany.getCompConPerson());
        listBate.add(big2);
        BigDataCompanyInfo big3 = new BigDataCompanyInfo();
        big3.setId("3");
        big3.setTitle("联系方式");
        big3.setData(detailCompany.getCompConNum());
        listBate.add(big3);

        BigDataCompanyInfo big4 = new BigDataCompanyInfo();

        big4.setId("4");
        big4.setTitle("资质");
        big4.setData(detailCompany.getCompQual());
        listBate.add(big4);

        BigDataCompanyInfo big5 = new BigDataCompanyInfo();
        big5.setId("5");
        big5.setTitle("类型");
        big5.setData(detailCompany.getCompanyType());
        listBate.add(big5);

        BigDataCompanyInfo big6 = new BigDataCompanyInfo();
        big6.setId("6");
        big6.setTitle("是否有种植基地");
        if (list != null && list.size()>0){
            big6.setData("有");
        }else {
            big6.setData("没有");
        }
        listBate.add(big6);
        BigDataCompanyInfo big7 = new BigDataCompanyInfo();
        big7.setId("7");
        big7.setTitle("地址");
        big7.setData(detailCompany.getProvinceId()+detailCompany.getCityId()+detailCompany.getCountyId());
        listBate.add(big7);
        Map<String,Object> map = new HashMap<>();
        map.put("desc",detailCompany.getCompSum());
        map.put("list",listBate);

        return CommonResult.success(map);
    }

    @GetMapping("/cat")
    @ApiOperation("涉林分布-产品分类信息（刺梨，竹子，竹笋）")
    public CommonResult cat(){
        String[] title = {"id","type","num"};
        List<String> dto = bigCompanyService.getCat();
        List<Map<String, Object>> title1 = getTitle(dto, title);
        Map<String,Object> list = new HashMap<>();
        list.put("list",title1);
        return CommonResult.success(title1);
    }

    /**
     *
     * @param strings 数据
     * @param params
     * @return
     */
    public List<Map<String,Object>> getTitle(List<String> strings,String[] params){
        if (strings == null|| strings.size()<1){
            return null;
        }
        Map<String,Object> maps = new HashMap<>();
        maps.put("id",null);
        maps.put("type","全部");

        List<Map<String,Object>> list = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal("0");
        for (String str:strings) {
            Map<String,Object> map = new HashMap<>();
            String[] split = str.split("---");
            String titles = null;
            BigDecimal decimal = new BigDecimal("0");
            for (int i = 0;i<split.length;i++){
                String param = params[i];
                String s = split[i];
                map.put(param,s);
                if ("num".equals(param)){
                    try {
                        decimal = new BigDecimal(s);
                        bigDecimal =bigDecimal.add(decimal);
                    }catch (Exception e){

                    }

                }
                if ("type".equals(param)){
                    titles=s;
                }

            }

            if (Integer.valueOf(decimal.toString())>0){
                map.put("title",titles+"涉林企统计");
                list.add(map);
            }

        }
        maps.put("num",bigDecimal);
        maps.put("title","全部涉林企业分布");
        List<Map<String,Object>> list1 = new ArrayList<>();
        list1.add(maps);
        list1.addAll(list);
        return list1;
    }
}
