<?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.XzWegihCorrectionMapper">

    <resultMap type="XzWegihCorrection" id="XzWegihCorrectionResult">
        <result property="id"    column="id"    />
        <result property="datetime"    column="datetime"    />
        <result property="factory"    column="factory"    />
        <result property="actual"    column="actual"    />
        <result property="systemMilk"    column="system_milk"    />
        <result property="coefficient"    column="coefficient"    />
        <result property="userId"    column="user_id"    />
        <result property="deptId"    column="dept_id"    />
    </resultMap>

    <sql id="selectXzWegihCorrectionVo">
        select
            w.id,
            w.datetime,
            w.factory,
            w.actual,
            w.system_milk,
            CASE
                WHEN w.system_milk = 0 THEN 0
                ELSE ROUND(w.actual / w.system_milk, 4)
                END as coefficient,
            w.user_id,
            w.dept_id
        from xz_wegih_correction w
    </sql>

    <select id="selectXzWegihCorrectionList" parameterType="XzWegihCorrection" resultMap="XzWegihCorrectionResult">
        <include refid="selectXzWegihCorrectionVo"/>
        <where>
            <if test="params.beginTime != null and params.beginTime != ''">
                and w.datetime &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and w.datetime &lt;= #{params.endTime}
            </if>
            <if test="factory != null  and factory != ''"> and w.factory = #{factory}</if>

            ${params.dataScope}
        </where>
    </select>

    <select id="selectXzWegihCorrectionById" parameterType="Long" resultMap="XzWegihCorrectionResult">
        <include refid="selectXzWegihCorrectionVo"/>
        where w.id = #{id}
    </select>

    <insert id="insertXzWegihCorrection" parameterType="XzWegihCorrection" useGeneratedKeys="true" keyProperty="id">
        insert into xz_wegih_correction
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="datetime != null">datetime,</if>
            <if test="factory != null">factory,</if>
            <if test="actual != null">actual,</if>
            <if test="systemMilk != null">system_milk,</if>
            user_id, dept_id
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="datetime != null">#{datetime},</if>
            <if test="factory != null">#{factory},</if>
            <if test="actual != null">#{actual},</if>
            <if test="systemMilk != null">#{systemMilk},</if>
            #{userId}, #{deptId}
        </trim>
    </insert>

    <update id="updateXzWegihCorrection" parameterType="XzWegihCorrection">
        update xz_wegih_correction
        <trim prefix="SET" suffixOverrides=",">
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="factory != null">factory = #{factory},</if>
            <if test="actual != null">actual = #{actual},</if>
            <if test="systemMilk != null">system_milk = #{systemMilk},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteXzWegihCorrectionById" parameterType="Long">
        delete from xz_wegih_correction where id = #{id}
    </delete>

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