package cn.wise.sc.energy.power.plant.business.controller;

import cn.hutool.core.util.StrUtil;
import cn.wise.sc.energy.power.plant.business.bean.DataPower;
import cn.wise.sc.energy.power.plant.business.domain.DetecInteractionQuery;
import cn.wise.sc.energy.power.plant.business.domain.vo.EntityVo;
import cn.wise.sc.energy.power.plant.business.service.IDetectionService;
import cn.wise.sc.energy.power.plant.business.service.IPlantInfoService;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @description: 电厂信息controller
 * @author: qh
 * @create: 2020-07-20 18:13
 **/
@CrossOrigin
@RestController
@RequestMapping("plantInfo/")
public class PlantInfoController {

    final
    IPlantInfoService iPlantInfoService;
    final
    IDetectionService iDetectionService;

    public PlantInfoController(IPlantInfoService iPlantInfoService,
                               IDetectionService iDetectionService) {
        this.iPlantInfoService = iPlantInfoService;
        this.iDetectionService = iDetectionService;
    }

    @GetMapping("allPlantInfo")
    public BaseResponse<List<EntityVo>> getAllPlantInfo() {
        List<EntityVo> list = iPlantInfoService.list();
        return BaseResponse.okData(list);
    }

    @GetMapping("power/{plantId}")
    public BaseResponse<Double> getTotalPower(@PathVariable("plantId")
                                              @NotEmpty(message = "电厂id不能为空!") String plantId) {
        return iPlantInfoService.getTotalPower(plantId);
    }

    @GetMapping("detection")
    public BaseResponse<String> startDetection() {
        String sessionId = UUID.randomUUID().toString();
        Boolean aBoolean = iDetectionService.startDetection(sessionId);
        if (aBoolean) {
            return BaseResponse.okData(sessionId);
        } else {
            return BaseResponse.errorMsg("启动失败!");
        }
    }

    @PostMapping("interaction")
    public BaseResponse<Boolean> detectionInteractionSelect(@RequestBody DetecInteractionQuery query) {
        Boolean aBoolean = iDetectionService.interactionChose(query.getId(), query.getQid(), query.getSelect());
        if (aBoolean) {
            return BaseResponse.okData(true);
        } else {
            return BaseResponse.errorMsg("请求失败!");
        }
    }

    @GetMapping("plant/initPower")
    public BaseResponse<DataPower> getInitPower(String plantId, String deviceId) {
        DataPower initPower = iPlantInfoService.getInitPower(plantId, deviceId);
        if (initPower == null) {
            return BaseResponse.errorMsg("当前时间点没有数据!");
        }

        List<DataPower.PowerPoints> dataList = initPower.getDataList();
        Map<String, DataPower.PowerPoints> result = new HashMap<>();
        if (!CollectionUtils.isEmpty(dataList)) {
            Map<String, List<DataPower.PowerPoints>> map = new HashMap<>();
            map = initPower.getDataList().stream().collect(Collectors.groupingBy(item -> item.getKksCode().substring(0, 8)));
            map.forEach((key, value) -> {
                //根据deviceId获取list,根据kkscode排序,选择value非空的第一个值
                result.putIfAbsent(key.substring(0, 8), value.stream().sorted(Comparator.comparing(DataPower.PowerPoints::getKksCode)).filter(item -> !CollectionUtils.isEmpty(item.getValue())).findFirst().orElse(null));
            });
        }
        initPower.setDataList(null);
        //如果是机组
        if (StrUtil.isNotBlank(deviceId)) {
            initPower.setDataMap(new HashMap<String, DataPower.PowerPoints>() {{
                put(deviceId, result.get(deviceId));
            }});
            return BaseResponse.okData(initPower);
        } else {
            initPower.setDataMap(result);
            return BaseResponse.okData(initPower);
        }

    }
}
