<?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.sgzb.base.mapper.MaHouseMapper">
    
    <resultMap type="com.bonus.sgzb.base.domain.MaHouse" id="SysHouseResult">
    	<id     property="houseId"      column="house_id"      />
        <result property="houseName"    column="house_name"    />
        <result property="parentId"     column="parent_id"     />
        <result property="status"     column="status"     />
        <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"    />
        <result property="companyId"    column="company_id"    />
        <result property="sort"    column="sort"    />
        <result property="concat"    column="concat"    />
        <result property="phone"    column="phone"    />
    </resultMap>

    <sql id="selectHouseVo">
        select house_id, house_name, parent_id, status, dept_id, del_flag, create_by, create_time, remark, company_id,phone ,concat
        from ma_house_info
    </sql>

    <insert id="insertHouse" parameterType="com.bonus.sgzb.base.domain.MaHouse" useGeneratedKeys="true" keyProperty="houseId">
        insert into ma_house_info(
        <if test="houseId != null and houseId != 0">house_id,</if>
        <if test="houseName != null and houseName != ''">house_name,</if>
        <if test="parentId != null and parentId != 0">parent_id,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="deptId != null and deptId != 0">dept_id,</if>
        <if test="delFlag != null and delFlag != ''">del_flag,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="companyId != null and companyId != ''">company_id,</if>
        <if test="concat != null and concat != ''">concat,</if>
        <if test="phone != null and phone != ''">phone,</if>
        create_time
        )values(
        <if test="houseId != null and houseId != 0">#{houseId},</if>
        <if test="houseName != null and houseName != ''">#{houseName},</if>
        <if test="parentId != null and parentId != 0">#{parentId},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="deptId != null and deptId != 0">#{deptId},</if>
        <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="companyId != null and companyId != ''">#{companyId},</if>
        <if test="concat != null and concat != ''">#{concat},</if>
        <if test="phone != null and phone != ''">#{phone},</if>
        sysdate()
        )
    </insert>

    <update id="updateHouse">
        update ma_house_info
        <set>
            <if test="houseName != null and houseName != ''">house_name = #{houseName},</if>
            <if test="parentId != null and parentId != ''">parent_id = #{parentId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="deptId != null and deptId != ''">dept_id = #{deptId},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="companyId != null and companyId != ''">company_id = #{companyId},</if>
            <if test="concat != null">concat = #{concat},</if>
            <if test="phone != null">phone = #{phone},</if>
            update_time = sysdate()
        </set>
        where house_id = #{houseId}
    </update>

    <delete id="deleteHouseById" parameterType="Long">
        update ma_house_info set del_flag = '2' where house_id = #{houseId}
    </delete>


    <select id="selectHouseList" parameterType="com.bonus.sgzb.base.domain.MaHouse" resultMap="SysHouseResult">
        select house_id, house_name, parent_id, status, dept_id, del_flag, create_by, create_time, remark, company_id, sort,concat,phone
        from ma_house_info
        where del_flag = '0'
            <if test="houseName != null and houseName != ''">
                AND house_name like concat('%', #{houseName}, '%')
            </if>
    </select>

    <select id="selectHouseById" parameterType="com.bonus.sgzb.base.domain.MaHouse" resultMap="SysHouseResult">
        <include refid="selectHouseVo"/>
        where house_id = #{houseId}
        order by sort
    </select>

    <select id="hasChildByHouseId" parameterType="Long">
        select count(1) from ma_house_info
        where del_flag = '0' and parent_id = #{houseId} limit 1
    </select>

    <select id="selectHouse" parameterType="com.bonus.sgzb.base.domain.MaHouse" resultMap="SysHouseResult">
        <include refid="selectHouseVo"/>
    </select>

    <select id="selectHouseParent" parameterType="com.bonus.sgzb.base.domain.MaHouse" resultMap="SysHouseResult">
        <include refid="selectHouseVo"/>
        where parent_id = #{parentId}
        order by sort
    </select>
    <select id="checkHouseNameUnique" parameterType= "String"  resultMap="SysHouseResult">
        <include refid="selectHouseVo"/>
        where house_name=#{houseName} limit 1
    </select>

    <select id="selectMaHouseByHouseId" parameterType="Long" resultMap="SysHouseResult">
        <include refid="selectHouseVo"/>
        where house_id = #{houseId}
    </select>
</mapper>