<?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.app.mapper.DryLossRatioMapper">
    
    <resultMap type="DryLossRatio" id="DryLossRatioResult">
        <result property="id"    column="id"    />
        <result property="dryLossRatio"    column="dry_loss_ratio"    />
        <result property="startTime"    column="start_time"    />
<!--        <result property="endTime"    column="end_time"    />-->
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="isUse"    column="is_use"    />
    </resultMap>

    <sql id="selectDryLossRatioVo">
        select id, dry_loss_ratio, start_time, end_time, remark, create_time, create_by, update_time, update_by, del_flag, is_use from dry_loss_ratio
    </sql>

    <select id="selectDryLossRatioList" parameterType="DryLossRatio" resultMap="DryLossRatioResult">
        <include refid="selectDryLossRatioVo"/>
        <where>
            del_flag = '0'
            <if test="dryLossRatio != null "> and dry_loss_ratio = #{dryLossRatio}</if>
            <if test="startTime != null "> and DATE(CONCAT('2025-', MONTH(start_time), '-', DAY(start_time))) = DATE(CONCAT('2025-', MONTH(#{startTime}), '-', DAY(#{startTime})))</if>
<!--            <if test="endTime != null "> and end_time <![CDATA[ <= ]]> #{endTime}</if>-->
        </where>
        order by DAYOFYEAR(start_time) asc
    </select>
    
    <select id="selectDryLossRatioById" parameterType="Long" resultMap="DryLossRatioResult">
        <include refid="selectDryLossRatioVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <!--判断是否有冲突的时间范围-->
    <select id="checkTimeOverLap" parameterType="DryLossRatio" resultMap="DryLossRatioResult">
        select id, dry_loss_ratio from dry_loss_ratio where DATE(CONCAT('2025-', MONTH(start_time), '-', DAY(start_time))) = DATE(CONCAT('2025-', MONTH(#{startTime}), '-', DAY(#{startTime}))) and del_flag = '0' limit 1
    </select>

    <!--获取满足时间条件的干损比例   将规则日期和当前日期都转换为相同的"2025年"的日期进行比较,还要比较跨年的情况-->
    <select id="selectDryLossRatioNow" resultMap="DryLossRatioResult">
        (
            -- 查询已生效且最接近当前日期的记录
            SELECT *
            FROM dry_loss_ratio
            WHERE
            ((MONTH(start_time) <![CDATA[ < ]]> MONTH(CURDATE())) OR
            (MONTH(start_time) = MONTH(CURDATE()) AND DAY(start_time) <![CDATA[ <= ]]> DAY(CURDATE())))
            AND del_flag = '0'
            ORDER BY
            CASE
            WHEN MONTH(start_time) = MONTH(CURDATE()) THEN
            DAY(CURDATE()) - DAY(start_time)
            ELSE
            (MONTH(CURDATE()) - MONTH(start_time)) * 30 + (DAY(CURDATE()) - DAY(start_time))
            END
            LIMIT 1
        )
        UNION ALL
        (
            -- 如果没有已生效记录，则选择全年最后生效的记录
            SELECT *
            FROM dry_loss_ratio
            WHERE
            NOT EXISTS (
                SELECT 1
                FROM dry_loss_ratio
                WHERE
                (((MONTH(start_time) <![CDATA[ < ]]> MONTH(CURDATE())) OR
                (MONTH(start_time) = MONTH(CURDATE()) AND DAY(start_time) <![CDATA[ <= ]]> DAY(CURDATE()))))
                AND del_flag = '0'
            )
            AND del_flag = '0'
            ORDER BY
            MONTH(start_time) DESC,
            DAY(start_time) DESC
            LIMIT 1
        )
        LIMIT 1;
    </select>

    <insert id="insertDryLossRatio" parameterType="DryLossRatio" useGeneratedKeys="true" keyProperty="id">
        insert into dry_loss_ratio
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dryLossRatio != null">dry_loss_ratio,</if>
            <if test="startTime != null">start_time,</if>
<!--            <if test="endTime != null">end_time,</if>-->
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="isUse != null">is_use,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dryLossRatio != null">#{dryLossRatio},</if>
            <if test="startTime != null">#{startTime},</if>
<!--            <if test="endTime != null">#{endTime},</if>-->
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="isUse != null">#{isUse},</if>
            '0'
         </trim>
    </insert>

    <update id="updateDryLossRatio" parameterType="DryLossRatio">
        update dry_loss_ratio
        <trim prefix="SET" suffixOverrides=",">
            <if test="dryLossRatio != null">dry_loss_ratio = #{dryLossRatio},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
<!--            <if test="endTime != null">end_time = #{endTime},</if>-->
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="isUse != null">is_use=#{isUse},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDryLossRatioById" parameterType="Long">
        update dry_loss_ratio set del_flag = '2' where id = #{id}
    </delete>

    <delete id="deleteDryLossRatioByIds" parameterType="String">
        update dry_loss_ratio set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>