<?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.datau.rpm.mapper.ChartClassMapper">
	<select id="listChartClassByName" resultType="com.datau.rpm.dto.chart.ChartClassDTO">
		SELECT
		id,
		class_code,
		class_name,
		class_level,
		update_time,
		update_user_id,
		if(class_level =
		1,0,substr(class_code,1,length(class_code)-4)) pCode

		FROM
		rpm_info_chart_class aicc
		where
		1=1
		<if test="className != null  &amp;&amp; className != ''">
			and class_name like concat('%',#{className},'%')
		</if>
		order by create_time
	</select>
	<select id="getChartClassByCode" resultType="com.datau.rpm.dto.chart.ChartClassDTO">
		SELECT
		aicc.id,
		class_code,
		class_name,
		class_level,
		update_time,
		update_user_id,
		if(class_level =
		1,0,substr(class_code,1,length(class_code)-4)) pCode,
		diu.nickname updateUserName
		FROM
		rpm_info_chart_class aicc left join
		dc_info_user diu on aicc.update_user_id = diu.id

		where
		class_code=#{classCode}
	</select>

	<delete id="removeChartClassByCode">
		delete from
		rpm_info_chart_class
		where
		class_code
		like
		concat(#{classCode},'%')
	</delete>
	<insert id="saveChartClass" parameterType="com.datau.rpm.dto.chart.ChartClassDTO">
		insert into
		rpm_info_chart_class
		(
		id,class_code,class_name,
		class_level,update_time,create_time,update_user_id
		)
		values
		(
		UUID(),#{classCode},#{className},
		#{classLevel},now(),now(),#{updateUserId}
		)
	</insert>
	<update id="updateChartClass" parameterType="com.datau.rpm.dto.chart.ChartClassDTO">
		update
		rpm_info_chart_class
		set
		class_name=#{className},update_time =
		now(),update_user_id = #{updateUserId}
		where
		class_code=#{classCode}
	</update>

	<select id="getChartClassByName" resultType="com.datau.rpm.dto.chart.ChartClassDTO">
		SELECT
		id,
		class_code,
		class_name,
		class_level,
		update_time,
		update_user_id,
		if(class_level =
		1,0,substr(class_code,1,length(class_code)-4)) pCode
		FROM
		rpm_info_chart_class aicc
		where
		class_name=#{className}
	</select>

	<select id="countChartByClassCode" resultType="java.lang.Integer">
		SELECT
		count(0)
		FROM
		apps_info_chart
		where
		chart_class_code
		like
		concat(#{classCode},'%')
	</select>

	<update id="updateChartClassCode" parameterType="java.lang.String">
		update
		rpm_info_chart_class aicc
		inner join (
		select
		id,replace(class_code,#{classCode},#{newClassCode})
		class_code from
		rpm_info_chart_class where class_code like
		concat(#{classCode},'%')
		)
		temp on aicc.id = temp.id set aicc.class_code
		= temp.class_code
		,aicc.class_level = length(temp.class_code)/4
	</update>
</mapper>