<?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.zhongzheng.modules.user.mapper.UserPlanGoodsMapper">

    <resultMap type="com.zhongzheng.modules.user.domain.UserPlanGoods" id="UserPlanGoodsResult">
        <result property="planId" column="plan_id"/>
        <result property="sourceId" column="source_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="id" column="id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsVo" id="GoodsResultVo">
        <result property="goodsId" column="goods_id"/>
        <result property="year" column="year"/>
        <result property="supplyId" column="supply_id"/>
        <result property="goodsType" column="goods_type"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="majorId" column="major_id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="standPrice" column="stand_price"/>
        <result property="lowestPrice" column="lowest_price"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="validityStartTime" column="validity_start_time"/>
        <result property="validityEndTime" column="validity_end_time"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="studyEndTime" column="study_end_time"/>
        <result property="certificateIds" column="certificate_ids"/>
        <result property="introduce" column="introduce"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="buyNote" column="buy_note"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="goodsStatus" column="goods_status"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="classHours" column="class_hours"/>
        <result property="standPriceJson" column="stand_price_json"/>
        <result property="code" column="code"/>
        <result property="projectId" column="project_id"/>
        <result property="goodsAuditionConfig" column="goods_audition_config"/>
        <result property="goodsPhotographConfig" column="goods_photograph_config"/>
        <result property="goodsAuditionExamConfig" column="goods_audition_exam_config"/>
        <result property="goodsPlayConfig" column="goods_play_config"/>
        <result property="goodsExamConfig" column="goods_exam_config"/>

        <result property="supplyName" column="supply_name"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="schoolName" column="school_name"/>
        <result property="categoryName" column="category_name"/>
        <result property="handoutsId" column="handouts_id"/>
        <result property="templateType" column="template_type"/>
        <result property="period" column="period"/>
        <result property="makeStartTime" column="make_start_time"/>
        <result property="makeEndTime" column="make_end_time"/>
        <result property="studyCount" column="study_count"/>
        <result property="makeGoodsName" column="make_goods_name"/>
        <result property="makeGoodsCode" column="make_goods_code"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsUserVo" id="GoodsUserVo">
        <result property="goodsId" column="goods_id"/>
        <result property="year" column="year"/>
        <result property="supplyId" column="supply_id"/>
        <result property="gradeId" column="gradeId"/>
        <result property="userId" column="user_id"/>
        <result property="goodsType" column="goods_type"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="majorId" column="major_id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="standPrice" column="stand_price"/>
        <result property="lowestPrice" column="lowest_price"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="validityStartTime" column="validity_start_time"/>
        <result property="validityEndTime" column="validity_end_time"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="studyEndTime" column="study_end_time"/>
        <result property="certificateIds" column="certificate_ids"/>
        <result property="introduce" column="introduce"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="buyNote" column="buy_note"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="goodsStatus" column="goods_status"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="classHours" column="class_hours"/>
        <result property="standPriceJson" column="stand_price_json"/>
        <result property="code" column="code"/>
        <result property="projectId" column="project_id"/>
        <result property="goodsAuditionConfig" column="goods_audition_config"/>
        <result property="goodsPhotographConfig" column="goods_photograph_config"/>
        <result property="goodsPlayConfig" column="goods_play_config"/>
        <result property="goodsExamConfig" column="goods_exam_config"/>
        <result property="gradeId" column="grade_id"/>

        <result property="supplyName" column="supply_name"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="schoolName" column="school_name"/>
        <result property="categoryName" column="category_name"/>
        <result property="handoutsId" column="handouts_id"/>
        <result property="templateType" column="template_type"/>
        <result property="period" column="period"/>
        <result property="makeStartTime" column="make_start_time"/>
        <result property="makeEndTime" column="make_end_time"/>
        <result property="studyCount" column="study_count"/>
        <result property="makeGoodsName" column="make_goods_name"/>
        <result property="makeGoodsCode" column="make_goods_code"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="beforeStatus" column="before_status"/>
        <result property="applyName" column="apply_name"/>
        <result property="beforeName" column="before_name"/>
        <result property="courseNum" column="course_num"/>
    </resultMap>

    <select id="selectGoods" parameterType="long"  resultMap="GoodsUserVo">
        SELECT
            g.*,
            (SELECT cet.education_name FROM  course_education_type cet  where cet.id = g.education_type_id) as education_name,
            (SELECT cet.project_name FROM  course_project_type cet where cet.id = g.project_id) as project_name,
            (SELECT cet.business_name FROM  course_business cet  where cet.id = g.business_id) as business_name,
            ( SELECT
                  cgu.grade_id
              FROM
                  class_grade_user cgu
                      LEFT JOIN class_grade_goods cgg on cgu.grade_id = cgg.grade_id
                      LEFT JOIN class_grade cg on cg.grade_id = cgg.grade_id
              WHERE
                  cgu.user_id = up.user_id
                and cgg.goods_id = upg.source_id
                and cg.`status` = 1
                and cgu.`status` = 1
              ORDER BY cgu.create_time desc
                                                                                                               LIMIT 1) as gradeId
        FROM
            goods g
            LEFT JOIN user_plan_goods upg on g.goods_id = upg.source_id
            LEFT JOIN user_plan up on upg.plan_id = up.plan_id
        WHERE
            1=1
          and upg.plan_id=#{planId}
          and g.goods_type =1
    </select>
</mapper>
