package com.yc.ship.module.resource.dal.mysql.machine;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.resource.api.dto.CheckMachineDTO;
import com.yc.ship.module.resource.controller.admin.machine.vo.ScenicMachinePageReqVO;
import com.yc.ship.module.resource.dal.dataobject.machine.ScenicMachineDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 设备 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface ScenicMachineMapper extends BaseMapperX<ScenicMachineDO> {

    default PageResult<ScenicMachineDO> selectPage(ScenicMachinePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ScenicMachineDO>()
                .likeIfPresent(ScenicMachineDO::getName, reqVO.getName())
                .eqIfPresent(ScenicMachineDO::getChannelId, reqVO.getChannelId())
                .eqIfPresent(ScenicMachineDO::getMachineType, reqVO.getMachineType())
                .eqIfPresent(ScenicMachineDO::getMachineStatus, reqVO.getMachineStatus())
                .eqIfPresent(ScenicMachineDO::getMachineId, reqVO.getMachineId())
                .eqIfPresent(ScenicMachineDO::getIsUse, reqVO.getIsUse())
                .eqIfPresent(ScenicMachineDO::getSortNum, reqVO.getSortNum())
                .eqIfPresent(ScenicMachineDO::getPayTerminalCode, reqVO.getPayTerminalCode())
                .eqIfPresent(ScenicMachineDO::getPaySerialNum, reqVO.getPaySerialNum())
                .eqIfPresent(ScenicMachineDO::getPaySecretText, reqVO.getPaySecretText())
                .eqIfPresent(ScenicMachineDO::getPayAppVersion, reqVO.getPayAppVersion())
                .eqIfPresent(ScenicMachineDO::getPayLongitude, reqVO.getPayLongitude())
                .eqIfPresent(ScenicMachineDO::getPayLatitude, reqVO.getPayLatitude())
                .eqIfPresent(ScenicMachineDO::getPosNo, reqVO.getPosNo())
                .betweenIfPresent(ScenicMachineDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ScenicMachineDO::getId));
    }

    @Select("SELECT t1.id,t1.name,t1.is_modify isModify,t2.id channelId,t2.`name` channelName,t3.id siteId,t3.name siteName,t3.site_type siteType FROM scenic_machine t1 INNER JOIN scenic_channel t2 ON t1.channel_id = t2.id AND t1.deleted = 0 AND t1.tenant_id = t2.tenant_id INNER JOIN scenic_site t3 ON t2.site_id = t3.id AND t2.tenant_id = t3.tenant_id WHERE  t1.machine_id = #{machineId} AND t3.site_type = #{siteType} AND t1.tenant_id = #{tenantId}")
    List<Map<String, Object>> getMachineSiteInfos(@Param("machineId") String machineId, @Param("siteType") Integer siteType, @Param("tenantId") Long tenantId);

    CheckMachineDTO getCheckMachine(@Param("machineId") String machineId,
                                    @Param("machineType") Integer machineType,
                                    @Param("tenantId") Long tenantId);
}
