<?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.eightwork.ConfinedspaceSignMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.Confinedspace_SIGN_ID,
		f.CONFINEDSPACE_ID,
		f.CORPINFO_ID,
		f.SIGN_USER_ID,
		f.SIGN_PATH,
		f.SIGN_TIME,
		f.DESCR,
		f.STEP_ID,
		f.IMG_PATH
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CONFINEDSPACE_SIGN_ID,
		CONFINEDSPACE_ID,
		CORPINFO_ID,
		SIGN_USER_ID,
		SIGN_PATH,
		SIGN_TIME,
		DESCR,
		STEP_ID,
		IMG_PATH
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CONFINEDSPACE_SIGN_ID},
		#{CONFINEDSPACE_ID},
		#{CORPINFO_ID},
		#{SIGN_USER_ID},
		#{SIGN_PATH},
		#{SIGN_TIME},
		#{DESCR},
		#{STEP_ID},
		#{IMG_PATH}
	</sql>

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

	<insert id="saveBatch" parameterType="pd">
		insert into
		<include refid="tableName"></include>
		(
		CONFINEDSPACE_SIGN_ID,
		CONFINEDSPACE_ID,
		CORPINFO_ID,
		SIGN_USER_ID,
		STEP_ID
		) values
		<foreach collection="list" item="item" index="index" separator="," >
		(#{item.CONFINEDSPACE_SIGN_ID},
		#{item.CONFINEDSPACE_ID},
		#{item.CORPINFO_ID},
		#{item.SIGN_USER_ID},
		#{item.STEP_ID})
		</foreach>
	</insert>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		DESCR = #{DESCR},
		SIGN_PATH = #{SIGN_PATH},
		SIGN_TIME= #{SIGN_TIME},
		IMG_PATH = #{IMG_PATH}
		where
		CONFINEDSPACE_ID = #{CONFINEDSPACE_ID} and SIGN_USER_ID = #{SIGN_USER_ID} and STEP_ID = #{STEP_ID}
	</update>

	<select id="listById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		 f.CONFINEDSPACE_ID = #{pd.CONFINEDSPACE_ID}
		order by f.SIGN_TIME  desc
	</select>

	<select id="listAll" parameterType="pd" resultType="pd">
		select
		f.CONFINEDSPACE_SIGN_ID,
		f.CONFINEDSPACE_ID,
		f.CORPINFO_ID,
		f.SIGN_USER_ID,
		f.SIGN_PATH,
		f.SIGN_TIME,
		f.STEP_ID,
		f.IMG_PATH,
		IFNULL(f.DESCR,'无') as DESCR,
		step.CODE
		from
		<include refid="tableName"></include> f
		left join bus_eightwork_step step on step.STEP_ID = f.STEP_ID
		where
		f.CONFINEDSPACE_ID = #{CONFINEDSPACE_ID}
		and
		f.SIGN_TIME is not null
	</select>

	<select id="checkUnsigned" parameterType="pd" resultType="pd">
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include> f
		where
			f.CONFINEDSPACE_ID = #{CONFINEDSPACE_ID}
		and
		    f.SIGN_TIME is null
	</select>


</mapper>
