package com.fs.store.mapper;

import java.util.List;
import com.fs.store.domain.FsStoreCouponUser;
import com.fs.store.param.FsCouponUserEnableParam;
import com.fs.store.vo.FsStoreCouponUserVO;
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 FsStoreCouponUserMapper
{
    /**
     * 查询优惠券发放记录
     *
     * @param id 优惠券发放记录ID
     * @return 优惠券发放记录
     */
    public FsStoreCouponUser selectFsStoreCouponUserById(Long id);

    /**
     * 查询优惠券发放记录列表
     *
     * @param fsStoreCouponUser 优惠券发放记录
     * @return 优惠券发放记录集合
     */
    public List<FsStoreCouponUser> selectFsStoreCouponUserList(FsStoreCouponUser fsStoreCouponUser);

    /**
     * 新增优惠券发放记录
     *
     * @param fsStoreCouponUser 优惠券发放记录
     * @return 结果
     */
    public int insertFsStoreCouponUser(FsStoreCouponUser fsStoreCouponUser);

    /**
     * 修改优惠券发放记录
     *
     * @param fsStoreCouponUser 优惠券发放记录
     * @return 结果
     */
    public int updateFsStoreCouponUser(FsStoreCouponUser fsStoreCouponUser);

    /**
     * 删除优惠券发放记录
     *
     * @param id 优惠券发放记录ID
     * @return 结果
     */
    public int deleteFsStoreCouponUserById(Long id);

    /**
     * 批量删除优惠券发放记录
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteFsStoreCouponUserByIds(Long[] ids);

    @Update("update fs_store_coupon_user set status=2 where status=0 and limit_time<now() ")
    int updateFsCouponByExpire();
    @Select({"<script> " +
            "select cu.* from fs_store_coupon_user cu inner join fs_store_coupon c on c.coupon_id=cu.coupon_id  " +
            "where cu.status=0 and cu.is_del=0 " +
            "<if test = 'maps.userId != null     '> " +
            "and cu.user_id =#{maps.userId} " +
            "</if>" +
            "<if test = 'maps.packageCateId != null     '> " +
            "and (find_in_set( #{maps.packageCateId},c.package_cate_ids) or c.package_cate_ids=0) " +
            "</if>" +
            "<if test = 'maps.useMinPrice != null     '> " +
            "and cu.use_min_price &lt; #{maps.useMinPrice} " +
            "</if>" +
            "<if test = 'maps.couponType != null     '> " +
            "and c.type = #{maps.couponType} " +
            "</if>" +
            "</script>"})
    List<FsStoreCouponUser> selectFsStoreCouponUserListByEnable(@Param("maps")FsCouponUserEnableParam param);
    @Select({"<script> " +
            "select cu.*,u.nickname,u.phone from fs_store_coupon_user cu inner join fs_user u  on u.user_id=cu.user_id  " +
            "where  cu.is_del=0 " +
            "<if test = 'maps.status != null     '> " +
            "and cu.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.couponTitle != null     '> " +
            "and cu.coupon_title =#{maps.couponTitle} " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(cu.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(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "order by cu.id desc " +
            "</script>"})
    List<FsStoreCouponUserVO> selectFsStoreCouponUserListVO(@Param("maps")FsStoreCouponUser fsStoreCouponUser);
}
