<?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.zhyc.module.dairyProducts.mapper.XzDryMatterCorrectionMapper">

    <resultMap type="XzDryMatterCorrection" id="XzDryMatterCorrectionResult">
        <result property="id" column="id"/>
        <result property="datetime" column="datetime"/>
        <result property="factory" column="factory"/>
        <result property="content" column="content"/>
        <result property="standard" column="standard"/>
        <result property="coefficient" column="coefficient"/>
    </resultMap>

    <sql id="selectXzDryMatterCorrectionVo">
        SELECT
            id,
            datetime,
            factory,
            content,
            COALESCE(standard, 18) as standard,
            CASE
                WHEN standard = 0 OR standard IS NULL THEN NULL
                ELSE ROUND(content / standard, 2)
                END AS coefficient
        FROM xz_dry_matter_correction
    </sql>

    <select id="selectXzDryMatterCorrectionList" parameterType="XzDryMatterCorrection" resultMap="XzDryMatterCorrectionResult">
        <include refid="selectXzDryMatterCorrectionVo"/>
        <where>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND DATE_FORMAT(datetime, '%Y-%m') &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND DATE_FORMAT(datetime, '%Y-%m') &lt;= #{params.endTime}
            </if>
            <if test="factory != null and factory != ''">
                AND factory = #{factory}
            </if>
            AND (standard IS NULL OR standard != 0)
        </where>
        ORDER BY datetime DESC
    </select>

    <select id="selectXzDryMatterCorrectionById" parameterType="Long" resultMap="XzDryMatterCorrectionResult">
        <include refid="selectXzDryMatterCorrectionVo"/>
        WHERE id = #{id}
    </select>

    <insert id="insertXzDryMatterCorrection" parameterType="XzDryMatterCorrection" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO xz_dry_matter_correction
        <trim prefix="(" suffix=")" suffixOverrides=",">
            datetime, factory, content, standard
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{datetime}, #{factory}, #{content}, #{standard}
        </trim>
    </insert>

    <update id="updateXzDryMatterCorrection" parameterType="XzDryMatterCorrection">
        UPDATE xz_dry_matter_correction
        <set>
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="factory != null">factory = #{factory},</if>
            <if test="content != null">content = #{content},</if>
            <if test="standard != null">standard = #{standard},</if>
        </set>
        WHERE id = #{id}
    </update>

    <delete id="deleteXzDryMatterCorrectionById" parameterType="Long">
        DELETE FROM xz_dry_matter_correction WHERE id = #{id}
    </delete>

    <delete id="deleteXzDryMatterCorrectionByIds" parameterType="Long[]">
        DELETE FROM xz_dry_matter_correction WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>