package cn.ysk.cashier.mybatis.mapper;

import cn.ysk.cashier.mybatis.entity.TbShopSongOrder;
import cn.ysk.cashier.mybatis.vo.SingerSongVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * (TbShopSongOrder)表数据库访问层
 *
 * @author ww
 * @since 2024-07-08 09:24:26
 */
public interface TbShopSongOrderMapper extends BaseMapper<TbShopSongOrder> {


    @Select("select " +
            "song.img,song.name as songName,song.origin_singer as originSinger,orders.from_name as fromName," +
            "orders.to_name as toName,orders.note " +
            "from tb_shop_song_order orders " +
            "left join tb_shop_song song on orders.song_id=song.id " +
            "where state = #{state} and orders.shop_id=#{shopId} order by orders.create_time limit 12")
    List<SingerSongVo> findByState(@Param("state") Integer state, @Param("shopId") Integer shopId);


    @Update("update tb_shop_song_order set state=3 where state=2 and shop_id=#{shopId}")
    int upState(@Param("shopId") Integer shopId);

    @Update("update tb_shop_song_order " +
            "set state = 2 " +
            "where id = (" +
            "    select id " +
            "    from (" +
            "        select id " +
            "        from tb_shop_song_order " +
            "        where state = 1 and shop_id = #{shopId}" +
            "        order by create_time " +
            "        limit 1" +
            "    ) as subquery" +
            ")")
    int setDuringSinging(@Param("shopId") Integer shopId);
}

