<?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.aegis.resource.supplier.mapper.ResourceSupplierLicenseMapper">

    <resultMap type="SupplierLicense" id="SupplierLicenseResult">
        <id     property="id"              column="id"               />
        <result property="supplierId"       column="supplier_id"       />
        <result property="licenseType"      column="license_type"      />
        <result property="licenseNo"        column="license_no"        />
        <result property="issuingAuthority" column="issuing_authority" />
        <result property="validFrom"        column="valid_from"        />
        <result property="validTo"          column="valid_to"          />
        <result property="attachmentUrl"    column="attachment_url"    />
        <result property="createBy"         column="create_by"          />
        <result property="createTime"       column="create_time"        />
        <result property="updateBy"         column="update_by"          />
        <result property="updateTime"      column="update_time"        />
        <result property="remark"           column="remark"             />
    </resultMap>

    <sql id="selectSupplierLicenseVo">
        select id, supplier_id, license_type, license_no, issuing_authority, valid_from, valid_to,
               attachment_url, create_by, create_time, update_by, update_time, remark
        from resource_supplier_license
    </sql>

    <select id="selectSupplierLicenseList" parameterType="SupplierLicense" resultMap="SupplierLicenseResult">
        <include refid="selectSupplierLicenseVo"/>
        <where>
            <if test="supplierId != null">
                AND supplier_id = #{supplierId}
            </if>
            <if test="licenseType != null and licenseType != ''">
                AND license_type = #{licenseType}
            </if>
            <if test="licenseNo != null and licenseNo != ''">
                AND license_no like concat('%', #{licenseNo}, '%')
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectSupplierLicenseListBySupplierId" parameterType="Long" resultMap="SupplierLicenseResult">
        <include refid="selectSupplierLicenseVo"/>
        where supplier_id = #{supplierId}
        order by create_time desc
    </select>

    <select id="selectSupplierLicenseById" parameterType="Long" resultMap="SupplierLicenseResult">
        <include refid="selectSupplierLicenseVo"/>
        where id = #{id}
    </select>

    <insert id="insertSupplierLicense" parameterType="SupplierLicense" useGeneratedKeys="true" keyProperty="id">
        insert into resource_supplier_license(
            <if test="supplierId != null">supplier_id,</if>
            <if test="licenseType != null and licenseType != ''">license_type,</if>
            <if test="licenseNo != null">license_no,</if>
            <if test="issuingAuthority != null">issuing_authority,</if>
            <if test="validFrom != null">valid_from,</if>
            <if test="validTo != null">valid_to,</if>
            <if test="attachmentUrl != null">attachment_url,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="supplierId != null">#{supplierId},</if>
            <if test="licenseType != null and licenseType != ''">#{licenseType},</if>
            <if test="licenseNo != null">#{licenseNo},</if>
            <if test="issuingAuthority != null">#{issuingAuthority},</if>
            <if test="validFrom != null">#{validFrom},</if>
            <if test="validTo != null">#{validTo},</if>
            <if test="attachmentUrl != null">#{attachmentUrl},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateSupplierLicense" parameterType="SupplierLicense">
        update resource_supplier_license
        <set>
            <if test="licenseType != null and licenseType != ''">license_type = #{licenseType},</if>
            <if test="licenseNo != null">license_no = #{licenseNo},</if>
            <if test="issuingAuthority != null">issuing_authority = #{issuingAuthority},</if>
            <if test="validFrom != null">valid_from = #{validFrom},</if>
            <if test="validTo != null">valid_to = #{validTo},</if>
            <if test="attachmentUrl != null">attachment_url = #{attachmentUrl},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

    <delete id="deleteSupplierLicenseById" parameterType="Long">
        delete from resource_supplier_license where id = #{id}
    </delete>

    <delete id="deleteSupplierLicenseBySupplierId" parameterType="Long">
        delete from resource_supplier_license where supplier_id = #{supplierId}
    </delete>

</mapper>