<?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="net.ferrum.business.mapper.QsUserSupplierMapper">

    <resultMap type="net.ferrum.business.domain.QsUserSupplier" id="QsUserSupplierResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="supplierId" column="supplier_id"/>
    </resultMap>


    <select id="selectSuppliersByUsersPages" resultType="net.ferrum.business.domain.vo.QsSupplierVo">
        <if test="bo.supplierManagers != null and bo.supplierManagers.size() > 0">
            select distinct qs.supplier_id, qs.supplier_name,qs.supplier_address,qs.supplier_contact,qs.supplier_contact_num,qs.remark
            from qs_supplier qs, sys_user su,qs_user_supplier qus
            where qs.supplier_id = qus.supplier_id
              and su.user_id = qus.user_id
              and qs.del_flag != '2'
              and su.del_flag != '2'
              and(
                    <foreach collection="bo.supplierManagers" item="name" separator=" OR ">su.nick_name LIKE CONCAT('%', #{name}, '%')
                    </foreach>
                 )
        </if>
        <if test="bo.supplierManagers == null or bo.supplierManagers.size() == 0">
            select distinct qs.supplier_id, qs.supplier_name,qs.supplier_address,qs.supplier_contact,qs.supplier_contact_num,qs.remark
            from qs_supplier qs
            where qs.del_flag != '2'
        </if>
        <if test="bo.supplierName != null and bo.supplierName != ''">
            AND qs.supplier_name  LIKE CONCAT('%', #{bo.supplierName}, '%')
        </if>
        <if test="bo.supplierAddress != null and bo.supplierAddress != ''">
            AND qs.supplier_address  LIKE CONCAT('%', #{bo.supplierAddress}, '%')
        </if>
        <if test="bo.supplierContact != null and bo.supplierContact != ''">
            AND qs.supplier_contact  LIKE CONCAT('%', #{bo.supplierContact}, '%')
        </if>
        <if test="bo.supplierContactNum != null and bo.supplierContactNum != ''">
            AND qs.supplier_contact_num  LIKE CONCAT('%', #{bo.supplierContactNum}, '%')
        </if>
    </select>

    <select id="selectUsersbySupplierId" resultType="net.ferrum.business.domain.vo.QsUserSupplierVo">
        select qus.id,qus.user_id,qus.supplier_id,qs.supplier_id, qs.supplier_name,su.user_name,su.nick_name
        from qs_supplier qs, sys_user su,qs_user_supplier qus
        where qs.supplier_id = qus.supplier_id
          and su.user_id = qus.user_id
          and qs.del_flag != '2'
          and su.del_flag != '2'
          and qs.supplier_id = #{supplierId}
    </select>
</mapper>
