<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.PersonInfoDAO">
    <resultMap id="PersonInfoMap" type="com.jpsoft.shinestar.modules.base.entity.PersonInfo">
        <id property="id" column="id_"/>
        <result property="companyId" column="company_id"/>
        <result property="companyName" column="company_name"/>
        <result property="name" column="name_"/>
        <result property="phone" column="phone_"/>
        <result property="idCard" column="id_card"/>
        <result property="openId" column="open_id"/>
        <result property="faceEnabled" column="face_enabled"/>
        <result property="faceBound" column="face_bound"/>
        <result property="cardEnabled" column="card_enabled"/>
        <result property="appEnabled" column="app_enabled"/>
        <result property="passwordEnabled" column="password_enabled"/>
        <result property="guestEnabled" column="guest_enabled"/>
        <result property="position1" column="position1_"/>
        <result property="position2" column="position2_"/>
        <result property="position3" column="position3_"/>
        <result property="position4" column="position4_"/>
        <result property="position5" column="position5_"/>
        <result property="faceImageUrl" column="face_image_url"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="popedom" column="popedom_"/>
        <result property="popedomName" column="popedom_name"/>
        <result property="wechatNoticeEnabled" column="wechat_notice_enabled"/>
        <result property="allowViewLocal" column="allow_view_local"/>
        <result property="healthErtificateUrl" column="health_ertificate_url"/>
        <result property="isSync" column="is_sync"/>
        <result property="sex" column="sex_"/>
        <result property="parkId" column="park_id"/>
        <result property="parkName" column="park_name"/>
        <result property="healthyCode" column="healthy_code"/>
        <result property="updateHealthyTime" column="update_healthy_time"/>
        <result property="healthyReason" column="healthy_reason"/>
        <result property="workStatus" column="work_status"/>
        <result property="jobNumber" column="job_number"/>
        <result property="carNum" column="car_num"/>
        <result property="hqSortNo" column="hq_sort_no"/>
        <result property="hqStatus" column="hq_status"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonInfo">
        <selectKey resultType="long" keyColumn="id_" keyProperty="id">
            SELECT LAST_INSERT_ID() AS id_
        </selectKey>
        <![CDATA[
		insert into base_person_info
	    (company_id,name_,phone_,id_card,open_id,face_enabled,face_bound,card_enabled,
	    app_enabled,password_enabled,guest_enabled,position1_,position2_,
	    position3_,position4_,position5_,face_image_url,del_flag,
	    create_by,create_time,update_by,update_time,popedom_,wechat_notice_enabled,allow_view_local,health_ertificate_url,is_sync,sex_,
	    park_id,work_status,job_number,car_num,hq_sort_no,hq_status)
		values
		(
            #{companyId,jdbcType=VARCHAR}
            ,#{name,jdbcType=VARCHAR}
            ,#{phone,jdbcType=VARCHAR}
            ,#{idCard,jdbcType=VARCHAR}
            ,#{openId,jdbcType= VARCHAR }
            ,#{faceEnabled,jdbcType= NUMERIC }
            ,#{faceBound,jdbcType= NUMERIC }
            ,#{cardEnabled,jdbcType= NUMERIC }
            ,#{appEnabled,jdbcType= NUMERIC }
            ,#{passwordEnabled,jdbcType= NUMERIC }
            ,#{guestEnabled,jdbcType= NUMERIC }
            ,#{position1,jdbcType=VARCHAR}
            ,#{position2,jdbcType=VARCHAR}
            ,#{position3,jdbcType=VARCHAR}
            ,#{position4,jdbcType=VARCHAR}
            ,#{position5,jdbcType=VARCHAR}
            ,#{faceImageUrl,jdbcType=VARCHAR}
            ,#{delFlag,jdbcType= NUMERIC }
            ,#{createBy,jdbcType=VARCHAR}
            ,#{createTime,jdbcType= TIMESTAMP }
            ,#{updateBy,jdbcType=VARCHAR}
            ,#{updateTime,jdbcType= TIMESTAMP }
            ,#{popedom,jdbcType= VARCHAR }
            ,#{wechatNoticeEnabled,jdbcType= NUMERIC }
            ,#{allowViewLocal,jdbcType= NUMERIC }
            ,#{healthErtificateUrl,jdbcType=VARCHAR}
            ,#{isSync,jdbcType= NUMERIC }
            ,#{sex,jdbcType=VARCHAR}
            ,#{parkId,jdbcType=VARCHAR}
            ,#{workStatus,jdbcType=VARCHAR}
            ,#{jobNumber,jdbcType=VARCHAR}
            ,#{carNum,jdbcType=VARCHAR}
            ,#{hqSortNo,jdbcType= NUMERIC }
            ,#{hqStatus,jdbcType=VARCHAR}
		)
	]]>
    </insert>
    <delete id="delete" parameterType="long">
        delete from base_person_info where id_=#{id,jdbcType=NUMERIC}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonInfo">
        update base_person_info
        <set>
            <if test="companyId!=null">
                company_id=#{companyId,jdbcType=VARCHAR},
            </if>
            <if test="name!=null">
                name_=#{name,jdbcType=VARCHAR},
            </if>
            <if test="phone!=null">
                phone_=#{phone,jdbcType=VARCHAR},
            </if>
            <if test="idCard!=null">
                id_card=#{idCard,jdbcType=VARCHAR},
            </if>
            <if test="openId!=null">
                open_id=#{openId,jdbcType= VARCHAR },
            </if>
            <if test="faceEnabled!=null">
                face_enabled=#{faceEnabled,jdbcType= NUMERIC },
            </if>
            <if test="faceBound!=null">
                face_bound=#{faceBound,jdbcType= NUMERIC },
            </if>
            <if test="cardEnabled!=null">
                card_enabled=#{cardEnabled,jdbcType= NUMERIC },
            </if>
            <if test="appEnabled!=null">
                app_enabled=#{appEnabled,jdbcType= NUMERIC },
            </if>
            <if test="passwordEnabled!=null">
                password_enabled=#{passwordEnabled,jdbcType= NUMERIC },
            </if>
            <if test="guestEnabled!=null">
                guest_enabled=#{guestEnabled,jdbcType= NUMERIC },
            </if>
            <if test="position1!=null">
                position1_=#{position1,jdbcType=VARCHAR},
            </if>
            <if test="position2!=null">
                position2_=#{position2,jdbcType=VARCHAR},
            </if>
            <if test="position3!=null">
                position3_=#{position3,jdbcType=VARCHAR},
            </if>
            <if test="position4!=null">
                position4_=#{position4,jdbcType=VARCHAR},
            </if>
            <if test="position5!=null">
                position5_=#{position5,jdbcType=VARCHAR},
            </if>
            <if test="faceImageUrl!=null">
                face_image_url=#{faceImageUrl,jdbcType=VARCHAR},
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="popedom!=null">
                popedom_=#{popedom,jdbcType= VARCHAR },
            </if>
            <if test="wechatNoticeEnabled!=null">
                wechat_notice_enabled=#{wechatNoticeEnabled,jdbcType= VARCHAR },
            </if>
            <if test="allowViewLocal!=null">
                allow_view_local=#{allowViewLocal,jdbcType= VARCHAR },
            </if>
            <if test="healthErtificateUrl!=null">
                health_ertificate_url=#{healthErtificateUrl,jdbcType=VARCHAR},
            </if>
            <if test="isSync!=null">
                is_sync=#{isSync,jdbcType=NUMERIC},
            </if>
            <if test="sex!=null">
                sex_=#{sex,jdbcType=VARCHAR},
            </if>
            <if test="parkId!=null">
                park_id=#{parkId,jdbcType=VARCHAR},
            </if>
            <if test="workStatus != null">
                work_status = #{workStatus,jdbcType=VARCHAR},
            </if>
            <if test="healthyCode != null">
                healthy_code = #{healthyCode,jdbcType=VARCHAR},
            </if>
            <if test="jobNumber!=null">
                job_number = #{jobNumber,jdbcType=VARCHAR},
            </if>
            <if test="carNum != null">
                car_num = #{carNum,jdbcType=VARCHAR},
            </if>
            <if test="hqSortNo != null">
                hq_sort_no = #{hqSortNo,jdbcType=NUMERIC},
            </if>
            <if test="hqStatus != null">
                hq_status = #{hqStatus,jdbcType=VARCHAR},
            </if>
        </set>
        where id_=#{id}
    </update>
    <update id="updateHealthyCode" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonInfo">
        update base_person_info
        <set>
            <if test="healthyCode!=null">
                healthy_code=#{healthyCode},
            </if>
            <if test="healthyReason!=null">
                healthy_reason=#{healthyReason},
            </if>
            <if test="updateHealthyTime!=null">
                update_healthy_time=#{updateHealthyTime},
            </if>
        </set>
        where id_card=#{idCard} and name_=#{name}
    </update>
    <update id="updateNullForHqSortNo" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonInfo">
        update base_person_info set hq_sort_no = null
        where id_=#{id} and del_flag = 0
    </update>
    <select id="get" parameterType="long" resultMap="PersonInfoMap">
        select
        a.*,b.name_ as company_name
        from base_person_info a
        left join base_company_info b on a.company_id = b.id_
        where a.id_=#{0} and a.del_flag=false limit 1
    </select>
    <select id="exist" parameterType="long" resultType="int">
        select count(*) from base_person_info where id_=#{0}  and del_flag = 0
    </select>
    <select id="list" resultMap="PersonInfoMap">
        select * from base_person_info where del_flag = 0
    </select>
    <select id="search" parameterType="hashmap" resultMap="PersonInfoMap">
        <![CDATA[
			SELECT
                a.*,
                b.name_ AS company_name,
                d.person_role_name AS popedom_name
            FROM
                base_person_info a
                LEFT JOIN base_company_info b ON b.id_ = a.company_id
                LEFT JOIN base_person_popedom c ON c.id_ = a.popedom_
                LEFT JOIN base_person_role_info d ON d.id_ = c.person_role_id
		]]>
        <where>
                a.del_flag = 0
            <if test="searchParams.id != null">
                and a.ID_ = #{searchParams.id}
            </if>
            <if test="searchParams.inCompanyId != null">
                and a.company_id = #{searchParams.inCompanyId}
            </if>
            <if test="searchParams.companyId != null">
                and (a.company_id = #{searchParams.companyId}
                    or exists(
                        select t1.company_id from base_person_popedom t1
                        where t1.person_id = a.id_
                        and t1.del_flag=0
                        and t1.company_id = #{searchParams.companyId}
                        <if test="searchParams.roleId != null">
                        and t1.person_role_id=#{searchParams.roleId}
                        </if>
                    )
                )
            </if>
            <trim prefix="and (" prefixOverrides="AND|OR" suffix=")">
                <if test="searchParams.companyCode != null">
                    and b.code_ like #{searchParams.companyCode}
                </if>
                <if test="searchParams.relateCompanyList!=null">
                    <foreach collection="searchParams.relateCompanyList" item="item" open="or a.company_id in(" separator="," close=")">
                        #{item.id}
                    </foreach>
                </if>
            </trim>
