<?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.zcloud.mapper.datasource.dynamicAssessment.ReportMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_REPORT
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.SORT,
		f.ROOT_NODE_ID,
		f.PARENT_ID,
		f.ANCESTORS_FLAG,
		f.DESCRITPTION,
		f.ROW_SPAN,
		f.COL_SPAN,
		f.COL_NUMBER,
		f.COL_MAX,
		f.HAND_MOVEMENT_FLAG,
		f.AUTOMATIC_FLAG,
		f.DEFAULT_FLAG,
		f.REPORT_ID,
		f.ISDELETE,
		f.FROM_ID,
		f.STYLE,
		f.SQL_INFO
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		SORT,
		ROOT_NODE_ID,
		PARENT_ID,
		ANCESTORS_FLAG,
		DESCRITPTION,
		ROW_SPAN,
		COL_SPAN,
		COL_NUMBER,
		COL_MAX,
		HAND_MOVEMENT_FLAG,
		AUTOMATIC_FLAG,
		DEFAULT_FLAG,
		REPORT_ID,
		ISDELETE,
		FROM_ID,
		STYLE,
		SQL_INFO
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{SORT},
		#{ROOT_NODE_ID},
		#{PARENT_ID},
		#{ANCESTORS_FLAG},
		#{DESCRITPTION},
		#{ROW_SPAN},
		#{COL_SPAN},
		#{COL_NUMBER},
		#{COL_MAX},
		#{HAND_MOVEMENT_FLAG},
		#{AUTOMATIC_FLAG},
		#{DEFAULT_FLAG},
		#{REPORT_ID},
		#{ISDELETE},
		#{FROM_ID},
		#{STYLE},
		#{SQL_INFO}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			REPORT_ID = #{REPORT_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			SORT = #{SORT},
			ROOT_NODE_ID = #{ROOT_NODE_ID},
			PARENT_ID = #{PARENT_ID},
			ANCESTORS_FLAG = #{ANCESTORS_FLAG},
			DESCRITPTION = #{DESCRITPTION},
			ROW_SPAN = #{ROW_SPAN},
			COL_SPAN = #{COL_SPAN},
			COL_NUMBER = #{COL_NUMBER},
			COL_MAX = #{COL_MAX},
			HAND_MOVEMENT_FLAG = #{HAND_MOVEMENT_FLAG},
			AUTOMATIC_FLAG = #{AUTOMATIC_FLAG},
			DEFAULT_FLAG = #{DEFAULT_FLAG},
			FROM_ID = #{FROM_ID},
			ISDELETE = #{ISDELETE},
			STYLE = #{STYLE},
			SQL_INFO = #{SQL_INFO},
			REPORT_ID = REPORT_ID
		where
			REPORT_ID = #{REPORT_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.REPORT_ID = #{REPORT_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
				<!--	根据需求自己加检索条件
					字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				-->
				)
		</if>
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
	</select>

	<select id="findByCondition" resultType="com.zcloud.entity.PageData">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="FROM_ID != null and FROM_ID != ''">
			and f.FROM_ID = #{FROM_ID}
		</if>
	</select>

	<select id="findTemplateOne" resultType="com.zcloud.entity.PageData">
		SELECT FLOOR((DATE_FORMAT(CREATTIME, '%m') - 1) / 3) + 1 as q,count(1) as score
		FROM bus_listmanager a
		WHERE DATE_FORMAT( a.CREATTIME, '%Y') = #{YEAR}
		  and a.CORPINFO_ID = #{CORPINFO_ID}
		  and a.USER_ID in (select a.USER_ID from sys_user b where b.ISHEAD = '1' and b.CORPINFO_ID = #{CORPINFO_ID})
		GROUP BY FLOOR((DATE_FORMAT(a.CREATTIME, '%m') - 1) / 3) + 1
		ORDER BY q asc;
	</select>
	<select id="findTemplateTwo" resultType="com.zcloud.entity.PageData">
		select case
				   when count(1) > 0 then '4'
				   when count(1) = 0 then '0' end as score
		from bus_riskpoint_analysis a
		where  a.CORPINFO_ID = #{CORPINFO_ID}
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			REPORT_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

</mapper>
