<?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.PlatformProductDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.PlatformProduct">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="name_" jdbcType="VARCHAR" property="name"/>
        <result column="menu_id_" jdbcType="VARCHAR" property="menuId"/>
        <result column="remark_" jdbcType="VARCHAR" property="remark"/>
        <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_, menu_id_, remark_, 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>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete
        from platform_product
        where id_ = #{id,jdbcType=INTEGER}
    </delete>

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

    <insert id="insertSelective" parameterType="com.ym.mec.biz.dal.entity.PlatformProduct">
        insert into platform_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">
                name_,
            </if>
            <if test="menuId != null">
                menu_id_,
            </if>
            <if test="remark != null">
                remark_,
            </if>
            <if test="deleted != null">
                deleted_,
            </if>
            <if test="createdBy != null">
                created_by_,
            </if>
            <if test="createdTime != null">
                created_time_,
            </if>
            <if test="updatedBy != null">
                updated_by_,
            </if>
            <if test="updatedTime != null">
                updated_time_,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="menuId != null">
                #{menuId,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="deleted != null">
                #{deleted,jdbcType=INTEGER},
            </if>
            <if test="createdBy != null">
                #{createdBy,jdbcType=VARCHAR},
            </if>
            <if test="createdTime != null">
                #{createdTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updatedBy != null">
                #{updatedBy,jdbcType=VARCHAR},
            </if>
            <if test="updatedTime != null">
                #{updatedTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ym.mec.biz.dal.entity.PlatformProduct">
        update platform_product
        <set>
            <if test="name != null">
                name_ = #{name,jdbcType=VARCHAR},
            </if>
            <if test="menuId != null">
                menu_id_ = #{menuId,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                remark_ = #{remark,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" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from platform_product
        <where>
            deleted_ = 0
            <if test="search != null">
              and name_ like CONCAT('%',#{search},'%')
            </if>
        </where>
    </select>

    <select id="queryMenuIdList" parameterType="integer" resultType="string">
        SELECT
            b.`menu_id_`
        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
          AND a.`id_` = #{serveId}
    </select>

    <select id="queryTenantByMultiId" parameterType="integer" resultType="map">
        SELECT
            a.id_ as tenantId,
            a.`user_id_` as userId
        FROM
        tenant_info AS a
                JOIN `tenant_product_info` AS b
                     ON a.`id_` = b.`tenant_id_` and b.using_ = 0
                JOIN
            (SELECT
                 a.id_ AS serveId,
                 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="productId != null">
                     and b.`id_` = #{productId}
                 </if>
                 <if test="serveId != null">
                     and a.id_ = #{serveId}
                 </if>
             GROUP BY a.`id_`) AS c
            ON b.`serve_id_` = c.serveId
        WHERE a.`pay_state_` = 1
            and a.user_id_ is not null
        <if test="tenantId != null">
            and a.id_ = #{tenantId}
        </if>
    </select>

    <insert id="batchInsertRoleMenu">
        INSERT INTO sys_role_menu (role_id_,menu_id_,tenant_id_) VALUES
        <foreach collection="menuIds" item="item" index="index" separator=",">
            (#{roleId}, #{item},#{tenantId})
        </foreach>
        ON DUPLICATE KEY UPDATE
        role_id_ = VALUES(role_id_),
        menu_id_ = VALUES(menu_id_),
        tenant_id_ = VALUES(tenant_id_)
    </insert>

</mapper>