<?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.shkj.wms.mapper.BaseConsignorMapper">

    <resultMap type="com.shkj.wms.vo.BranchTree" id="SysbranchResult">
        <result property="id" column="id"/>
        <result property="branchCode" column="branch_code"/>
        <result property="branchName" column="branch_name"/>
        <result property="isActivity" column="is_activity"/>
        <result property="extend1" column="extend1"/>
        <result property="extend2" column="extend2"/>
        <result property="extend3" column="extend3"/>
        <result property="extend4" column="extend4"/>
        <result property="extend5" column="extend5"/>
        <result property="remark" column="remark"/>
        <result property="isDel" column="is_del"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <collection property="baseConsignors" javaType="java.util.List" resultMap="BaseConsignorResult"></collection>
    </resultMap>


    <resultMap type="com.shkj.common.core.domain.entity.BaseConsignor" id="BaseConsignorResult">
        <result property="id" column="c_id"/>
        <result property="consignorCode" column="c_consignor_code"/>
        <result property="consignorNum" column="c_consignor_num"/>
        <result property="consignorUser" column="c_consignor_user"/>
        <result property="consignorTel" column="c_consignor_tel"/>
        <result property="branchId" column="c_branch_id"/>
        <result property="consignorName" column="c_consignor_name"/>
        <result property="mnemonic" column="c_mnemonic"/>
        <result property="consignorType" column="c_consignor_type"/>
        <result property="extend1" column="c_extend1"/>
        <result property="extend2" column="c_extend2"/>
        <result property="extend3" column="c_extend3"/>
        <result property="extend4" column="c_extend4"/>
        <result property="extend5" column="c_extend5"/>
        <result property="remark" column="c_remark"/>
        <result property="isDel" column="c_is_del"/>
        <result property="createTime" column="c_create_time"/>
        <result property="createBy" column="c_create_by"/>
        <result property="updateTime" column="c_update_time"/>
        <result property="updateBy" column="c_update_by"/>
    </resultMap>
    <select id="selectBranchConsignor" resultMap="SysbranchResult">
        SELECT
        w.*,
        c.consignor_num AS c_consignor_num,
        c.consignor_code AS c_consignor_code,
        c.consignor_name AS c_consignor_name,
        c.id AS c_id,
        c.mnemonic AS c_mnemonic,
        c.consignor_type AS c_consignor_type,
        c.address AS c_address,
        c.consignor_user AS c_consignor_user,
        c.consignor_tel AS c_consignor_tel,
        c.isuse AS c_isuse,
        c.extend1 AS c_extend1,
        c.extend2 AS c_extend2,
        c.extend3 AS c_extend3,
        c.extend4 AS c_extend4,
        c.extend5 AS c_extend5,
        c.remark AS c_remark,
        c.branch_id AS c_branch_id,
        c.is_del AS c_is_del,
        c.create_time AS c_create_time,
        c.create_by AS c_create_by,
        c.update_time AS c_update_time,
        c.update_by AS c_update_by
        FROM
        base_branch w
        LEFT JOIN base_consignor c ON w.id = c.branch_id and c.is_del = '0'
        where w.is_del = '0'

        <if test="branchIds != null and branchIds.size > 0">
            and w.id in
            <foreach collection="branchIds" item="branchId" open="(" separator="," close=")">
                #{branchId}
            </foreach>
        </if>
    </select>
    <select id="selectConsignorListByRoleId" resultType="java.lang.Long" parameterType="java.lang.Long">
         select b.id
                from base_consignor b
                left join sys_role_consignor rc on b.id = rc.consignor_id
                where rc.role_id = #{roleId} and b.is_del = '0'
                order by b.create_time

    </select>
     <select id="queryList" resultType="com.shkj.wms.vo.BaseConsignorVo"
            parameterType="com.shkj.wms.bo.BaseConsignorQueryBo">
        select DISTINCT bc.* from base_consignor bc
        left join sys_role_consignor sr on bc.id = sr.consignor_id

        where bc.is_del = '0'
        <if test="branchIds != null and branchIds.size > 0">
            and bc.branch_id in
            <foreach collection="branchIds" item="branchId" open="(" separator="," close=")">
                #{branchId}
             </foreach>
        </if>
        <if test="rolesId != null and rolesId.size > 0">
            and sr.role_id in
            <foreach collection="rolesId" item="roleId" open="(" separator="," close=")">
               #{roleId}
            </foreach>
        </if>
        <if test="searchValue != null and searchValue != ''">
            and (
                bc.consignor_code like concat('%',#{searchValue},'%') or
                bc.consignor_name like concat('%',#{searchValue},'%') or
                bc.mnemonic like concat('%',#{searchValue},'%')
                )
        </if>

    </select>
</mapper>
