/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2023/10/12
 */
package org.springblade.modules.business.datagram.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.business.datagram.dto.RtuDatagramInfoDTO;
import org.springblade.modules.business.datagram.entity.RtuDatagramInfoEntity;
import org.springblade.modules.business.datagram.service.IRtuDatagramService;
import org.springblade.modules.business.datagram.vo.RtuDatagramInfoVO;
import org.springblade.modules.business.datagram.wrapper.RtuDatagramWrapper;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.vo.RtuWarningInfoVO;
import org.springblade.modules.business.warning.wrapper.RtuWarningWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/***
 * Date:2023/10/12
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
@RestController
@RequestMapping("galaxy-business/rtu/datagram")
@RequiredArgsConstructor
@Api(value = "测站设备管理工作台", tags = "测站设备管理工作台")
public class RtuDatagramController extends BladeController {
    private final IRtuDatagramService datagramService;


    /**
     * 多表联合查询自定义分页
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<RtuDatagramInfoVO>> page(@ApiIgnore RtuDatagramInfoDTO dto, Query query) {
        IPage<RtuDatagramInfoEntity> pages = this.datagramService.selectPage(Condition.getPage(query), dto);
        return R.data(RtuDatagramWrapper.build().pageVO(pages));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "")
    public R<RtuDatagramInfoVO> detail(RtuDatagramInfoDTO dto) {
        LambdaQueryWrapper<RtuDatagramInfoEntity> wrapper = Wrappers.<RtuDatagramInfoEntity>query().lambda();
        wrapper.eq(RtuDatagramInfoEntity::getRtuCode, dto.getRtuCode());
        wrapper.orderByDesc(RtuDatagramInfoEntity::getPickTime);
        wrapper.last("limit 1");
        List<RtuDatagramInfoEntity> list = this.datagramService.list(wrapper);
        if (list.size()==0){
            return R.data(null);
        }
        return R.data(RtuDatagramWrapper.build().entityVO(list.get(0)));
    }
}
