<?xml version="1.0" encoding="UTF-8" ?>
<!--
  ~ Copyright 2016 http://www.hswebframework.org
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~       http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~  See the License for the specific language governing permissions and
  ~  limitations under the License.
  ~
  -->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.hswebframework.web.dao.organizational.PersonDao">
    <resultMap id="PersonResultMap" type="org.hswebframework.web.entity.organizational.SimplePersonEntity">
        <id property="id" column="u_id" javaType="string" jdbcType="VARCHAR"/>
        <result property="name" column="name" javaType="String" jdbcType="VARCHAR"/>
        <result property="sex" column="sex" javaType="Byte" jdbcType="DECIMAL"/>
        <result property="email" column="email" javaType="String" jdbcType="VARCHAR"/>
        <result property="phone" column="phone" javaType="String" jdbcType="VARCHAR"/>
        <result property="photo" column="photo" javaType="String" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" javaType="String" jdbcType="VARCHAR"/>
        <result property="status" column="status" javaType="Byte" jdbcType="DECIMAL"/>
        <result property="remark" column="remark" javaType="String" jdbcType="VARCHAR"/>
    </resultMap>

    <!--用于动态生成sql所需的配置-->
    <sql id="config">
        <bind name="resultMapId" value="'PersonResultMap'"/>
        <bind name="tableName" value="'s_person'"/>
    </sql>

    <insert id="insert" parameterType="org.hswebframework.web.entity.organizational.SimplePersonEntity">
        <include refid="config"/>
        <include refid="BasicMapper.buildInsertSql"/>
    </insert>

    <delete id="deleteByPk" parameterType="String">
        delete from s_person where u_id =#{id}
    </delete>

    <delete id="delete" parameterType="org.hswebframework.web.commons.entity.Entity">
        <include refid="config"/>
        <include refid="BasicMapper.buildDeleteSql"/>
    </delete>

    <update id="update" parameterType="org.hswebframework.web.commons.entity.Entity">
        <include refid="config"/>
        <include refid="BasicMapper.buildUpdateSql"/>
    </update>

    <select id="selectByPositionId" parameterType="String" resultMap="PersonResultMap">
        select * from s_person where u_id in (select person_id from s_person_position where position_id=#{positionId})
    </select>

    <select id="selectByRoleId" parameterType="String" resultMap="PersonResultMap">
        select * from s_person where u_id in (select user_id from s_user_role where role_id=#{roleId})
    </select>

    <select id="query" parameterType="org.hswebframework.web.commons.entity.Entity" resultMap="PersonResultMap">
        <include refid="config"/>
        <include refid="BasicMapper.buildSelectSql"/>
    </select>

    <select id="count" parameterType="org.hswebframework.web.commons.entity.Entity" resultType="int">
        <include refid="config"/>
        <include refid="BasicMapper.buildTotalSql"/>
    </select>
</mapper>