<!--            <if test="searchParams.companyCode != null">-->
<!--                and (b.code_ like #{searchParams.companyCode}-->
<!--                or exists(-->
<!--                    select t1.company_id from base_person_popedom t1,base_company_info t2-->
<!--                    where t1.company_id = t2.id_-->
<!--                    and t1.person_id = a.id_-->
<!--                    and t1.del_flag=0-->
<!--                    and t2.code_ like #{searchParams.companyCode}-->
<!--                    <if test="searchParams.roleId != null">-->
<!--                    and t1.person_role_id=#{searchParams.roleId}-->
<!--                    </if>-->
<!--                ))-->
<!--            </if>-->
            <if test="searchParams.roleId != null">
                and exists(
                    select t1.company_id from base_person_popedom t1
                    where
                    t1.person_id = a.id_
                    and t1.del_flag=0
                    and t1.person_role_id=#{searchParams.roleId}
                )
            </if>
            <if test="searchParams.name != null">
                and a.name_ like #{searchParams.name}
            </if>
            <if test="searchParams.popedom != null">
                and a.popedom_ like #{searchParams.popedom}
            </if>
            <if test="searchParams.nameList != null">
                <foreach item="name" collection="searchParams.nameList" open="and (" separator="or" close=")">
                    a.name_ like #{name}
                </foreach>
            </if>
            <if test="searchParams.idCard != null">
                and a.id_card like #{searchParams.idCard}
            </if>
            <if test="searchParams.phone != null">
                and a.phone_ like #{searchParams.phone}
            </if>
            <if test="searchParams.position1 != null">
                and a.position1_ like #{searchParams.position1}
            </if>
            <if test="searchParams.position2 != null">
                and a.position2_ like #{searchParams.position2}
            </if>
            <if test="searchParams.position3 != null">
                and a.position3_ like #{searchParams.position3}
            </if>
            <if test="searchParams.position4 != null">
                and a.position4_ like #{searchParams.position4}
            </if>
            <if test="searchParams.position5 != null">
                and a.position5_ like #{searchParams.position5}
            </if>
            <if test="searchParams.faceBound != null">
                and a.face_bound = #{searchParams.faceBound}
            </if>
            <if test="'1'.toString() == searchParams.isUploadPhoto and null != searchParams.isUploadPhoto">
                and a.face_image_url is not null
            </if>
            <if test="'0'.toString() == searchParams.isUploadPhoto and null != searchParams.isUploadPhoto">
                and a.face_image_url is null
            </if>
            <if test="searchParams.abnormalDate != null">
                <![CDATA[
                and a.id_ in (
                    select bf.person_id from base_person_device_filter_log bf,base_person_info pc
                    where bf.person_id = pc.id_
                    and pc.company_id = a.company_id
                    and DATEDIFF(bf.record_time,#{searchParams.abnormalDate})=0
                    and bf.del_flag=0
                    and bf.temperature_>#{searchParams.temperatureMax}
                )
                ]]>
            </if>
            <if test="searchParams.relationPersonIds != null">
                <foreach item="relationPersonId" collection="searchParams.relationPersonIds" open="and a.id_ in (" separator="," close=")">
                    #{relationPersonId}
                </foreach>
            </if>
            <if test="searchParams.parkId != null">
                and a.park_id = #{searchParams.parkId}
            </if>
            <if test="searchParams.personRoleName != null">
                AND a.id_ IN (
                    SELECT person_id FROM base_person_popedom WHERE person_role_id IN (
                    SELECT id_ FROM base_person_role_info WHERE person_role_name = #{searchParams.personRoleName}
                ) )
            </if>
            <if test="searchParams.enableAttendance != null">
                AND a.id_ IN (
                    select person_id FROM base_person_popedom
                    where enable_attendance = #{searchParams.enableAttendance}
                )
            </if>
            <if test="searchParams.enableRegisterExamine != null">
                AND a.id_ IN (
                select person_id FROM base_person_popedom
                where enable_register_examine = #{searchParams.enableRegisterExamine}
                )
            </if>
            <if test="searchParams.healthyCode != null">
                <if test="searchParams.healthyCode != '11'">
                    and a.healthy_code = #{searchParams.healthyCode}
                </if>
                <if test="searchParams.healthyCode == '11'">
                    <![CDATA[
                      and (a.healthy_code = #{searchParams.healthyCode} or a.healthy_code is null)
                    ]]>
                </if>
            </if>
            <if test="searchParams.isOutsourcer != null">
                and b.is_outsourcer = #{searchParams.isOutsourcer}
            </if>
            <if test="searchParams.jobNumber != null">
                and a.job_number = #{searchParams.jobNumber}
            </if>
            <if test="searchParams.popedomRemark != null">
                and c.remark_ = #{searchParams.popedomRemark}
            </if>
            <trim prefix="and (" prefixOverrides="AND|OR" suffix=")">
                <if test="searchParams.companyInfos != null">
                    <foreach item="companyInfo" collection="searchParams.companyInfos" open="and a.company_id in (" separator="," close=")">
                        #{companyInfo.id}
                    </foreach>
                </if>
                <if test="searchParams.popedomRemarkList != null">
                    <foreach item="popedomRemark" collection="searchParams.popedomRemarkList" open="or c.remark_ in (" separator="," close=")">
                        #{popedomRemark}
                    </foreach>
                </if>
                <if test="searchParams.attendanceCompanyCode != null">
                    or b.code_ like #{searchParams.attendanceCompanyCode}
                </if>
            </trim>
            <if test="searchParams.hqStatus != null">
                and a.hq_status = #{searchParams.hqStatus}
            </if>
            <if test="searchParams.querHq != null">
                and a.id_ in(
                select person_id from base_hq_person
                where del_flag=0
                )
            </if>
            <if test="searchParams.popedomList != null">
                <foreach item="popedom" collection="searchParams.popedomList" open="and a.popedom_ in (" separator="," close=")">
                    #{popedom}
                </foreach>
            </if>
            <if test="searchParams.carNum != null">
                and a.car_num like #{searchParams.carNum}
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="searchNew" parameterType="hashmap" resultMap="PersonInfoMap">
        <![CDATA[
			SELECT a.*,b.name_ AS company_name FROM base_person_info a
            LEFT JOIN base_company_info b ON a.company_id = b.id_
		]]>
        <where>
            a.del_flag = 0
            <if test="searchParams.name != null">
                and a.name_ like #{searchParams.name}
            </if>
            <if test="searchParams.companyCode != null">
                and b.code_ like #{searchParams.companyCode}
            </if>
            <if test="searchParams.status != null">
                and a.status_ = #{searchParams.status}
            </if>
            <if test="searchParams.isReport == 1">
                AND a.id_ in ( SELECT c.person_id FROM base_person_health_ledger c
                WHERE a.id_ = c.person_id
                <if test="searchParams.addressOut != null">
                    and c.address_ NOT like #{searchParams.addressOut}
                </if>
                <if test="searchParams.addressIn != null">
                    and c.address_ like #{searchParams.addressIn}
                </if>
                <if test="searchParams.temperature != null">
                    <![CDATA[
					and c.temperature_ > #{searchParams.temperature}
				]]>
                </if>
                <if test="searchParams.queryTime != null">
                    and DATE_FORMAT(c.create_time,'%Y-%m-%d') =  DATE_FORMAT(#{searchParams.queryTime},'%Y-%m-%d')
                </if>
                )
            </if>
            <if test="searchParams.isReport == 0">
                AND a.id_ not in ( SELECT c.person_id FROM base_person_health_ledger c
                WHERE a.id_ = c.person_id
                <if test="searchParams.queryTime != null">
                    and DATE_FORMAT(c.create_time,'%Y-%m-%d') =  DATE_FORMAT(#{searchParams.queryTime},'%Y-%m-%d')
                </if>
                )
            </if>
            <if test="searchParams.isClassTeacher != null">
                and a.id_ in (select person_id from base_person_company d where d.is_class_teacher = #{searchParams.isClassTeacher})
            </if>
            <if test="searchParams.neJobType != null">
                and a.id_ in (select person_id from base_teacher_info e where e.job_type != #{searchParams.neJobType})
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="findByNameAndPhone" resultMap="PersonInfoMap">
        select
        a.*,b.name_ as company_name
        from base_person_info a
        left join base_company_info b on a.company_id = b.id_
        where a.name_=#{name} and a.phone_=#{phone} and a.del_flag=0 limit 1
    </select>
    <select id="findByOpenId" resultMap="PersonInfoMap">
        select
        a.*,b.name_ as company_name
        from base_person_info a
        left join base_company_info b on a.company_id = b.id_
        where a.open_id=#{openId} and a.del_flag = 0 limit 1
    </select>
    <select id="findByCompanyId" resultMap="PersonInfoMap">
        select a.* from base_person_info a
        where
        a.company_id=#{companyId} and a.del_flag = 0
        order by a.id_ asc
    </select>
    <select id="findListByCompanyCode" resultMap="PersonInfoMap">
        select a.*,b.name_ as company_name
        from base_person_info
        a left join base_company_info b on a.company_id = b.id_
        where
        b.code_ like #{companyCode}
        and a.del_flag = 0
        and b.del_flag = 0
        order by a.id_ asc
    </select>
    <select id="findByCompanyIdAndPopedom" resultMap="PersonInfoMap">
        select * from base_person_info
        where
        company_id=#{companyId} and popedom_ like #{type}  and del_flag = 0
        order by id_ asc
    </select>
    <select id="countByCompanyCode" resultType="long">
        select count(*)
        from base_person_info a
        where a.del_flag = 0
        and
        (
            a.id_ in (
                select c.person_id from base_company_info b,base_person_popedom c
                where c.company_id = b.id_
                and b.code_ like #{code}
                and c.del_flag=0
            )
            <if test="personId!=null">
            or a.company_id in (
                select c.company_id from base_person_company c where c.person_id = #{personId}
            )
            </if>
        )
    </select>
    <select id="findByCompanyCode" resultMap="com.jpsoft.shinestar.modules.base.dao.PersonInfoDAO.PersonInfoMap">
        select a.*,b.name_ as company_name
        from base_person_info a
        left join base_company_info b on a.company_id = b.id_
        where a.del_flag = 0
        and b.code_ like #{searchParams.code}
        <if test="searchParams.nameList != null">
            <foreach item="name" collection="searchParams.nameList" open="and (" separator="or" close=")">
                a.name_ like #{name}
            </foreach>
        </if>
        <if test="searchParams.name != null">
            and a.name_ like #{searchParams.name}
        </if>
        <if test="searchParams.position1 != null">
            and a.position1_ like #{searchParams.position1}
        </if>
        <if test="searchParams.position2 != null">
            and a.position2_ like #{searchParams.position2}
        </if>
        <if test="searchParams.position3 != null">
            and a.position3_ like #{searchParams.position3}
        </if>
        <if test="searchParams.position4 != null">
            and a.position4_ like #{searchParams.position4}
        </if>
        <if test="searchParams.position5 != null">
            and a.position5_ like #{searchParams.position5}
        </if>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            a.${sort.name} ${sort.order}
        </foreach>
    </select>

    <select id="getIgnoreDelFlag" parameterType="long" resultMap="PersonInfoMap">
        select
        a.*,b.name_ as company_name
        from base_person_info a
        left join base_company_info b on a.company_id = b.id_
        where a.id_=#{0}  limit 1
    </select>

    <select id="countByCompanyList" resultType="Long">
        <![CDATA[
        select count(*) from base_person_info where del_flag = 0
        and guest_enabled is not true
        ]]>
        <foreach collection="list" index="index" item="item" open="and company_id in(" separator="," close=")">
            #{item.id}
        </foreach>
    </select>

    <select id="countByCompanyListAndPopedom" resultType="Long">
        select count(*) from base_person_info where del_flag = 0
        and popedom_ like #{type}
        and guest_enabled is not true
        <foreach collection="list" index="index" item="item" open="and company_id in(" separator="," close=")">
            #{item.id}
        </foreach>
    </select>

    <select id="countStudentByCompanyListAndPopedom" resultType="Long">
        select count(*) from base_person_info where del_flag = 0
        and guest_enabled is not true
        and popedom_ = #{type}
        <foreach collection="list" index="index" item="item" open="and company_id in(" separator="," close=")">
            #{item.id}
        </foreach>
    </select>

    <select id="findById" resultMap="PersonInfoMap">
        select a.* from base_person_info a
        where
        a.id_=#{id} and a.del_flag = 0
        order by a.id_ asc
    </select>

    <select id="findByUpdateHealthyTime" resultMap="PersonInfoMap">
        <![CDATA[
            select * from base_person_info
            where del_flag = 0
            and length(id_card)>0
            and (update_healthy_time is null or update_healthy_time<#{startDate})
            order by update_healthy_time asc
            limit ${limit}
        ]]>
    </select>


    <select id="countByCompanyId" resultType="long">
        select count(*) from base_person_info where company_id=#{0} and del_flag=0
    </select>

    <select id="countByCompanyIds" resultType="long">
        <![CDATA[
        select count(*) from base_person_info where  del_flag=0
        ]]>
        <foreach collection="list" index="index" item="item" open="and company_id in(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="findByIdCardAndCompanyId" resultMap="PersonInfoMap">
        <![CDATA[
         select * from base_person_info where del_flag = 0
            and id_card = #{idCard}
            and company_id = #{companyId}
                ]]>
    </select>
    <select id="notGuestList" resultType="Long">
        <![CDATA[
        select count(*) from base_person_info where del_flag = 0
                and guest_enabled is not true
        ]]>
    </select>
    <update id="resetOpenId" parameterType="long">
        update base_person_info set open_id=null where id_=#{0} limit 1
    </update>
    <select id="groupByHealthyCode" resultType="java.util.Map">
        select healthy_code as healthyCode,count(*) as total
        from base_person_info t1
        where t1.del_flag=0
        and exists(
            select t2.company_id from base_person_popedom t2,base_company_info t3
            where t2.company_id = t3.id_
            and t2.person_id = t1.id_
            and t2.del_flag=0
            and t3.del_flag=0
            and t3.code_ like #{companyCode}
        )
        group by healthy_code
    </select>

    <select id="groupHealthyById" resultType="java.util.Map">
        select healthy_code as healthyCode,count(*) as total
        from base_person_info
        where del_flag = 0
        <foreach item="id" collection="idList" open="and id_ in (" separator="," close=")">
            #{id}
        </foreach>
        group by healthy_code
    </select>
    <select id="findByPhone" resultMap="PersonInfoMap">
        <![CDATA[
            select * from base_person_info
            where del_flag = 0
            and ifnull(guest_enabled,0) = 0
            and phone_ = #{0}
            limit 1
        ]]>
    </select>

    <select id="findByOrderProperty" parameterType="hashmap" resultType="Long">
        select a.id_ from base_person_info a  where a.del_flag = 0
        <if test="searchParams.name != null">
            and a.name_ like #{searchParams.name}
        </if>
        <if test="searchParams.companyId != null">
            and a.company_id = #{searchParams.companyId}
        </if>
    </select>
    <select id="findByNameAndIdCard" resultMap="PersonInfoMap">
        <![CDATA[
            select * from base_person_info
            where del_flag = 0
            and name_ = #{name}
            and id_card = #{idCard}
            order by create_time asc
            limit 1
        ]]>
    </select>
    <select id="searchCanteenOutsourcerBalance" parameterType="hashmap" resultMap="PersonInfoMap">
    <![CDATA[
			SELECT a.* FROM base_person_info a
			LEFT JOIN base_company_info c ON a.company_id = c.id_
			LEFT JOIN base_canteen_outsourcer_balance b ON a.id_ = b.person_id
		]]>
    <where>
        a.del_flag = 0 and c.del_flag = 0
        <if test="searchParams.name != null">
            and a.name_ like #{searchParams.name}
        </if>
        <if test="searchParams.jobNumber != null">
            and a.job_number like #{searchParams.jobNumber}
        </if>
        <if test="searchParams.companyId != null">
            and c.code_ like #{searchParams.companyId}
        </if>
        <if test="searchParams.isOutsourcer != null">
            and c.is_outsourcer = #{searchParams.isOutsourcer}
        </if>
        <if test="searchParams.beginTime != null">
            <![CDATA[
                  and b.last_use_time >= #{searchParams.beginTime}
                ]]>
        </if>
        <if test="searchParams.endTime != null">
            <![CDATA[
                  and b.last_use_time <= #{searchParams.endTime}
                ]]>
        </if>
    </where>
    <foreach item="sort" collection="sortList" open="order by" separator=",">
        ${sort.name} ${sort.order}
    </foreach>
    </select>

    <select id="findByIdIgnoreDel" resultMap="PersonInfoMap">
        <![CDATA[

        select * from base_person_info
        where id_ = #{personId}
        ]]>
    </select>
    <select id="findByCarNum" resultMap="PersonInfoMap">
        <![CDATA[
        select * from base_person_info
        where del_flag = 0
        and find_in_set(#{carNum},car_num)
        limit 1
        ]]>
    </select>
    <select id="findJobNumber" resultMap="PersonInfoMap">
        select * from base_person_info where del_flag=false and job_number = #{0} limit 1
    </select>
    <select id="findByHq" resultMap="PersonInfoMap">
        select * from base_person_info where del_flag=false and hq_status='1' or hq_status='2'
    </select>

</mapper>