<?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.dcsoft.system.mapper.SysPeopleMapper">

    <resultMap type="SysPeople" id="SysPeopleResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="phone"    column="phone"    />
        <result property="sex"    column="sex"    />
        <result property="avatar"    column="avatar"    />
        <result property="branchId"    column="branch_id"    />
        <result property="position"    column="position"    />
        <result property="idcard"    column="idcard"    />
        <result property="carNo"    column="car_no"    />
        <result property="doorNo"    column="door_no"    />
        <result property="guid"    column="guid"    />
        <result property="faceGuid"    column="face_guid"    />
        <result property="userId"    column="user_id"    />
        <result property="remark"    column="remark"    />
        <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="gh"    column="gh"    />
        <result property="fingerprint"    column="fingerprint"    />
        <result property="joinTime"    column="join_time"    />
        <result property="down"    column="down"    />
        <result property="branchName"    column="branchName"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="openid"    column="openid"    />
        <result property="isExamine"    column="is_examine"    />
        <result property="carryStuffExamine"    column="carry_stuff_examine"    />
        <association property="branch"    column="branch_id" javaType="SysBranch" resultMap="branchResult" />
    </resultMap>

    <resultMap id="branchResult" type="SysBranch">
        <id     property="id"    column="branch_id"     />
        <result property="name"  column="branchName"   />
        <result property="ruleId"  column="rule_id"   />
    </resultMap>

    <sql id="selectSysPeopleVo">
        select p.id, p.name, p.phone, p.sex, p.avatar, p.branch_id, p.position, p.idcard, p.car_no,p.door_no, p.guid, p.face_guid, p.openid, b.is_examine,
               p.user_id, p.remark, p.create_by, p.create_time, p.update_by, p.update_time ,b.name branchName,p.gh,p.fingerprint,p.join_time,p.down,p.del_flag,b.rule_id,
               p.carry_stuff_examine
        from sys_people p left join sys_branch b on p.branch_id=b.id
        left join sys_user u on u.user_id = p.user_id
        left join sys_dept d on u.dept_id = d.dept_id
    </sql>

    <select id="selectSysPeopleList" parameterType="SysPeople" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        <where>
            <if test="sysPeople.id != null  and sysPeople.id != ''"> and p.id = #{sysPeople.id}</if>
            <if test="sysPeople.delFlag != null  and sysPeople.delFlag != ''"> and p.del_flag = #{sysPeople.delFlag}</if>
            <if test="sysPeople.name != null  and sysPeople.name != ''"> and p.name like concat('%', #{sysPeople.name}, '%')</if>
            <if test="sysPeople.phone != null  and sysPeople.phone != ''"> and p.phone like concat('%', #{sysPeople.phone}, '%')</if>
            <if test="sysPeople.sex != null  and sysPeople.sex != ''"> and p.sex = #{sysPeople.sex}</if>
            <if test="sysPeople.doorNo != null  and sysPeople.doorNo != ''"> and p.door_no = #{sysPeople.doorNo}</if>
<!--            <if test="branchId != null ">-->
<!--                AND (p.branch_id = #{branchId} OR p.branch_id IN ( SELECT t.id FROM sys_branch t WHERE find_in_set(#{branchId}, ancestors) ))-->
<!--            </if>-->
            <if test="list != null and list.size() > 0">
                and p.branch_id in
                <foreach collection="list" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="sysPeople.downFlag != null  and sysPeople.downFlag != ''">
                <if test="sysPeople.downFlag=='yxf'">
                    and p.down >0
                </if>
                <if test="sysPeople.downFlag=='wxf'">
                    and p.down =0
                </if>
            </if>
            <if test="sysPeople.guidFlag != null  and sysPeople.guidFlag != ''">
                <if test="sysPeople.guidFlag=='yzc'">
                    and p.guid is not null
                </if>
                <if test="sysPeople.guidFlag=='wzc'">
                    and p.guid is  null
                </if>

             </if>
            <if test="sysPeople.faceFlag != null  and sysPeople.faceFlag != ''">
                <if test="sysPeople.faceFlag=='yzc'">
                    and p.face_guid is not null
                </if>
                <if test="sysPeople.faceFlag=='wzc'">
                    and p.face_guid is  null
                </if>

            </if>
            <if test="sysPeople.cardFlag != null  and sysPeople.cardFlag != ''">
                <if test="sysPeople.cardFlag=='yzc'">
                    and p.door_no is not null
                </if>
                <if test="sysPeople.cardFlag=='wzc'">
                    and p.door_no is  null
                </if>

            </if>
            <if test="sysPeople.delFlag != null"> and p.del_flag = #{sysPeople.delFlag}</if>
            <if test="sysPeople.branchParent != null  and sysPeople.branchParent != ''"> and b.id  like concat(#{sysPeople.branchParent}, '%')</if>
            <if test="sysPeople.carryStuffExamine != null  and sysPeople.carryStuffExamine != ''"> and p.carry_stuff_examine = #{sysPeople.carryStuffExamine}</if>
        </where>
    </select>

    <select id="selectSysPeopleById" parameterType="Long" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.id = #{id}
    </select>

    <insert id="insertSysPeople" parameterType="SysPeople" useGeneratedKeys="true" keyProperty="id">
        insert into sys_people
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="phone != null">phone,</if>
            <if test="sex != null">sex,</if>
            <if test="avatar != null">avatar,</if>
            <if test="branchId != null">branch_id,</if>
            <if test="position != null">position,</if>
            <if test="idcard != null">idcard,</if>
            <if test="carNo != null">car_no,</if>
            <if test="doorNo != null">door_no,</if>
            <if test="guid != null">guid,</if>
            <if test="faceGuid != null">face_guid,</if>
            <if test="userId != null">user_id,</if>
            <if test="remark != null">remark,</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="gh != null">gh,</if>
            <if test="fingerprint != null">fingerprint,</if>
            <if test="joinTime != null">join_time,</if>
            <if test="down != null">down,</if>
            <if test="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="phone != null">#{phone},</if>
            <if test="sex != null">#{sex},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="branchId != null">#{branchId},</if>
            <if test="position != null">#{position},</if>
            <if test="idcard != null">#{idcard},</if>
            <if test="carNo != null">#{carNo},</if>
            <if test="doorNo != null">#{doorNo},</if>
            <if test="guid != null">#{guid},</if>
            <if test="faceGuid != null">#{faceGuid},</if>
            <if test="userId != null">#{userId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="gh != null">#{gh},</if>
            <if test="fingerprint != null">#{fingerprint},</if>
            <if test="joinTime != null">#{joinTime},</if>
            <if test="down != null">#{down},</if>
            <if test="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updateSysPeople" parameterType="SysPeople">
        update sys_people
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="branchId != null">branch_id = #{branchId},</if>
            <if test="position != null">position = #{position},</if>
            <if test="idcard != null">idcard = #{idcard},</if>
            <if test="carNo != null">car_no = #{carNo},</if>
            <if test="doorNo != null">door_no = #{doorNo},</if>
            <if test="guid != null">guid = #{guid},</if>
            <if test="faceGuid != null">face_guid = #{faceGuid},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="gh != null">gh = #{gh},</if>
            <if test="fingerprint != null">fingerprint = #{fingerprint},</if>
            <if test="joinTime != null">join_time = #{joinTime},</if>
            <if test="down != null">down = #{down},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="openid != null">openid = #{openid},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteSysPeopleById" parameterType="Long">
        update  sys_people set del_flag='2'  where id = #{id}
    </update>

    <update id="deleteSysPeopleByIds" parameterType="String">
        update  sys_people set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="deleteSysPeopleByGuid" parameterType="String">
        update  sys_people set del_flag='2'  where guid = #{guid}
    </update>
    <update id="updatePeople">
        update sys_people set del_flag='2', update_time = now()  where gh = #{gh}
    </update>
    <update id="updatePeopleInfo">
        update sys_people set openid= #{openid} where phone = #{phone} and name = #{name}
    </update>

    <select id="selectByGuid" parameterType="SysPeople" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.guid = #{guid}
    </select>

    <select id="selectSysPeopleByUserId" parameterType="Long" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.id = #{userId}
    </select>


    <select id="selectSysPeopleByBranchUser" parameterType="String" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.name = #{userName} and b.name = #{branchName} and  p.del_flag = '0'
    </select>

    <select id="selectSysPeopleByGh" parameterType="String" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.gh = #{gh} limit 1
    </select>

    <select id="selectSysPeopleByPhoneUser" parameterType="SysPeople" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.name = #{name} and
        p.phone = #{phone} and  p.del_flag = '0'
    </select>
    <select id="queryPeopleInfo" resultType="com.dcsoft.system.domain.SysPeople">
        select id from sys_people where del_flag = '0' and down = '0' limit 5
    </select>
    <select id="queryPeople" resultType="com.dcsoft.system.domain.SysPeople">
        select id, gh
        from sys_people
        where del_flag = '0'
        <if test="list != null and list.size() > 0">
            and gh in
            <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="queryPeopleDetails" resultType="com.dcsoft.system.domain.SysPeople">
        select name from sys_people where del_flag = '0' and gh = #{gh}
    </select>

    <select id="selectSysPeopleByUserName" parameterType="String" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.name = #{userName}
        limit 1
    </select>
    <select id="selectDoorNo" resultType="java.lang.String">
        select sp.door_no doorNo
        from sys_empower_record ser
                 left join sys_people sp on ser.people_id = sp.id
        where sp.del_flag = '0'
          and ser.id = #{id}
        limit 1
    </select>
    <select id="selectPeopleById" resultType="java.lang.String">
        select id from sys_people where del_flag = '0' and door_no = #{card} limit 1
    </select>
    <select id="queryPeopleById" resultType="com.dcsoft.system.domain.vo.OfficialAccountVo">
        select name, phone, id, date_format(create_time, '%Y-%m-%d %H:%i:%s') createTime, avatar, position from sys_people  where id = #{id}
        union all
        select user_name name, phone, id, date_format(created_time, '%Y-%m-%d %H:%i:%s') createTime, '' avatar, '' position from vis_visitor_register where id = #{id}
    </select>
    <select id="selectSysPeopleByOpenId" parameterType="String" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.openid = #{openid} limit 1
    </select>
    <select id="selectSysPeopleGh" parameterType="String" resultMap="SysPeopleResult">
        <include refid="selectSysPeopleVo"/>
        where p.phone = #{phone} and p.name = #{userName} limit 1
    </select>
    <select id="queryPeopleName" resultType="com.dcsoft.system.domain.SysPeople">
        select phone, name, id, branch_id branchId, position, id, carry_stuff_examine carryStuffExamine
        from sys_people
        where del_flag = '0'
          and phone = #{phone}
        <if test="list != null and list.size() > 0">
            and branch_id in
            <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        limit 1
    </select>
    <select id="selectSysPeopleByDoorNo" resultType="com.dcsoft.system.domain.SysPeople">
        select * from sys_people where del_flag = '0' and door_no = #{doorNo} limit 1
    </select>
    <select id="queryEmpowerRecord" resultType="com.dcsoft.system.domain.SysPeople">
        select phone, p.name, p.id, branch_id branchId
        from sys_empower_record ser
                 left join sys_people p on ser.people_id=p.id
                 left join sys_equipment e on ser.equipment_id=e.id
        where rule_id != '10' and p.phone = #{phone} limit 1
    </select>
    <select id="queryPeopleByOpenid" resultType="com.dcsoft.system.domain.SysPeople">
        select id visitorRegisterId, user_name name, phone, openid from vis_visitor_register where openid = #{openid}
    </select>
    <select id="queryPeopleByPosition" resultType="com.dcsoft.system.domain.SysPeople">
        select id, name, position from sys_people where del_flag = '0' and position = #{position}
    </select>
    <select id="querySysPeopleInfo" resultType="com.dcsoft.system.domain.SysPeople">
        select ifnull(phone, '0') phone, id from sys_people where del_flag = '0' and user_id = #{userId}
    </select>
</mapper>
