<?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.bootdo.data.dao.DataApprovalDao">

	<select id="get" resultType="com.bootdo.data.domain.DataApprovalDO">
		select `ID`,
			   `APPLY_ID`,
			   `APPROVAL_USER`,
			   `APPROVAL_USERID`,
			   `APPROVAL_TIME`,
			   `APPROVAL_STATUS`,
			   `APPROVAL_DESC`,
			   `UPDATE_TIME`,
			   `STATUS`
		from tf_data_approval
		where ID = #{value}
	</select>


	<select id="list" resultType="com.bootdo.data.domain.UnitApplyInfoDO">
		SELECT b.ID AS id,b.UNIT_ID AS unitId,a.REPORTER_UNIT AS reporterUnit,a.`UNIT_CITY` AS unitCity,a.`UNIT_PROVINCE` AS unitProvince,
		a.`UNIT_OTHER` AS unitOther,a.`DATA_INDUSTRY` AS dataIndustry, a.`OTHER_INDUSTRY` AS otherIndustry,
		a.`LIAISE_MAN` AS liaiseMan,a.`LIAISE_WAY` AS liaiseWay,a.`STATUS` AS STATUS,
		b.`CREATE_TIME` AS upLoadTime,b.`LIAISE_MAN` AS liaiseMans,b.`LIAISE_WAY` AS liaiseWays,b.`TOTAL` AS total,
		b.APPROVAL_TIME AS approvalTime,b.APPROVAL_USER AS approvalUser,b.APPROVAL_STATUS AS approvalStatus,b.APPROVAL_DESC AS approvalDesc
		FROM tf_unit_info a,
		(SELECT c.`TOTAL`,c.ID,c.UNIT_ID,c.`CREATE_TIME`,c.`LIAISE_MAN`,c.`LIAISE_WAY`,
		d.`APPROVAL_USER`,d.`ID` AS APPROVAL_id,d.`APPROVAL_STATUS`,d.`APPROVAL_TIME`,d.`APPROVAL_DESC` FROM TF_DATA_APPLY c
		LEFT JOIN TF_DATA_APPROVAL d  ON  c.`ID` = d.`APPLY_ID`) b
		<where>
			a.id=b.UNIT_ID
		    <!--备案id -->
			<if test="applyId != null and applyId != ''">and b.ID = #{applyId}</if>
			<if test="reporterUnit != null and reporterUnit != ''">and a.REPORTER_UNIT = #{reporterUnit}</if>
			<if test="unitProvince != null and unitProvince != ''">and a.UNIT_PROVINCE = #{unitProvince}</if>
			<if test="unitCity != null and unitCity != ''">and a.UNIT_CITY = #{unitCity}</if>
			<if test="unitOther != null and unitOther != ''">and a.UNIT_OTHER = #{unitOther}</if>
			<if test="dataIndustry != null and dataIndustry != ''">and a.DATA_INDUSTRY = #{dataIndustry}</if>
			<if test="otherIndustry != null and otherIndustry != ''">and a.OTHER_INDUSTRY = #{otherIndustry}</if>
			<if test="desc != null and desc != ''">and a.DESC = #{desc}</if>
			<if test="status != null and status != ''">and a.STATUS = #{status}</if>
			<if test="uploadUser != null and uploadUser != ''">and a.UPLOAD_USER = #{uploadUser}</if>
			<if test="uploadUserid != null and uploadUserid != ''">and a.UPLOAD_USERID = #{uploadUserid}</if>
			<if test="createTime != null and createTime != ''">and a.CREATE_TIME = #{createTime}</if>
			<if test="updateTime != null and updateTime != ''">and a.UPDATE_TIME = #{updateTime}</if>
			<if test="liaiseMan != null and liaiseMan != ''">and a.LIAISE_MAN = #{liaiseMan}</if>
			<if test="liaiseWay != null and liaiseWay != ''">and a.LIAISE_WAY = #{liaiseWay}</if>
		</where>
		<choose>
			<when test="sort != null and sort.trim() != ''">
				order by ${sort} ${order}
			</when>
			<otherwise>
				order by a.ID desc
			</otherwise>
		</choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="count" resultType="int">
		SELECT COUNT(1)
		FROM tf_unit_info a,
		(SELECT c.`TOTAL`,c.ID,c.UNIT_ID,c.`CREATE_TIME`,c.`LIAISE_MAN`,c.`LIAISE_WAY`,
		d.`APPROVAL_USER`,d.`ID` AS APPROVAL_id,d.`APPROVAL_STATUS`,d.`APPROVAL_TIME` FROM TF_DATA_APPLY c
		LEFT JOIN TF_DATA_APPROVAL d  ON  c.`ID` = d.`APPLY_ID`) b WHERE a.id=b.UNIT_ID
		<where>
			<!--备案id -->
			<if test="applyId != null and applyId != ''">and b.ID = #{applyId}</if>
			<if test="reporterUnit != null and reporterUnit != ''">and a.REPORTER_UNIT = #{reporterUnit}</if>
			<if test="unitProvince != null and unitProvince != ''">and a.UNIT_PROVINCE = #{unitProvince}</if>
			<if test="unitCity != null and unitCity != ''">and a.UNIT_CITY = #{unitCity}</if>
			<if test="unitOther != null and unitOther != ''">and a.UNIT_OTHER = #{unitOther}</if>
			<if test="dataIndustry != null and dataIndustry != ''">and a.DATA_INDUSTRY = #{dataIndustry}</if>
			<if test="otherIndustry != null and otherIndustry != ''">and a.OTHER_INDUSTRY = #{otherIndustry}</if>
			<if test="desc != null and desc != ''">and a.DESC = #{desc}</if>
			<if test="status != null and status != ''">and a.STATUS = #{status}</if>
			<if test="uploadUser != null and uploadUser != ''">and a.UPLOAD_USER = #{uploadUser}</if>
			<if test="uploadUserid != null and uploadUserid != ''">and a.UPLOAD_USERID = #{uploadUserid}</if>
			<if test="createTime != null and createTime != ''">and a.CREATE_TIME = #{createTime}</if>
			<if test="updateTime != null and updateTime != ''">and a.UPDATE_TIME = #{updateTime}</if>
			<if test="liaiseMan != null and liaiseMan != ''">and a.LIAISE_MAN = #{liaiseMan}</if>
			<if test="liaiseWay != null and liaiseWay != ''">and a.LIAISE_WAY = #{liaiseWay}</if>
		</where>
	</select>

