package com.yc.ship.module.otc.dal.mysql.store;

import com.yc.ship.framework.common.pojo.PageParam;
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.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.otc.dal.dataobject.store.StoreMachineDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 关联设备 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface StoreMachineMapper extends BaseMapperX<StoreMachineDO> {

    default PageResult<StoreMachineDO> selectPage(PageParam reqVO, Long storeId) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StoreMachineDO>()
                .eq(StoreMachineDO::getStoreId, storeId)
                .orderByDesc(StoreMachineDO::getId));
    }

    default int deleteByStoreId(Long storeId) {
        return delete(StoreMachineDO::getStoreId, storeId);
    }

    default List<StoreMachineDO> getStoreMachine(String machineId) {
        return selectList(new LambdaQueryWrapperX<StoreMachineDO>()
                .eq(StoreMachineDO::getMachineId, machineId));
    }

    @TenantIgnore
    @Select("SELECT sm.* FROM otc_agency_store_machine sm INNER JOIN otc_agency_store s ON s.id=sm.store_id " +
            "INNER JOIN ota_distributor d ON d.account_tenant_id=s.tenant_id " +
            "WHERE s.deleted=0 AND sm.deleted=0 AND d.id=#{distributorId}")
    List<StoreMachineDO> selectByDistributorId(@Param("distributorId") Long distributorId);
}
