package com.fs.live.mapper;


import com.fs.live.domain.LiveOrder;
import com.fs.live.vo.LiveOrderListVo;
import com.fs.live.vo.LiveOrderVo;
import com.fs.store.domain.FsStoreOrder;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 订单Mapper接口
 *
 * @author fs
 * @date 2025-07-08
 */
public interface LiveOrderMapper {
    /**
     * 查询订单
     *
     * @param orderId 订单主键
     * @return 订单
     */
    LiveOrder selectLiveOrderByOrderId(String orderId);

    @Select("select order_code from live_order where order_id=#{orderId}")
    String selectLiveOrderCodeByOrderId(@Param("orderId") String orderId);

    /**
     * 查询订单列表
     *
     * @param liveOrder 订单
     * @return 订单集合
     */
    List<LiveOrder> selectLiveOrderList(LiveOrder liveOrder);

    /**
     * 新增订单
     *
     * @param liveOrder 订单
     * @return 结果
     */
    int insertLiveOrder(LiveOrder liveOrder);

    /**
     * 修改订单
     *
     * @param liveOrder 订单
     * @return 结果
     */
    int updateLiveOrder(LiveOrder liveOrder);

    /**
     * 删除订单
     *
     * @param orderId 订单主键
     * @return 结果
     */
    int deleteLiveOrderByOrderId(String orderId);

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的数据主键集合
     * @return 结果
     */
    int deleteLiveOrderByOrderIds(String[] orderIds);

    /**
     * 查询订单时间粒度
     *
     * @param liveOrder 订单
     * @return 订单集合
     */
    List<LiveOrder> getLiveOrderTimeGranularity(LiveOrderVo liveOrder);

    @Select("select * from live_order where extend_order_id is null or extend_order_id = '' and status = 2")
    List<LiveOrder> selectNotPushedLiveOrder();

    @Select("select * from live_order where order_code = #{orderCode}")
    LiveOrder selectFsUserVipOrderByOrderCode(String orderSn);

    @Update("update live_order set `status`=-3 where order_id=#{orderId}")
    int cancelOrder(Long orderId);

    @Select({"<script> " +
            "select * from live_order " +
            "<where>" +
            "       <if test=\"userId != null  and userId != ''\"> and user_id = #{userId}</if>\n" +
            "       <if test=\"status != null \"> and status = #{status}</if>\n" +
            "</where> " +
            "order by create_time desc" +
            "</script>"})
    List<LiveOrderListVo> selectLiveOrderListVo(@Param("userId") String userId,@Param("status") Integer status);

    @Select("select * from live_order where `status` = 3 AND TIMESTAMPDIFF(HOUR, start_time, NOW()) >= 48  ")
    List<LiveOrder> selectLiveOrderByFinish();

    @Select("select * from live_order where `status` = 2 and extend_order_id is not null ")
    List<LiveOrder> selectUpdateExpress();

    @Select("select order_id from live_order where `status` = 2")
    List<Long> selectSyncExpressIds();

    @Select("select order_id from live_order where `status` = 2 and extend_order_id is null ")
    List<Long> selectOrderIdByNoErp();

    @Select("select * from live_order where extend_order_id is not null and `status`=2")
    List<LiveOrder> selectDeliverPenddingData();

    @Select("select * from live_order where extend_order_id = #{extendOrderId}")
    LiveOrder selectLiveOrderByExtendId(@Param("extendOrderId") String extendOrderId);

    @Select("select * from live_order where order_code=#{orderCode} limit 1")
    LiveOrder selectLiveOrderByOrderCode(@Param("orderCode") String orderCode);
}
