<?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.yeejoin.equipmanage.mapper.UnitMapper">
	<select id="getUnitClassification" resultType="Map">
		SELECT
			wl.id,
			wl. name,
			wl.code
		FROM
			wl_unit wl
		WHERE
			wl.parent_id IS NULL
	</select>
	
	<select id="getUnitFirstLevelCode" resultType="String">
		SELECT
			CONCAT(
				(
					IFNULL(MAX(unit. CODE), 0) + 100
				),
				''
			) AS code
		FROM
			wl_unit unit
		WHERE
			unit.parent_id IS NULL
	</select>
	
	<select id="getUnitSecondLevelCode" resultType="String">
		SELECT
			LPAD(IFNULL(MAX(wu. CODE), wl. CODE) + 1,4,0) code
		FROM
			wl_unit wl
		LEFT JOIN wl_unit wu ON wl.id = wu.parent_id
		WHERE
			wl.id = #{id}
		AND wl.parent_id IS NULL;
	</select>
	
	<select id="getUnitList" resultType="com.yeejoin.equipmanage.common.entity.Unit">
		SELECT
			u.*
		FROM
			`wl_unit` u
		<if test="name != null and name !=''">
			LEFT JOIN wl_unit u1 ON u1.id = u.id
		OR u1.parent_id = u.id
		WHERE
			u1.`name` LIKE concat(concat('%',#{name}),'%')
		</if>
		ORDER BY
			u.code
	</select>

	<select id="getOne" resultType="com.yeejoin.equipmanage.common.entity.Unit">
		SELECT
		u.*
		FROM
		`wl_unit` u
		WHERE
		u.id=#{id}
	</select>
</mapper>
