<?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="cn.cecep.talroad.mapper.MAmProblemFiRatioMapper">
    <resultMap id="BaseResultMap" type="cn.cecep.talroad.domain.MAmProblemFiRatio">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="factory_id" jdbcType="VARCHAR" property="factoryId"/>
        <result column="station_id" jdbcType="VARCHAR" property="stationId"/>
        <result column="poll_id" jdbcType="VARCHAR" property="pollId"/>
        <result column="problem_ratio" jdbcType="VARCHAR" property="problemRatio"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id,
        factory_id,
        station_id,
        poll_id,
        problem_ratio,
        create_time,
        update_time
    </sql>
    <insert id="insertSelective" keyColumn="id" keyProperty="id"
            parameterType="cn.cecep.talroad.domain.MAmProblemFiRatio" useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into m_am_problem_fi_ratio
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="factoryId != null and factoryId != ''">
                factory_id,
            </if>
            <if test="stationId != null and stationId != ''">
                station_id,
            </if>
            <if test="pollId != null and pollId != ''">
                poll_id,
            </if>
            <if test="problemRatio != null and problemRatio != ''">
                problem_ratio,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="factoryId != null and factoryId != ''">
                #{factoryId,jdbcType=VARCHAR},
            </if>
            <if test="stationId != null and stationId != ''">
                #{stationId,jdbcType=VARCHAR},
            </if>
            <if test="pollId != null and pollId != ''">
                #{pollId,jdbcType=VARCHAR},
            </if>
            <if test="problemRatio != null and problemRatio != ''">
                #{problemRatio,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="cn.cecep.talroad.domain.MAmProblemFiRatio">
        <!--@mbg.generated-->
        update m_am_problem_fi_ratio
        <set>
            <if test="factoryId != null and factoryId != ''">
                factory_id = #{factoryId,jdbcType=VARCHAR},
            </if>
            <if test="stationId != null and stationId != ''">
                station_id = #{stationId,jdbcType=VARCHAR},
            </if>
            <if test="pollId != null and pollId != ''">
                poll_id = #{pollId,jdbcType=VARCHAR},
            </if>
            <if test="problemRatio != null and problemRatio != ''">
                problem_ratio = #{problemRatio,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="queryMamProblemFiRatioListByFactoriesIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from m_am_problem_fi_ratio
        where factory_id in
        <foreach close=")" collection="list" item="item" open="(" separator=",">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>

    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO m_am_problem_fi_ratio(id, factory_id, station_id, poll_id, problem_ratio, create_time, update_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.id,jdbcType=BIGINT},
             #{item.factoryId,jdbcType=VARCHAR},
             #{item.stationId,jdbcType=VARCHAR},
             #{item.pollId,jdbcType=VARCHAR},
             #{item.problemRatio,jdbcType=VARCHAR},
             #{item.createTime,jdbcType=TIMESTAMP},
             #{item.updateTime,jdbcType=TIMESTAMP})
        </foreach>
    </insert>

    <update id="batchUpdate">
        UPDATE m_am_problem_fi_ratio
        SET problem_ratio = CASE
        <foreach collection="list" index="index" item="item" separator=" ">
            WHEN id = #{item.id, jdbcType=VARCHAR} THEN #{item.problemRatio, jdbcType=VARCHAR}
        </foreach>
        END,
        update_time = CURRENT_TIMESTAMP
        WHERE id IN
        <foreach close=")" collection="list" item="item" open="(" separator=", ">
            #{item.id, jdbcType=VARCHAR}
        </foreach>
    </update>

    <select id="queryMamProblemFiRatioListByProblemRatioDTOList" resultMap="BaseResultMap">
        <foreach collection="list" separator="UNION ALL" item="item" index="index">
            select
            <include refid="Base_Column_List"/>
            from m_am_problem_fi_ratio
            where factory_id = #{item.factoryId}
            and station_id = #{item.stationId}
            and poll_id = #{item.pollId}
        </foreach>
    </select>
</mapper>