package com.fs.store.mapper;

import java.util.List;

import com.fs.store.domain.FsStoreCoupon;
import com.fs.store.domain.FsStoreCouponIssue;
import com.fs.store.param.FsCouponIssueParam;
import com.fs.store.vo.FsStoreCouponIssueVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 优惠券领取Mapper接口
 *
 * @author fs
 * @date 2022-03-15
 */
public interface FsStoreCouponIssueMapper
{
    /**
     * 查询优惠券领取
     *
     * @param id 优惠券领取ID
     * @return 优惠券领取
     */
    public FsStoreCouponIssue selectFsStoreCouponIssueById(Long id);

    /**
     * 查询优惠券领取列表
     *
     * @param fsStoreCouponIssue 优惠券领取
     * @return 优惠券领取集合
     */
    public List<FsStoreCouponIssue> selectFsStoreCouponIssueList(FsStoreCouponIssue fsStoreCouponIssue);

    /**
     * 新增优惠券领取
     *
     * @param fsStoreCouponIssue 优惠券领取
     * @return 结果
     */
    public int insertFsStoreCouponIssue(FsStoreCouponIssue fsStoreCouponIssue);

    /**
     * 修改优惠券领取
     *
     * @param fsStoreCouponIssue 优惠券领取
     * @return 结果
     */
    public int updateFsStoreCouponIssue(FsStoreCouponIssue fsStoreCouponIssue);

    /**
     * 删除优惠券领取
     *
     * @param id 优惠券领取ID
     * @return 结果
     */
    public int deleteFsStoreCouponIssueById(Long id);

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

    @Select({"<script> " +
            "select i.*,c.coupon_price,c.use_min_price,c.coupon_time,ifnull((select count(1) from fs_store_coupon_issue_user where user_id=#{userId} and issue_id=i.id),0) as  receive_count from fs_store_coupon_issue i left join fs_store_coupon c on c.coupon_id=i.coupon_id  " +
            "where i.limit_time &gt; now() " +
            "<if test = 'maps.isDel != null     '> " +
            "and i.is_del =#{maps.isDel} " +
            "</if>" +
            "<if test = 'maps.status != null     '> " +
            "and i.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.couponType != null     '> " +
            "and i.coupon_type =#{maps.couponType} " +
            "</if>" +
            " order by i.id desc "+
            "</script>"})
    List<FsStoreCouponIssueVO> selectFsStoreCouponIssueListQueryVO(@Param("userId") String userId, @Param("maps")FsStoreCouponIssue map);
    @Update({"<script> " +
            "update fs_store_coupon_issue  set remain_count=(select ifnull(count(1),0) from fs_store_coupon_issue_user where issue_id=#{id}) where id=#{id}  " +
            "</script>"})
    void updateFsStoreCouponIssueCount(Long id);
    @Select({"<script> " +
            "select i.*,c.coupon_price,c.use_min_price,c.coupon_time   from fs_store_coupon_issue i left join fs_store_coupon c on c.coupon_id=i.coupon_id  " +
            "where i.limit_time &gt; now() and i.is_del=0 and i.status=1   " +
            "<if test = 'maps.cateId != null and maps.cateId!=0     '> " +
            "and find_in_set(#{maps.cateId},c.package_cate_ids) " +
            "</if>" +
            "<if test = 'maps.couponType != null     '> " +
            "and i.coupon_type= #{maps.couponType} " +
            "</if>" +
            "<if test = 'maps.couponPrice != null     '> " +
            "and c.coupon_price =  #{maps.couponPrice} " +
            "</if>" +
            " order by c.coupon_price desc "+
            "</script>"})
    List<FsStoreCouponIssueVO> getCompanyCouponIssueList(@Param("maps") FsCouponIssueParam map);
    @Select({"<script> " +
            "select i.*,c.coupon_price,c.use_min_price,c.coupon_time  from fs_store_coupon_issue i left join fs_store_coupon c on c.coupon_id=i.coupon_id  " +
            "where i.id=#{id}  order by c.coupon_price desc " +
            "</script>"})
    FsStoreCouponIssueVO selectFsStoreCouponIssueVOById(Long id);
    @Select({"<script> " +
            "select i.*,c.coupon_price,c.use_min_price,c.coupon_time  from fs_store_coupon_issue i left join fs_store_coupon c on c.coupon_id=i.coupon_id  " +
            "where i.limit_time &gt; now() " +
            "<if test = 'maps.isDel != null     '> " +
            "and i.is_del =#{maps.isDel} " +
            "</if>" +
            "<if test = 'maps.status != null     '> " +
            "and i.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.couponType != null     '> " +
            "and i.coupon_type =#{maps.couponType} " +
            "</if>" +
            "<if test = 'maps.couponPrice != null     '> " +
            "and c.coupon_price =#{maps.couponPrice} " +
            "</if>" +
            "<if test = 'maps.couponName != null and  maps.couponName !=\"\"     '> " +
            "and i.coupon_name like CONCAT('%',#{maps.couponName},'%') " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(i.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(i.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            " order by c.coupon_price "+
            "</script>"})
    List<FsStoreCouponIssueVO> selectFsStoreCouponIssueListVO(@Param("maps")FsStoreCouponIssue fsStoreCouponIssue);


    /**
     * 返回所有可用套餐优惠券
     * @return List<FsStoreCouponIssueVO>
     */
    List<FsStoreCouponIssueVO> listAllAvailable();

    /**
     * 根据优惠券id查询已发布的优惠券
     * @param couponId
     * @return
     */
    @Select("select * from fs_store_coupon_issue where coupon_id=${couponId} and status=1 and is_del=0 and limit_time > now() limit 1")
    FsStoreCouponIssue selectFsStoreCouponIssueByCouponId(@Param("couponId") Long couponId);
}
