<?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.boot.manager.manager.dao.OrgDao">

	<resultMap id="ZNode" type="com.bonus.boot.manager.basic.entity.ZNode"></resultMap>

	<select id="getAll" parameterType="com.bonus.boot.manager.manager.entity.OrgBean" resultMap="ZNode">
           SELECT
			   po.ID AS id,
			   po.PARENT_ID AS pId ,
			   po.COMPANY_ID as companyId,
			   po.`NAME` as name
		FROM
			pm_organization po
		WHERE
			po.is_active = '1'
		<if test="companyId!=null and companyId!='' ">
			and pc.ID =#{companyId}
		</if>


    </select>

	<select id="getCompanyTree" parameterType="com.bonus.boot.manager.manager.entity.OrgBean" resultMap="ZNode">
           SELECT
			ID AS id,
			PARENT_ID AS pId ,
			`NAME` as name,
            COMPANY_ID as companyId
		FROM
			pm_organization
		WHERE
			is_active = '1'
		<if test="companyId!=null and companyId!='' and companyId!='0' ">
			and COMPANY_ID = #{companyId}
		</if>
    </select>


	<insert id="addOrg" parameterType="com.bonus.boot.manager.manager.entity.OrgBean">
        insert into pm_organization( NAME,PARENT_ID,IS_ACTIVE,TYPE,update_time,COMPANY_ID)
        values ( #{name},#{parentId},'1',#{type},now(),#{companyId})
    </insert>

	<update id="updateOrg" parameterType="com.bonus.boot.manager.manager.entity.OrgBean">
		update pm_organization
		<set>
			<if test="name != null">
				NAME =#{name},
			</if>
		</set>
		where ID = #{id}
	</update>

	<update id="deleteOrg" parameterType="com.bonus.boot.manager.manager.entity.OrgBean">
        update pm_organization set IS_ACTIVE = 0 WHERE ID = #{id}
    </update>
	<select id="findCompanyInfo" resultType="com.bonus.boot.manager.manager.entity.OrgBean" parameterType="com.bonus.boot.manager.manager.entity.OrgBean" >
		 SELECT po.ID as companyId,po.`NAME` as companyName,po.PARENT_ID as parentId
		  FROM pm_organization po

		WHERE po.ID = #{id} AND po.IS_ACTIVE = 1
	</select>
</mapper>