package com.liquidnet.service.adam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liquidnet.service.adam.dto.AdamCaomeiPassportListDto;
import com.liquidnet.service.adam.dto.AdamCaomeiPassportUserBadgeDto;
import com.liquidnet.service.adam.entity.AdamCaomeiPassport;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface AdamCaomeiPassportMapper extends BaseMapper<AdamCaomeiPassport> {

    List<AdamCaomeiPassportListDto> selectPassportAdminList(@Param("passportNo") String passportNo,
                                                            @Param("userName") String userName,
                                                            @Param("mobile") String mobile,
                                                            @Param("idCard") String idCard,
                                                            @Param("bindStatus") Integer bindStatus);

    @Select("select count(1) from adam_caomei_passport where status = 1")
    int countBoundPassports();

    List<AdamCaomeiPassportUserBadgeDto> selectUserBadgesByUid(@Param("uid") String uid);

    /**
     * 原子操作：仅当护照状态为 0（未绑定）时，更新为 1（已绑定）
     */
    @Update("update adam_caomei_passport set status = 1, user_id = #{userId}, bound_at = now(), updated_at = now() where passport_no = #{passportNo} and status = 0")
    int bindPassportAtomic(@Param("passportNo") String passportNo, @Param("userId") String userId);
}
