package com.fs.live.mapper;

import java.util.List;
import java.util.Set;

import com.fs.live.domain.LiveCoupon;
import com.fs.live.domain.LiveCouponIssue;
import com.fs.live.domain.LiveCouponIssueRelation;
import com.fs.live.vo.LiveCouponListVo;
import com.fs.store.domain.FsStoreCoupon;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 优惠券Mapper接口
 *
 * @author fs
 * @date 2025-09-30
 */
public interface LiveCouponMapper
{
    /**
     * 查询优惠券
     *
     * @param couponId 优惠券ID
     * @return 优惠券
     */
    public LiveCoupon selectLiveCouponById(Long couponId);

    /**
     * 查询优惠券列表
     *
     * @param liveCoupon 优惠券
     * @return 优惠券集合
     */
    public List<LiveCoupon> selectLiveCouponList(LiveCoupon liveCoupon);

    /**
     * 新增优惠券
     *
     * @param liveCoupon 优惠券
     * @return 结果
     */
    public int insertLiveCoupon(LiveCoupon liveCoupon);

    /**
     * 修改优惠券
     *
     * @param liveCoupon 优惠券
     * @return 结果
     */
    public int updateLiveCoupon(LiveCoupon liveCoupon);

    /**
     * 删除优惠券
     *
     * @param couponId 优惠券ID
     * @return 结果
     */
    public int deleteLiveCouponById(Long couponId);

    /**
     * 批量删除优惠券
     *
     * @param couponIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteLiveCouponByIds(Long[] couponIds);

    @Select("select * from live_coupon where find_in_set(coupon_id,#{ids})")
    List<LiveCoupon> selectLiveCouponByIds(String ids);

    @Select("select lc.*,lci.id,lcir.is_show,lcir.goods_id,fsp.product_name,fsp.image " +
            "from live_coupon_issue_relation lcir left join live_coupon_issue lci " +
            "left join live_coupon lc on lc.coupon_id=lci.coupon_id on lci.id = lcir.coupon_issue_id  " +
            "left join live_goods lg on lg.goods_id = lcir.goods_id " +
            "left join fs_store_product fsp on lg.product_id = fsp.product_id " +
            "where lcir.live_id = #{liveId}")
    List<LiveCoupon> selectLiveCouponByLiveId(@Param("liveId") Long liveId);

    @Select("<script>" +
            "select lc.*,lci.id from live_coupon_issue lci left join live_coupon lc on lc.coupon_id=lci.coupon_id " +
            "where lci.status=1" +
            " and lc.title like concat('%', #{couponName}, '%')" +
            " and id not in (select coupon_issue_id as id from live_coupon_issue_relation where live_id = #{liveId})" +
            "</script>"
    )
    List<LiveCoupon> listCoupon(@Param("liveId") Long liveId, @Param("couponName") String couponName);

    @Insert("insert into live_coupon_issue_relation(live_id,coupon_issue_id,is_show) values(#{liveId},#{couponId},0)")
    void insertLiveCouponEntity(@Param("liveId")Long liveId,@Param("couponId") Long couponId);


    @Insert("insert into live_coupon_issue_relation(live_id,coupon_issue_id,is_show) values(#{liveId},#{couponId},0)")
    void insertLiveCouponEntityWithProduct(@Param("liveId")Long liveId,@Param("couponId") Long couponId,@Param("goodsId") Long goodsId);

    @Select("select coupon_issue_id as id from live_coupon_issue_relation where live_id = #{liveId}")
    List<Long> selectLiveCouponIssueByLiveId(Long liveId);

    @Select("select lci.*,lc.product_id from live_coupon_issue_relation lcir left join live_coupon_issue lci on lci.id=lcir.coupon_issue_id " +
            " left join live_coupon lc on lc.coupon_id = lci。coupon_id " +
            "where lcir.live_id = #{liveId}")
    List<LiveCouponIssue> selectCouponIssueByLiveId(@Param("liveId") Long liveId);

    void handleDeleteSelectedAdmin(@Param("listVo") LiveCouponListVo listVo);

    @Select("select count(1) from live_coupon_issue_relation where live_id = #{liveId} and is_show = 1")
    int selectShowByLiveId(@Param("liveId") Long liveId);

    @Update("update live_coupon_issue_relation set is_show = #{isShow} where live_id = #{liveId} and coupon_issue_id = #{couponId}")
    void updateShow(@Param("liveId")Long liveId,@Param("couponId") Long couponId ,@Param("isShow") int isShow);

    int updateLiveCouponBind(@Param("listVo") LiveCouponListVo listVo);

    @Select("select * from live_coupon_issue_relation where live_id = #{liveId} and coupon_issue_id = #{couponId}")
    LiveCouponIssueRelation selectRelation(@Param("liveId")Long liveId,@Param("couponId") Long couponId);

    @Select("select lci.* from live_coupon_issue lci left join live_coupon_issue_relation lcir on lci.id=lcir.coupon_issue_id where lcir.live_id = #{liveId} and lci.id = #{couponId}")
    LiveCouponIssue selectLiveCouponIssueByLiveIdAndCouponId(@Param("liveId")Long liveId,@Param("couponId") Long couponId);

    @Select("select lc.*,lci.id,lcir.is_show,lcir.goods_id,fsp.product_name,fsp.image " +
            "from live_coupon_issue_relation lcir left join live_coupon_issue lci " +
            "left join live_coupon lc on lc.coupon_id=lci.coupon_id on lci.id = lcir.coupon_issue_id  " +
            "left join live_goods lg on lg.goods_id = lcir.goods_id " +
            "left join fs_store_product fsp on lg.product_id = fsp.product_id " +
            "where lcir.live_id = #{liveId} and lcir.goods_id is not null")
    List<LiveCoupon> listOn(@Param("liveId") Long liveId);

    @Update("update live_coupon_issue_relation set is_show = case WHEN coupon_issue_id = #{couponId} THEN 1 else 0 end where live_id = #{liveId}")
    void updateChangeShow(@Param("liveId")Long liveId,@Param("couponId") Long couponId);

    @Select("select * from live_coupon_issue_relation where live_id = #{liveId} and coupon_issue_id = #{couponId}")
    LiveCouponIssueRelation selectCouponRelation(@Param("liveId")Long liveId,@Param("couponId") Long couponId);

    @Select("select * from live_coupon_issue_relation where live_id = #{liveId}")
    List<LiveCouponIssueRelation> selectCouponRelationByLiveId(@Param("liveId") Long liveId);

    @Select("select * from live_coupon_issue_relation where coupon_issue_id = #{couponIssueId}")
    List<LiveCouponIssueRelation> selectCouponRelationByCouponIssueId(@Param("couponIssueId") Long couponIssueId);
}
