<?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.xxfc.platform.vehicle.mapper.VehicleBookInfoMapper" >
  <resultMap id="BaseResultMap" type="com.xxfc.platform.vehicle.entity.VehicleBookInfo" >
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="vehicle" property="vehicle" jdbcType="VARCHAR" />
    <result column="year_month" property="yearMonth" jdbcType="VARCHAR" />
    <result column="booked_date" property="bookedDate" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <select id="getByVehicleIdAndYearMonth" resultType="com.xxfc.platform.vehicle.entity.VehicleBookInfo" parameterType="java.util.Map">
    select id, vehicle, `year_month`, booked_date from vehicle_book_info where vehicle = #{vehicle} and `year_month`=#{yearMonth}
  </select>

    <select id="getByVehicleIdAndYearMonths" resultType="com.xxfc.platform.vehicle.entity.VehicleBookInfo" parameterType="java.util.Map">
        select id, vehicle, `year_month`, booked_date from vehicle_book_info
        where vehicle = #{vehicle}
        and `year_month` in
        <foreach collection="yearMonths" index="i" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

  <insert id="insertIgnore" parameterType="com.xxfc.platform.vehicle.entity.VehicleBookInfo">
    insert ignore into vehicle_book_info ( vehicle, `year_month`, booked_date)
    values(#{vehicle},#{yearMonth},#{bookedDate})
  </insert>

  <update id="updateBookedInfo" parameterType="java.util.Map">
    update vehicle_book_info set
          <choose>
            <when test="orRsOperationFactor!=null">
              booked_date = booked_date | #{orRsOperationFactor}
            </when>
            <when test="andRsOperationFactor!=null">
              booked_date = booked_date &amp; #{andRsOperationFactor}
            </when>
          </choose>

    where vehicle = #{vehicleId} and `year_month`=#{yearMonth}
  </update>
    <update id="updateById" parameterType="com.xxfc.platform.vehicle.entity.VehicleBookInfo">
         update vehicle_book_info set
             booked_date = #{bookedDate}
         where
        vehicle = #{vehicle} and `year_month`=#{yearMonth}
    </update>

    <select id="getByPage4YearMonth" parameterType="java.util.Map" resultType="com.xxfc.platform.vehicle.entity.VehicleBookInfo">
        select id, vehicle, `year_month`, booked_date, create_time, update_time from vehicle_book_info where `year_month` = #{yearMonth} order by id limit #{pageStart},#{pageSize}
    </select>

    <delete id="del4YearMoth" parameterType="java.util.Map">
        delete  from vehicle_book_info where `year_month` = #{yearMonth} limit #{batchSize}
    </delete>

    <insert id="insertHis" parameterType="java.util.Map">
        insert into
          ${tbName}(
                    `id`,
                    vehicle,
                    `year_month`,
                    booked_date
                    )
          values(
                 #{id},
                 #{vehicle},
                 #{yearMonth},
                 #{bookedDate}
                 );
    </insert>

  <insert id="createTbIfNotExists" parameterType="java.lang.String">
    CREATE TABLE IF NOT EXISTS ${_parameter} (
     `id` bigint(10) NOT NULL,
     `vehicle` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '车辆id',
     `year_month` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '预定信息所属年月（格式yyyy-MM）',
     `booked_date` int(10) NOT NULL COMMENT '预定信息，int格式保存，当作位图使用（从小到大每位代表每个月1-31号）',
     `create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
     `update_time` datetime(0) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(0),
     PRIMARY KEY (`id`) USING BTREE,
     UNIQUE INDEX `i_vehicle_year_month`(`vehicle`, `year_month`) USING BTREE,
     INDEX `i_year_month`(`year_month`) USING BTREE COMMENT '迁移历史表查询时使用'
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '预定信息历史表，按年分表' ROW_FORMAT = Dynamic;

  </insert>

<insert id="insertBatch">
    INSERT IGNORE INTO vehicle_book_info(`vehicle`,`year_month`,`booked_date`)VALUES
    <foreach collection="vbfs" item="vbf" separator=",">
        (#{vbf.vehicle} ,#{vbf.yearMonth} ,0)
    </foreach>
</insert>
</mapper>