<?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.jeeplus.test.materialManagement.materialType.mapper.MaterialTypeMapper">
	<resultMap id="BaseResultMap" type="com.jeeplus.test.materialManagement.materialType.domain.MaterialTypeInfo">
		<id property="id" column="id" jdbcType="VARCHAR"/>
		<result property="createBy" column="create_by" jdbcType="VARCHAR"/>
		<result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
		<result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
		<result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
		<result property="delFlag" column="del_flag" jdbcType="INTEGER"/>

		<result property="level" column="level" jdbcType="VARCHAR"/>
		<result property="name" column="name" jdbcType="VARCHAR"/>
		<result property="sort" column="sort" jdbcType="VARCHAR"/>
		<result property="parentId" column="parent_id" jdbcType="VARCHAR"/>
		<result property="parentIds" column="parent_ids" jdbcType="VARCHAR"/>

	</resultMap>


	<sql id="Base_Column_List">
        a.id,
        a.create_by,
        a.create_date,
        a.update_by,
        a.update_date,
        a.del_flag,
        a.level,
		a.name,
		a.sort,
		a.parent_id,
		a.parent_ids
    </sql>


	<select id="findList" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List"></include>
		from material_management_type a
		${ew.customSqlSegment}
	</select>


	<select id="getById" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List"></include>
		from material_management_type a
		<where>
			a.del_flag = 0 and a.id = #{id}
		</where>
	</select>

	<select id="checkNameIsExist" resultType="java.lang.Integer">
        SELECT
			COUNT( 0 )
		FROM
			material_management_type
		WHERE
			del_flag = 0
			AND `name` = #{name}
			AND parent_id = #{parentId}
    </select>
    <select id="getNo" resultType="java.lang.Integer">
        SELECT
			(MAX(`sort`) + 1) AS `sort`
		FROM
			material_management_type
		WHERE
			del_flag = 0
			AND parent_id = #{parentId}
    </select>
    <select id="getLevel" resultType="java.lang.Integer">
        SELECT
			(MAX(level) + 1) AS `level`
		FROM
			material_management_type
		WHERE
			del_flag = 0
			AND id = #{parentId}
    </select>
	<select id="getChildId" resultType="java.lang.String">
		select id from material_management_type where parent_id = #{id}
	</select>
	<select id="getAllList"
			resultType="com.jeeplus.test.materialManagement.materialType.domain.MaterialTypeInfo">
		select * from material_management_type where  del_flag = '0' order by sort asc
	</select>

	<delete id="deleteReimbursementTypeOfficeInfo">
		delete from material_management_type where reimbursement_type_id = #{id}
	</delete>
	<delete id="deleteLevel3">
		UPDATE material_management_type set del_flag = 1 WHERE id in (
		SELECT a.id FROM
		( SELECT id FROM material_management_type WHERE parent_id in
		(SELECT id FROM material_management_type WHERE parent_id = #{id}
		)) a)
	</delete>
	<delete id="deleteLevel2">
		UPDATE material_management_type set del_flag = 1 WHERE id in (
		SELECT a.id FROM
		(SELECT id FROM material_management_type WHERE parent_id = #{id}
		) a)
	</delete>

</mapper>
