<?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.zfire.mall.manager.commonmapper.AdminMapper">

    <select id="mchList" resultType="com.zfire.mall.manager.bean.admin.respDto.AdminUserDto">
        select
            a.*,
            b.use_org_number,
            b.use_org_name
        from admin_user a
        join admin_company b on a.admin_company_id = b.admin_company_id
        join admin_user_websit_rela c on a.admin_user_id = c.admin_user_id
        join admin_websit d on d.admin_websit_id = c.admin_websit_id
        <where>

            <if test="isJiaXian != null">
                and a.is_jiaxian = #{isJiaXian}
            </if>
            <if test="isJiaXian == null">
                and a.is_jiaxian = 0
            </if>

            <if test="isGongcheng != null">
                and a.is_gongcheng = #{isGongcheng}
            </if>
            <if test="isGongcheng == null">
                and a.is_gongcheng = 0
            </if>
             <if test="isMaster != null">
                and a.is_master = #{isMaster}
            </if>
            <if test="userName != null and userName !=''">
                and (a.user_name like concat('%',#{userName},'%') or a.nick_name like concat('%',#{userName},'%'))
            </if>
            <if test="nickName != null and nickName !=''">
                and a.nick_name like concat('%',#{nickName},'%')
            </if>
            <if test="linkPhone != null and linkPhone !=''">
                and a.link_phone like concat('%',#{linkPhone},'%')
            </if>
            <if test="email != null and email !=''">
                and a.email like concat('%',#{email},'%')
            </if>
            <if test="roleId != null and roleId !=''">
                and a.role_id =#{roleId}
            </if>

            <if test="roleName != null and roleName.size > 0">
                and a.role_name in
                <foreach item="item" index="index" collection="roleName" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="isCustomer != null">
                and a.is_customer=#{isCustomer}
            </if>
            <if test="customerId != null and customerId !=''">
                and a.customer_id = #{customerId}
            </if>
            <if test="status != null">
                and a.status = #{status}
            </if>
            <if test="websitNumber != null and websitNumber !=''">
                and d.websit_number = #{websitNumber}
            </if>
            <if test="adminCompanyIds != null and adminCompanyIds.size > 0">
                and b.admin_company_id in
                <foreach item="item" index="index" collection="adminCompanyIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
             <if test="customerNumbers != null and customerNumbers.size > 0">
                and a.customer_id in
                <foreach item="item" index="index" collection="customerNumbers" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                and c.admin_websit_id in
                <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by a.create_time desc
    </select>

    <select id="queryAdminCompanyWechat" resultType="com.zfire.mall.manager.plus.entity.AdminCompany">
        select DISTINCT b.* from admin_user_websit_rela a join admin_company b on a.admin_company_id = b.admin_company_id
        where  1=1
      <if test="adminUserId != null and adminUserId !=''">
          and a.admin_user_id = #{adminUserId}
      </if>
    </select>

    <select id="StockCorrespondList" resultType="com.zfire.mall.manager.plus.entity.StockCorrespond">
     select sc.* from admin_user_stock aus  join stock_correspond sc on aus.correspond_id = sc.id where aus.admin_user_id = #{adminUserId}
    </select>


    <select id="queryChildServiceId" resultType="java.lang.String">
        select
            a.admin_user_id
        from admin_user_websit_rela a
        join admin_user b on a.admin_user_id=b.admin_user_id
        where b.is_customer=0
        <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
            and a.admin_websit_id in
            <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        <if test="roleName !=null and roleName !=''">
            and b.role_name = #{roleName}
        </if>
        <if test="notWebsitId != null and notWebsitId != ''">
            and a.admin_websit_id != #{notWebsitId}
        </if>

    </select>

    <select id="selectByUserName" resultType="com.zfire.mall.manager.plus.entity.AdminUser">
        select
            *
        from admin_user

        where 1=1
        and user_name =#{userName}


    </select>

    <delete id="delByUserName" >
      DELETE FROM admin_user WHERE user_name =#{userName};
    </delete>

</mapper>