<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.erp.supplier.repository.SupplierContactRepository">

    <resultMap id="BaseResultMap" type="com.erp.supplier.entity.SupplierContact">
        <id column="id" property="id"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="contact_name" property="contactName"/>
        <result column="gender" property="gender"/>
        <result column="department" property="department"/>
        <result column="position" property="position"/>
        <result column="mobile" property="mobile"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
        <result column="qq" property="qq"/>
        <result column="wechat" property="wechat"/>
        <result column="is_primary" property="isPrimary"/>
        <result column="is_emergency" property="isEmergency"/>
        <result column="remark" property="remark"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <select id="selectBySupplierId" resultMap="BaseResultMap">
        SELECT * FROM supplier_contacts
        WHERE supplier_id = #{supplierId} AND deleted = 0
        ORDER BY is_primary DESC, created_at DESC
    </select>

    <select id="selectPrimaryContact" resultMap="BaseResultMap">
        SELECT * FROM supplier_contacts
        WHERE supplier_id = #{supplierId} AND is_primary = 1 AND deleted = 0
        LIMIT 1
    </select>

</mapper>
