package com.fzm.mall.server.front.user.mapper;

import com.fzm.mall.server.front.goods.model.MarketingCoupon;
import com.fzm.mall.server.front.goods.model.vo.CouponVo;
import com.fzm.mall.server.front.goods.model.vo.UserCouponVo;
import com.fzm.mall.server.front.user.model.Coupon;
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;

/**
 * <p>
 * 用户与优惠券对应表 Mapper 接口
 * </p>
 *
 * @author fzm
 * @since 2021-03-19
 */
public interface CouponMapper extends BaseMapper<Coupon> {
    @Update("update user_coupon set status = 3 where uid = #{uid} and coupon_id = #{couponId} and status = 1")
    int updateUserCoupon(String uid, String couponId);


    @Select("select a.uid,b.*,c.category_id,d.sku_id from user_coupon a left join marketing_coupon b on a.coupon_id = b.coupon_id left join marketing_coupon_category c on a.coupon_id = c.coupon_id left join marketing_coupon_sku d on a.coupon_id = d.coupon_id where a.uid = #{uid} and a.status = 1 and b.scope in (1,#{type}) and b.start_time<=#{currTime} and b.end_time>=#{currTime}")
    List<UserCouponVo> getUserCoupon(@Param("uid") String uid, @Param("type") Integer type, @Param("currTime") long currentTimeMillis);

    @Select("select * from marketing_coupon where status = 1 and range_type = 1 and start_time<=#{currTime} and end_time>=#{currTime} and scope in(1,#{type}) and merchant_id in (#{pingtai},#{merchantId})\n" +
            "union all\n" +
            "select * from marketing_coupon where status = 1 and range_type = 2 and start_time<=#{currTime} and end_time>=#{currTime} and merchant_id in (#{pingtai},#{merchantId}) and scope in(1,#{type}) and coupon_id in (select coupon_id from marketing_coupon_category where category_id in (select category_id from goods_category_spu where goods_id = #{goodsId} and status=1))\n" +
            "union all\n" +
            "select * from marketing_coupon where status = 1 and range_type = 3  and start_time<=#{currTime} and end_time>=#{currTime} and scope in(1,#{type}) and coupon_id in (select coupon_id from marketing_coupon_sku where goods_id = #{goodsId}); ")
    List<UserCouponVo> getCouponByGoodsId(@Param("goodsId") String goodsId,@Param("type") Integer type,@Param("currTime") long currentTimeMillis, @Param("pingtai") String pt,@Param("merchantId") String merchantId);


    @Select("select * from marketing_coupon where type in (1,#{type}) and status = 1 and start_time <= #{time} and end_time >= #{time} and coupon_id = #{couponId}")
    MarketingCoupon check(@Param("couponId") String couponId, @Param("type") Integer type, @Param("time") long currentTimeMillis);

    @Select("select * from user_coupon where uid = #{uid} and coupon_id = #{couponId}")
    Coupon checkHave(@Param("uid") String uid, @Param("couponId") String couponId);

    @Select("select b.* from user_coupon a left join marketing_coupon b on a.coupon_id = b.coupon_id where a.uid = #{uid} and a.status = 1 and b.status = 1 and b.type in (1,#{type} ) and b.start_time <= #{time} and b.end_time >= #{time} order by a.create_time limit #{start},#{size}")
    List<MarketingCoupon> getUseful(@Param("uid") String uid, @Param("type") Integer type, @Param("time") long currentTimeMillis,@Param("start") Integer start,@Param("size") Integer size);

    @Select("select b.* from user_coupon a left join marketing_coupon b on a.coupon_id = b.coupon_id where a.uid = #{uid} and a.status = 3  and b.type in (1,#{type} )  order by a.create_time limit #{start},#{size}")
    List<MarketingCoupon> getUsed(@Param("uid") String uid,@Param("type") Integer type, @Param("start") Integer start, @Param("size") Integer size);

    @Select("select b.* from user_coupon a left join marketing_coupon b on a.coupon_id = b.coupon_id where a.uid = #{uid} and b.end_time < #{time} and b.type in (1,#{type} ) and a.status!=2  order by a.create_time limit #{start},#{size}")
    List<MarketingCoupon> getOld(@Param("uid") String uid,@Param("type") Integer type,@Param("time") long currentTimeMillis, @Param("start") Integer start, @Param("size") Integer size);

    @Select("select count(*) from user_coupon a left join marketing_coupon b on a.coupon_id = b.coupon_id where a.uid = #{uid} and a.status = 1 and b.status = 1 and b.type in (1,#{type} ) and b.start_time <= #{time} and b.end_time >= #{time} ")
    Integer getUsefulCount(@Param("uid") String uid, @Param("type") Integer type, @Param("time") long currentTimeMillis);

    @Select("select count(*) from user_coupon a  left join marketing_coupon b on a.coupon_id = b.coupon_id where a.uid = #{uid} and a.status = 3 and b.type in (1,#{type})")
    Integer getUsedCount(@Param("uid") String uid, @Param("type") Integer type);

    @Select("select count(*) from user_coupon a left join marketing_coupon b on a.coupon_id = b.coupon_id where a.uid = #{uid} and b.end_time < #{time} and b.type in (1,#{type} ) and a.status!=3")
    Integer getOldCount(@Param("uid") String uid, @Param("type") Integer type, @Param("time") long currentTimeMillis);


    @Select("select b.* from user_coupon a left join marketing_coupon b on a.coupon_id = b.coupon_id left join marketing_coupon_category c on c.coupon_id = a.coupon_id where a.uid = #{uid} and a.status = 1 and b.type in (1,#{type}) and b.start_time <= #{time} and b.end_time >=#{time}")
    List<MarketingCoupon> getAllUserCoupon(@Param("uid") String uid, @Param("type") Integer type, @Param("time") long currentTimeMillis);

    @Select("select category_id from marketing_coupon_category where coupon_id = #{couponId}")
    List<String> getCategorys(@Param("couponId") String couponId);

    CouponVo getAll(@Param("couponId") String couponId, @Param("type") Integer type, @Param("time") long currentTimeMillis);

    @Select("select a.coupon_id from user_coupon a left join marketing_coupon b on a.coupon_id = b.coupon_id where a.status = 1 and a.uid = #{uid} and b.status = 1 and b.start_time <=#{time} and b.end_time >=#{time} and b.scope in (1,#{type})")
    List<String> getCouponIdByUid(@Param("uid") String uid, @Param("type") Integer type, @Param("time") long currentTimeMillis);


    @Select("select * from marketing_coupon where coupon_id = #{couponId}")
    MarketingCoupon getByCouponId(@Param("couponId")String couponId);

    @Update("update marketing_coupon set amt = amt - 1 where coupon_id = #{couponId} and amt-1>=0 ")
    int delAmt(@Param("couponId")String couponId);
}
