package com.community.huiju.dao;

import com.community.huiju.model.TpBillOrder;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TpBillOrderMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(TpBillOrder record);

    int insertSelective(TpBillOrder record);

    TpBillOrder selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(TpBillOrder record);

    int updateByPrimaryKey(TpBillOrder record);

    /**
     * 批量插入
     * @param list
     * @return
     */
    int batchInsertSelective(List<TpBillOrder> list);

    /**
     * 根据订单查询， 所有数据
     * @param orderNumber
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("select " +
            "tbo.* from tp_bill_order tbo where tbo.order_bumber=#{orderNumber}")
    List<TpBillOrder> selectByOrderBumber(@Param("orderNumber") String orderNumber);

    /**
     * 根据房间号查询订单
     * @param roomNoId
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("SELECT tbo.* FROM tp_bill_order tbo LEFT JOIN tp_bill_invoice tbi on tbo.tp_bill_invoice_id = tbi.id " +
            "WHERE tbi.room_no_id = #{roomNoId} and tbo.order_status = 1 GROUP BY tbo.order_bumber ORDER BY tbo.create_date DESC")
    List<TpBillOrder> selectByRoomNoId(@Param("roomNoId") Integer roomNoId);

    /**
     * 根据订单号 查询缴费组
     * @param orderBumber
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("SELECT * FROM tp_bill_order WHERE order_bumber = #{orderBumber} GROUP BY tp_bill_id")
    List<TpBillOrder> selectByOrderNumberBill(@Param("orderBumber") String orderBumber);

    /**
     * 可关闭的订单
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("SELECT t.* from tp_bill_order t where TIMESTAMPDIFF(MINUTE,create_date,now()) >= 10 and t.order_status in (0,2)")
    List<TpBillOrder> selectCloseOrder();

    /**
     * 根据收费组id 和 订单号 查询出这个订单号内该收费组所有的数据
     * @param tpBillId
     * @param orderNumber
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("SELECT * FROM tp_bill_order WHERE order_bumber = #{orderNumber} and tp_bill_id = #{tpBillId}")
    List<TpBillOrder> selectByBillIdAndOrderNumber(@Param("tpBillId") Integer tpBillId, @Param("orderNumber") String orderNumber);
}