package com.starry.admin.modules.custom.mapper;

import com.github.yulichang.base.MPJBaseMapper;
import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity;
import java.math.BigDecimal;
import java.util.Date;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 顾客Mapper接口
 *
 * @author admin
 * @since 2024-04-08
 */
public interface PlayCustomUserInfoMapper extends MPJBaseMapper<PlayCustomUserInfoEntity> {

    @Update({
        "<script>",
        "UPDATE play_custom_user_info",
        "SET accumulated_consumption_amount = COALESCE(accumulated_consumption_amount, 0) + #{consumptionDelta},",
        "    last_purchase_time = #{completionTime},",
        "    first_purchase_time = CASE WHEN first_purchase_time IS NULL THEN #{completionTime} ELSE first_purchase_time END",
        "    <if test='weiChatCode != null and weiChatCode != \"\"'>, wei_chat_code = #{weiChatCode}</if>",
        "WHERE id = #{userId}",
        "</script>"
    })
    int applyOrderCompletionUpdate(@Param("userId") String userId,
                                   @Param("consumptionDelta") BigDecimal consumptionDelta,
                                   @Param("completionTime") Date completionTime,
                                   @Param("weiChatCode") String weiChatCode);
}
