package com.hfkj.dao;

import com.hfkj.model.DiscountUserModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * mapper扩展类
 */
public interface BsDiscountUserMapperExt {

    @Select({"" +
            "<script>" +
            " select" +
            " id," +
            " user_id userId," +
            " discount_id discountId," +
            " discount_no discountNo," +
            " discount_name discountName," +
            " discount_type discountType," +
            " discount_condition discountCondition," +
            " discount_price discountPrice," +
            " discount_stock_code discountStockCode," +
            " use_scope useScope," +
            " use_date useDate," +
            " expiration_date expirationDate," +
            " status," +
            " create_time createTime," +
            " update_time updateTime" +
            " from bs_discount_user " +
            " where 1 = 1" +
            " <if test='param.userId != null'> and user_id = #{param.userId} </if>" +
            " <if test='param.userPhone != null'> and user_phone = #{param.userPhone} </if>" +
            " <if test='param.useScope != null'> and FIND_IN_SET(#{param.useScope} ,use_scope) </if>" +
            " <if test='param.status != null'> and `status` = #{param.status} </if>" +
            " <if test='param.useMerNo != null'> and discount_id in (select discount_id from bs_discount_use_mer where mer_no = #{param.useMerNo} GROUP BY discount_id) </if>" +
            "</script>"})
    List<DiscountUserModel> queryUserDiscountList(@Param("param") Map<String,Object> param);

    @Update("<script>" +
            " update bs_discount_user a" +
            " LEFT JOIN bs_discount_stock_code b on b.id = a.discount_stock_code " +
            " SET a.`status` = 3,b.`status` = 4" +
            " where  a.`status` = 1 <![CDATA[ and a.expiration_date <= NOW() ]]>" +
            " </script>")
    void expiration();
}