<!--	<select id="list" resultType="com.bootdo.data.domain.UnitApplyInfoDO">-->
<!--		select-->
<!--		`ID`,`APPLY_ID`,`APPROVAL_USER`,`APPROVAL_USERID`,`APPROVAL_TIME`,`APPROVAL_STATUS`,`APPROVAL_DESC`,`UPDATE_TIME`,`STATUS`-->
<!--		from tf_data_approval-->
<!--		<where>-->
<!--			<if test="id != null and id != ''">and ID = #{id}</if>-->
<!--			<if test="applyId != null and applyId != ''">and APPLY_ID = #{applyId}</if>-->
<!--			<if test="approvalUser != null and approvalUser != ''">and APPROVAL_USER = #{approvalUser}</if>-->
<!--			<if test="approvalUserid != null and approvalUserid != ''">and APPROVAL_USERID = #{approvalUserid}</if>-->
<!--			<if test="approvalTime != null and approvalTime != ''">and APPROVAL_TIME = #{approvalTime}</if>-->
<!--			<if test="approvalStatus != null and approvalStatus != ''">and APPROVAL_STATUS = #{approvalStatus}</if>-->
<!--			<if test="approvalDesc != null and approvalDesc != ''">and APPROVAL_DESC = #{approvalDesc}</if>-->
<!--			<if test="updateTime != null and updateTime != ''">and UPDATE_TIME = #{updateTime}</if>-->
<!--			<if test="status != null and status != ''">and STATUS = #{status}</if>-->
<!--		</where>-->
<!--		<choose>-->
<!--			<when test="sort != null and sort.trim() != ''">-->
<!--				order by ${sort} ${order}-->
<!--			</when>-->
<!--			<otherwise>-->
<!--				order by ID desc-->
<!--			</otherwise>-->
<!--		</choose>-->
<!--		<if test="offset != null and limit != null">-->
<!--			limit #{offset}, #{limit}-->
<!--		</if>-->
<!--	</select>-->

<!--	<select id="count" resultType="int">-->
<!--		select count(*) from tf_data_approval-->
<!--		<where>-->
<!--			<if test="id != null and id != ''">and ID = #{id}</if>-->
<!--			<if test="applyId != null and applyId != ''">and APPLY_ID = #{applyId}</if>-->
<!--			<if test="approvalUser != null and approvalUser != ''">and APPROVAL_USER = #{approvalUser}</if>-->
<!--			<if test="approvalUserid != null and approvalUserid != ''">and APPROVAL_USERID = #{approvalUserid}</if>-->
<!--			<if test="approvalTime != null and approvalTime != ''">and APPROVAL_TIME = #{approvalTime}</if>-->
<!--			<if test="approvalStatus != null and approvalStatus != ''">and APPROVAL_STATUS = #{approvalStatus}</if>-->
<!--			<if test="approvalDesc != null and approvalDesc != ''">and APPROVAL_DESC = #{approvalDesc}</if>-->
<!--			<if test="updateTime != null and updateTime != ''">and UPDATE_TIME = #{updateTime}</if>-->
<!--			<if test="status != null and status != ''">and STATUS = #{status}</if>-->
<!--		</where>-->
<!--	</select>-->

	<insert id="save" parameterType="com.bootdo.data.domain.DataApprovalDO" useGeneratedKeys="true" keyProperty="id">
		insert into tf_data_approval
		(`APPLY_ID`,
		 `APPROVAL_USER`,
		 `APPROVAL_USERID`,
		 `APPROVAL_TIME`,
		 `APPROVAL_STATUS`,
		 `APPROVAL_DESC`,
		 `UPDATE_TIME`,
		 `STATUS`)
		values (#{applyId},
				#{approvalUser},
				#{approvalUserid},
				now(),
				#{approvalStatus},
				#{approvalDesc},
				#{updateTime},
				#{status})
	</insert>

	<update id="update" parameterType="com.bootdo.data.domain.DataApprovalDO">
		update tf_data_approval
		<set>
			<if test="applyId != null">`APPLY_ID` = #{applyId},</if>
			<if test="approvalUser != null">`APPROVAL_USER` = #{approvalUser},</if>
			<if test="approvalUserid != null">`APPROVAL_USERID` = #{approvalUserid},</if>
			<if test="approvalTime != null">`APPROVAL_TIME` = #{approvalTime},</if>
			<if test="approvalStatus != null">`APPROVAL_STATUS` = #{approvalStatus},</if>
			<if test="approvalDesc != null">`APPROVAL_DESC` = #{approvalDesc},</if>
			<if test="status != null">`STATUS` = #{status}</if>
			`UPDATE_TIME` = now()
		</set>
		where ID = #{id}
	</update>

	<delete id="remove">
		delete
		from tf_data_approval
		where ID = #{value}
	</delete>

	<!--删除备案ID-->
	<delete id="removeByApplyId">
		delete
		from tf_data_approval
		where APPLY_ID = #{value}
	</delete>



	<delete id="batchRemove">
		delete from tf_data_approval where ID in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>