<?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.bonus.material.clz.mapper.BmProDepartMapper">

    <insert id="insert">
        INSERT INTO clz_pro_depart
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="departName != null">depart_name,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="headUser != null">head_user,</if>
            <if test="headUserPhone != null">head_user_phone,</if>
            create_time,
            <if test="createUser != null">create_by,</if>
            del_flag
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="departName != null">#{departName},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="headUser != null">#{headUser},</if>
            <if test="headUserPhone != null">#{headUserPhone},</if>
            NOW(),
            <if test="createUser != null">#{createUser},</if>
            0
        </trim>
    </insert>

    <update id="update">
        update clz_pro_depart
        <trim prefix="SET" suffixOverrides=",">
            <if test="departName != null">depart_name = #{departName},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="headUser != null">head_user = #{headUser},</if>
            <if test="headUserPhone != null">head_user_phone = #{headUserPhone},</if>
            update_time = NOW(),
            <if test="updateUser != null">update_by = #{updateUser},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteById">
        update clz_pro_depart
        set del_flag = '2'
        where id = #{id}
    </delete>

    <select id="queryByPage" resultType="com.bonus.material.clz.domain.BmProDepart">
        select tpd.id              as id,
               tpd.depart_name     as departName,
               tpd.dept_id         as deptId,
               sd.dept_name        as deptName,
               tpd.head_user       as headUser,
               tpd.head_user_phone as headUserPhone,
               tpd.create_time     as createTime,
               tpd.create_by     as createUser,
               tpd.update_time     as updateTime,
               tpd.update_by     as updateUser,
               tpd.del_flag        as delFlag
        from clz_pro_depart tpd
        left join sys_dept sd on sd.dept_id = tpd.dept_id
        where tpd.del_flag = '0'
        <if test="keyWord != null and keyWord != ''">
            and (
            tpd.depart_name like concat('%', #{keyWord}, '%') or
            sd.dept_name like concat('%', #{keyWord}, '%') or
            tpd.head_user like concat('%', #{keyWord}, '%') or
            tpd.head_user_phone like concat('%', #{keyWord}, '%')
            )
        </if>
        ORDER BY tpd.create_time DESC
    </select>

    <select id="selectByName" resultType="com.bonus.material.clz.domain.BmProDepart">
        select id as id, depart_name as departName, head_user as headUser,
        head_user_phone as headUserPhone, create_time as createTime, create_by as createUser,
        update_time as updateTime, update_by as updateUser, del_flag as delFlag
        from clz_pro_depart
        where del_flag = '0'
        <if test="departName != null and departName != ''">
            and depart_name = #{departName}
        </if>
    </select>
</mapper>