<?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.mdm.mapper.InterfaceDataSourcesMapper">
	<insert id="addInterfaceDataSources" parameterType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceDTO">
		INSERT INTO `mdm_info_data_source_interface` (
  			interface_id,
  			interface_name,
  			interface_desc,
  			project_id,
  			interface_url,
  			method_id,
  			update_time
			)
		VALUES(#{interfaceId},#{interfaceName},#{interfaceDesc},#{projectId},#{interfaceUrl},#{methodId},now())
	</insert>



	<select id="getProjectId" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceDTO">
		SELECT project_id FROM `mdm_info_data_source_interface`
		WHERE
		interface_id = #{InterfaceId}
	</select>
	<select id="getProtocolId" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceDTO">
		SELECT protocol_id FROM `mdm_info_data_source_interface`
		WHERE
		interface_id = #{InterfaceId}
	</select>
	<select id="getMethodId" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceDTO">
		SELECT method_id FROM `mdm_info_data_source_interface`
		WHERE
		interface_id = #{InterfaceId}
	</select>

	<delete id="deleteProjectById" parameterType="java.lang.String">
		delete from
		mdm_info_data_source_interface_project where project_id = #{projectId}
	</delete>

	<delete id="deleteInputParam" parameterType="java.lang.String">
		DELETE
		FROM
  			`mdm_info_data_source_interface_param_in`
		WHERE interface_id = #{interfaceId}
	</delete>

	<delete id="deleteOutParam" parameterType="java.lang.String">
		DELETE
		FROM
 			 `mdm_info_data_source_interface_param_out`
		WHERE interface_id = #{interfaceId}
	</delete>
	<delete id="deleteInterfaceDataSources" parameterType="java.lang.String">
			DELETE
			FROM
			  `mdm_info_data_source_interface`
			WHERE interface_id = #{interfaceId}
	</delete>


	<select id="listRequestMode" resultType="com.datau.mdm.dto.MdmDimDataSourceInterfaceMethodDTO">
		SELECT method_id,method_name FROM `mdm_dim_data_source_interface_method`
	</select>


	<select id="listInterfaceDataSourcesStatus" resultType="com.datau.mdm.dto.MdmDimDataSourceInterfaceStatusDTO">
		SELECT status_id,STATUS FROM `mdm_dim_data_source_interface_status`
	</select>
	<select id="getStatusById" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceDTO">
		SELECT status_id FROM `mdm_info_data_source_interface`
		WHERE
		interface_id = #{InterfaceId}
	</select>

	<update id="updateInterfaceDataSourcesStatus" parameterType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceDTO">
		 UPDATE
		  `mdm_info_data_source_interface`
		SET
		  status_id = #{statusId}
		WHERE interface_id = #{interfaceId}
    </update>

	<select id="listInterfaceDataSources" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceDTO">
		SELECT
		mds.interface_id AS interfaceId,
		mds.`interface_name` AS interfaceName,
		mds.`interface_desc` AS interfaceDesc,
		mds.`project_id` AS projectId,
		mds.`interface_url` AS interfaceUrl,
		mds.`protocol_id` AS protocolId,
		mds.`method_id` AS methodId,
		mds.`update_time` AS updateTime,
		mds.`operator_id` AS operatorId,
		mds.`status_id` AS statusId,
		mddsim.method_name AS methodName,
		midsip.project_name AS projectName,
		mddsi.`status_name` AS statusName,
		(SELECT
		COUNT(mdi.`interface_id`)
		FROM
		`mdm_info_data_source_interface` mds
		INNER JOIN `mdm_info_data_source_interface_param_in` mdi
		ON mds.`interface_id` = mdi.`interface_id`) AS numberOfInputNodes,
		(SELECT
		COUNT(mdo.`interface_id`)
		FROM
		`mdm_info_data_source_interface` mds
		INNER JOIN `mdm_info_data_source_interface_param_out` mdo
		ON mds.`interface_id` = mdo.`interface_id`) AS numberOfOutNodes
		FROM
		`mdm_info_data_source_interface` mds
		LEFT JOIN `mdm_dim_data_source_interface_method` mddsim
		ON mds.`method_id` = mddsim.`method_id`
		LEFT JOIN `mdm_info_data_source_interface_project` midsip
		ON mds.`project_id` = midsip.`project_id`
		LEFT JOIN `mdm_dim_data_source_interface_status` mddsi
		ON mds.`status_id` = mddsi.`status_id`
		WHERE 1 = 1
		<if test="methodName !=null &amp;&amp; methodName !=''">
			and mddsim.method_name like concat('%',#{methodName},'%')
		</if>
		<if test="projectName !=null &amp;&amp; projectName !=''">
			and midsip.project_name like concat('%',#{projectName},'%')
		</if>
		<if test="status !=null &amp;&amp; status !=''">
			and mddsi.`status` like concat('%',#{status},'%')
		</if>
	</select>

	<update id="updateInterfaceDataSourcesById" parameterType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceVO">
		UPDATE
		`mdm_info_data_source_interface`
		SET
		<if test="interfaceName !=null &amp;&amp; interfaceName !=''">
			 interface_name = #{interfaceName}
		</if>
		<if test="interfaceDesc !=null &amp;&amp; interfaceDesc !=''">
			,interface_desc = #{interfaceDesc}
		</if>
		<if test="projectId !=null &amp;&amp; projectId !=''">
			,project_id = #{projectId}
		</if>
		<if test="interfaceUrl !=null &amp;&amp; interfaceUrl !=''">
			,interface_url = #{interfaceUrl}
		</if>
		<if test="methodId !=null &amp;&amp; methodId !=''">
			,method_id = #{methodId}
		</if>
		,update_time = now()
		WHERE interface_id = #{interfaceId}
	</update>

	<insert id="updateInputNodes" parameterType="java.util.Map">
		INSERT INTO `mdm_info_data_source_interface_param_in`(param_id,param_key,param_value,update_time,interface_id)
		values
		<foreach collection="list" separator="," item="column">
			(uuid(),#{column.paramKey},#{column.paramValue},now(),#{column.interfaceId})
		</foreach>
	</insert>

	<insert id="updateOutNodes" parameterType="java.util.Map">
		insert into `mdm_info_data_source_interface_param_out`(param_id,node_name,node_desc,node_site,update_time,interface_id)
		values
		<foreach collection="list" separator="," item="column">
			(uuid(),#{column.nodeName},#{column.nodeDesc},#{column.nodeSite},now()
			,#{column.interfaceId})
		</foreach>
	</insert>

	<select id="getInterfaceDataSourcesById" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceVO">
		SELECT
			  mds.`interface_id` AS interfaceId,
			  mds.`interface_name` AS interfaceName,
			  mds.`interface_desc` AS interfaceDesc,
			  mddsim.`method_name` AS methodName,
			  midsip.`project_Name` AS projectName

			FROM
			  `mdm_info_data_source_interface` mds
			  LEFT JOIN `mdm_dim_data_source_interface_method` mddsim
				ON mds.`method_id` = mddsim.`method_id`
			  LEFT JOIN `mdm_info_data_source_interface_project` midsip
				ON mds.`project_id` = midsip.`project_id`
			WHERE mds.`interface_id` = #{interfaceId}
	</select>

	<select id="getInterfaceUrlById" resultType="java.lang.String">
		SELECT
			mds.`interface_url`
		FROM
			  `mdm_info_data_source_interface` mds
		WHERE mds.`interface_id` = #{interfaceId}
	</select>

	<select id="getinterfaceMethodById" resultType="java.lang.String">
		SELECT
		  mdd.`method_name`
		FROM
		  `mdm_info_data_source_interface` mds
		  LEFT JOIN `mdm_dim_data_source_interface_method` mdd
			ON mds.`method_id` = mdd.`method_id`
		WHERE mds.`interface_id` = #{interfaceId}
	</select>

	<select id="getInterfaceParamIn" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceParamInDTO">
		SELECT
  			*
			FROM
  				`mdm_info_data_source_interface_param_in`
			WHERE interface_id =#{interfaceId}
	</select>

	<select id="getInterfaceParamOut" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceParamOutDTO">
			SELECT
			  *
			FROM
			  `mdm_info_data_source_interface_param_out`
			WHERE interface_id =#{interfaceId}
	</select>

	<insert id="addInputNodes" parameterType="java.util.Map">
		INSERT INTO `mdm_info_data_source_interface_param_in`(param_id,param_key,param_value,update_time,interface_id)
		values
		<foreach collection="list" separator="," item="column">
			(uuid(),#{column.paramKey},#{column.paramValue},now(),#{column.interfaceId})
		</foreach>
	</insert>

	<insert id="addOutNodes" parameterType="java.util.Map">
		insert into `mdm_info_data_source_interface_param_out`(param_id,node_name,node_desc,node_site,update_time,interface_id)
		values
		<foreach collection="list" separator="," item="column">
			(uuid(),#{column.nodeName},#{column.nodeDesc},#{column.nodeSite},now()
			,#{column.interfaceId})
		</foreach>
	</insert>

	<select id="listInterfaceName" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceVO">
			SELECT
				 mds.`interface_id`,
 				 mds.`interface_name`
			FROM
  			`mdm_info_data_source_interface` mds
	</select>

	<select id="getInterfaceByProjectId" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceVO">
		SELECT
 			 mds.`interface_id`,
  			 mds.`interface_name`
		FROM
  			`mdm_info_data_source_interface_project` midp
  		LEFT JOIN `mdm_info_data_source_interface` mds
    	ON midp.`project_id` = mds.`project_id`
    	WHERE midp.`project_id`=#{projectId}
	</select>

	<select id="getInterfaceOutParamsByInterfaceId" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceParamOutDTO">
		SELECT
			 mido.`param_id`,
 			 mido.`node_name`
		FROM
		  `mdm_info_data_source_interface` mds
 		LEFT JOIN `mdm_info_data_source_interface_param_out` mido
    	ON mds.`interface_id`=mido.`interface_id`
    	WHERE mds.`interface_id`=#{interfaceId}
	</select>


	<select id="getCheckCodeById" resultType="java.lang.String">
		SELECT
  			midp.`check_code`
		FROM
 			 `mdm_info_data_source_interface` mds
  		LEFT JOIN `mdm_info_data_source_interface_project` midp
    	ON midp.`project_id`=mds.`project_id`
		WHERE mds.`interface_id` = #{interfaceId}
	</select>

	<select id="getInterfaceInParamsByInterfaceId" resultType="com.datau.mdm.dto.MdmInfoDataSourceInterfaceParamInDTO">
		SELECT
 			 midi.`param_id`,
 			 midi.`param_key`,
  			midi.`param_value`
		FROM
 			 `mdm_info_data_source_interface` mds
  		LEFT JOIN `mdm_info_data_source_interface_param_in` midi
    	ON mds.`interface_id` = midi.`interface_id`
		WHERE mds.`interface_id` = #{interfaceId}
	</select>

	<select id="listInterfaceInfo" resultType="com.datau.mdm.dto.MdmTreeDTO">
			SELECT
  				mds.`interface_id` AS id,
  				mds.`interface_name` AS name,
  				mds.`project_id` AS pid

			FROM
 				 `mdm_info_data_source_interface` mds
	</select>
	<select id="listInterfaceProjectInfo" resultType="com.datau.mdm.dto.MdmTreeDTO">
			SELECT
  				midp.`project_id` AS id,
  				midp.`project_name` AS name,
  				'0' AS pid
			FROM
  				`mdm_info_data_source_interface_project` midp
	</select>


</mapper>