<?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.ruoyi.generator.mapper.SourceProjectMapper">

    <resultMap type="SourceProject" id="SourceProjectResult">
        <id property="projectId" column="project_id" />
        <result property="slug" column="slug" />
        <result property="projectName" column="project_name" />
        <result property="category" column="category" />
        <result property="coverText" column="cover_text" />
        <result property="summary" column="summary" />
        <result property="descriptionMd" column="description_md" />
        <result property="price" column="price" />
        <result property="viewCount" column="view_count" />
        <result property="tags" column="tags" />
        <result property="modules" column="modules" />
        <result property="scenes" column="scenes" />
        <result property="resourceName" column="resource_name" />
        <result property="resourceUrl" column="resource_url" />
        <result property="resourceVersion" column="resource_version" />
        <result property="resourceSize" column="resource_size" />
        <result property="sort" column="sort" />
        <result property="status" column="status" />
        <result property="delFlag" column="del_flag" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
        <result property="remark" column="remark" />
    </resultMap>

    <sql id="selectSourceProjectVo">
        select project_id, slug, project_name, category, cover_text, summary, description_md, price, view_count,
               tags, modules, scenes, resource_name, resource_url, resource_version, resource_size, sort, status,
               del_flag, create_by, create_time, update_by, update_time, remark
        from source_project
    </sql>

    <select id="selectSourceProjectByProjectId" parameterType="Long" resultMap="SourceProjectResult">
        <include refid="selectSourceProjectVo"/>
        where project_id = #{projectId} and del_flag = '0'
    </select>

    <select id="selectSourceProjectBySlug" parameterType="String" resultMap="SourceProjectResult">
        <include refid="selectSourceProjectVo"/>
        where slug = #{slug} and del_flag = '0'
    </select>

    <select id="selectSourceProjectList" parameterType="SourceProject" resultMap="SourceProjectResult">
        <include refid="selectSourceProjectVo"/>
        <where>
            and del_flag = '0'
            <if test="projectName != null and projectName != ''"> and project_name like concat('%', #{projectName}, '%')</if>
            <if test="slug != null and slug != ''"> and slug like concat('%', #{slug}, '%')</if>
            <if test="category != null and category != '' and category != 'all'"> and category = #{category}</if>
            <if test="status != null and status != ''"> and status = #{status}</if>
            <if test="keyword != null and keyword != ''">
                and (
                    project_name like concat('%', #{keyword}, '%')
                    or summary like concat('%', #{keyword}, '%')
                    or tags like concat('%', #{keyword}, '%')
                    or modules like concat('%', #{keyword}, '%')
                    or scenes like concat('%', #{keyword}, '%')
                )
            </if>
        </where>
        order by sort asc, update_time desc, create_time desc
    </select>

    <select id="selectPublicSourceProjectList" parameterType="SourceProject" resultMap="SourceProjectResult">
        <include refid="selectSourceProjectVo"/>
        <where>
            and del_flag = '0'
            and status = '0'
            <if test="category != null and category != '' and category != 'all'"> and category = #{category}</if>
            <if test="keyword != null and keyword != ''">
                and (
                    project_name like concat('%', #{keyword}, '%')
                    or summary like concat('%', #{keyword}, '%')
                    or tags like concat('%', #{keyword}, '%')
                    or modules like concat('%', #{keyword}, '%')
                    or scenes like concat('%', #{keyword}, '%')
                )
            </if>
        </where>
        order by sort asc, update_time desc, create_time desc
    </select>

    <insert id="insertSourceProject" parameterType="SourceProject" useGeneratedKeys="true" keyProperty="projectId">
        insert into source_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            slug,
            project_name,
            <if test="category != null">category,</if>
            <if test="coverText != null">cover_text,</if>
            <if test="summary != null">summary,</if>
            <if test="descriptionMd != null">description_md,</if>
            <if test="price != null">price,</if>
            <if test="viewCount != null">view_count,</if>
            <if test="tags != null">tags,</if>
            <if test="modules != null">modules,</if>
            <if test="scenes != null">scenes,</if>
            <if test="resourceName != null">resource_name,</if>
            <if test="resourceUrl != null">resource_url,</if>
            <if test="resourceVersion != null">resource_version,</if>
            <if test="resourceSize != null">resource_size,</if>
            <if test="sort != null">sort,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{slug},
            #{projectName},
            <if test="category != null">#{category},</if>
            <if test="coverText != null">#{coverText},</if>
            <if test="summary != null">#{summary},</if>
            <if test="descriptionMd != null">#{descriptionMd},</if>
            <if test="price != null">#{price},</if>
            <if test="viewCount != null">#{viewCount},</if>
            <if test="tags != null">#{tags},</if>
            <if test="modules != null">#{modules},</if>
            <if test="scenes != null">#{scenes},</if>
            <if test="resourceName != null">#{resourceName},</if>
            <if test="resourceUrl != null">#{resourceUrl},</if>
            <if test="resourceVersion != null">#{resourceVersion},</if>
            <if test="resourceSize != null">#{resourceSize},</if>
            <if test="sort != null">#{sort},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateSourceProject" parameterType="SourceProject">
        update source_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="slug != null">slug = #{slug},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="category != null">category = #{category},</if>
            <if test="coverText != null">cover_text = #{coverText},</if>
            <if test="summary != null">summary = #{summary},</if>
            <if test="descriptionMd != null">description_md = #{descriptionMd},</if>
            <if test="price != null">price = #{price},</if>
            <if test="viewCount != null">view_count = #{viewCount},</if>
            <if test="tags != null">tags = #{tags},</if>
            <if test="modules != null">modules = #{modules},</if>
            <if test="scenes != null">scenes = #{scenes},</if>
            <if test="resourceName != null">resource_name = #{resourceName},</if>
            <if test="resourceUrl != null">resource_url = #{resourceUrl},</if>
            <if test="resourceVersion != null">resource_version = #{resourceVersion},</if>
            <if test="resourceSize != null">resource_size = #{resourceSize},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where project_id = #{projectId}
    </update>

    <update id="deleteSourceProjectByProjectIds" parameterType="Long">
        update source_project set del_flag = '2', update_time = sysdate()
        where project_id in
        <foreach item="projectId" collection="array" open="(" separator="," close=")">
            #{projectId}
        </foreach>
    </update>

    <update id="changeSourceProjectStatus" parameterType="SourceProject">
        update source_project
        set status = #{status}, update_time = #{updateTime}
        where project_id = #{projectId}
    </update>

    <update id="increaseViewCount" parameterType="Long">
        update source_project set view_count = ifnull(view_count, 0) + 1 where project_id = #{projectId}
    </update>
</mapper>
