package cn.iocoder.yudao.module.iot.dal.mysql.device;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceOperationStatusRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DevicePageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.LineDeviceRequestVO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.LineDeviceRespVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 物联设备 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface DeviceMapper extends BaseMapperX<DeviceDO> {

    default PageResult<DeviceDO> selectPage(DevicePageReqVO reqVO) {

        LambdaQueryWrapperX<DeviceDO> deviceDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        deviceDOLambdaQueryWrapperX.likeIfPresent(DeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(DeviceDO::getDeviceName, reqVO.getDeviceName())
                .likeIfPresent(DeviceDO::getTopic, reqVO.getTopic())
                .eqIfPresent(DeviceDO::getDeviceType, reqVO.getDeviceType())
                .eqIfPresent(DeviceDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DeviceDO::getReadTopic, reqVO.getReadTopic())
                .eqIfPresent(DeviceDO::getWriteTopic, reqVO.getWriteTopic())
                .eqIfPresent(DeviceDO::getGatewayId, reqVO.getGatewayId())
                .eqIfPresent(DeviceDO::getDeviceBrandId, reqVO.getDeviceBrandId())
                .eqIfPresent(DeviceDO::getOffLineDuration, reqVO.getOffLineDuration())
                .betweenIfPresent(DeviceDO::getLastOnlineTime, reqVO.getLastOnlineTime())
                .eqIfPresent(DeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(DeviceDO::getIsEnable, reqVO.getIsEnable())
                .betweenIfPresent(DeviceDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DeviceDO::getCreateTime);

        // 单独处理 ids 条件
        if (StringUtils.isNotBlank(reqVO.getIds())) {
            List<Long> idList = Arrays.stream(reqVO.getIds().split(","))
                    .map(String::trim)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            deviceDOLambdaQueryWrapperX.in(DeviceDO::getId, idList);
        }

        PageResult<DeviceDO> deviceDOPageResult = selectPage(reqVO, deviceDOLambdaQueryWrapperX);

        return deviceDOPageResult;
    }

    default PageResult<DeviceDO> selectPage(PageParam reqVO, Long gatewayId) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DeviceDO>()
                .eq(DeviceDO::getGatewayId, gatewayId)
                .orderByDesc(DeviceDO::getId));
    }

    default int deleteByGatewayId(Long gatewayId) {
        return delete(DeviceDO::getGatewayId, gatewayId);
    }

    default List<DeviceDO> selectByGatewayId(Long gatewayId) {
        return selectList(DeviceDO::getGatewayId, gatewayId);
    }
    default DeviceDO selectByName(String name) {
        return selectOne(DeviceDO::getDeviceName,name);
    }
    default DeviceDO selectByTopic(String topic) {
        return selectOne(DeviceDO::getReadTopic,topic);
    }
    default List<DeviceDO> selectList(DevicePageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DeviceDO>()
                .eqIfPresent(DeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(DeviceDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(DeviceDO::getDeviceType, reqVO.getDeviceType())
                .eqIfPresent(DeviceDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DeviceDO::getReadTopic, reqVO.getReadTopic())
                .eqIfPresent(DeviceDO::getWriteTopic, reqVO.getWriteTopic())
                .eqIfPresent(DeviceDO::getGatewayId, reqVO.getGatewayId())
                .eqIfPresent(DeviceDO::getDeviceBrandId, reqVO.getDeviceBrandId())
                .eqIfPresent(DeviceDO::getOffLineDuration, reqVO.getOffLineDuration())
                .betweenIfPresent(DeviceDO::getLastOnlineTime, reqVO.getLastOnlineTime())
                .eqIfPresent(DeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(DeviceDO::getIsEnable, reqVO.getIsEnable())
                .orderByDesc(DeviceDO::getId));
    }

    IPage<LineDeviceRespVO> lineDevicePage(Page<LineDeviceRespVO> page, @Param("pageReqVO") LineDeviceRequestVO pageReqVO);

    String lineDeviceLedgerPage(@Param("id") Long id);

    List<Long> deviceLedgerList();

    List<Map<String,Object>> selectWorkshopBatch(@Param("deviceIds") List<Long> deviceIds);

    List<LineDeviceRespVO> lineDeviceList(@Param("pageReqVO") LineDeviceRequestVO pageReqVO);


    DeviceOperationStatusRespVO getDeviceOperationalStatus();

    int selectIsReference(@Param("deviceId") Long deviceId);

    /**
     * 根据goviewId查询对应的deviceIds
     *
     * @param goviewId 可视化大屏ID
     * @return deviceIds字符串
     */
    @Select("SELECT device_ids FROM mes_goview WHERE id = #{goviewId}")
    String selectDeviceIdsByGoviewId(@Param("goviewId") Long goviewId);
}