<?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.web.modules.prosperity.mapper.BizCommonProsperityProjectMapper">

    <resultMap type="com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject" id="BizCommonProsperityProjectResult">
        <id     property="id"                column="id"/>
        <result property="projectName"       column="project_name"/>
        <result property="projectType"       column="project_type"/>
        <result property="introduction"      column="introduction"/>
        <result property="projectContent"    column="project_content"/>
        <result property="investmentAmount"  column="investment_amount"/>
        <result property="implYear"          column="impl_year"/>
        <result property="implStatus"        column="impl_status"/>
        <result property="videoFileUrl"      column="video_file_url"/>
        <result property="videoFilePath"     column="video_file_path"/>
        <result property="coverFileUrl"      column="cover_file_url"/>
        <result property="coverFilePath"     column="cover_file_path"/>
        <result property="publishStatus"     column="publish_status"/>
        <result property="publishTime"       column="publish_time"/>
        <result property="offlineTime"       column="offline_time"/>
        <result property="opImageCount"      column="op_image_count"/>
        <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="selectVo">
        select p.id, p.project_name, p.project_type, p.introduction, p.project_content,
               p.investment_amount, p.impl_year, p.impl_status,
               p.video_file_url, p.video_file_path, p.cover_file_url, p.cover_file_path,
               p.publish_status, p.publish_time, p.offline_time,
               p.create_by, p.create_time, p.update_by, p.update_time, p.remark,
               (select count(*) from biz_common_prosperity_project_op_image o where o.project_id = p.id) as op_image_count
        from biz_common_prosperity_project p
    </sql>

    <sql id="selectVoById">
        select id, project_name, project_type, introduction, project_content,
               investment_amount, impl_year, impl_status,
               video_file_url, video_file_path, cover_file_url, cover_file_path,
               publish_status, publish_time, offline_time,
               create_by, create_time, update_by, update_time, remark
        from biz_common_prosperity_project
    </sql>

    <select id="selectBizCommonProsperityProjectById" parameterType="long" resultMap="BizCommonProsperityProjectResult">
        <include refid="selectVoById"/>
        where id = #{id}
    </select>

    <select id="selectBizCommonProsperityProjectList" parameterType="com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject" resultMap="BizCommonProsperityProjectResult">
        <include refid="selectVo"/>
        <where>
            <if test="projectName != null and projectName != ''">
                and p.project_name like concat('%', #{projectName}, '%')
            </if>
            <if test="projectType != null">
                and p.project_type = #{projectType}
            </if>
            <if test="implYear != null">
                and p.impl_year = #{implYear}
            </if>
            <if test="publishStatus != null">
                and p.publish_status = #{publishStatus}
            </if>
        </where>
        order by p.create_time desc, p.id desc
    </select>

    <insert id="insertBizCommonProsperityProject" parameterType="com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject" useGeneratedKeys="true" keyProperty="id">
        insert into biz_common_prosperity_project (
            project_name, project_type, introduction, project_content,
            investment_amount, impl_year, impl_status,
            video_file_url, video_file_path, cover_file_url, cover_file_path,
            publish_status, publish_time, offline_time,
            create_by, create_time, remark
        ) values (
            #{projectName}, #{projectType}, #{introduction}, #{projectContent},
            #{investmentAmount}, #{implYear}, #{implStatus},
            #{videoFileUrl}, #{videoFilePath}, #{coverFileUrl}, #{coverFilePath},
            #{publishStatus}, #{publishTime}, #{offlineTime},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizCommonProsperityProject" parameterType="com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject">
        update biz_common_prosperity_project set
            project_name = #{projectName},
            project_type = #{projectType},
            introduction = #{introduction},
            project_content = #{projectContent},
            investment_amount = #{investmentAmount},
            impl_year = #{implYear},
            impl_status = #{implStatus},
            video_file_url = #{videoFileUrl},
            video_file_path = #{videoFilePath},
            cover_file_url = #{coverFileUrl},
            cover_file_path = #{coverFilePath},
            publish_status = #{publishStatus},
            publish_time = #{publishTime},
            offline_time = #{offlineTime},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

    <delete id="deleteBizCommonProsperityProjectByIds" parameterType="long">
        delete from biz_common_prosperity_project where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <sql id="screenPublishedWhere">
        where p.publish_status = 1
          and p.impl_year = #{statYear}
    </sql>

    <select id="selectPublishedForScreen" resultMap="BizCommonProsperityProjectResult">
        <include refid="selectVo"/>
        <include refid="screenPublishedWhere"/>
        order by p.publish_time desc, p.id desc
    </select>

    <select id="selectDistinctStatYearsFromPublished" resultType="string">
        select distinct cast(impl_year as char) as y
        from biz_common_prosperity_project
        where publish_status = 1
          and impl_year is not null
        order by y desc
    </select>
</mapper>
