<?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.PlatformServeDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.PlatformServe">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="name_" jdbcType="VARCHAR" property="name"/>
        <result column="product_id_" jdbcType="VARCHAR" property="productId"/>
        <result column="deleted_" jdbcType="INTEGER" property="deleted"/>
        <result column="created_by_" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time_" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="updated_by_" jdbcType="INTEGER" property="updatedBy"/>
        <result column="updated_time_" jdbcType="TIMESTAMP" property="updatedTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id_
        , name_, product_id_, deleted_, created_by_, created_time_, updated_by_,
    updated_time_
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from platform_product
        where id_ = #{id,jdbcType=INTEGER}
    </select>

    <insert id="insert" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.PlatformServe">
        insert into platform_serve (name_, product_id_, deleted_,
                                    created_by_, created_time_, updated_by_,
                                    updated_time_)
        values (#{name,jdbcType=VARCHAR}, #{productId,jdbcType=VARCHAR}, #{deleted,jdbcType=INTEGER},
                #{createdBy,jdbcType=INTEGER}, #{createdTime,jdbcType=TIMESTAMP}, #{updatedBy,jdbcType=INTEGER},
                #{updatedTime,jdbcType=TIMESTAMP})
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ym.mec.biz.dal.entity.PlatformServe">
        update platform_serve
        <set>
            <if test="name != null">
                name_ = #{name,jdbcType=VARCHAR},
            </if>
            <if test="productId != null">
                product_id_ = #{productId,jdbcType=VARCHAR},
            </if>
            <if test="deleted != null">
                deleted_ = #{deleted,jdbcType=INTEGER},
            </if>
            <if test="createdBy != null">
                created_by_ = #{createdBy,jdbcType=VARCHAR},
            </if>
            <if test="createdTime != null">
                created_time_ = #{createdTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updatedBy != null">
                updated_by_ = #{updatedBy,jdbcType=VARCHAR},
            </if>
            <if test="updatedTime != null">
                updated_time_ = #{updatedTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id_ = #{id,jdbcType=INTEGER}
    </update>

    <select id="queryPage" parameterType="object" resultType="com.ym.mec.biz.dal.vo.PlatformServePageVo">
        SELECT
        a.id_ AS id,
        a.`name_` AS serveName,
        GROUP_CONCAT(b.`name_`) AS productName
        FROM
        `platform_serve` AS a
        JOIN `platform_product` AS b
        ON FIND_IN_SET(b.`id_`, a.`product_id_`)
        <where>
            a.deleted_ = 0 and b.deleted_ = 0
            <if test="search != null">
               and a.`name_` like CONCAT('%',#{search},'%')
            </if>
            <if test="id != null">
                and a.id_ = #{id}
            </if>
        </where>
        GROUP BY a.`id_`
    </select>

    <resultMap id="PlatformServeInfoVoResult" type="com.ym.mec.biz.dal.vo.PlatformServeInfoVo">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="name_" jdbcType="VARCHAR" property="name"/>
        <result column="product_id_" jdbcType="VARCHAR" property="productId"/>
        <collection property="detailList" ofType="com.ym.mec.biz.dal.entity.PlatformServeDetail">
            <result column="bId" jdbcType="INTEGER" property="id" />
            <result column="serve_id_" jdbcType="INTEGER" property="serveId" />
            <result column="mode_" jdbcType="VARCHAR" property="mode" />
            <result column="student_up_limit_" jdbcType="INTEGER" property="studentUpLimit" />
            <result column="original_price_" jdbcType="DECIMAL" property="originalPrice" />
        </collection>
    </resultMap>

    <select id="queryInfo" resultMap="PlatformServeInfoVoResult">
        SELECT
            a.id_,
            a.name_,
            a.product_id_,
            b.id_ as bId,
            b.serve_id_,
            b.`mode_`,
            b.`student_up_limit_`,
            b.`original_price_`
        FROM
            `platform_serve` AS a
                LEFT JOIN `platform_serve_detail` AS b
                          ON a.`id_` = b.`serve_id_`
        WHERE a.deleted_ = 0 and a.id_ = #{id}
    </select>


</mapper>