<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.CompanyStructureInfoDAO">
    <resultMap id="CompanyStructureInfoMap" type="com.jpsoft.shinestar.modules.base.entity.CompanyStructureInfo">
        <id property="id" column="id_"/>
        <result property="companyId" column="company_id"/>
        <result property="structureName" column="structure_name"/>
        <result property="positionName" column="position_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="remark" column="remark_"/>
        <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="delFlag" column="del_flag"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.CompanyStructureInfo">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_company_structure_info
	    (id_,company_id,structure_name,position_name,parent_id,remark_,create_by,create_time,update_by,update_time,del_flag)
		values
		(
			#{id,jdbcType=VARCHAR}
			,#{companyId,jdbcType=VARCHAR }
			,#{structureName,jdbcType=VARCHAR}
			,#{positionName,jdbcType=VARCHAR}
			,#{parentId,jdbcType=VARCHAR}
			,#{remark,jdbcType=VARCHAR}
			,#{createBy,jdbcType=VARCHAR}
			,#{createTime,jdbcType= TIMESTAMP }
			,#{updateBy,jdbcType=VARCHAR}
			,#{updateTime,jdbcType= TIMESTAMP }
			,#{delFlag,jdbcType= NUMERIC }
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_company_structure_info where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.CompanyStructureInfo">
        update base_company_structure_info
        <set>
            <if test="companyId!=null">
                company_id=#{companyId,jdbcType= VARCHAR },
            </if>
            <if test="structureName!=null">
                structure_name=#{structureName,jdbcType=VARCHAR},
            </if>
            <if test="positionName!=null">
                position_name=#{positionName,jdbcType=VARCHAR},
            </if>
            <if test="parentId!=null">
                parent_id=#{parentId,jdbcType=VARCHAR},
            </if>
            <if test="remark!=null">
                remark_=#{remark,jdbcType=VARCHAR},
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
        </set>
        where id_=#{id}
    </update>

    <select id="get" parameterType="string" resultMap="CompanyStructureInfoMap">
        select *
        from base_company_structure_info
        where id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_company_structure_info where id_=#{0} and del_flag = 0
    </select>
    <select id="list" resultMap="CompanyStructureInfoMap">
        select * from base_company_structure_info where del_flag=0 order by code_ asc,sort_no asc
    </select>
    <select id="search" parameterType="hashmap" resultMap="CompanyStructureInfoMap">
        <![CDATA[
           SELECT
                a.*
            FROM
                base_company_structure_info a
                LEFT JOIN base_company_info b ON a.company_id = b.id_
		]]>
        <where>
            a.del_flag = 0
            <if test="searchParams.companyId != null">
                and a.company_id = #{searchParams.companyId}
            </if>
            <if test="searchParams.structureName != null">
                and a.structure_name like #{searchParams.structureName}
            </if>
            <if test="searchParams.positionName != null">
                and a.position_name like #{searchParams.positionName}
            </if>
            <if test="searchParams.parentId != null">
                and a.parent_id = #{searchParams.parentId}
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>

    <select id="findTopStructureByCompanyId" resultMap="CompanyStructureInfoMap">
        <![CDATA[

        select * from base_company_structure_info
        where del_flag = 0
        and company_id = #{0}
        and parent_id is null
        ]]>
    </select>

    <select id="findByCompanyId" resultMap="CompanyStructureInfoMap">
        <![CDATA[
        select * from base_company_structure_info
        where del_flag = 0
        and company_id = #{companyId}
        ]]>
    </select>
    <select id="findByCompanyIdAndName" resultMap="CompanyStructureInfoMap">
        <![CDATA[
        select * from base_company_structure_info
        where del_flag = 0
        and company_id = #{companyId}
        and structure_name =#{structureName}
        limit 1
        ]]>
    </select>

    <select id="findByParentId" resultMap="CompanyStructureInfoMap">

        <![CDATA[

        select * from base_company_structure_info
        where del_flag = 0
        and parent_id = #{0}
        ]]>
    </select>

    <select id="findByCompanyIdAndPositionName" resultMap="CompanyStructureInfoMap">
        <![CDATA[

        select * from base_company_structure_info
        where del_flag = 0
        and company_id = #{companyId}
        and position_name = #{positionName}
        ]]>
    </select>

</mapper>