<?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.basic.mapper.BmUnitMapper">
    <resultMap type="com.bonus.material.basic.domain.BmUnit" id="BmUnitResult">
        <result property="unitId"    column="unit_id"    />
        <result property="unitName"    column="unit_name"    />
        <result property="status"    column="status"    />
        <result property="typeId"    column="type_id"    />
        <result property="linkMan"    column="link_man"    />
        <result property="telphone"    column="telphone"    />
        <result property="deptId"    column="dept_id"    />
        <result property="delFlag"    column="del_flag"    />
        <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="remark"    column="remark"    />
    </resultMap>

    <sql id="selectBmUnitVo">
        select unit_id, unit_name, status, type_id, link_man, telphone, dept_id, del_flag, create_by, create_time, update_by, update_time, remark from bm_unit
    </sql>

    <select id="selectBmUnitList1" parameterType="com.bonus.material.basic.domain.BmUnit" resultMap="BmUnitResult">
        <include refid="selectBmUnitVo"/>
        <where>
            del_flag = 0
            <if test="unitName != null  and unitName != ''"> and unit_name like concat('%', #{unitName}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="typeId != null "> and type_id = #{typeId}</if>
            <if test="linkMan != null  and linkMan != ''"> and link_man = #{linkMan}</if>
            <if test="telphone != null  and telphone != ''"> and telphone = #{telphone}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
        </where>
    </select>
    


    <select id="selectBmUnitByProName" resultType="com.bonus.material.basic.domain.BmUnit">
        select unit_id as unitId, unit_name as unitName from bm_unit where del_flag = 0 and unit_name = #{unitName}
    </select>

    <select id="countBmUnitPersonByUnitId" resultType="java.lang.Integer">
        select count(1) from bm_unit_person where unit_id = #{unitId}
    </select>

    <select id="selectBmUnitList" resultType="com.bonus.material.basic.domain.BmUnit">
        select unit_id as unitId, unit_name as unitName, status as status, type_id as typeId, link_man as
        linkMan, telphone as telphone,
        dept_id as deptId, del_flag as delFlag, create_by as createBy, create_time as createTime,
        update_by as updateBy, update_time as updateTime, remark as remark from bm_unit
        where
        del_flag = 0
            <if test="unitName != null  and unitName != ''">and unit_name like concat('%', #{unitName}, '%')</if>
    </select>

    <select id="selectBmUnitByUnitId" resultType="com.bonus.material.basic.domain.BmUnit">
        select unit_id     as unitId,
               unit_name   as unitName,
               status      as status,
               type_id     as typeId,
               link_man    as linkMan,
               telphone    as telphone,
               dept_id     as deptId,
               del_flag    as delFlag,
               create_by   as createBy,
               create_time as createTime,
               update_by   as updateBy,
               update_time as updateTime,
               remark      as remark
        from bm_unit
        where del_flag = 0 and unit_id = #{unitId}
    </select>

    <insert id="insertBmUnit" parameterType="com.bonus.material.basic.domain.BmUnit" useGeneratedKeys="true" keyProperty="unitId">
        insert into bm_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unitName != null and unitName != ''">unit_name,</if>
            <if test="status != null">status,</if>
            <if test="typeId != null">type_id,</if>
            <if test="linkMan != null">link_man,</if>
            <if test="telphone != null">telphone,</if>
            <if test="deptId != null">dept_id,</if>
            del_flag,
            <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="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unitName != null and unitName != ''">#{unitName},</if>
            <if test="status != null">#{status},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="linkMan != null">#{linkMan},</if>
            <if test="telphone != null">#{telphone},</if>
            <if test="deptId != null">#{deptId},</if>
            0,
            <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="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateBmUnit" parameterType="com.bonus.material.basic.domain.BmUnit">
        update bm_unit
        <trim prefix="SET" suffixOverrides=",">
            <if test="unitName != null and unitName != ''">unit_name = #{unitName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="linkMan != null">link_man = #{linkMan},</if>
            <if test="telphone != null">telphone = #{telphone},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where unit_id = #{unitId}
    </update>

    <delete id="deleteBmUnitByUnitId" parameterType="Long">
        update bm_unit set del_flag = 2 where unit_id = #{unitId}
    </delete>

    <delete id="deleteBmUnitByUnitIds" parameterType="String">
        update bm_unit set del_flag = 2 where unit_id in
        <foreach item="unitId" collection="array" open="(" separator="," close=")">
            #{unitId}
        </foreach>
    </delete>
</mapper>