<?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.yonge.cooleshow.biz.dal.dao.EmployeeDao">

    <resultMap type="com.yonge.cooleshow.biz.dal.entity.Employee" id="Employee">
        <result column="user_id_" property="userId"/>
        <result column="job_nature_" property="jobNature"/>
        <result column="is_probation_period_" property="isProbationPeriod"/>
        <result column="education_background_" property="educationBackground"/>
        <result column="graduate_school_" property="graduateSchool"/>
        <result column="technical_titles_" property="technicalTitles"/>
        <result column="entry_date_" property="entryDate"/>
        <result column="update_time_" property="updateTime"/>
        <result column="create_time_" property="createTime"/>
        <result column="introduction_" property="introduction"/>
        <result column="demission_date_" property="demissionDate"/>
        <result column="contact_address_" property="contactAddress"/>
        <result column="lock_flag_" property="lockFlag" />
    </resultMap>

    <resultMap type="com.yonge.cooleshow.auth.api.entity.SysUser" id="SysUser">
        <result column="id_" property="id"/>
        <result column="username_" property="username"/>
        <result column="password_" property="password"/>
        <result column="salt_" property="salt"/>
        <result column="phone_" property="phone"/>
        <result column="avatar_" property="avatar"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="lock_flag_" property="lockFlag"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="wx_openid_" property="wxOpenid"/>
        <result column="qq_openid_" property="qqOpenid"/>
        <result column="user_type_" property="userType"/>
        <result column="gender_" property="gender"/>
        <result column="nation_" property="nation"/>
        <result column="birthdate_" property="birthdate"/>
        <result column="email_" property="email"/>
        <result column="im_token_" property="imToken"/>
        <result column="real_name_" property="realName"/>
        <result column="id_card_no_" property="idCardNo"/>
        <result column="wechat_id_" property="wechatId"/>
    </resultMap>

    <sql id="baseColumns">
         t.user_id_ as "userId"
        , t.job_nature_ as "jobNature"
        , t.is_probation_period_ as "isProbationPeriod"
        , t.education_background_ as "educationBackground"
        , t.graduate_school_ as "graduateSchool"
        , t.technical_titles_ as "technicalTitles"
        , t.entry_date_ as "entryDate"
        , t.update_time_ as "updateTime"
        , t.create_time_ as "createTime"
        , t.introduction_ as "introduction"
        , t.demission_date_ as "demissionDate"
        , t.contact_address_ as "contactAddress"
    </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.EmployeeVo">
        SELECT
            <include refid="baseColumns" />,
            if(u.lock_flag_ = 0 and t.lock_flag_ = 0,0,1) as lockFlag,
            username_ as username,
            phone_ as phone,
            gender_ as gender
        FROM employee t
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0 and t.user_id_ = #{id}
    </select>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.EmployeeVo">
        SELECT
            <include refid="baseColumns" />,
            if(u.lock_flag_ = 0 and t.lock_flag_ = 0,0,1) as lockFlag,
            u.username_ as username,
            u.phone_ as phone,
            u.gender_ as gender
        FROM employee t
        left join sys_user u on t.user_id_ = u.id_
        <where>
            <if test="null != param.search and '' != param.search">
                AND (
                t.user_id_ LIKE CONCAT('%', #{param.search}, '%') or
                u.username_ LIKE CONCAT('%', #{param.search}, '%') or
                u.phone_ LIKE CONCAT('%', #{param.search}, '%')
                )
            </if>
            <if test="null != param.gender">
                and u.gender_ = #{param.gender}
            </if>
            <if test="param.startTime !=null">
                <![CDATA[AND t.create_time_ >= #{param.startTime} ]]>
            </if>
            <if test="param.endTime !=null">
                <![CDATA[AND t.create_time_ < #{param.endTime} ]]>
            </if>
            <if test="null != param.lockFlag">
                and t.lock_flag_ = #{param.lockFlag}
            </if>
            <if test="null != param.delFlag">
                and u.del_flag_ = #{param.delFlag}
            </if>
        </where>
    </select>

    <insert id="insertSysUser" parameterType="com.yonge.cooleshow.auth.api.entity.SysUser" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO sys_user
        (is_super_admin_,im_token_,id_,username_,salt_,phone_,avatar_,create_time_,update_time_,wx_openid_,qq_openid_,user_type_,
        gender_,nation_,birthdate_,email_,id_card_no_,wechat_id_,real_name_,certificate_type_,password_)
        VALUES(#{isSuperAdmin},#{imToken},#{id},#{username},#{salt},#{phone},#{avatar},now(),now(),#{wxOpenid},#{qqOpenid},#{userType},
        #{gender},#{nation},#{birthdate},#{email},#{idCardNo},#{wechatId},#{realName},#{certificateType},#{password})
    </insert>

    <update id="updateSysUser" parameterType="com.yonge.cooleshow.auth.api.entity.SysUser">
        UPDATE sys_user
        <set>
            <if test="wxOpenid != null and wxOpenid != ''">
                wx_openid_ = #{wxOpenid},
            </if>
            <if test="avatar != null and avatar != ''">
                avatar_ = #{avatar},
            </if>
            <if test="email != null and email != ''">
                email_ = #{email},
            </if>
            <if test="gender != null">
                gender_ = #{gender},
            </if>
            <if test="salt != null and salt != ''">
                salt_ = #{salt},
            </if>
            <if test="username != null and username != ''">
                username_ = #{username},
            </if>
            <if test="userType != null and userType != ''">
                user_type_ = #{userType},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="lockFlag != null">
                lock_flag_ = #{lockFlag},
            </if>
            <if test="birthdate != null">
                birthdate_ = #{birthdate},
            </if>
            <if test="phone != null and phone != ''">
                phone_ = #{phone},
            </if>
            <if test="qqOpenid != null and qqOpenid != ''">
                qq_openid_ = #{qqOpenid},
            </if>
            <if test="nation != null and nation != ''">
                nation_ = #{nation},
            </if>
            <if test="imToken != null and imToken != ''">
                im_token_ = #{imToken},
            </if>
            <if test="idCardNo != null and idCardNo != ''">
                id_card_no_ = #{idCardNo},
            </if>
            <if test="password != null and password != ''">
                password_ = #{password},
            </if>
            <if test="wechatId != null and wechatId != ''">
                wechat_id_ = #{wechatId},
            </if>
            <if test="realName != null and realName != ''">
                real_name_ = #{realName},
            </if>
            <if test="isSuperAdmin != null">
                is_super_admin_ = #{isSuperAdmin},
            </if>
            <if test="certificateType != null">
                certificate_type_ = #{certificateType},
            </if>
        </set>
        WHERE del_flag_ = 0 and id_ = #{id}
    </update>

    <insert id="insert" parameterType="com.yonge.cooleshow.biz.dal.entity.Employee" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO employee
        (user_id_,job_nature_,is_probation_period_,education_background_,graduate_school_,technical_titles_,
        entry_date_,update_time_,create_time_,introduction_,demission_date_,contact_address_)
        VALUES(#{userId},#{jobNature},#{isProbationPeriod},#{educationBackground},#{graduateSchool},
        #{technicalTitles},#{entryDate},now(),now(),#{introduction},#{demissionDate},#{contactAddress})
    </insert>

    <update id="update" parameterType="com.yonge.cooleshow.biz.dal.entity.Employee">
        UPDATE employee
        <set>
            <if test="jobNature != null">
                job_nature_ = #{jobNature},
            </if>
            <if test="isProbationPeriod != null">
                is_probation_period_ = #{isProbationPeriod},
            </if>
            <if test="educationBackground != null">
                education_background_ = #{educationBackground},
            </if>
            <if test="graduateSchool != null">
                graduate_school_ = #{graduateSchool},
            </if>
            <if test="technicalTitles != null">
                technical_titles_ = #{technicalTitles},
            </if>
            <if test="entryDate != null">
                entry_date_ = #{entryDate},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="introduction != null">
                introduction_ = #{introduction},
            </if>
            <if test="demissionDate != null">
                demission_date_ = #{demissionDate},
            </if>
            <if test="contactAddress != null">
                contact_address_ = #{contactAddress},
            </if>
            <if test="lockFlag != null">
                lock_flag_ = #{lockFlag},
            </if>
        </set>
        WHERE user_id_ = #{userId}
    </update>

    <select id="queryUserRole" resultType="java.lang.Long">
        SELECT sur.role_id_ FROM sys_user_role sur
        LEFT JOIN sys_role r ON sur.role_id_ = r.id_
        WHERE r.del_flag_ = 0 and r.is_enable_ = 1 and sur.user_id_ = #{userId}
    </select>
    <insert id="batchAddEmployeeRole">
        INSERT INTO sys_user_role(user_id_,role_id_) values
        <foreach collection="roleIds" item="item" index="index" separator=",">
            (#{userId},#{item})
        </foreach>
    </insert>
    <delete id="delEmployeeRole">
        DELETE FROM sys_user_role WHERE user_id_ = #{userId}
    </delete>

    <insert id="batchInsertRoleMenu">
        INSERT INTO sys_role_menu (role_id_,menu_id_) VALUES
        <foreach collection="menuIds" item="item" index="index" separator=",">
            (#{roleId}, #{item})
        </foreach>
    </insert>

    <select id="querySysUserByPhoneAndLock" resultMap="SysUser">
		select * from sys_user where del_flag_ = 0 and (phone_ = #{phone} OR username_ = #{phone}) LIMIT 1 FOR UPDATE
	</select>

    <select id="querySysUserByPhone" resultMap="SysUser">
		select * from sys_user where del_flag_ = 0 and (phone_ = #{phone} OR username_ = #{phone})
	</select>

    <select id="querySysUserById" resultMap="SysUser">
		select * from sys_user where del_flag_ = 0 and id_ = #{id}
	</select>

    <update id="updateUserDemissionDate">
        UPDATE employee SET demission_date_ = IF(demission_date_ IS NULL,now(),NULL) WHERE user_id_=#{userID}
    </update>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="Employee">
		SELECT * FROM employee WHERE user_id_ = #{userId}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="Employee">
		SELECT * FROM employee ORDER BY user_id_
	</select>


    <delete id="delRoleMenu">
        DELETE FROM `sys_role_menu`
        WHERE  menu_id_ in
        <foreach collection="menuIds" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
    </delete>

    <insert id="insertSysRole" keyColumn="id_" keyProperty="id" parameterType="com.yonge.cooleshow.auth.api.entity.SysRole"
            useGeneratedKeys="true">
        INSERT INTO `sys_role` (`role_name_`, `role_code_`, `role_desc_`, `create_time_`,
                                `update_time_`, `del_flag_`, `organ_id_`, `tenant_id_`)
        VALUES (#{roleName}, #{roleCode}, #{roleDesc}, #{createTime},
                #{updateTime}, #{delFlag}, #{organId}, #{tenantId});
    </insert>

    <update id="updatePasswordReq">
        UPDATE sys_user SET password_ = #{password} WHERE del_flag_ = 0 and id_ = #{userID}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM employee WHERE user_id_ = #{userId}
	</delete>


    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM employee where tenant_id_ = #{tenantId}
	</select>



    <select id="getUsers" resultMap="SysUser">
        select * from sys_user where del_flag_ = 0 and id_ IN
        <foreach collection="userIds" open="(" close=")" separator="," item="userId">
            #{userId}
        </foreach>
    </select>
</mapper>
