<?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.ReplacementInstrumentActivityDao">

    <resultMap type="com.ym.mec.biz.dal.entity.ReplacementInstrumentActivity" id="ReplacementInstrumentActivity">
        <result column="id_" property="id"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="replacement_instrument_cooperation_id_" property="replacementInstrumentCooperationId"/>
        <result column="topic_id_" property="topicId"/>
        <result column="cooperationOrganName" property="cooperationOrganName"/>
        <result column="user_id_" property="userId"/>
        <result column="user_name_" property="userName"/>
        <result column="grade_" property="grade"/>
        <result column="classes_" property="classes"/>
        <result column="mobile_no_" property="mobileNo"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="question_result_" property="questionResult"/>
        <result column="instruments_id_" property="instrumentsId"/>
        <result column="goods_id_" property="goodsId"/>
        <result column="other_suggestion_" property="otherSuggestion"/>
        <result column="open_flag_" property="openFlag"/>
        <result column="pay_status_" property="payStatus"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

    <resultMap id="ReplacementInstrumentActivityStatDto"
               type="com.ym.mec.biz.dal.dto.ReplacementInstrumentActivityStatDto"
               extends="ReplacementInstrumentActivity">
        <result property="organName" column="organ_name_"/>
        <result property="subjectName" column="subject_name_"/>
        <result property="brand" column="brand_"/>
        <result property="specification" column="specification_"/>
        <result property="param" column="param_"/>
        <result property="marketPrice" column="market_price_"/>
        <result property="discountPrice" column="discount_price_"/>
        <result property="depreciationPrice" column="depreciation_price_"/>
        <result property="salePrice" column="sale_price_"/>
        <result property="title" column="title_"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="ReplacementInstrumentActivity">
        SELECT *
        FROM replacement_instrument_activity
        WHERE id_ = #{id}
    </select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="ReplacementInstrumentActivity">
        SELECT *
        FROM replacement_instrument_activity where tenant_id_ = #{tenantId}
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ReplacementInstrumentActivity" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO replacement_instrument_activity (cooperation_organ_id_, user_id_, user_name_, grade_, classes_,
                                                     mobile_no_, subject_id_, question_result_, instruments_id_,
                                                     goods_id_,other_suggestion_,topic_id_,replacement_instrument_cooperation_id_,create_time_, update_time_,tenant_id_)
        VALUES (#{cooperationOrganId}, #{userId}, #{userName}, #{grade}, #{classes}, #{mobileNo}, #{subjectId},
                #{questionResult}, #{instrumentsId}, #{goodsId}, #{otherSuggestion}, #{topicId},#{replacementInstrumentCooperationId}, NOW(), NOW(),#{tenantId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ReplacementInstrumentActivity">
        UPDATE replacement_instrument_activity
        <set>
            <if test="topicId != null">
                topic_id_ = #{topicId},
            </if>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="openFlag != null">
                open_flag_ = #{openFlag},
            </if>
            <if test="payStatus != null">
                pay_status_ = #{payStatus},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="cooperationOrganId != null">
                cooperation_organ_id_ = #{cooperationOrganId},
            </if>
            <if test="replacementInstrumentCooperationId != null">
                replacement_instrument_cooperation_id_ = #{replacementInstrumentCooperationId},
            </if>
            <if test="userName != null">
                user_name_ = #{userName},
            </if>
            <if test="questionResult != null">
                question_result_ = #{questionResult},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
            instruments_id_ = #{instrumentsId},
            goods_id_ = #{goodsId},
            other_suggestion_ = #{otherSuggestion},
            <if test="mobileNo != null">
                mobile_no_ = #{mobileNo},
            </if>
            <if test="classes != null">
                classes_ = #{classes},
            </if>
            <if test="grade != null">
                grade_ = #{grade},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

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

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ReplacementInstrumentActivityStatDto" parameterType="map">
        SELECT * FROM replacement_instrument_activity where tenant_id_ = #{tenantId}
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM replacement_instrument_activity where tenant_id_ = #{tenantId}
    </select>
    <select id="findByUserId" resultMap="ReplacementInstrumentActivity">
        SELECT *
        FROM replacement_instrument_activity
        WHERE user_id_ = #{userId}
          AND cooperation_organ_id_ = #{cooperationOrganId}
          <if test="cooperationId != null">
              AND replacement_instrument_cooperation_id_ = #{cooperationId}
          </if>
        LIMIT 1
    </select>

    <sql id="queryReplacementsCondition">
        <where>
        	ria.tenant_id_ = #{tenantId}
            <if test="cooperationOrganId!=null">
                AND ria.cooperation_organ_id_ = #{cooperationOrganId}
            </if>
            <if test="cooperationId != null">
                AND ria.replacement_instrument_cooperation_id_ = #{cooperationId}
            </if>
            <if test="subjectId!=null">
                AND ria.subject_id_ = #{subjectId}
            </if>
            <if test="hasInstrumentsId!=null and hasInstrumentsId==true">
                AND ria.instruments_id_ >=1
            </if>
            <if test="hasInstrumentsId!=null and hasInstrumentsId==false">
                AND ria.instruments_id_ IS NULL
            </if>
            <if test="hasYesFirstAnswer!=null and hasYesFirstAnswer==true">
                AND ria.question_result_ LIKE CONCAT('1', '%')
            </if>
            <if test="hasYesFirstAnswer!=null and hasYesFirstAnswer==false ">
                AND ria.question_result_ LIKE CONCAT('0', '%')
            </if>
            <if test="brand!=null and brand!=''">
                AND g.brand_ = #{brand}
            </if>
            <if test="specification!=null and specification!=''">
                AND g.specification_ = #{specification}
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(o.id_ , #{organId})
            </if>
            <if test="search!=null and search!=''">
                AND (ria.mobile_no_ LIKE CONCAT('%', #{search}, '%') OR ria.user_name_ LIKE CONCAT('%', #{search}, '%'))
            </if>
            <if test="cooperationOrganIdOrName!=null and cooperationOrganIdOrName!=''">
                AND (ria.cooperation_organ_id_ = #{cooperationOrganIdOrName} OR co.name_ LIKE CONCAT('%',
                #{cooperationOrganIdOrName}, '%'))
            </if>
            <if test="payStatus!=null">
                AND ria.pay_status_ = #{payStatus}
            </if>
        </where>
    </sql>

    <select id="queryReplacements" resultMap="ReplacementInstrumentActivityStatDto">
        SELECT
        ria.*,
        sub.name_ subject_name_,
        g.brand_,
        g.specification_
        FROM
        replacement_instrument_activity ria
        LEFT JOIN goods g ON ria.instruments_id_ = g.id_
        LEFT JOIN `subject` sub ON ria.subject_id_ = sub.id_
        <include refid="queryReplacementsCondition"/>
        ORDER BY ria.id_ DESC
        <include refid="global.limit"></include>
    </select>

    <select id="countReplacements" resultType="int">
        SELECT
        COUNT(ria.id_)
        FROM
        replacement_instrument_activity ria
        LEFT JOIN goods g ON ria.instruments_id_ = g.id_
        LEFT JOIN `subject` sub ON ria.subject_id_ = sub.id_
        <include refid="queryReplacementsCondition"/>
    </select>

    <select id="countReplacementsInfo"
            resultType="com.ym.mec.biz.dal.dto.ReplacementInstrumentActivityStatHead">
        SELECT ric.open_pay_ openPay,
               COUNT( DISTINCT ria.user_id_ ) surveyNum,
               COUNT( DISTINCT IF (ria.instruments_id_ IS NOT NULL, ria.user_id_, NULL)) replacementNum,
               TRUNCATE ( COUNT(DISTINCT IF ( ria.instruments_id_ IS NOT NULL, ria.user_id_, NULL )) / COUNT( DISTINCT ria.user_id_ ) * 100, 2 ) replacementRate,
               TRUNCATE ( COUNT(DISTINCT IF ( ria.pay_status_ = 2, ria.user_id_, NULL )) / COUNT( DISTINCT IF ( ria.instruments_id_ IS NOT NULL, ria.user_id_, NULL )) * 100, 2) paymentRate
        FROM
            replacement_instrument_activity ria
                LEFT JOIN replacement_instrument_cooperation ric ON ria.cooperation_organ_id_ = ric.cooperation_organ_id_
        WHERE ria.replacement_instrument_cooperation_id_ = #{cooperationId}
    </select>

    <update id="openPay" parameterType="integer">
        UPDATE replacement_instrument_activity
        SET open_flag_ = 1
        WHERE replacement_instrument_cooperation_id_ = #{replacementInstrumentCooperationId}
          AND open_flag_ = 0
          AND instruments_id_ > 0
    </update>

    <select id="getLock" resultMap="ReplacementInstrumentActivity">
        SELECT *
        FROM replacement_instrument_activity
        WHERE id_ = #{id} FOR
        UPDATE
    </select>

    <select id="getPushUsers" resultMap="ReplacementInstrumentActivityStatDto">
        SELECT ria.id_, ria.user_id_, su.phone_ mobile_no_, g.brand_
        FROM replacement_instrument_activity ria
                 LEFT JOIN sys_user su ON su.id_ = ria.user_id_
                 LEFT JOIN goods g ON g.id_ = ria.instruments_id_
        WHERE replacement_instrument_cooperation_id_ = #{replacementInstrumentCooperationId}
          AND open_flag_ = 0
          AND instruments_id_ > 0
    </select>

    <select id="getInfo" resultMap="ReplacementInstrumentActivityStatDto">
        SELECT ria.*,
               g.brand_,
               g.specification_,
               g.brief_                                      param_,
               g.market_price_,
               g.discount_price_,
               g.group_purchase_price_                       sale_price_,
               (g.discount_price_ - g.group_purchase_price_) depreciation_price_,
               s.name_                                       subject_name_,
               co.name_                                      cooperationOrganName
        FROM replacement_instrument_activity ria
                 LEFT JOIN goods g ON g.id_ = ria.instruments_id_
                 LEFT JOIN subject s ON s.id_ = ria.subject_id_
                 LEFT JOIN cooperation_organ co ON co.id_ = ria.cooperation_organ_id_
        WHERE ria.id_ = #{id}
    </select>

    <!-- 获取问卷分页数据 -->
    <select id="getPageList" resultMap="ReplacementInstrumentActivityStatDto" parameterType="map">
        SELECT ria.*,su.phone_ mobile_no_,o.name_
        organ_name_,
        g.brand_,
        g.specification_,
        g.brief_ param_,
        g.market_price_,
        g.discount_price_,
        g.group_purchase_price_ sale_price_,
        (g.discount_price_ - g.group_purchase_price_) depreciation_price_,
        s.name_ subject_name_,co.name_ cooperationOrganName,qt.title_
        FROM replacement_instrument_activity ria
        LEFT JOIN questionnaire_topic qt ON qt.id_ = ria.topic_id_
        LEFT JOIN goods g ON g.id_ = ria.instruments_id_
        LEFT JOIN subject s ON s.id_ = ria.subject_id_
        lEFT JOIN sys_user su ON su.id_ = ria.user_id_
        LEFT JOIN cooperation_organ co ON co.id_ = ria.cooperation_organ_id_
        LEFT JOIN organization o ON o.id_ = co.organ_id_
        <include refid="queryReplacementsCondition"/>
        ORDER BY ria.id_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 获取问卷总条数 -->
    <select id="getCount" resultType="int">
        SELECT COUNT(*)
        FROM replacement_instrument_activity ria
        LEFT JOIN goods g ON g.id_ = ria.instruments_id_
        LEFT JOIN cooperation_organ co on co.id_ = ria.cooperation_organ_id_
        LEFT JOIN organization o on o.id_ = co.organ_id_
        <include refid="queryReplacementsCondition"/>
    </select>

    <select id="getActivityOrder" resultMap="com.ym.mec.biz.dal.dao.StudentPaymentOrderDao.StudentPaymentOrder">
        SELECT * FROM student_payment_order WHERE group_type_ = 'REPLACEMENT' AND status_ = 'SUCCESS' AND music_group_id_ IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY id_ DESC
    </select>
</mapper>
