<?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.fs.survey.mapper.DrugResearchMapper">

    <resultMap type="DrugResearch" id="DrugResearchResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="description"    column="description"    />
        <result property="coverImage"    column="cover_image"    />
        <result property="formId"    column="form_id"    />
        <result property="doctorId"    column="doctor_id"    />
        <result property="category"    column="category"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="status"    column="status"    />
        <result property="sort"    column="sort"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deleted"    column="deleted"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName" column="company_name"/>
        <result property="doctorName" column="doctor_name"/>
        <result property="formName" column="form_name"/>
        <result property="surveyQuestionnaireType" column="surveyQuestionnaireType"/>

    </resultMap>

    <sql id="selectDrugResearchVo">
        SELECT
        dr.id,
        dr.company_id,
        c.company_name,
        dr.title,
        dr.description,
        q.name AS form_name,
        q.survey_questionnaire_type AS surveyQuestionnaireType,
        dr.cover_image,
        dr.form_id,
        dr.doctor_id,
        d.doctor_name,
        dr.category,
        dr.start_time,
        dr.end_time,
        dr.status,
        dr.sort,
        dr.create_by,
        dr.create_time,
        dr.update_time,
        dr.deleted
        FROM drug_research dr
        LEFT JOIN company c ON dr.company_id = c.company_id
        LEFT JOIN survey_questionnaire q ON dr.form_id = q.id
        LEFT JOIN doctor d ON dr.doctor_id = d.id

    </sql>


    <select id="selectDrugResearchList" parameterType="DrugResearch" resultMap="DrugResearchResult">
        <include refid="selectDrugResearchVo"/>
        <where>
            <if test="companyId != null">
                AND dr.company_id = #{companyId}
            </if>
            <if test="doctorId != null">
                AND dr.doctor_id = #{doctorId}
            </if>
            <if test="title != null and title != ''">
                AND dr.title = #{title}
            </if>
            <if test="description != null and description != ''">
                AND dr.description = #{description}
            </if>
            <if test="coverImage != null and coverImage != ''">
                AND dr.cover_image = #{coverImage}
            </if>
            <if test="formId != null">
                AND dr.form_id = #{formId}
            </if>
            <if test="category != null and category != ''">
                AND dr.category = #{category}
            </if>
            <if test="status != null">
                AND dr.status = #{status}
            </if>
            <if test="sort != null">
                AND dr.sort = #{sort}
            </if>
            <!-- 可选：按公司名筛选（如果你前端会传 companyName） -->
            <if test="companyName != null and companyName != ''">
                AND c.company_name LIKE CONCAT('%', #{companyName}, '%')
            </if>
            <if test="startTime != null ">
                AND dr.start_time &gt;= #{startTime}
            </if>
            <if test="endTime != null ">
                AND dr.end_time &lt;= #{endTime}
            </if>
            <if test="timeStatus != null and timeStatus != ''">
                <choose>
                    <!-- 未开始：当前时间 < 开始时间 -->
                    <when test="timeStatus == 'before'">
                        AND (dr.start_time > NOW() OR dr.start_time IS NULL)
                    </when>
                    <!-- 进行中：当前时间 BETWEEN 开始时间 AND 结束时间 -->
                    <when test="timeStatus == 'during'">
                        AND (NOW() BETWEEN dr.start_time AND dr.end_time)
                    </when>
                    <!-- 已结束：当前时间 > 结束时间 -->
                    <when test="timeStatus == 'after'">
                        AND dr.end_time &lt; NOW()
                        AND dr.end_time IS NOT NULL
                    </when>
                </choose>
            </if>
        </where>
        order by dr.sort, dr.create_time desc
    </select>


    <select id="selectDrugResearchById" parameterType="Long" resultMap="DrugResearchResult">
        <include refid="selectDrugResearchVo"/>
        WHERE dr.id = #{id}
    </select>

    <insert id="insertDrugResearch" parameterType="DrugResearch" useGeneratedKeys="true" keyProperty="id">
        insert into drug_research
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="description != null">description,</if>
            <if test="coverImage != null">cover_image,</if>
            <if test="formId != null">form_id,</if>
            <if test="category != null and category != ''">category,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">status,</if>
            <if test="sort != null">sort,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleted != null">deleted,</if>
            <if test="companyId != null">company_id,</if>
            <if test="doctorId != null">doctor_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="description != null">#{description},</if>
            <if test="coverImage != null">#{coverImage},</if>
            <if test="formId != null">#{formId},</if>
            <if test="category != null and category != ''">#{category},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="doctorId != null">#{doctorId},</if>
        </trim>
    </insert>

    <update id="updateDrugResearch" parameterType="DrugResearch">
        update drug_research
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="description != null">description = #{description},</if>
            <if test="coverImage != null">cover_image = #{coverImage},</if>
            <if test="formId != null">form_id = #{formId},</if>
            <if test="category != null and category != ''">category = #{category},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="doctorId != null">doctor_id = #{doctorId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDrugResearchById" parameterType="Long">
        delete from drug_research where id = #{id}
    </delete>

    <delete id="deleteDrugResearchByIds" parameterType="String">
        delete from drug_research where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
