<?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.StudentInstrumentDao">
    <resultMap id="StudentInstrument" type="com.ym.mec.biz.dal.entity.StudentInstrument">
        <!--@mbg.generated-->
        <!--@Table student_instrument-->
        <id column="id_" property="id"/>
        <result column="student_id_" property="studentId"/>
        <result column="organ_id_" property="organId"/>
        <result column="goods_id_" property="goodsId"/>
        <result column="goods_category_id_" property="goodsCategoryId"/>
        <result column="goods_category_name_" property="goodsCategoryName"/>
        <result column="status_" property="status"/>
        <result column="start_time_" property="startTime"/>
        <result column="end_time_" property="endTime"/>
        <result column="order_id_" property="orderId"/>
        <result column="change_order_id_" property="changeOrderId"/>
        <result column="operation_" property="operation"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="goods_name_" property="goodsName"/>
        <result column="specification_" property="specification"/>
        <result column="goods_brand_" property="goodsBrand"/>
        <result column="goods_img_" property="goodsImg"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

    <select id="get" parameterType="java.lang.Long" resultMap="StudentInstrument">
        <!--@mbg.generated-->
        SELECT * FROM student_instrument
        where id_ = #{id}
    </select>
    <delete id="delete" parameterType="java.lang.Long">
        <!--@mbg.generated-->
        delete from student_instrument
        where id_ = #{id}
    </delete>

    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.StudentInstrument"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into student_instrument
        (student_id_,organ_id_,goods_id_,goods_category_id_,goods_category_name_,goods_name_,goods_brand_,specification_,goods_img_,
        status_,order_id_,change_order_id_,operation_, start_time_, end_time_, create_time_, update_time,tenant_id_
        )
        values
        (#{studentId},#{organId},#{goodsId},#{goodsCategoryId},#{goodsCategoryName},#{goodsName},#{goodsBrand},#{specification},#{goodsImg},
        #{status},#{orderId},#{changeOrderId},#{operation}, #{startTime}, #{endTime}, NOW(), NOW(),#{tenantId}
        )
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentInstrument">
        <!--@mbg.generated-->
        update student_instrument
        <set>
            <if test="studentId != null">
                student_id_ = #{studentId},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="goodsId != null">
                goods_id_ = #{goodsId},
            </if>
            <if test="goodsName != null">
                goods_name_ = #{goodsName},
            </if>
            <if test="goodsBrand != null">
                goods_brand_ = #{goodsBrand},
            </if>
            <if test="specification != null">
                specification_ = #{specification},
            </if>
            <if test="goodsImg != null">
                goods_img_ = #{goodsImg},
            </if>
            <if test="status != null">
                status_ = #{status},
            </if>
            <if test="operation != null">
                operation_ = #{operation},
            </if>
            start_time_ = #{startTime},
            end_time_ = #{endTime},
            <if test="changeOrderId != null">
                change_order_id_ = #{changeOrderId},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
            <if test="goodsCategoryId != null">
                goods_category_id_ = #{goodsCategoryId},
            </if>
            <if test="goodsCategoryName != null">
                goods_category_name_ = #{goodsCategoryName},
            </if>
            update_time = NOW(),
        </set>
        where id_ = #{id} and tenant_id_ = #{tenantId}
    </update>
    
    <update id="batchModify" parameterType="com.ym.mec.biz.dal.entity.StudentInstrument">
        <!--@mbg.generated-->
        <foreach collection="studentInstruments" item="studentInstrument" separator=";">
        update student_instrument
        <set>
            <if test="studentInstrument.studentId != null">
                student_id_ = #{studentInstrument.studentId},
            </if>
            <if test="studentInstrument.organId != null">
                organ_id_ = #{studentInstrument.organId},
            </if>
            <if test="studentInstrument.goodsId != null">
                goods_id_ = #{studentInstrument.goodsId},
            </if>
            <if test="studentInstrument.goodsName != null">
                goods_name_ = #{studentInstrument.goodsName},
            </if>
            <if test="studentInstrument.goodsBrand != null">
                goods_brand_ = #{studentInstrument.goodsBrand},
            </if>
            <if test="studentInstrument.specification != null">
                specification_ = #{studentInstrument.specification},
            </if>
            <if test="studentInstrument.goodsImg != null">
                goods_img_ = #{studentInstrument.goodsImg},
            </if>
            <if test="studentInstrument.status != null">
                status_ = #{studentInstrument.status},
            </if>
            <if test="studentInstrument.operation != null">
                operation_ = #{studentInstrument.operation},
            </if>
            start_time_ = #{studentInstrument.startTime},
            end_time_ = #{studentInstrument.endTime},
            <if test="studentInstrument.changeOrderId != null">
                change_order_id_ = #{studentInstrument.changeOrderId},
            </if>
            <if test="studentInstrument.createTime != null">
                create_time_ = #{studentInstrument.createTime},
            </if>
            <if test="studentInstrument.delFlag != null">
                del_flag_ = #{studentInstrument.delFlag},
            </if>
            <if test="studentInstrument.goodsCategoryId != null">
                goods_category_id_ = #{studentInstrument.goodsCategoryId},
            </if>
            <if test="studentInstrument.goodsCategoryName != null">
                goods_category_name_ = #{studentInstrument.goodsCategoryName},
            </if>
            update_time = NOW(),
        </set>
        where id_ = #{studentInstrument.id} and tenant_id_ = #{studentInstrument.tenantId}
        </foreach>
    </update>

    <!-- 全查询 -->
    <select id="findAll" resultMap="StudentInstrument">
        SELECT * FROM student where tenant_id_ = #{tenantId}
    </select>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="StudentInstrument" parameterType="map">
        SELECT si.*,su.username_ studentName,su.phone_ phone,o.name_ organName,sub.name_ subjectName FROM student_instrument si
        LEFT JOIN sys_user su ON su.id_ = si.student_id_
        left join student stu on stu.user_id_ = si.student_id_
        LEFT JOIN organization o ON o.id_ = si.organ_id_
        left join subject sub on find_in_set(sub.id_, stu.subject_id_list_)
        <include refid="queryPageSql"/>
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(si.id_)
        FROM student_instrument si
        LEFT JOIN sys_user su ON su.id_ = si.student_id_
        left join student stu on stu.user_id_ = si.student_id_
        <include refid="queryPageSql"/>
    </select>

    <sql id="queryPageSql">
        <where>
        	si.tenant_id_ = #{tenantId}
            <if test="studentId != null">
                AND si.student_id_ = #{studentId}
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(si.organ_id_,#{organId})
            </if>
            <if test="goodsId != null">
                AND si.goods_id_ = #{goodsId}
            </if>
            <if test="goodsCategoryId != null">
                AND si.goods_category_id_ = #{goodsCategoryId}
            </if>
            <if test="goodsBrand != null">
                AND si.goods_brand_ LIKE CONCAT('%',#{goodsBrand},'%')
            </if>
            <if test="specification != null">
                AND si.specification_ LIKE CONCAT('%',#{specification},'%')
            </if>
            <if test="status != null">
                AND si.status_ = #{status}
            </if>
            <if test="subjectId != null">
                AND find_in_set(#{subjectId}, stu.subject_id_list_)
            </if>
            <if test="search != null">
                AND (si.student_id_ = #{search} OR su.phone_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%'))
            </if>
            AND si.del_flag_ = 0
        </where>
    </sql>

    <insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into student_instrument
        (student_id_,organ_id_,goods_id_,goods_category_id_,goods_category_name_,goods_name_,goods_brand_,specification_,goods_img_,
        order_id_,change_order_id_,status_, start_time_, end_time_, create_time_, update_time,tenant_id_)
        VALUE
        <foreach collection="studentInstruments" item="item" separator=",">
            (#{item.studentId},#{item.organId},#{item.goodsId},#{item.goodsCategoryId},#{item.goodsCategoryName},#{item.goodsName},#{item.goodsBrand},#{item.specification},#{item.goodsImg},
            #{item.orderId},#{item.changeOrderId},#{item.status}, #{item.startTime},
            #{item.endTime}, NOW(), NOW(),#{item.tenantId})
        </foreach>
    </insert>

    <update id="batchUpdate">
   <![CDATA[
        UPDATE student_instrument
        SET status_ = 0
        WHERE status_ = 1
          AND end_time_ <= NOW()
        ]]>
    </update>
    <select id="getListByEndTime" resultMap="StudentInstrument">
        <![CDATA[
        SELECT *
        FROM student_instrument
        WHERE end_time_ >= #{startTime}
          AND end_time_ <= #{endTime}
        ]]>
    </select>

    <select id="getByOrderId" resultMap="StudentInstrument">
        SELECT *
        FROM student_instrument
        WHERE change_order_id_ = #{orderId}
    </select>

    <select id="getStudentInstrument" resultMap="StudentInstrument">
        SELECT *
        FROM student_instrument
        WHERE student_id_ = #{studentId}
          AND goods_id_ = #{goodsId}
          AND del_flag_ = 0
        ORDER BY id_ DESC
        LIMIT 1
    </select>

    <select id="getOldStudentInstrument" resultMap="StudentInstrument">
        SELECT spo.user_id_ student_id_,
        spo.organ_id_,
        g.id_ goods_id_,
        g.goods_category_id_,
        gc.name_ goods_category_name_,
        g.name_ goods_name_,
        g.brand_ goods_brand_,
        g.specification_,
        g.image_ goods_img_,
        spo.create_time_ start_time_,
        DATE_ADD(spo.create_time_, INTERVAL 1 YEAR) end_time_,
        spo.id_ order_id_,spod.tenant_id_
        FROM student_payment_order_detail spod
        LEFT JOIN student_payment_order spo ON spo.id_ = spod.payment_order_id_
        LEFT JOIN student_payment_order_detail spod2 ON spod2.payment_order_id_ = spo.id_
        LEFT JOIN goods g ON g.id_ = spod2.goods_id_list_
        LEFT JOIN goods_category gc on g.goods_category_id_ = gc.id_
        WHERE spo.status_ = 'SUCCESS'
        AND spod2.type_ = 'MUSICAL'
        AND (
            spod.goods_id_list_ IN
            <foreach collection="goodsIds" item="goodsId" open="(" close=")" separator=",">
                #{goodsId}
            </foreach>
            OR
        FIND_IN_SET('76', spod.goods_id_list_)
        )
        <if test="startTime != null ">
            AND spo.pay_time_ >= #{startTime}
        </if>
        ORDER BY spo.create_time_ ASC
    </select>

    <resultMap id="StudentInstrumentExportDto" type="com.ym.mec.biz.dal.dto.StudentInstrumentExportDto"
               extends="StudentInstrument">
        <result column="order_no_" property="orderNo"/>
        <result column="trans_no_" property="transNo"/>
        <result column="type_" property="type"/>
    </resultMap>

    <select id="getInstruments" resultMap="StudentInstrumentExportDto">
        SELECT si.*,spo.create_time_,o.name_ organName,su.username_
        studentName,spo.order_no_,spo.trans_no_,spod.is_renew_ type_,spo.id_
        FROM student_payment_order_detail spod
        LEFT JOIN student_payment_order spo ON spo.id_ = spod.payment_order_id_
        LEFT JOIN student_instrument si ON si.id_ = spod.student_instrument_id_
        LEFT JOIN organization o ON spo.organ_id_ = o.id_
        LEFT JOIN sys_user su ON su.id_ = si.student_id_
        WHERE spo.status_ = 'SUCCESS' AND spod.type_='MAINTENANCE' and spo.tenant_id_ = #{tenantId}
        <if test="startTime != null ">
            AND spo.create_time_ >= #{startTime}
        </if>
        <if test="endTime != null ">
            <![CDATA[AND spo.create_time_ <= #{endTime}]]>
        </if>
        <if test="organId != null">
            AND FIND_IN_SET(spo.organ_id_,#{organId})
        </if>
    </select>

    <select id="findStudentMusicGroup"
            resultMap="com.ym.mec.biz.dal.dao.StudentRegistrationDao.StudentRegistration">
        SELECT sr.id_, sr.user_id_, mg.name_ class_group_name_, su.real_name_ parents_name_, sr.music_group_status_
        FROM student_registration sr
                 LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
                 LEFT JOIN sys_user su ON su.id_ = mg.repair_user_id_
        WHERE sr.user_id_ = #{userId}
          AND sr.payment_status_ = 2
          AND mg.status_ IN ('PREPARE', 'PROGRESS', 'PAUSE', 'CLOSE')
        ORDER BY sr.id_ DESC
        LIMIT 1
    </select>

    <select id="getStudentsMaintenanceNum" resultMap="StudentInstrument">
        SELECT student_id_,COUNT(*) goods_id_
        FROM student_instrument
        WHERE order_id_ IN (
        SELECT id_
        FROM student_payment_order
        WHERE type_ = 'APPLY'
        AND music_group_id_ = #{musicGroupId}
        AND status_ = 'SUCCESS'
        AND user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        )
        AND student_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        AND del_flag_ = 0 AND end_time_ > NOW()
        GROUP BY student_id_
    </select>

    <select id="getStudentMaintenance" resultMap="StudentInstrument">
        SELECT * FROM student_instrument
        WHERE order_id_ in (
        SELECT id_
        FROM student_payment_order
        WHERE type_ = 'APPLY'
        AND music_group_id_ = #{musicGroupId}
        AND status_ = 'SUCCESS'
        AND user_id_ = #{studentId}
        )
        AND student_id_ = #{studentId} AND del_flag_ = 0 AND end_time_ > NOW()
    </select>

    <select id="queryById" resultMap="StudentInstrument">
        SELECT * FROM student_instrument
        where id_ IN
        <foreach collection="idList" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

</mapper>