package com.bonus.zlpt.bigscreen.controller;

import com.bonus.zlpt.bigscreen.domain.vo.*;
import com.bonus.zlpt.common.core.web.controller.BaseController;
import com.bonus.zlpt.common.core.web.domain.AjaxResult;
import com.bonus.zlpt.bigscreen.service.BmCompanyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 企业信息表(BmCompanyInfo)表控制层
 *
 * @author makejava
 * @since 2023-11-30 15:01:48
 */
@RestController
@RequestMapping("bmCompanyInfo")
public class BmCompanyInfoController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private BmCompanyInfoService bmCompanyInfoService;

    /**
     * 根据企业类型查询对应数量以及总数量
     *
     * @param
     * @return
     */
    @GetMapping("/type-list")
    public AjaxResult typeList() {
        List<CoTypeVo> list = bmCompanyInfoService.selectTypeList();
        HashMap<String, Object> map = new HashMap<>(3);
        int totalCount = 0;
        for (CoTypeVo coTypeVo : list) {
            totalCount += coTypeVo.getCount();
        }
        map.put("total", totalCount);
        for (CoTypeVo coTypeVo : list) {
            if (coTypeVo.getCoType().equals("社会企业")) {
                map.put("social", coTypeVo);
            } else if (coTypeVo.getCoType().equals("南网集团企业")) {
                map.put("grid", coTypeVo);
            } else {
                map.put("corporation", coTypeVo);
            }
        }
        return success(map);
    }

    /**
     * 根据企业类型所属查询设备类型数量及经纬度
     *
     * @return
     */
    @GetMapping("/coltd-list")
    public AjaxResult coltdList() {

        List<ColtdTypeVo> list = bmCompanyInfoService.selectColtdList();

        List<TestVo> voList = new ArrayList<>();
        for (ColtdTypeVo coltdTypeVo : list) {
            String companyType = coltdTypeVo.getCompanyType();
            TestVo testVo;
            String maStatus = coltdTypeVo.getMaStatus();
            testVo = isExsit(companyType, voList);
            if(testVo == null){
                testVo = new TestVo();
                voList.add(testVo);
                testVo.setCompanyName(companyType);
            }
            ColtdVo coltdVo = getColtdVoByStatus(testVo.getColtdVo(),maStatus);
            coltdVo.setCount(coltdVo.getCount()+1);
            coltdVo.getList().add(new LonVo(coltdTypeVo.getMaId(), coltdTypeVo.getLon(), coltdTypeVo.getLat()));

        }
        return success(voList);
    }

    private ColtdVo getColtdVoByStatus(List<ColtdVo> coltdVos, String maStatus) {

        for (ColtdVo coltdVo : coltdVos) {
            if(maStatus.equals(coltdVo.getStatus())){
                return coltdVo;
            }
        }
        return null;

    }

    private TestVo isExsit(String companyType,List<TestVo> testVos) {

        for (TestVo testVo : testVos) {
            if(companyType.equals(testVo.getCompanyName())){
                return testVo;
            }
        }
        return null;
    }


    /**
     * 根据时间查询设备历史经纬度
     * @param
     * @return
     */
    @GetMapping("/time-list/{beginTime}/{endTime}/{maId}")
    public AjaxResult timeList(@PathVariable("beginTime") String beginTime,
                               @PathVariable("endTime") String endTime,@PathVariable("maId") String maId)
    {
        List<LatVo> list = bmCompanyInfoService.selectTimeList(beginTime, endTime, maId);
        return success(list);
    }
}

