<?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.api.manage.dao.businessManagement.SysUserDao">
    <select id="list" parameterType="com.api.model.businessManagement.SearchUser" resultType="com.api.vo.businessManagement.VoUser">
        select su.*,so.name as organization_name,si.name as position_name
        from sys_organization as so,sys_user as su
        left join sys_identity as si on su.position_id = si.id
        <where>
            su.organization_id = so.id
            and su.is_delete = 1
            <if test="organizationId != null">
                and su.organization_id_path like concat('%',#{organizationId},':','%')
            </if>
            <if test="nickName != null and nickName != ''">
                and su.nick_name like concat('%',#{nickName},'%')
            </if>
            <if test="actualName != null and actualName != ''">
                and su.actual_name like concat('%',#{actualName},'%')
            </if>
            <if test="status == 1">
                and su.status = 1
            </if>
            <if test="status == 2">
                and su.status = 2
            </if>
            <if test="status == 3">
                and su.status in (3,4)
            </if>
        </where>
    </select>

    <insert id="insert" parameterType="com.api.model.businessManagement.SysUser">
        insert into sys_user(
            user_name,
            pwd,
            <if test="actualName != null and actualName != ''">
                actual_name,
            </if>
            tel,
            sex,
            user_code,
            <if test="birthday != null">
                birthday,
            </if>
            <if test="email != null and email != ''">
                email,
            </if>
            id_card,
            organization_id,
            organization_id_path,
            <if test="positionId != null">
                position_id,
            </if>
            status,
            is_delete,
            create_id,
            create_date,
            <if test="remake != null and remake != ''">
                remake,
            </if>
            <if test="reportTo != null">
                report_to,
            </if>
            <if test="entryDate != null">
                entry_date,
            </if>
            nick_name
        )values (
            #{userName},
            #{pwd},
            <if test="actualName != null and actualName != ''">
                #{actualName},
            </if>
            #{tel},
            #{sex},
            #{userCode},
            <if test="birthday != null">
                #{birthday},
            </if>
            <if test="email != null and email != ''">
                #{email},
            </if>
            #{idCard},
            #{organizationId},
            #{organizationIdPath},
            <if test="positionId != null">
                #{positionId},
            </if>
            #{status},
            #{isDelete},
            #{createId},
            #{createDate},
            <if test="remake != null and remake != ''">
                #{remake},
            </if>
            <if test="reportTo != null">
                #{reportTo},
            </if>
            <if test="entryDate != null">
                #{entryDate},
            </if>
            #{nickName}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findByTel" parameterType="java.lang.String" resultType="com.api.model.businessManagement.SysUser">
        select * from sys_user where tel = #{tel} and is_delete = 1
    </select>

    <select id="findByIdCard" parameterType="java.lang.String" resultType="com.api.model.businessManagement.SysUser">
        select * from sys_user where id_card = #{idCard}
    </select>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.businessManagement.VoFindByIdUser">
        select * from sys_user where id = #{id}
    </select>

    <update id="update" parameterType="com.api.model.businessManagement.SysUser">
        update sys_user
        <set>
            <if test="nickName != null and nickName != ''">
                nick_name = #{nickName},
            </if>
            <if test="actualName != null and actualName != ''">
                actual_name = #{actualName},
            </if>
            <if test="tel != null and  tel != ''">
                tel = #{tel},
            </if>
            <if test="sex != null">
                sex = #{sex},
            </if>
            <if test="pwd != null and pwd != ''">
                pwd = #{pwd},
            </if>
            <if test="organizationId != null">
                organization_id = #{organizationId},
            </if>
            <if test="organizationIdPath != null and organizationIdPath != ''">
                organization_id_path = #{organizationIdPath},
            </if>
            <if test="positionId != null">
                position_id = #{positionId},
            </if>
            <if test="remake != null and remake != ''">
                remake = #{remake},
            </if>
            <if test="reportTo != null">
                report_to = #{reportTo},
            </if>
            <if test="entryDate != null">
                entry_date = #{entryDate},
            </if>
            <if test="userCode != null and userCode != ''">
                user_code = #{userCode},
            </if>
            <if test="birthday != null">
                birthday = #{birthday},
            </if>
            <if test="email != null and email != ''">
                email = #{email},
            </if>
            <if test="idCard != null and idCard != ''">
                id_card = #{idCard},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <update id="falseDelete" parameterType="java.lang.Integer">
        update sys_user set is_delete = 0 where id = #{id}
    </update>

    <update id="disableLogins" parameterType="java.lang.Integer">
        update sys_user set status = status + 1 where id = #{id}
    </update>

    <update id="allowLogins" parameterType="java.lang.Integer">
        update sys_user set status = status - 1 where id = #{id}
    </update>

    <update id="stop" parameterType="java.lang.Integer">
        update sys_user set status = status + 2 where id = #{id}
    </update>

    <update id="recovery" parameterType="java.lang.Integer">
        update sys_user set status = status - 2 where id = #{id}
    </update>

    <update id="resetPWD" parameterType="com.api.model.businessManagement.SysUser">
        update sys_user
        set pwd = #{pwd},modify_id = #{modifyId},modify_date = #{modifyDate}
        where id = #{id}
    </update>

    <select id="findRoleNameByUserId" parameterType="java.lang.Integer" resultType="java.lang.String">
        select role_id from sys_user where id = #{id}
    </select>
    <select id="findStatusById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select status from sys_user where id = #{id}
    </select>

    <update id="updateRole" parameterType="com.api.model.businessManagement.UserIdAndRoleId">
        update sys_user set role_id = #{roleId} where id =#{id}
    </update>

</mapper>