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

import com.baomidou.mybatisplus.core.metadata.IPage;
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.api.store.dto.StoreCardRespDTO;
import com.yc.ship.module.otc.controller.admin.store.machine.SellerMachineRespVO;
import com.yc.ship.module.otc.controller.admin.store.vo.StoreAuditPageReqVO;
import com.yc.ship.module.otc.controller.admin.store.vo.StorePageReqVO;
import com.yc.ship.module.otc.controller.admin.store.vo.StoreRespVO;
import com.yc.ship.module.otc.controller.admin.store.vo.StoreSimpleReqVO;
import com.yc.ship.module.otc.dal.dataobject.store.StoreDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 旅行社门店 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface StoreMapper extends BaseMapperX<StoreDO> {

    default PageResult<StoreDO> selectPage(StorePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StoreDO>()
                .eqIfPresent(StoreDO::getStoreCode, reqVO.getStoreCode())
                .likeIfPresent(StoreDO::getStoreName, reqVO.getStoreName())
                .eqIfPresent(StoreDO::getIsUse, reqVO.getIsUse())
                .betweenIfPresent(StoreDO::getCreateTime, reqVO.getCreateTime())
                .eq(StoreDO::getAgencyId, reqVO.getAgencyId())
                .orderByDesc(StoreDO::getId));
    }

    List<SellerMachineRespVO> getSellerMachine(Long sellerId);

    List<StoreDO> selectListByStatus(@Param("tenantId") long tenantId, @Param("status") Integer status);

    default List<StoreDO> selectStoreByStoreIds(List<Long> storeIds) {
        LambdaQueryWrapperX<StoreDO> wrapper = new LambdaQueryWrapperX<>();
        wrapper.inIfPresent(StoreDO::getId, storeIds);
        wrapper.orderByDesc(StoreDO::getId);
        return selectList(wrapper);
    }

    StoreCardRespDTO getStoreByUserId(@Param("userId") long userId);

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

    void updateStoreProductIsUse(@Param("storeId") Long storeId, @Param("isUse") Integer isUse,
                                 @NotNull @Param("ids") List<Long> idList);

    @TenantIgnore
    @Select("select * from ota_bill b where deleted = 0 and order_num > 0 and bill_status = 4 and agency_id=#{agencyId}")
    List<Map<String, Object>> selectOtaBillList(@Param("agencyId") long agencyId);

    IPage<StoreRespVO> selectAuditPage(IPage<StoreRespVO> ipage, @Param("reqVO") StoreAuditPageReqVO reqVO);

    default List<StoreDO> selectList(StoreSimpleReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<StoreDO>()
                .eqIfPresent(StoreDO::getAgencyId, reqVO.getAgencyId())
                .eqIfPresent(StoreDO::getIsUse, reqVO.getStatus())
        );
    }
}
