<?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.tofly.water.mapper.PlanPumpMapper">

    <resultMap type="com.tofly.water.entity.PlanPump" id="PlanPumpMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="planId" column="PLAN_ID" jdbcType="INTEGER"/>
        <result property="planType" column="PLAN_TYPE" jdbcType="VARCHAR"/>
        <result property="pumpDetailId" column="PUMP_DETAIL_ID" jdbcType="INTEGER"/>
        <result property="sort" column="SORT" jdbcType="INTEGER"/>
        <result property="status" column="STATUS" jdbcType="VARCHAR"/>
        <result property="deleted" column="DELETED" jdbcType="VARCHAR"/>
        <result property="createdUser" column="CREATED_USER" jdbcType="INTEGER"/>
        <result property="createdTime" column="CREATED_TIME" jdbcType="TIMESTAMP"/>
        <result property="updatedUser" column="UPDATED_USER" jdbcType="INTEGER"/>
        <result property="updatedTime" column="UPDATED_TIME" jdbcType="TIMESTAMP"/>
        <association property="createdUserName" column="CREATED_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="updatedUserName" column="UPDATED_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="basePumpDetail" column="id = PUMP_DETAIL_ID" select="selectPumpDetailById"></association>
    </resultMap>

    <select id="selectByLaborPlanId" resultMap="PlanPumpMap">
        select * from TF_PLAN_PUMP where PLAN_ID = #{planId} and  PLAN_TYPE = 0 and DELETED != 1
    </select>
    <select id="selectByOperationPlanId" resultMap="PlanPumpMap">
        select * from TF_PLAN_PUMP where PLAN_ID = #{planId} and  PLAN_TYPE = 1 and DELETED != 1
    </select>

    <select id="selectPumpDetailById" resultType="com.tofly.water.entity.BasePumpDetail">
        select * from TF_BASE_PUMP_DETAIL where id = #{id} and DELETED != 1
    </select>

    <!-- 批量插入 -->
    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into TF_PLAN_PUMP(PLAN_ID, PLAN_TYPE, PUMP_DETAIL_ID, SORT, STATUS, DELETED, CREATED_USER,
        CREATED_TIME, UPDATED_USER, UPDATED_TIME)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.planId}, #{entity.planType}, #{entity.pumpDetailId}, #{entity.sort}, #{entity.status},
            #{entity.deleted}, #{entity.createdUser}, #{entity.createdTime}, #{entity.updatedUser},
            #{entity.updatedTime})
        </foreach>
    </insert>
    <!-- 批量插入或按主键更新 -->
    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into TF_PLAN_PUMP(PLAN_ID, PLAN_TYPE, PUMP_DETAIL_ID, SORT, STATUS, DELETED, CREATED_USER,
        CREATED_TIME, UPDATED_USER, UPDATED_TIME)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.planId}, #{entity.planType}, #{entity.pumpDetailId}, #{entity.sort}, #{entity.status},
            #{entity.deleted}, #{entity.createdUser}, #{entity.createdTime}, #{entity.updatedUser},
            #{entity.updatedTime})
        </foreach>
        on duplicate key update
        PLAN_ID = values(PLAN_ID) , PLAN_TYPE = values(PLAN_TYPE) , PUMP_DETAIL_ID = values(PUMP_DETAIL_ID) , SORT =
        values(SORT) , STATUS = values(STATUS) , DELETED = values(DELETED) , CREATED_USER = values(CREATED_USER) ,
        CREATED_TIME = values(CREATED_TIME) , UPDATED_USER = values(UPDATED_USER) , UPDATED_TIME = values(UPDATED_TIME)
    </insert>

</mapper>

