<?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.ym.mec.biz.dal.dao.ReplacementInstrumentCooperationDao">
    <resultMap id="ReplacementInstrumentCooperation" type="com.ym.mec.biz.dal.entity.ReplacementInstrumentCooperation">
        <result column="id_" property="id"/>
        <result column="organ_id_" property="organId"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="topic_id_" property="topicId"/>
        <result column="topic_title_" property="topicTitle"/>
        <result column="open_pay_" property="openPay" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="show_question_" property="showQuestion" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="organName" property="organName"/>
        <result column="cooperationOrganName" property="cooperationOrganName"/>
        <result column="title_" property="title"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="ReplacementInstrumentCooperation">
        SELECT *
        FROM replacement_instrument_cooperation
        WHERE id_ = #{id}
    </select>
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ReplacementInstrumentCooperation"
            useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        <!--@mbg.generated-->
        insert into replacement_instrument_cooperation (organ_id_, cooperation_organ_id_, open_pay_,show_question_,topic_id_, create_time_,
        update_time_,title_
        )
        values (#{organId}, #{cooperationOrganId},
        #{openPay,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{showQuestion,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{topicId}, NOW(), NOW(),#{title}
        )
    </insert>

    <delete id="delete">
        <!--@mbg.generated-->
        delete from replacement_instrument_cooperation WHERE id_ = #{id}
    </delete>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ReplacementInstrumentCooperation">
        <!--@mbg.generated-->
        update replacement_instrument_cooperation
        <set>
            <if test="title != null">
                title_ = #{title},
            </if>
            <if test="topicId != null">
                topic_id_ = #{topicId},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="cooperationOrganId != null">
                cooperation_organ_id_ = #{cooperationOrganId},
            </if>
            <if test="openPay != null">
                open_pay_ = #{openPay,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ReplacementInstrumentCooperation" parameterType="map">
        SELECT ric.*,o.name_ organName,co.name_ cooperationOrganName,qt.title_ topic_title_ FROM replacement_instrument_cooperation ric
        LEFT JOIN cooperation_organ co ON co.id_ = ric.cooperation_organ_id_
        LEFT JOIN organization o ON o.id_ = ric.organ_id_
        LEFT JOIN questionnaire_topic qt ON qt.id_ = ric.topic_id_
        <include refid="queryPageSql"/>
        ORDER BY ric.id_ DESC
        <include refid="global.limit"/>
    </select>
    <sql id="queryPageSql">
        <where>
            <if test="organId != null">
                AND FIND_IN_SET(ric.organ_id_ , #{organId})
            </if>
            <if test="openPay != null">
                AND ric.open_pay_ = #{openPay,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="showQuestion != null">
                AND ric.show_question_ = #{showQuestion,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="search != null and search != ''">
                AND (ric.cooperation_organ_id_ = #{search} OR co.name_ LIKE CONCAT('%',#{search},'%') OR ric.title_ LIKE CONCAT('%',#{search},'%'))
            </if>
        </where>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM replacement_instrument_cooperation ric
        LEFT JOIN cooperation_organ co ON co.id_ = ric.cooperation_organ_id_
        <include refid="queryPageSql"/>
    </select>

    <select id="countAllReplacementsInfo" resultType="com.ym.mec.biz.dal.entity.ReplacementInstrumentCooperation">
        SELECT cooperation_organ_id_              cooperationOrganId,
               COUNT(user_id_)                           activeNum,
               SUM(IF(instruments_id_ IS NOT NULL AND instruments_id_ > 0, 1, 0)) replaceNum,
               SUM(IF(pay_status_ = 2, 1, 0)) payNum,replacement_instrument_cooperation_id_ id
        FROM replacement_instrument_activity
        GROUP BY replacement_instrument_cooperation_id_
    </select>
    <select id="findByCooperationOrganIdAndTopicId" resultMap="ReplacementInstrumentCooperation">
        SELECT *
        FROM replacement_instrument_cooperation
        WHERE cooperation_organ_id_ = #{cooperationOrganId}
        <if test="topicId != null">
            AND topic_id_ = #{topicId}
        </if>
        <if test="topicId == null">
            AND topic_id_ IS NULL
        </if>
        LIMIT 1
    </select>

</mapper>