package com.ozs.web.controller.accountmanagment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.common.annotation.Log;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.StringUtils;
import com.ozs.entity.BaseTerminal;
import com.ozs.entity.BaseVehicle;
import com.ozs.entity.BaseVehicleTerminal;
import com.ozs.entity.MsgHeartbeatAlarmMessage;
import com.ozs.entity.VehiclePosition;
import com.ozs.entity.vo.MsgAlarmVo;
import com.ozs.service.BaseTerminalService;
import com.ozs.service.BaseVehicleService;
import com.ozs.service.BaseVehicleTerminalService;
import com.ozs.service.MsgHeartbeatAlarmMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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 java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/service/msgHeartbeatAlarmMessage")
public class MsgHeartbeatAlarmMessageController extends BaseController {

    @Autowired
    private MsgHeartbeatAlarmMessageService msgHeartbeatAlarmMessageService;
    @Autowired
    private BaseVehicleTerminalService baseVehicleTerminalService;
    @Autowired
    private BaseVehicleService baseVehicleService;
    @Autowired
    private BaseTerminalService baseTerminalService;

    /**
     * 心跳报警信息记录表分页
     *
     * @param
     * @return
     */
    @PostMapping("/list")
    @Log(title = "报警和心跳交互信息表", businessType = BusinessType.SELECT)
    public AjaxResult selectBaseLineManagement(@RequestBody MsgAlarmVo msgAlarmVo) {
        Integer pageNum = msgAlarmVo.getPageNum().intValue();
        Integer pageSize = msgAlarmVo.getPageSize().intValue();
        List<MsgHeartbeatAlarmMessage> list = msgHeartbeatAlarmMessageService.selectBaseLineManagement(msgAlarmVo.getAlarmId());
        IPage<MsgHeartbeatAlarmMessage> page = msgHeartbeatAlarmMessageService.listToPage(list, pageNum, pageSize);
        if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
            List<MsgHeartbeatAlarmMessage> dto1 = page.getRecords().stream().map(o -> {
                LambdaQueryWrapper<BaseTerminal> wrapper5 = new LambdaQueryWrapper<>();
                wrapper5.eq(BaseTerminal::getTerminalCode, o.getTerminalCode());
                BaseTerminal baseTerminal = baseTerminalService.getOne(wrapper5);
                if (!ObjectUtils.isEmpty(baseTerminal)) {
                    o.setTerminalName(baseTerminal.getTerminalName());
                }
                LambdaQueryWrapper<BaseVehicleTerminal> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(BaseVehicleTerminal::getTerminalCode, o.getTerminalCode());
                BaseVehicleTerminal baseVehicleTerminal = baseVehicleTerminalService.getOne(wrapper1);
                if (!ObjectUtils.isEmpty(baseVehicleTerminal)) {
                    o.setVehicle(baseVehicleTerminal.getVehicleCode());
                }
                return o;
            }).collect(Collectors.toList());
            page.setRecords(dto1);
        }
        return AjaxResult.success(page);
    }
}
