<?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.modules.warehouse.materialtype.mapper.MaterialTypeMapper">

    <resultMap id="materialTypeResult" type="MaterialType">
   		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="sort" column="sort" />
		<result property="parentIds" column="parentIds" />
		<result property="code" column="code" />
		<association property="hasChildren" column="id" select="hasChildren" />
    </resultMap>
    
	<sql id="materialTypeColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.parent_id AS "parent.id",
		a.parent_ids AS "parentIds",
		a.name AS "name",
		a.sort AS "sort",
		a.code AS "code",
		a.last AS "last",
		a.amount AS "amount",
		a.unit AS "unit"
	</sql>
	

	

	<sql id="materialTypeJoins">
		
	</sql>
	

    
	<select id="get" resultType="MaterialType">
		SELECT 
			<include refid="materialTypeColumns"/>
		FROM t_wh_material_type a
		<include refid="materialTypeJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="MaterialType">
		SELECT 
			<include refid="materialTypeColumns"/>
		FROM t_wh_material_type a
		<include refid="materialTypeJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="parent != null and parent.id != null and parent.id != ''">
				AND a.parent_id = #{parent.id}
			</if>
			<if test="parentIds != null and parentIds != ''">
				AND a.parent_ids LIKE 
					<if test="dbName == 'oracle'">'%'||#{parentIds}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{parentIds}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{parentIds},'%')</if>
			</if>
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
		</where>
		ORDER BY a.sort ASC
	</select>
	
	<select id="findAllList" resultType="MaterialType">
		SELECT 
			<include refid="materialTypeColumns"/>
		FROM t_wh_material_type a
		<include refid="materialTypeJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>
		ORDER BY a.sort ASC
	</select>
	
	<select id="getChildren" parameterType="String" resultMap="materialTypeResult">  
        select * from t_wh_material_type where parent_id = #{id} ORDER BY sort 
    </select> 
     
    <select id="hasChildren" parameterType="String" resultType="Boolean">
        select case when exists(select 1 from t_wh_material_type where parent_id = #{id}) then 1 else 0 end ${dual}
    </select>
    
	<select id="findByParentIdsLike" resultType="MaterialType">
		SELECT
			a.id,
			a.parent_id AS "parent.id",
			a.parent_ids
		FROM t_wh_material_type a
		<include refid="materialTypeJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.parent_ids LIKE #{parentIds}
		</where>
		ORDER BY a.sort ASC
	</select>
	
	<insert id="insert">
		INSERT INTO t_wh_material_type(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			parent_id,
			parent_ids,
			name,
			sort,
			code,
			last,
			amount,
			unit
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{parent.id},
			#{parentIds},
			#{name},
			#{sort},
			#{code},
			#{last},
			#{amount},
			#{unit}
		)
	</insert>
	
	<update id="update">
		UPDATE t_wh_material_type SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			parent_id = #{parent.id},
			parent_ids = #{parentIds},
			name = #{name},
			sort = #{sort},
			code = #{code},
			last = #{last},
			amount = #{amount},
			unit = #{unit}
		WHERE id = #{id}
	</update>
	
	<update id="updateParentIds">
		UPDATE t_wh_material_type SET 
			parent_id = #{parent.id}, 
			parent_ids = #{parentIds}
		WHERE id = #{id}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM t_wh_material_type
		WHERE id = #{id} OR parent_ids LIKE 
		<if test="dbName == 'oracle'">'%,'||#{id}||',%'</if>
		<if test="dbName == 'mssql'">'%,'+#{id}+',%'</if>
        <if test="dbName == 'mysql'">CONCAT('%,', #{id}, ',%')</if>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE t_wh_material_type SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id} OR parent_ids LIKE 
		<if test="dbName == 'oracle'">'%,'||#{id}||',%'</if>
		<if test="dbName == 'mssql'">'%,'+#{id}+',%'</if>
        <if test="dbName == 'mysql'">CONCAT('%,', #{id}, ',%')</if>
	</update>
	
</mapper>