package net.vtstar.zhongtong.avi.ledlamp.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.user.mybatis.provider.OrderBy;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.utils.CollecUtils;
import net.vtstar.utils.domain.Return;
import net.vtstar.zhongtong.avi.equipment.mapper.EquipmentDataMapper;
import net.vtstar.zhongtong.avi.gateway.painting.domain.VocMachine;
import net.vtstar.zhongtong.avi.ledlamp.domain.vo.LedStationVO;
import net.vtstar.zhongtong.avi.ledlamp.service.LedService;
import net.vtstar.zhongtong.avi.monitoring.domain.vo.VocVO;
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.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(description = "led大屏监控模块")
@RequestMapping("/api/led")
@RestController
public class LedController {

    @Autowired
    private LedService ledService;

    @Autowired
    private EquipmentDataMapper dataMapper;

    @ApiOperation(value = "工位级led大屏信息监控", response = LedStationVO.class)
    @GetMapping(value = "/ledInfo")
    private Return getLEDStationInfo(@RequestParam(value = "ledNo") String ledNo) {

        LedStationVO ledStationVO = new LedStationVO();
        /*ledStationVO.setName("电泳");
        ArrayList<Warning> warnings = new ArrayList<>();
        Warning warning = new Warning();
        warning.setInfo("报警信息1111111111111111111111111111111111111");
        warning.setName("报警1");
        Warning warning1 = new Warning();
        warning1.setInfo("报警信息2222222222222222222222222222222222222222222");
        warning1.setName("报警2");
        warnings.add(warning);
        warnings.add(warning1);

        ledStationVO.setBusIn(new BigDecimal(33));
        ledStationVO.setBusOut(new BigDecimal(44));
        ledStationVO.setWarningList(warnings);

        ArrayList<WorkRoom> roomInfos = new ArrayList<>();
        WorkRoom roomInfo = new WorkRoom();
        roomInfo.setName("喷漆室1");
        roomInfo.setStationState(StationState.USE);
        roomInfo.setWorkingHours(new BigDecimal(30));

        WorkRoom roomInfo2 = new WorkRoom();
        roomInfo2.setName("喷漆室2");
        roomInfo2.setStationState(StationState.FREE);
        roomInfo2.setWorkingHours(new BigDecimal(22));

        roomInfos.add(roomInfo);
        roomInfos.add(roomInfo2);
        ledStationVO.setRoomInfoList(roomInfos);*/

        ledStationVO = ledService.getInfoByNo(ledNo);
        return Return.success(ledStationVO);
    }

    @ApiOperation(value = "led VOC浓度接口", response = VocVO.class)
    @GetMapping(value = "/info/VOC")
    private Return getVOCInfo() {

        List<Map<String, Object>> datas = dataMapper.findJoin(new WhereFilter() {{
            addOrderBy("create_time", OrderBy.DESC);
            setRows(0);
            setOffset(1);
        }}, VocMachine.class);
        VocVO vocVO = new VocVO();
        if(CollecUtils.isEmpty(datas)){
            vocVO.setExitCONC(new BigDecimal(100));
            vocVO.setNorthCONC(new BigDecimal(85));
            vocVO.setSouthCONC(new BigDecimal(5));
        }else {
            Map<String, Object> map = datas.get(0);
            vocVO.setSouthCONC(new BigDecimal(map.get("concentration2").toString()));
            vocVO.setNorthCONC(new BigDecimal(map.get("concentration1").toString()));
            if (null != map.get("concentration3")){
                vocVO.setExitCONC(new BigDecimal(map.get("concentration3").toString()));
            }
        }
        return Return.success(vocVO);
    }

    @ApiOperation(value = "当前本地时间请求")
    @GetMapping(value = "/currentTime")
    private Return getCurrentTime() {
        return Return.success(new Date());
    }
}
