<?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.keao.edu.user.dao.ExamOrganizationRelationDao">
	
	<resultMap type="com.keao.edu.user.entity.ExamOrganizationRelation" id="ExamOrganizationRelation">
		<result column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="organ_id_" property="organId" />
		<result column="settlement_type_" property="settlementType" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler" />
		<result column="share_profit_amount_" property="shareProfitAmount" />
		<result column="self_registration_student_num_" property="selfRegistrationStudentNum" />
		<result column="self_payment_amount_" property="selfPaymentAmount" />
		<result column="total_registration_student_num_" property="totalRegistrationStudentNum"/>
		<result column="total_payment_amount_" property="totalPaymentAmount"/>
		<result column="total_trans_amount_" property="totalTransAmount" />
		<result column="url_" property="url" />
		<result column="send_url_flag_" property="sendUrlFlag" />
		<result column="is_allow_arrange_exam_" property="isAllowArrangeExam" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<association property="organization" columnPrefix="organ_" resultMap="com.keao.edu.user.dao.OrganizationDao.Organization" />
	</resultMap>

	<resultMap id="ExamOrganizationRelationExtraDto" type="com.keao.edu.user.dto.ExamOrganizationRelationExtraDto" extends="ExamOrganizationRelation">
		<result column="organ_name_" property="organizationName"/>
	</resultMap>

	<resultMap id="ExamOrganStatisticsDto" type="com.keao.edu.user.dto.ExamOrganStatisticsDto" extends="ExamOrganizationRelation">

	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamOrganizationRelation" >
		SELECT * FROM exam_organization_relation WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamOrganizationRelation">
		SELECT * FROM exam_organization_relation WHERE tenant_id_#{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExamOrganizationRelation" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_organization_relation (examination_basic_id_,organ_id_,settlement_type_,share_profit_amount_,
		self_registration_student_num_,self_payment_amount_,total_registration_student_num_,total_payment_amount_,
		url_,send_url_flag_,is_allow_arrange_exam_,total_trans_amount_,
		create_time_,update_time_,tenant_id_)
		VALUES(#{examinationBasicId},#{organId},#{settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{shareProfitAmount},
		#{selfRegistrationStudentNum},#{selfPaymentAmount},#{totalRegistrationStudentNum},#{totalPaymentAmount},
		#{url},#{sendUrlFlag},#{isAllowArrangeExam},#{totalTransAmount},
		NOW(),NOW(),#{tenantId})
	</insert>

    <insert id="batchInsert" parameterType="com.keao.edu.user.entity.ExamOrganizationRelation" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_organization_relation (examination_basic_id_,organ_id_,settlement_type_,share_profit_amount_,
		self_registration_student_num_,self_payment_amount_,total_registration_student_num_,total_payment_amount_,
		url_,send_url_flag_,is_allow_arrange_exam_,total_trans_amount_,
		create_time_,update_time_,tenant_id_)
		VALUES
		<foreach collection="examOrganizationRelations" item="examOrganizationRelation" separator=",">
			(#{examOrganizationRelation.examinationBasicId},#{examOrganizationRelation.organId},
			#{examOrganizationRelation.settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			#{examOrganizationRelation.shareProfitAmount},
			#{examOrganizationRelation.selfRegistrationStudentNum},
			#{examOrganizationRelation.selfPaymentAmount},
			#{examOrganizationRelation.totalRegistrationStudentNum},
			#{examOrganizationRelation.totalPaymentAmount},#{examOrganizationRelation.url},
			#{examOrganizationRelation.sendUrlFlag},
			#{examOrganizationRelation.isAllowArrangeExam},
			#{examOrganizationRelation.totalTransAmount},
			NOW(),NOW(),#{examOrganizationRelation.tenantId})
		</foreach>
	</insert>

    <!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.ExamOrganizationRelation">
		UPDATE exam_organization_relation
		<set>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="sendUrlFlag != null">
				send_url_flag_ = #{sendUrlFlag},
			</if>
			<if test="isAllowArrangeExam != null">
				is_allow_arrange_exam_ = #{isAllowArrangeExam},
			</if>
			<if test="url != null">
				url_ = #{url},
			</if>
			<if test="tenantId != null and tenantId != 0">
				tenant_id_ = #{tenantId},
			</if>
			<if test="shareProfitAmount != null">
				share_profit_amount_ = #{shareProfitAmount},
			</if>
			<if test="selfRegistrationStudentNum != null">
				self_registration_student_num_ = #{selfRegistrationStudentNum},
			</if>
			<if test="selfPaymentAmount != null">
				self_payment_amount_ = #{selfPaymentAmount},
			</if>
			<if test="totalRegistrationStudentNum != null">
				total_registration_student_num_ = #{totalRegistrationStudentNum},
			</if>
			<if test="totalPaymentAmount != null">
				total_payment_amount_ = #{totalPaymentAmount},
			</if>
			<if test="totalTransAmount != null">
				total_trans_amount_ = #{totalTransAmount},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="settlementType != null">
				settlement_type_ = #{settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>

	<update id="batchUpdate" parameterType="com.keao.edu.user.entity.ExamOrganizationRelation">
		<foreach collection="examOrganizationRelations" item="eo" separator=";">
			UPDATE exam_organization_relation
			<set>
				<if test="eo.examinationBasicId != null">
					examination_basic_id_ = #{eo.examinationBasicId},
				</if>
				<if test="eo.sendUrlFlag != null">
					send_url_flag_ = #{eo.sendUrlFlag},
				</if>
				<if test="eo.isAllowArrangeExam != null">
					is_allow_arrange_exam_ = #{eo.isAllowArrangeExam},
				</if>
				<if test="eo.url != null">
					url_ = #{eo.url},
				</if>
				<if test="eo.tenantId != null">
					tenant_id_ = #{eo.tenantId},
				</if>
				<if test="eo.shareProfitAmount != null">
					share_profit_amount_ = #{eo.shareProfitAmount},
				</if>
				<if test="eo.selfRegistrationStudentNum != null">
					self_registration_student_num_ = #{eo.selfRegistrationStudentNum},
				</if>
				<if test="eo.selfPaymentAmount != null">
					self_payment_amount_ = #{eo.selfPaymentAmount},
				</if>
				<if test="eo.totalRegistrationStudentNum != null">
					total_registration_student_num_ = #{eo.totalRegistrationStudentNum},
				</if>
				<if test="eo.totalPaymentAmount != null">
					total_payment_amount_ = #{eo.totalPaymentAmount},
				</if>
				<if test="eo.totalTransAmount != null">
					total_trans_amount_ = #{eo.totalTransAmount},
				</if>
				<if test="eo.organId != null">
					organ_id_ = #{eo.organId},
				</if>
				<if test="eo.settlementType != null">
					settlement_type_ = #{eo.settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
				</if>
				update_time_ = NOW()
			</set> WHERE id_ = #{eo.id}
		</foreach>
	</update>

	<update id="batchUpdateSendUrlFlag">
		UPDATE exam_organization_relation SET send_url_flag_=#{sendUrlFlag,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler}
		WHERE examination_basic_id_=#{examId}
		<if test="organIds!=null">
			AND organ_id_ IN
			<foreach collection="organIds" item="organId" separator="," open="(" close=")">
				#{organId}
			</foreach>
		</if>
	</update>

	<update id="updateOrganAllowArrangeExamStatus">
		UPDATE exam_organization_relation SET is_allow_arrange_exam_=#{isAllowArrangeExam}
		WHERE examination_basic_id_=#{examId}
		AND organ_id_ IN
		<foreach collection="organIds" item="organId" separator="," open="(" close=")">
			#{organId}
		</foreach>
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM exam_organization_relation WHERE id_ = #{id} 
	</delete>

    <delete id="deleteExamOrgans">
		DELETE FROM exam_organization_relation
		WHERE examination_basic_id_=#{examId} AND organ_id_ IN
		<foreach collection="organIds" item="organId" separator="," open="(" close=")">
			#{organId}
		</foreach>
	</delete>

    <sql id="queryCondition">
		<where>
			ear.examination_basic_id_=#{examId}
			<if test="organIds!=null">
				AND ear.organ_id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
			<if test="settlementType!=null">
				AND ear.settlement_type_=#{settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="isAllowArrangeExam!=null">
				AND ear.is_allow_arrange_exam_ = #{isAllowArrangeExam}
			</if>
			<if test="search!=null">
				AND (ear.organ_id_=#{search} OR a.name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExamOrganizationRelationExtraDto" parameterType="map">
		SELECT
			ear.*,
			a.name_ organ_name_
		FROM exam_organization_relation ear
		LEFT JOIN organization a ON ear.organ_id_=a.id_
		<include refid="queryCondition"/>
		ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM exam_organization_relation ear
		LEFT JOIN organization a ON ear.organ_id_=a.id_
		<include refid="queryCondition"/>
	</select>

	<select id="queryExamOrgans" resultMap="ExamOrganizationRelationExtraDto" parameterType="map">
		SELECT
			ear.*,
			a.name_ organ_name_
		FROM exam_organization_relation ear
		LEFT JOIN organization a ON ear.organ_id_=a.id_
		<include refid="queryCondition"/>
		ORDER BY id_ <include refid="global.limit"/>
	</select>

	<select id="countExamOrgans" resultType="int">
		SELECT COUNT(*) FROM exam_organization_relation ear
		LEFT JOIN organization a ON ear.organ_id_=a.id_
		<include refid="queryCondition"/>
	</select>

	<select id="getWithExam" resultMap="ExamOrganizationRelation">
		SELECT * FROM exam_organization_relation WHERE examination_basic_id_=#{examId}
	</select>

	<select id="getOrganInfoWithExam" resultMap="ExamOrganizationRelation">
		SELECT
			eor.*,
			o.user_id_ organ_user_id_,
			o.contact_name_ organ_contact_name_,
			o.contact_phone_ organ_contact_phone_
		FROM
			exam_organization_relation eor
			LEFT JOIN organization o ON eor.organ_id_ = o.id_
		WHERE
			eor.examination_basic_id_ = #{examId}
	</select>

	<select id="getOrganIdsWithExam" resultType="int">
		SELECT organ_id_ FROM exam_organization_relation WHERE examination_basic_id_=#{examId} FOR UPDATE
	</select>

    <select id="getWithExams" resultMap="ExamOrganizationRelation">
		SELECT * FROM exam_organization_relation WHERE examination_basic_id_ IN
		<foreach collection="examIds" item="examId" separator="," open="(" close=")">
			#{examId}
		</foreach>
    </select>

	<select id="getWithExamsAndOrgans" resultMap="ExamOrganizationRelation">
		SELECT * FROM exam_organization_relation WHERE examination_basic_id_ IN
		<foreach collection="examIds" item="examId" separator="," open="(" close=")">
			#{examId}
		</foreach>
		<if test="organIds!=null">
			AND organ_id_ IN
			<foreach collection="organIds" item="organId" separator="," open="(" close=")">
				#{organId}
			</foreach>
		</if>
	</select>

	<select id="countExamOrganStudentAndPayment" resultMap="ExamOrganizationRelation">
		SELECT
			o.id_ organ_id_,
			SUM(eor.total_registration_student_num_) total_registration_student_num_,
			SUM(eor.total_payment_amount_) total_payment_amount_
		FROM
			organization o
			LEFT JOIN (
			SELECT
				eor1.id_,
				eor1.organ_id_,
				eor1.total_payment_amount_,
				eor1.total_registration_student_num_,
				o1.parent_organ_id_tag_
			FROM
				exam_organization_relation eor1
				LEFT JOIN organization o1 ON o1.id_ = eor1.organ_id_
			WHERE eor1.examination_basic_id_=#{examId}
			) eor ON eor.parent_organ_id_tag_ LIKE CONCAT( o.parent_organ_id_tag_, '%' )
		<if test="organIds!=null">
			WHERE
			o.id_ IN
			<foreach collection="organIds" item="organId" separator="," open="(" close=")">
				#{organId}
			</foreach>
		</if>
		GROUP BY o.id_
	</select>

    <select id="findByOrganId" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT id_) FROM exam_organization_relation WHERE organ_id_ = #{organId}
	</select>

	<sql id="queryUnRelatedOrgansCondition">
		<where>
			o.del_flag_=0
			<if test="tenantId != null and tenantId != 0">
				AND o.tenant_id_ = #{tenantId}
			</if>
			<if test="organIds!=null">
				AND o.id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
			AND NOT EXISTS ( SELECT organ_id_ FROM exam_organization_relation WHERE examination_basic_id_ = #{examId} AND organ_id_ = o.id_ )
		</where>
	</sql>

    <select id="queryUnRelatedOrgans" resultMap="com.keao.edu.user.dao.OrganizationDao.Organization">
		SELECT
			<include refid="com.keao.edu.user.dao.OrganizationDao.organizationColumn"/>
		FROM
			organization o
		<include refid="queryUnRelatedOrgansCondition"/>
		ORDER BY o.id_
		<include refid="global.limit"/>
    </select>

	<select id="countUnRelatedOrgans" resultType="int">
		SELECT
			COUNT(o.id_)
		FROM
			organization o
		<include refid="queryUnRelatedOrgansCondition"/>
    </select>

	<!-- 获取项目合作机构关联信息 -->
    <select id="getExamOrganizationRelation" resultMap="ExamOrganStatisticsDto">
		SELECT * FROM exam_organization_relation WHERE examination_basic_id_=#{examId} AND organ_id_=#{OrganId} FOR UPDATE
	</select>
	<select id="getExamOrganizationRelations" resultMap="ExamOrganizationRelation">
		SELECT * FROM exam_organization_relation WHERE examination_basic_id_=#{examId} AND organ_id_ IN
		<foreach collection="organIds" item="organId" separator="," open="(" close=")">
			#{organId}
		</foreach>
	</select>

	<select id="getExamChildOrgans" resultMap="ExamOrganizationRelationExtraDto">
		SELECT
			ear.*,
			a.name_ organ_name_
		FROM exam_organization_relation ear
		LEFT JOIN organization a ON ear.organ_id_=a.id_
		WHERE
		ear.examination_basic_id_=#{examId}
		<if test="organIds!=null">
			AND ear.organ_id_ IN
			<foreach collection="organIds" item="organId" separator="," open="(" close=")">
				#{organId}
			</foreach>
		</if>
	</select>

</mapper>
