<?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.yida.data.school.transaction.mapper.EduProductMapper">
    <resultMap id="productMap" type="com.yida.data.common.core.entity.apply.EduProduct">
        <result column="product_id" property="productId"/>
        <result column="product_name" property="productName"/>
        <result column="product_code" property="productCode"/>
        <result column="price" property="price"/>
        <result column="putaway_date" property="putawayDate"/>
        <result column="pay_way" property="payWay"/>
        <result column="product_model" property="productModel"/>
        <result column="product_description" property="productDescription"/>
        <result column="discount_price" property="discountPrice"/>
        <result column="stock" property="stock"/>
        <result column="discount_family_type" property="discountFamilyType"/>
        <result column="area_id" property="areaId"/>
        <result column="school_id" property="schoolId"/>
        <result column="school_name" property="schoolName"/>
        <result column="status" property="status"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_id" property="createId"/>
        <result column="create_date" property="createDate"/>
        <result column="update_id" property="updateId"/>
        <result column="update_date" property="updateDate"/>
        <result column="province_id" property="provinceId"/>
        <result column="city_id" property="cityId"/>
        <result column="county_id" property="countyId"/>
        <result column="village_id" property="villageId"/>
        <collection property="applyList"
                    ofType="com.yida.data.common.core.entity.apply.EduProductApply">
            <result property="applyId" column="apply_id"/>
            <result property="applyName" column="apply_name"/>
            <result column="apply_code" property="applyCode"/>
            <result column="begin_valid_date" property="beginValidDate"/>
            <result column="end_valid_date" property="endValidDate"/>
        </collection>
        <collection property="realProductList" ofType="com.yida.data.common.core.entity.apply.EduRealProduct">
            <id property="id" column="real_id"/>
            <result property="firstProductCategory" column="first_product_category"/>
            <result property="secondProductCategory" column="second_product_category"/>
            <result property="productName" column="real_name"/>
            <result property="productType" column="product_type"/>
            <result property="productValue" column="product_value"/>
            <result property="service" column="service"/>
        </collection>
    </resultMap>

    <resultMap id="productWithStudentMap" type="com.yida.data.school.vo.transcation.ProductWithStudentVO">
        <result property="productName" column="product_name"/>
        <result property="productId" column="product_id"/>
        <result property="beginValidDate" column="begin_valid_date"/>
        <result property="endValidDate" column="end_valid_date"/>
        <collection property="studentList" ofType="com.yida.data.common.core.entity.user.EduStudent">
            <result column="student_id" property="id"/>
            <result column="student_name" property="stuName"/>
            <result column="grade_name" property="gradeName"/>
            <result column="class_name" property="className"/>
        </collection>
    </resultMap>
    <select id="listUsingProduct" parameterType="com.yida.data.common.core.entity.system.SystemUser"
            resultMap="productWithStudentMap">
        SELECT DISTINCT
        distinct
        a.product_id,
        a.product_name,
        a.create_date,
        c.student_id,
        c.student_name,
        c.grade_name,
        c.class_name,
        d.begin_valid_date,
        d.end_valid_date
        FROM
        edu_product a
        LEFT JOIN edu_apply_product_order b ON a.product_id =
        b.product_id
        left join edu_apply_product_order_student c on b.order_id=c.order_id
        left join (SELECT DISTINCT
        product_id,
        begin_valid_date,
        end_valid_date
        FROM
        `edu_product_apply`
        WHERE
        del_flag =0) d on d.product_id = a.product_id
        WHERE
        a.status = 1
        and b.del_flag=0
        and a.del_flag = 0
        and a.school_id = #{schoolId}
        and c.student_id in
        <foreach collection="studentIds" item="item" close=")" open="(" separator=",">
            #{item}
        </foreach>
        and b.order_status=2
        and b.pay_status=1
        ORDER BY
        a.create_date DESC
    </select>

    <select id="student" resultType="com.yida.data.common.core.entity.apply.EduApplyProductOrderStudent">
        select
        b.student_name,
        b.student_id
        from edu_apply_product_order a
        left join edu_apply_product_order_student b on a.order_id = b.order_id
        where
        a.product_id = #{param1}
        and a.phone = #{param2}
        and a.pay_status=1
        and a.order_status=2
        and del_flag = 0
    </select>
    <select id="selectProduct" parameterType="com.yida.data.common.core.entity.system.SystemUser"
            resultType="com.yida.data.common.core.entity.apply.EduProduct">
        SELECT
        a.*,
        d.end_valid_date,
        d.begin_valid_date
        FROM
        edu_product a
        left join (SELECT DISTINCT
        product_id,
        begin_valid_date,
        end_valid_date
        FROM
        `edu_product_apply`
        WHERE
        del_flag =0) d on d.product_id = a.product_id
        WHERE
        a.product_id NOT IN (
        SELECT
        edu_product.product_id
        FROM
        edu_product
        LEFT JOIN edu_apply_product_order ON edu_product.product_id = edu_apply_product_order.product_id
        left join edu_apply_product_order_student d on edu_apply_product_order.order_id=d.order_id
        WHERE
        edu_product.del_flag=0
        and edu_product.status=1
        and edu_apply_product_order.order_status=2
        and edu_apply_product_order.pay_status=1
        and d.student_id in
        <foreach collection="studentIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        )
        and a.del_flag = 0
        and a.school_id = #{schoolId}
        and a.status = 1
        ORDER BY
        a.create_date DESC
    </select>

    <select id="selectProductAndApply" parameterType="java.lang.Long"
            resultType="com.yida.data.common.core.entity.apply.EduProduct">
        SELECT
        a.product_id,
        a.price,
        a.product_name,
        a.product_description,
        GROUP_CONCAT( c.apply_name ) AS apply_name,
        d.begin_valid_date,
        d.end_valid_date
        FROM
        edu_product a
        LEFT JOIN edu_product_apply b ON a.product_id = b.product_id
        LEFT JOIN edu_apply c ON c.id = b.apply_id
        LEFT JOIN ( SELECT DISTINCT product_id, begin_valid_date, end_valid_date FROM `edu_product_apply` WHERE del_flag
        = 0 ) d ON d.product_id = a.product_id WHERE
        a.product_id = #{productId}
        GROUP BY
        a.product_id,
        d.begin_valid_date,
        d.end_valid_date
    </select>


    <select id="selectProductPageList" parameterType="com.yida.data.school.dto.transaction.ProductSelectDTO"
            resultType="com.yida.data.common.core.entity.apply.EduProduct">
        SELECT
        distinct
        a.product_id,
        a.product_name,
        a.stock,
        a.price,
        a.school_name,
        a.`status`,
        a.create_id,
        a.create_date,
        a.school_id,
        f.end_valid_date
        FROM `edu_product` a
        LEFT JOIN edu_product_apply b on a.product_id = b.product_id
        left join edu_product_real_product d on a.product_id=d.product_id
        left join edu_real_product e on d.real_product_id=e.id
        LEFT JOIN edu_product_apply f on a.product_id = f.product_id AND f.del_flag = 0
        WHERE a.del_flag = '0'
        and a.school_id in
        <foreach collection="schoolIds" item="schoolId" open="(" close=")" separator=",">
            #{schoolId}
        </foreach>
        <if test="dto.startPrice != null">
            and a.price <![CDATA[>=]]> #{dto.startPrice}
        </if>
        <if test="dto.endPrice != null">
            and a.price <![CDATA[<=]]> #{dto.endPrice}
        </if>
        <if test="dto.firstCategory != null and dto.firstCategory != ''">
            <choose>
                <when test="dto.firstCategory == 'apply'">
                    and b.id is not null
                    <if test="dto.secondCategory != null and dto.secondCategory != ''">
                        and b.apply_code=#{dto.secondCategory}
                    </if>
                </when>
                <otherwise>
                    and e.first_product_category=#{dto.firstCategory}
                    <if test="dto.secondCategory != null and dto.secondCategory != ''">
                        and e.second_product_category=#{dto.secondCategory}
                    </if>
                </otherwise>
            </choose>
        </if>
        <if test="dto.status != null">
            and a.`status` = #{dto.status}
        </if>

        <if test="dto.name != null and dto.name != ''">
            and a.product_name like concat('%',#{dto.name},'%')
        </if>
        order by a.create_date desc
    </select>

    <select id="updateProductStock">
        update edu_product
        set stock=stock-#{num}
        where product_id=#{productId} and stock-#{num}<![CDATA[>=]]>0
    </select>

    <select id="getInfo" resultMap="productMap">
        select a.*,
        b.apply_id apply_id,
        b.apply_code,
        b.begin_valid_date,
        b.end_valid_date,
        c.apply_name,
        e.id real_id,
        e.service,
        e.product_type,
        e.product_value,
        e.product_name real_name,
        e.first_product_category,
        e.second_product_category
        from edu_product a
        left join (select * from edu_product_apply where del_flag=0) b on a.product_id =
        b.product_id
        left join edu_apply c on b.apply_id=c.id
        left join edu_product_real_product d on a.product_id = d.product_id
        left join (select * from edu_real_product where del_flag=0) e on d.real_product_id = e.id
        where a.del_flag=0 and a.product_id=#{id}
    </select>

    <select id="listOneCategoryProduct"
            resultType="com.yida.data.common.core.entity.apply.EduProduct">
        SELECT a.*
        FROM edu_product a
        LEFT JOIN (SELECT count(1) count, product_id
        FROM edu_product_real_product
        GROUP BY product_id) b ON a.product_id = b.product_id
        left join edu_product_real_product d on a.product_id = d.product_id
        LEFT JOIN edu_real_product c ON d.real_product_id = c.id
        WHERE a.del_flag = 0
        and a.status=1
        and a.school_id=#{schoolId}
        and c.first_product_category= #{firstCatrgory}
        and c.second_product_category=#{secondCategory}
        and b.count = 1
    </select>

    <select id="listBuyStudent" parameterType="com.yida.data.school.dto.transaction.ListBuyStudentDTO"
            resultType="com.yida.data.school.vo.transcation.EduProductNoticeVO">
        SELECT DISTINCT
        a.section_name,
        a.student_id,
        a.campus_name,
        a.grade_name,
        a.class_name,
        a.student_number,
        a.student_name,
        <if test="dto.dateTime != null">
            d.end_valid_date,
        </if>
        e.put_date,
        e.product_id
        FROM
        edu_apply_product_order_student a
        LEFT JOIN edu_apply_product_order b ON a.order_id = b.order_id
        LEFT JOIN edu_product c ON b.product_id = c.product_id
        <if test="dto.dateTime != null">
            LEFT JOIN ( SELECT student_id, MIN( end_time ) AS end_valid_date FROM edu_student_apply WHERE del_flag = 0
            AND end_time >
            #{dto.dateTime}
            GROUP BY student_id ) d ON a.student_id = d.student_id
        </if>
        LEFT JOIN (SELECT product_id, MAX(create_date) AS put_date FROM edu_product_notice_student GROUP BY
        product_id) e ON e.product_id = c.product_id
        WHERE
        c.product_id = #{dto.productId}
        and b.order_status = 2

        <if test="dto.campusId != null">
            and a.campus_id = #{dto.campusId}
        </if>
        <if test="dto.sectionId != null">
            and a.section_id =#{dto.sectionId}
        </if>
        <if test="dto.schoolId != null">
            and a.school_id =#{dto.schoolId}
        </if>
        <if test="dto.gradeId != null">
            and a.section_id = #{dto.sectionId}
        </if>
        <if test="dto.classId != null">
            and a.class_id = #{dto.classId}
        </if>
        <if test="dto.studentName != null and dto.studentName != ''">
            and a.student_name like concat('%',#{dto.studentName},'%')
        </if>
        <if test="dto.stuNum != null and dto.stuNum != ''">
            and a.student_number like concat('%',#{dto.stuNum},'%')
        </if>
    </select>

    <select id="getMaxTime" resultType="com.yida.data.school.vo.transcation.EduProductNoticeVO">
        SELECT
        student_id,
        MAX(create_date) as put_date
        FROM
        edu_product_notice_student
        WHERE 1=1
        <if test="notBuyId != null and notBuyId.size() > 0">
            and student_id IN
            <foreach collection="notBuyId" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        and product_id = #{productId}
        GROUP BY student_id
    </select>

    <select id="getMinTime" resultType="java.time.LocalDateTime">
        SELECT
        end_valid_date as date
        FROM
        edu_product_apply
        WHERE
        product_id = #{productId}
        AND del_flag = 0
        ORDER BY end_valid_date
    </select>

    <select id="productMonitor">
    </select>
    <select id="findNotBuyStudent" resultType="java.lang.Long">
        SELECT
        DISTINCT
        b.student_id
        FROM
        edu_apply_product_order a
        LEFT JOIN edu_apply_product_order_student b on a.order_id = b.order_id
        WHERE
        a.order_status = 2
        and a.product_id = #{productId}
        AND b.student_id in
        <foreach collection="studentIds" separator="," close=")" open="(" item="item">
            #{item}
        </foreach>
    </select>
</mapper>
