<?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.study.TempStudyMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CORPINFO_ID,
		f.NAME,
		f.COURSEWARE_TYPE,
		f.COURSEWARE_ID,
		f.STUDY_TYPE,
		f.USER_NUM,
		f.STATUS,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.TEMPSTUDY_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CORPINFO_ID,
		NAME,
		COURSEWARE_TYPE,
		COURSEWARE_ID,
		STUDY_TYPE,
		USER_NUM,
		STATUS,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		TEMPSTUDY_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CORPINFO_ID},
		#{NAME},
		#{COURSEWARE_TYPE},
		#{COURSEWARE_ID},
		#{STUDY_TYPE},
		#{USER_NUM},
		#{STATUS},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{TEMPSTUDY_ID}
	</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
			TEMPSTUDY_ID = #{TEMPSTUDY_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			CORPINFO_ID = #{CORPINFO_ID},
			NAME = #{NAME},
			COURSEWARE_TYPE = #{COURSEWARE_TYPE},
			COURSEWARE_ID = #{COURSEWARE_ID},
			STUDY_TYPE = #{STUDY_TYPE},
			USER_NUM = #{USER_NUM},
			STATUS = #{STATUS},
			ISDELETE = #{ISDELETE},
			CREATOR = #{CREATOR},
			CREATTIME = #{CREATTIME},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			TEMPSTUDY_ID = TEMPSTUDY_ID
		where
			TEMPSTUDY_ID = #{TEMPSTUDY_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		v.TITLE as COURSEWARE_NAME,
		(select GROUP_CONCAT(u.USER_ID) from sys_user u where exists (select 1 from BUS_TEMPSTUDYUSERS s where s.TEMPSTUDY_ID = f.TEMPSTUDY_ID and u.USER_ID = s.USER_ID )) as STUDY_USER_IDS,
		(select GROUP_CONCAT(u.NAME) from sys_user u where exists (select 1 from BUS_TEMPSTUDYUSERS s where s.TEMPSTUDY_ID = f.TEMPSTUDY_ID and u.USER_ID = s.USER_ID )) as STUDY_USER_NAMES
		from
		<include refid="tableName"></include> f
		left join BUS_STUDYSECTION v on v.STUDYSECTION_ID = f.COURSEWARE_ID
		where
			f.TEMPSTUDY_ID = #{TEMPSTUDY_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		d.NAME as STUDY_TYPE_NAME,
		v.TITLE as COURSEWARE_NAME,
		(select count(1) from BUS_TEMPSTUDYUSERS u where u.TEMPSTUDY_ID = f.TEMPSTUDY_ID and u.STATUS = 1 ) as done_num
		from
		<include refid="tableName"></include> f
		left join BUS_STUDYSECTION v on v.STUDYSECTION_ID = f.COURSEWARE_ID
		 left join SYS_DICTIONARIES d on d.BIANMA = f.STUDY_TYPE
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.COURSEWARE_TYPE != null and pd.COURSEWARE_TYPE != ''"><!-- 关键词检索 -->
			and f.COURSEWARE_TYPE = #{pd.COURSEWARE_TYPE}
		</if>
		<if test="pd.COURSEWARE_ID != null and pd.COURSEWARE_ID != ''"><!-- 关键词检索 -->
			and f.COURSEWARE_ID = #{pd.COURSEWARE_ID}
		</if>
		<if test="pd.STATUS != null and pd.STATUS != ''"><!-- 关键词检索 -->
			and f.STATUS = #{pd.STATUS}
		</if>
	</select>

	<!-- 列表 -->
	<select id="listAllbyId" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		d.NAME as STUDY_TYPE_NAME,
		v.TITLE as COURSEWARE_NAME,
		(select count(1) from BUS_TEMPSTUDYUSERS u where u.TEMPSTUDY_ID = f.TEMPSTUDY_ID and u.STATUS = 1 ) as done_num
		from
		<include refid="tableName"></include> f
		left join BUS_STUDYSECTION v on v.STUDYSECTION_ID = f.COURSEWARE_ID
		left join SYS_DICTIONARIES d on d.BIANMA = f.STUDY_TYPE
		where f.ISDELETE = '0'
		<if test="STUDYSECTION_ID != null and STUDYSECTION_ID != ''">
			and f.COURSEWARE_ID=#{STUDYSECTION_ID}
		</if>
	</select>


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

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

</mapper>
