<?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.ruoyi.inventory.mapper.OwnersMapper">
    
    <resultMap type="Owners" id="OwnersResult">
        <result property="id"    column="id"    />
        <result property="ownerCode"    column="owner_code"    />
        <result property="ownerName"    column="owner_name"    />
        <result property="ownerType"    column="owner_type"    />
        <result property="contactPerson"    column="contact_person"    />
        <result property="contactPhone"    column="contact_phone"    />
        <result property="email"    column="email"    />
        <result property="address"    column="address"    />
        <result property="taxNumber"    column="tax_number"    />
        <result property="bankAccount"    column="bank_account"    />
        <result property="isActive"    column="is_active"    />
        <result property="sortNo"    column="sort_no"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUserCode"    column="create_user_code"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateUserCode"    column="update_user_code"    />
        <result property="isUsed"    column="is_used"    />
    </resultMap>

    <sql id="selectOwnersVo">
        select id, owner_code, owner_name, owner_type, contact_person, contact_phone, email, address, tax_number, bank_account, is_active, sort_no, create_time, create_user_code, update_time, update_user_code, is_used from owners
    </sql>

    <select id="selectOwnersList" parameterType="Owners" resultMap="OwnersResult">
        <include refid="selectOwnersVo"/>
            where is_used = 0
            <if test="ownerCode != null  and ownerCode != ''"> and owner_code = #{ownerCode}</if>
            <if test="ownerName != null  and ownerName != ''"> and owner_name like concat('%', #{ownerName}, '%')</if>
            <if test="ownerType != null "> and owner_type = #{ownerType}</if>
            <if test="contactPerson != null  and contactPerson != ''"> and contact_person = #{contactPerson}</if>
            <if test="contactPhone != null  and contactPhone != ''"> and contact_phone = #{contactPhone}</if>
            <if test="email != null  and email != ''"> and email = #{email}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="taxNumber != null  and taxNumber != ''"> and tax_number = #{taxNumber}</if>
            <if test="bankAccount != null  and bankAccount != ''"> and bank_account = #{bankAccount}</if>
            <if test="isActive != null "> and is_active = #{isActive}</if>
            <if test="sortNo != null "> and sort_no = #{sortNo}</if>
            <if test="createUserCode != null  and createUserCode != ''"> and create_user_code = #{createUserCode}</if>
            <if test="updateUserCode != null  and updateUserCode != ''"> and update_user_code = #{updateUserCode}</if>
            order by sort_no asc
    </select>
    
    <select id="selectOwnersById" parameterType="String" resultMap="OwnersResult">
        <include refid="selectOwnersVo"/>
        where id = #{id}
    </select>

    <insert id="insertOwners" parameterType="Owners">
        insert into owners
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="ownerCode != null">owner_code,</if>
            <if test="ownerName != null">owner_name,</if>
            <if test="ownerType != null">owner_type,</if>
            <if test="contactPerson != null">contact_person,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="email != null">email,</if>
            <if test="address != null">address,</if>
            <if test="taxNumber != null">tax_number,</if>
            <if test="bankAccount != null">bank_account,</if>
            <if test="isActive != null">is_active,</if>
            <if test="sortNo != null">sort_no,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUserCode != null">create_user_code,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateUserCode != null">update_user_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="ownerCode != null">#{ownerCode},</if>
            <if test="ownerName != null">#{ownerName},</if>
            <if test="ownerType != null">#{ownerType},</if>
            <if test="contactPerson != null">#{contactPerson},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="email != null">#{email},</if>
            <if test="address != null">#{address},</if>
            <if test="taxNumber != null">#{taxNumber},</if>
            <if test="bankAccount != null">#{bankAccount},</if>
            <if test="isActive != null">#{isActive},</if>
            <if test="sortNo != null">#{sortNo},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUserCode != null">#{createUserCode},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateUserCode != null">#{updateUserCode},</if>
         </trim>
    </insert>

    <update id="updateOwners" parameterType="Owners">
        update owners
        <trim prefix="SET" suffixOverrides=",">
            <if test="ownerCode != null">owner_code = #{ownerCode},</if>
            <if test="ownerName != null">owner_name = #{ownerName},</if>
            <if test="ownerType != null">owner_type = #{ownerType},</if>
            <if test="contactPerson != null">contact_person = #{contactPerson},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="address != null">address = #{address},</if>
            <if test="taxNumber != null">tax_number = #{taxNumber},</if>
            <if test="bankAccount != null">bank_account = #{bankAccount},</if>
            <if test="isActive != null">is_active = #{isActive},</if>
            <if test="sortNo != null">sort_no = #{sortNo},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUserCode != null">create_user_code = #{createUserCode},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateUserCode != null">update_user_code = #{updateUserCode},</if>
            <if test="isUsed != null">is_used = #{isUsed},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteOwnersById" parameterType="String">
        update owners set is_used = 1 where id = #{id}
    </update>

    <update id="deleteOwnersByIds" parameterType="String">
        update owners set is_used = 1 where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>