<?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.huimv.management.dao.TransferPeriodLogDao">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.huimv.management.entity.TransferPeriodLogEntity" id="transferPeriodLogMap">
        <result property="id" column="id"/>
        <result property="farmId" column="farm_id" />
        <result property="pigId" column="pig_id"/>
        <result property="earTag" column="ear_tag"/>
        <result property="oldPigstyId" column="old_period_id"/>
        <result property="newPigstyId" column="new_period_id"/>
        <result property="oldUnitId" column="old_unit_id"/>
        <result property="newUnitId" column="new_unit_id"/>
        <result property="oldPeriodId" column="old_period_id"/>
        <result property="newPeriodId" column="new_period_id"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="gmtModified" column="gmt_modified"/>
        <result property="operatorUsername" column="operator_username"/>
    </resultMap>

    <!--批量插入-->
    <insert id="batchInsert">
        INSERT INTO mgt_transfer_period_log(farm_id, pig_id, ear_tag, old_pigsty_id, new_pigsty_id, old_unit_id, new_unit_id, old_period_id, new_period_id, gmt_create, gmt_modified, operator_username) values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.farmId},
            #{item.pigId},
            #{item.earTag},
            #{item.oldPigstyId},
            #{item.newPigstyId},
            #{item.oldUnitId},
            #{item.newUnitId},
            #{item.oldPeriodId},
            #{item.newPeriodId},
            now(),
            now(),
            #{item.operatorUsername}
            )
        </foreach>
    </insert>

    <!--分页条件查询-->
    <select id="page" resultType="com.huimv.management.entity.vo.TransferPeriodLogVo">
        SELECT
          log.id,
          log.pig_id,
          log.ear_tag,
          old_pigsty_id,
          pigsty1.number old_pigsty_name,
          new_pigsty_id,
          pigsty2.number new_pigsty_name,
          old_unit_id,
          unit1.number old_unit_name,
          new_unit_id,
          unit2.number new_unit_name,
          old_period_id,
          period1.number old_period_name,
          new_period_id,
          period2.number new_period_name,
          operator_username
        FROM mgt_transfer_period_log log
        JOIN mgt_pigsty pigsty1 ON log.old_pigsty_id = pigsty1.id
        JOIN mgt_pigsty pigsty2 ON log.new_pigsty_id = pigsty2.id
        JOIN mgt_unit unit1 ON log.old_unit_id = unit1.id
        JOIN mgt_unit unit2 ON log.new_unit_id = unit2.id
        JOIN mgt_period period1 ON  log.old_period_id = period1.id
        JOIN mgt_period period2 ON log.new_period_id = period2.id
        ${ew.customSqlSegment}
    </select>


</mapper>