package com.ycwl.basic.pricing.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.pricing.entity.VoucherPrintRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 优惠券打印记录Mapper
 */
@Mapper
public interface VoucherPrintRecordMapper extends BaseMapper<VoucherPrintRecord> {
    
    /**
     * 根据faceId、brokerId、scenicId查询已存在的打印记录
     * @param faceId 用户faceId
     * @param scenicId 景区ID
     * @return 打印记录
     */
    @Select("SELECT id, code, face_id, broker_id, scenic_id, voucher_code_id, voucher_code, " +
            "print_status, error_message, create_time, update_time, deleted, deleted_at " +
            "FROM voucher_print_record WHERE face_id = #{faceId} AND scenic_id = #{scenicId} AND deleted = 0 LIMIT 1")
    VoucherPrintRecord selectByFaceBrokerScenic(@Param("faceId") Long faceId,
                                                @Param("scenicId") Long scenicId);
    
    /**
     * 根据券码ID查询是否已被打印
     * @param voucherCodeId 券码ID
     * @return 打印记录
     */
    @Select("SELECT id, code, face_id, broker_id, scenic_id, voucher_code_id, voucher_code, " +
            "print_status, error_message, create_time, update_time, deleted, deleted_at " +
            "FROM voucher_print_record WHERE voucher_code_id = #{voucherCodeId} AND deleted = 0 LIMIT 1")
    VoucherPrintRecord selectByVoucherCodeId(@Param("voucherCodeId") Long voucherCodeId);
    
    /**
     * 更新打印状态
     * @param id 记录ID
     * @param printStatus 打印状态
     * @param errorMessage 错误信息（可为null）
     * @return 影响行数
     */
    @Update("UPDATE voucher_print_record SET print_status = #{printStatus}, error_message = #{errorMessage}, " +
            "update_time = NOW() WHERE id = #{id}")
    int updatePrintStatus(@Param("id") Long id,
                          @Param("printStatus") Integer printStatus,
                          @Param("errorMessage") String errorMessage);
}