<?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.pcloud.book.applet.dao.impl.AppletResourceApplyDaoImpl">

    <resultMap type="com.pcloud.book.applet.entity.AppletResourceApply" id="AppletResourceApplyMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="targetId" column="target_id" jdbcType="INTEGER"/>
        <result property="targetType" column="target_type" jdbcType="VARCHAR"/>
        <result property="crowdType" column="crowd_type" jdbcType="INTEGER"/>
        <result property="contentType" column="content_type" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="AppletResourceApplyMap">
        select
          id, target_id, target_type, crowd_type, content_type, create_time
        from book.applet_resource_apply
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_resource_apply(target_id, target_type, crowd_type, content_type, create_time)
        values (#{targetId}, #{targetType}, #{crowdType}, #{contentType}, now())
    </insert>

    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_resource_apply(target_id, target_type, crowd_type, content_type, create_time)
        values
        <foreach collection="list" separator="," index="index" item="item">
            (#{item.targetId}, #{item.targetType}, #{item.crowdType}, #{item.contentType}, now())
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.applet_resource_apply
        <set>
            <if test="targetId != null">
                target_id = #{targetId},
            </if>
            <if test="targetType != null and targetType != ''">
                target_type = #{targetType},
            </if>
            <if test="crowdType != null">
                crowd_type = #{crowdType},
            </if>
            <if test="contentType != null">
                content_type = #{contentType},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from book.applet_resource_apply where id = #{id}
    </delete>

    <delete id="deleteCrowdType" parameterType="map">
         delete from book.applet_resource_apply
         where
         target_id = #{targetId}
         and target_type = #{targetType}
         and crowd_type>0
    </delete>

    <delete id="deleteContentType" parameterType="map">
        delete from book.applet_resource_apply
        where
        target_id = #{targetId}
        and target_type = #{targetType}
        and content_type>0
    </delete>

    <select id="getCrowdTypeList" parameterType="map" resultType="integer">
        SELECT DISTINCT
            crowd_type
        FROM
            applet_resource_apply
        WHERE
            target_id = #{targetId}
        AND target_type = #{targetType}
        AND crowd_type > 0
    </select>

    <select id="getContentTypeList" parameterType="map" resultType="integer">
        SELECT DISTINCT
            content_type
        FROM
            applet_resource_apply
        WHERE
            target_id = #{targetId}
        AND target_type = #{targetType}
        AND content_type > 0
    </select>

</mapper>