<?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.sckw.system.dao.KwsEnterpriseDao">
    <resultMap id="BaseResultMap" type="com.sckw.system.model.KwsEnterprise">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="pid" jdbcType="BIGINT" property="pid"/>
        <result column="firm_name" jdbcType="VARCHAR" property="firmName"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="contacts" jdbcType="VARCHAR" property="contacts"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="legal_name" jdbcType="VARCHAR" property="legalName"/>
        <result column="legal_phone" jdbcType="VARCHAR" property="legalPhone"/>
        <result column="head" jdbcType="VARCHAR" property="head"/>
        <result column="integral" jdbcType="INTEGER" property="integral"/>
        <result column="balance" jdbcType="DECIMAL" property="balance"/>
        <result column="experience" jdbcType="INTEGER" property="experience"/>
        <result column="member_level" jdbcType="INTEGER" property="memberLevel"/>
        <result column="reg_time" jdbcType="TIMESTAMP" property="regTime"/>
        <result column="reg_source" jdbcType="VARCHAR" property="regSource"/>
        <result column="org_code" jdbcType="VARCHAR" property="orgCode"/>
        <result column="city_code" jdbcType="INTEGER" property="cityCode"/>
        <result column="detail_address" jdbcType="VARCHAR" property="detailAddress"/>
        <result column="city_name" jdbcType="VARCHAR" property="cityName"/>
        <result column="lat" jdbcType="VARCHAR" property="lat"/>
        <result column="lng" jdbcType="VARCHAR" property="lng"/>
        <result column="approval" jdbcType="INTEGER" property="approval"/>
        <result column="approval_time" jdbcType="TIMESTAMP" property="approvalTime"/>
        <result column="manager" jdbcType="BIGINT" property="manager"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="create_by" jdbcType="BIGINT" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by" jdbcType="BIGINT" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="del_flag" jdbcType="INTEGER" property="delFlag"/>
        <result column="website" jdbcType="VARCHAR" property="website"/>
        <result column="business" jdbcType="VARCHAR" property="business"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,
        pid,
        firm_name,
        code,
        contacts,
        phone,
        legal_name,
        legal_phone,
        head,
        integral,
        balance,
        experience,
        member_level,
        reg_time,
        reg_source,
        org_code,
        city_code,
        detail_address,
        lat,
        lng,
        approval,
        approval_time,
        manager,
        remark,
        status,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag,
        city_name,
        website,
        business
    </sql>

    <select id="selectByKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from kws_enterprise
        where id = #{id,jdbcType=BIGINT}
    </select>


    <select id="findPojo" resultType="com.sckw.system.model.pojo.FindEntListPojo"
            parameterType="com.sckw.system.model.pojo.FindPojoParam">
        select tab.*,
               case when tab.status = 0 then '正常' else '冻结' end statusName
        from
        (
        select a.id,
               min(a.firm_name)                          firm_name,
               min(a.code)                               code,
               min(a.contacts)                           contacts,
               min(a.phone)                              phone,
               min(a.legal_name)                         legal_name,
               min(a.legal_phone)                        legal_phone,
               min(a.head)                               head,
               min(a.integral)                           integral,
               min(a.balance)                            balance,
               min(a.experience)                         experience,
               min(a.member_level)                       member_level,
               min(a.reg_time)                           reg_time,
               min(a.reg_source)                         reg_source,
               min(a.org_code)                           org_code,
               min(a.city_code)                          city_code,
               min(a.detail_address)                     detail_address,
               min(a.lat)                                lat,
               min(a.lng)                                lng,
               min(a.approval)                           approval,
               min(a.approval_time)                      approval_time,
               min(a.manager)                            manager,
               min(a.remark)                             remark,
               min(a.status)                             status,
               min(a.create_by)                          create_by,
               min(a.create_time)                        create_time,
               min(a.update_by)                          update_by,
               min(a.update_time)                        update_time,
               min(a.del_flag)                           del_flag,
               ifnull(GROUP_CONCAT(distinct b.type), '') type,
               min(k.system_type)                        system_type,
               min(k.name)                               createByName,
               min(ku.name)                              updateByName,
               min(d.id)                                 pid,
               min(d.firm_name)                          pentName
        from kws_enterprise a
            left join kws_user k on a.create_by = k.id
            left join kws_user ku on a.update_by = ku.id
            left join kws_ent_type b on a.id = b.ent_id and b.del_flag = 0
        <if test="dto.approval == null or (dto.approval != null and dto.approval != 2)">
            and b.status != 1
        </if>
        left join kws_enterprise d on d.id = a.pid
        where a.del_flag = 0
        <if test="dto.typeList != null and dto.typeList.size() > 0">
            and exists (select 1
                        from kws_ent_type kt
            where a.id = kt.ent_id
              and kt.del_flag = 0
              and kt.type in
            <foreach collection="dto.typeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            )
        </if>
        <choose>
            <when test="dto.approval != null and dto.approval == 3">
                and a.approval in (3, 4)
            </when>
            <when test="dto.approval != null and dto.approval != 3">
                and a.approval = #{dto.approval}
            </when>
        </choose>
        <if test="dto.customer != null and dto.customer == 1">
            and a.approval in (1, 4)
        </if>
        <if test="dto.keywords != null">
            and (a.contacts like concat('%', #{dto.keywords}, '%') or a.phone like concat('%', #{dto.keywords}, '%') or
                 a.firm_name like concat('%', #{dto.keywords}, '%'))
        </if>
        <if test="dto.startTime != null">
            and a.reg_time >= #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and a.reg_time &lt;= #{dto.endTime}
        </if>
        <if test="dto.approvalStartTime != null">
            and a.approval_time >= #{dto.approvalStartTime}
        </if>
        <if test="dto.approvalEndTime != null">
            and a.approval_time &lt; date_add(#{dto.approvalEndTime}, INTERVAL 1 DAY)
        </if>
        <if test="dto.status != null and dto.status != ''">
            and a.status = #{dto.status}
        </if>
        <if test="dto.entryType != null and dto.entryType == 1">
            and k.system_type = 2
        </if>
        <if test="dto.entryType != null and dto.entryType == 2">
            and k.system_type = 1
        </if>
        <if test="dto.idList != null and dto.idList.size() > 0">
            and a.id in
            <foreach collection="dto.idList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by a.id
        ) tab
        where tab.id is not null
        order by tab.update_time desc
    </select>

    <select id="findList" resultType="com.sckw.system.model.vo.res.FindEntListResVo"
            parameterType="com.sckw.system.model.vo.req.FindListReqVo">
        select a.*,
               d.id userId,
               d.account,
               d.name
        from kws_enterprise a
                 left join kws_user d on a.phone = d.phone and d.del_flag = 0 and d.status = 0
        where a.del_flag = 0
        <if test="approval != null">
            and a.approval = #{approval}
        </if>
        <if test="entName != null and entName != ''">
            and a.firm_name like concat('%', #{entName}, '%')
        </if>
        <if test="entType != null">
            and exists (select 1 from kws_ent_type e where a.id = e.ent_id and e.type = #{entType})
        </if>
        order by a.create_time
    </select>

    <select id="findByUserId" resultType="com.sckw.system.model.KwsEnterprise">
        select distinct a.*
        from kws_enterprise a
                 inner join kws_user d on a.id = d.ent_id
        where d.id = #{id}
          and a.del_flag = 0
          and d.del_flag = 0
          and a.status = 0
          and d.status = 0
    </select>

    <select id="selectByKeys" resultType="com.sckw.system.model.KwsEnterprise">
        select a.*
        from kws_enterprise a
        where del_flag = 0
          and exists(select 1 from kws_ent_check_track b where a.id = b.ent_id and b.status = 0)
        <if test="list != null and list.size() > 0">
            and id in
            <foreach collection="list" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="findUserByEntIds" resultType="com.sckw.system.model.pojo.FindEntUserPojo">
        select distinct a.id        entId,
                        a.firm_name entName,
                        d.id        userId,
                        d.name,
                        d.account,
                        d.phone,
                        f.name      roleName
        from kws_enterprise a
                 left join kws_user d on d.ent_id = a.id
                 left join kws_user_role e on d.id = e.user_id
                 left join kws_role f on e.role_id = f.id
        where a.del_flag = 0
          and d.del_flag = 0
          and f.del_flag = 0
          and a.id in
        <foreach collection="list" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </select>

    <select id="findManageInfoByEntId" resultType="com.sckw.system.model.pojo.FindManagePojo">
        select distinct d.id userId,
                        f.id roleId,
                        a.id entId
        from kws_enterprise a
                 left join kws_user d on a.id = d.ent_id
                 left join kws_user_role e on d.id = e.user_id
                 left join kws_role f on e.role_id = f.id
        where a.del_flag = 0
          and d.del_flag = 0
          and e.del_flag = 0
          and f.del_flag = 0
          and d.is_main = 1
          and a.id = #{entId}
    </select>

    <select id="findManageInfoByEntIdBeforeApproval" resultType="java.lang.Long">
        select distinct r.id roleId
        from kws_enterprise a
                 LEFT JOIN kws_user d ON a.phone = d.account and d.del_flag = 0
                 left join kws_role r on r.ent_id = a.id and r.del_flag = 0
        where a.del_flag = 0
          and d.is_main = 1
          and d.system_type = 2
          and a.id = #{entId}
    </select>

    <select id="findEnts" resultType="com.sckw.system.model.KwsEnterprise" parameterType="java.util.Map">
        select *
        from kws_enterprise ke
        where ke.del_flag = 0
        <if test="firmName != null and firmName != ''">
            and ke.firm_name like concat('%', #{firmName}, '%')
        </if>
        <if test="entName != null and entName != ''">
            and ke.firm_name = #{entName}
        </if>
        <choose>
            <when test="entTypes != null and entTypes != '' and entTypes.size() > 0">
                and exists (select 1
                            from kws_ent_type ket where ket.del_flag = 0
                                                    and ke.id = ket.ent_id
                                                    and ket.type in
                <foreach collection="entTypes" item="type" open="(" close=")" separator=",">
                    #{type,jdbcType=BIGINT}
                </foreach>
                )
            </when>
        </choose>
    </select>

    <select id="checkEntRepeat" resultType="com.sckw.system.model.KwsEnterprise">
        select *
        from kws_enterprise where  (
                firm_name = #{entName}
        <if test="phone != null and phone != ''">
            or phone = #{phone}
        </if>
        )
          and del_flag = 0
          and approval in (1, 3, 4)
    </select>

    <select id="selectAllByKeys" resultType="com.sckw.system.model.KwsEnterprise">
        select
        <include refid="Base_Column_List"/>
        from kws_enterprise
        where del_flag = 0
        <if test="list != null and list.size() > 0">
            and id in
            <foreach collection="list" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="queryEntInfoByKeys" resultType="com.sckw.system.model.vo.res.EntInfo">
        select id as entId, firm_name as entName
        from kws_enterprise
        where del_flag = 0
        <if test="list != null and list.size() > 0">
            and id in
            <foreach collection="list" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="queryEntInfoByCityCodeAndEntTypesWithPage" resultType="com.sckw.system.model.KwsEnterprise">
        SELECT a.*
        FROM kws_enterprise a
                 LEFT JOIN sys_area b ON a.city_code = b.CODE
        where 1 = 1
        <if test="cityCode != null">
            and (b.code = #{cityCode} or b.province_code = #{cityCode} or b.pcode = #{cityCode})
        </if>
        <if test="entIdList != null and entIdList.size() > 0">
            and a.id in
            <foreach collection="entIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="entName != null and entName != ''">
            and a.firm_name like concat('%', #{entName}, '%')
        </if>
        and exists (select 1
                    from kws_ent_type k
        where a.id = k.ent_id
          and k.status = 0
          and k.del_flag = 0
          and k.type in
        <foreach collection="entTypeList" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        )
          and exists (select 1
                      from kws_ent_check_track e
                      where a.id = e.ent_id
                        and a.status = 0)
    </select>

    <select id="findManageInfoByEntIds" resultType="com.sckw.system.model.pojo.FindManagePojo">
        select distinct d.id userId,
                        f.id roleId,
                        a.id entId
        from kws_enterprise a
                 left join kws_user d on a.id = d.ent_id
                 left join kws_user_role e on d.id = e.user_id
                 left join kws_role f on e.role_id = f.id
        where a.del_flag = 0
          and d.del_flag = 0
          and e.del_flag = 0
          and f.del_flag = 0
          and d.is_main = 1
          and d.system_type = 2
          and a.id in
        <foreach collection="list" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </select>

<!--    <insert id="insert" parameterType="com.sckw.system.model.KwsEnterprise">-->
<!--        insert into kws_enterprise-->
<!--        <trim prefix="(" suffix=")" suffixOverrides=",">-->
<!--            <if test="id != null">-->
<!--                id,-->
<!--            </if>-->
<!--            <if test="firmName != null">-->
<!--                firm_name,-->
<!--            </if>-->
<!--            <if test="code != null">-->
<!--                code,-->
<!--            </if>-->
<!--            <if test="contacts != null">-->
<!--                contacts,-->
<!--            </if>-->
<!--            <if test="phone != null">-->
<!--                phone,-->
<!--            </if>-->
<!--            <if test="legalName != null">-->
<!--                legal_name,-->
<!--            </if>-->
<!--            <if test="legalPhone != null">-->
<!--                legal_phone,-->
<!--            </if>-->
<!--            <if test="head != null">-->
<!--                head,-->
<!--            </if>-->
<!--            <if test="integral != null">-->
<!--                integral,-->
<!--            </if>-->
<!--            <if test="balance != null">-->
<!--                balance,-->
<!--            </if>-->
<!--            <if test="experience != null">-->
<!--                experience,-->
<!--            </if>-->
<!--            <if test="memberLevel != null">-->
<!--                member_level,-->
<!--            </if>-->
<!--            <if test="regTime != null">-->
<!--                reg_time,-->
<!--            </if>-->
<!--            <if test="regSource != null">-->
<!--                reg_source,-->
<!--            </if>-->
<!--            <if test="orgCode != null">-->
<!--                org_code,-->
<!--            </if>-->
<!--            <if test="cityCode != null">-->
<!--                city_code,-->
<!--            </if>-->
<!--            <if test="detailAddress != null">-->
<!--                detail_address,-->
<!--            </if>-->
<!--            <if test="lat != null">-->
<!--                lat,-->
<!--            </if>-->
<!--            <if test="lng != null">-->
<!--                lng,-->
<!--            </if>-->
<!--            <if test="approval != null">-->
<!--                approval,-->
<!--            </if>-->
<!--            <if test="approvalTime != null">-->
<!--                approval_time,-->
<!--            </if>-->
<!--            <if test="manager != null">-->
<!--                manager,-->
<!--            </if>-->
<!--            <if test="remark != null">-->
<!--                remark,-->
<!--            </if>-->
<!--            <if test="status != null">-->
<!--                status,-->
<!--            </if>-->
<!--            <if test="createBy != null">-->
<!--                create_by,-->
<!--            </if>-->
<!--            <if test="createTime != null">-->
<!--                create_time,-->
<!--            </if>-->
<!--            <if test="updateBy != null">-->
<!--                update_by,-->
<!--            </if>-->
<!--            <if test="updateTime != null">-->
<!--                update_time,-->
<!--            </if>-->
<!--            <if test="delFlag != null">-->
<!--                del_flag,-->
<!--            </if>-->
<!--        </trim>-->
<!--        <trim prefix="values (" suffix=")" suffixOverrides=",">-->
<!--            <if test="id != null">-->
<!--                #{id,jdbcType=BIGINT},-->
<!--            </if>-->
<!--            <if test="firmName != null">-->
<!--                #{firmName,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="code != null">-->
<!--                #{code,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="contacts != null">-->
<!--                #{contacts,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="phone != null">-->
<!--                #{phone,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="legalName != null">-->
<!--                #{legalName,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="legalPhone != null">-->
<!--                #{legalPhone,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="head != null">-->
<!--                #{head,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="integral != null">-->
<!--                #{integral,jdbcType=INTEGER},-->
<!--            </if>-->
<!--            <if test="balance != null">-->
<!--                #{balance,jdbcType=DECIMAL},-->
<!--            </if>-->
<!--            <if test="experience != null">-->
<!--                #{experience,jdbcType=INTEGER},-->
<!--            </if>-->
<!--            <if test="memberLevel != null">-->
<!--                #{memberLevel,jdbcType=INTEGER},-->
<!--            </if>-->
<!--            <if test="regTime != null">-->
<!--                #{regTime,jdbcType=TIMESTAMP},-->
<!--            </if>-->
<!--            <if test="regSource != null">-->
<!--                #{regSource,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="orgCode != null">-->
<!--                #{orgCode,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="cityCode != null">-->
<!--                #{cityCode,jdbcType=INTEGER},-->
<!--            </if>-->
<!--            <if test="detailAddress != null">-->
<!--                #{detailAddress,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="lat != null">-->
<!--                #{lat,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="lng != null">-->
<!--                #{lng,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="approval != null">-->
<!--                #{approval,jdbcType=INTEGER},-->
<!--            </if>-->
<!--            <if test="approvalTime != null">-->
<!--                #{approvalTime,jdbcType=TIMESTAMP},-->
<!--            </if>-->
<!--            <if test="manager != null">-->
<!--                #{manager,jdbcType=BIGINT},-->
<!--            </if>-->
<!--            <if test="remark != null">-->
<!--                #{remark,jdbcType=VARCHAR},-->
<!--            </if>-->
<!--            <if test="status != null">-->
<!--                #{status,jdbcType=INTEGER},-->
<!--            </if>-->
<!--            <if test="createBy != null">-->
<!--                #{createBy,jdbcType=BIGINT},-->
<!--            </if>-->
<!--            <if test="createTime != null">-->
<!--                #{createTime,jdbcType=TIMESTAMP},-->
<!--            </if>-->
<!--            <if test="updateBy != null">-->
<!--                #{updateBy,jdbcType=BIGINT},-->
<!--            </if>-->
<!--            <if test="updateTime != null">-->
<!--                #{updateTime,jdbcType=TIMESTAMP},-->
<!--            </if>-->
<!--            <if test="delFlag != null">-->
<!--                #{delFlag,jdbcType=INTEGER},-->
<!--            </if>-->
<!--        </trim>-->
<!--    </insert>-->

    <update id="update" parameterType="com.sckw.system.model.KwsEnterprise">
        update kws_enterprise
        <set>
            <if test="firmName != null">
                firm_name = #{firmName,jdbcType=VARCHAR},
            </if>
            <if test="code != null">
                code = #{code,jdbcType=VARCHAR},
            </if>
            <if test="contacts != null">
                contacts = #{contacts,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                phone = #{phone,jdbcType=VARCHAR},
            </if>
            <if test="legalName != null">
                legal_name = #{legalName,jdbcType=VARCHAR},
            </if>
            <if test="legalPhone != null">
                legal_phone = #{legalPhone,jdbcType=VARCHAR},
            </if>
            <if test="head != null">
                head = #{head,jdbcType=VARCHAR},
            </if>
            <if test="integral != null">
                integral = #{integral,jdbcType=INTEGER},
            </if>
            <if test="balance != null">
                balance = #{balance,jdbcType=DECIMAL},
            </if>
            <if test="experience != null">
                experience = #{experience,jdbcType=INTEGER},
            </if>
            <if test="memberLevel != null">
                member_level = #{memberLevel,jdbcType=INTEGER},
            </if>
            <if test="regTime != null">
                reg_time = #{regTime,jdbcType=TIMESTAMP},
            </if>
            <if test="regSource != null">
                reg_source = #{regSource,jdbcType=VARCHAR},
            </if>
            <if test="orgCode != null">
                org_code = #{orgCode,jdbcType=VARCHAR},
            </if>
            <if test="cityCode != null">
                city_code = #{cityCode,jdbcType=INTEGER},
            </if>
            <if test="cityName != null">
                city_name = #{cityName,jdbcType=VARCHAR},
            </if>
            <if test="detailAddress != null">
                detail_address = #{detailAddress,jdbcType=VARCHAR},
            </if>
            <if test="lat != null">
                lat = #{lat,jdbcType=VARCHAR},
            </if>
            <if test="lng != null">
                lng = #{lng,jdbcType=VARCHAR},
            </if>
            <if test="approval != null">
                approval = #{approval,jdbcType=INTEGER},
            </if>
            <if test="approvalTime != null">
                approval_time = #{approvalTime,jdbcType=TIMESTAMP},
            </if>
            <if test="manager != null">
                manager = #{manager,jdbcType=BIGINT},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
            <if test="createBy != null">
                create_by = #{createBy,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=BIGINT},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag,jdbcType=INTEGER},
            </if>
            <if test="website != null">
                website = #{website,jdbcType=VARCHAR},
            </if>
            <if test="business != null">
                business = #{business,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <update id="approval">
        update kws_enterprise
        set approval      = #{approval},
            approval_time = now(),
            remark        = #{remark}
        where id = #{id,jdbcType=BIGINT}
          and del_flag = 0
    </update>

    <select id="findEnterpriseIdsByUserRoleId" resultType="java.lang.Long">
        SELECT id
        from kws_enterprise
        where del_flag = 0
          and status = 0
        <if test="userId != null">
            and (manager = #{userId} or manager in (SELECT user_id
                                                    from kws_user_dept
                                                    where dept_id in (SELECT dept_id from kws_authority where role_id = #{roleId})))
        </if>
    </select>

    <select id="findEnterpriseIdsByUserRoleIds" resultType="java.lang.Long">
        SELECT id
        from kws_enterprise
        <where>
            del_flag = 0
              and status = 0
            <if test="userId != null">
                and            (manager = #{userId} or manager in (SELECT user_id
                                                                   from kws_user_dept
                where dept_id in (SELECT dept_id
                                  from kws_authority
                <where>
                    status = 0
                      and del_flag = 0
                    <if test="roleIds != null and roleIds.size() != 0">
                        and role_id in
                        <foreach collection="roleIds" item="item" separator="," open="(" close=")">
                            #{item}
                        </foreach>
                    </if>
                </where>
                )
                )
                    )
            </if>
        </where>
    </select>

    <select id="findPojoAndManager" resultType="com.sckw.system.model.pojo.FindEntListPojo">
        select tab.*,
               case when tab.status = 0 then '正常' else '冻结' end statusName
        from
        (
        select a.id,
               min(a.firm_name)                          firm_name,
               min(a.code)                               code,
               min(a.contacts)                           contacts,
               min(a.phone)                              phone,
               min(a.legal_name)                         legal_name,
               min(a.legal_phone)                        legal_phone,
               min(a.head)                               head,
               min(a.integral)                           integral,
               min(a.balance)                            balance,
               min(a.experience)                         experience,
               min(a.member_level)                       member_level,
               min(a.reg_time)                           reg_time,
               min(a.reg_source)                         reg_source,
               min(a.org_code)                           org_code,
               min(a.city_code)                          city_code,
               min(a.detail_address)                     detail_address,
               min(a.lat)                                lat,
               min(a.lng)                                lng,
               min(a.approval)                           approval,
               min(a.approval_time)                      approval_time,
               min(a.manager)                            manager,
               min(a.remark)                             remark,
               min(a.status)                             status,
               min(a.create_by)                          create_by,
               min(a.create_time)                        create_time,
               min(a.update_by)                          update_by,
               min(a.update_time)                        update_time,
               min(a.del_flag)                           del_flag,
               ifnull(GROUP_CONCAT(distinct b.type), '') type,
               min(k.system_type)                        system_type,
               min(k.name)                               createByName,
               min(ku.name)                              updateByName,
               min(d.id)                                 pid,
               min(d.firm_name)                          pentName
        from kws_enterprise a
            left join kws_user k on a.create_by = k.id and k.del_flag = 0
            left join kws_user ku on a.update_by = ku.id and ku.del_flag = 0
            left join kws_ent_type b on a.id = b.ent_id and b.del_flag = 0
        <if test="dto.approval == null or (dto.approval != null and dto.approval != 2)">
            and b.status != 1
        </if>
        left join kws_enterprise d on d.id = a.pid
        where a.del_flag = 0
        <!--        <if test="authEntIdList != null and authEntIdList.size() != 0">-->
        <!--            and a.id in-->
        <!--            <foreach collection="authEntIdList" item="item" open="(" close=")" separator=",">-->
        <!--                #{item}-->
        <!--            </foreach>-->
        <!--        </if>-->
        <if test="dto.typeList != null and dto.typeList.size() > 0">
            and exists (select 1
                        from kws_ent_type kt
            where a.id = kt.ent_id
              and kt.del_flag = 0
              and kt.type in
            <foreach collection="dto.typeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            )
        </if>
        <choose>
            <when test="dto.approval != null and dto.approval == 3">
                and a.approval in (3, 4)
            </when>
            <when test="dto.approval != null and dto.approval != 3">
                and a.approval = #{dto.approval}
            </when>
        </choose>
        <if test="dto.customer != null and dto.customer == 1">
            and a.approval in (1, 4)
        </if>
        <if test="dto.keywords != null">
            and (a.contacts like concat('%', #{dto.keywords}, '%') or a.phone like concat('%', #{dto.keywords}, '%') or
                 a.firm_name like concat('%', #{dto.keywords}, '%')
                or d.firm_name like concat('%', #{dto.keywords}, '%') or
                 d.contacts like concat('%', #{dto.keywords}, '%') or d.phone like concat('%', #{dto.keywords}, '%'))
        </if>
        <if test="dto.startTime != null">
            and a.reg_time >= #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and a.reg_time &lt;= #{dto.endTime}
        </if>
        <if test="dto.approvalStartTime != null">
            and a.approval_time >= #{dto.approvalStartTime}
        </if>
        <if test="dto.approvalEndTime != null">
            and a.approval_time &lt; date_add(#{dto.approvalEndTime}, INTERVAL 1 DAY)
        </if>
        <if test="dto.status != null and dto.status != ''">
            and a.status = #{dto.status}
        </if>
        <if test="dto.entryType != null and dto.entryType == 1">
            and k.system_type = 2
        </if>
        <if test="dto.entryType != null and dto.entryType == 2">
            and k.system_type = 1
        </if>
        <!--    查询子公司-->
        <if test="dto.entId != null">
            and a.pid = #{dto.entId}
        </if>
        <if test="dto.idList != null and dto.idList.size() > 0">
            and a.id in
            <foreach collection="dto.idList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by a.id
        ) tab
        order by tab.update_time desc
    </select>

    <select id="findEnterpriseIdsByUserIdAndUserRoleIds" resultType="java.lang.Long">
        SELECT id
        from kws_enterprise
        <where>
            del_flag = 0
              and status = 0
            <if test="userIds != null and userIds.size() != 0">
                and            ( (manager in
                <foreach collection="userIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
                ) or manager in (SELECT user_id
                                 from kws_user_dept
                where dept_id in (SELECT dept_id
                                  from kws_authority
                <where>
                    status = 0
                      and del_flag = 0
                    <if test="roleIds != null and roleIds.size() != 0">
                        and role_id in
                        <foreach collection="roleIds" item="item" separator="," open="(" close=")">
                            #{item}
                        </foreach>
                    </if>
                </where>
                )
                )
                    )
            </if>
        </where>
    </select>

    <select id="findEnterpriseIdsByUserIds" resultType="java.lang.Long">
        SELECT id
        from kws_enterprise
        where status = 0
          and del_flag = 0
        <if test="userIds != null and userIds.size() != 0">
            and manager in
            <foreach collection="userIds" separator="," close=")" open="(" item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="findAllEnterprise" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from kws_enterprise
        where status = 0
          and del_flag = 0
    </select>

    <select id="queryEnterpriseByEntIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from kws_enterprise where status = 0
                              and del_flag = 0
        <if test="entIds != null and entIds.size() != 0">
            and id in
            <foreach collection="entIds" separator="," close=")" open="(" item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="findEnterpriseByName" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from kws_enterprise
        where firm_name = #{enterpriseName}
    </select>

    <select id="query" resultType="com.sckw.system.model.vo.res.EntInfo">
        select id as entId, firm_name as entName
        from kws_enterprise
        where del_flag = 0
          and pid = 0
          and firm_name = #{entName}
          and status = 0
          and approval = 1
    </select>

    <select id="queryCte" resultType="com.sckw.system.model.vo.res.EntBaseInfo">
        with RECURSIVE ent_tree as
                           (select id, pid, firm_name
                            from kws_enterprise
                            where del_flag = 0
                              and id = #{entId}
                            union all
                            select ke.id, ke.pid, ke.firm_name
                            from kws_enterprise ke
                                     inner join ent_tree et on et.id = ke.pid
                            where ke.del_flag = 0)
        select *
        from ent_tree;
    </select>
</mapper>
