<?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.casic.missiles.mapper.meter.MeterTrainPlanMapper">

    <resultMap id="TrainPlanDetailResultMap" type="com.casic.missiles.dto.meter.TrainPlanDetailResponse">
        <result column="plan_no" property="planNo"/>
        <result column="plan_name" property="planName"/>
        <result column="train_person" property="trainPerson"/>
        <result column="train_number" property="trainNumber"/>
        <result column="train_time" property="trainTime"/>
        <result column="train_hour" property="trainHour"/>
        <result column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="effective_company" property="effectiveCompany"/>
        <result column="director" property="director"/>
        <result column="train_address" property="trainAddress"/>
        <result column="train_content" property="trainContent"/>
        <result column="train_remark" property="remark"/>

        <collection property="trainStaffList" javaType="list" ofType="com.casic.missiles.model.meter.MeterTrainStaff">
<!--            <result column="id" property="id"/>-->
            <result column="staff_id" property="staffId"/>
            <result column="plan_id" property="planId"/>
            <result column="name" property="name"/>
            <result column="company" property="company"/>
            <result column="technology_job" property="technologyJob"/>
            <result column="exam_result" property="examResult"/>
            <result column="sign_time" property="signTime"/>
            <result column="remark" property="remark"/>
        </collection>
    </resultMap>

    <select id="selectTrainPlanById" resultMap="TrainPlanDetailResultMap">
        SELECT mtp.plan_no, mtp.plan_name, mtp.train_person, mtp.train_number, mtp.train_time, mtp.train_hour,
        mtp.dept_id, sd.SIMPLE_NAME AS dept_name, mtp.effective_company, mtp.director, mtp.train_address,
        mtp.train_content, mtp.remark AS train_remark, mts.id, mts.staff_id, mts.plan_id, mts.name, mts.company,
        mts.technology_job, mts.exam_result, mts.sign_time, mts.remark, mtp.process_id
        FROM meter_train_plan mtp
        LEFT JOIN sys_dept sd ON mtp.dept_id = sd.id
        LEFT JOIN meter_train_staff mts ON (mtp.id = mts.plan_id AND mts.is_del = 0)
        WHERE mtp.id = #{id} AND mtp.is_del = 0
    </select>

    <select id="selectMaxTrainPlanNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(plan_no, 12)), 0) from meter_train_plan
    </select>

    <select id="meterTrainStatistic" resultType="com.casic.missiles.dto.meter.MeterWorkbenchResponse">
        SELECT
            DATE_FORMAT( train_time, '%Y-%m' ) AS `date`,
            IFNULL(sum(train_number),0) AS `count`
        FROM
            meter_train_plan
        WHERE
            is_del = 0
          and DATE_FORMAT( train_time, '%Y-%m' ) &gt;= #{startTime}
          and DATE_FORMAT( train_time, '%Y-%m' ) &lt;= #{endTime}
        GROUP BY
            DATE_FORMAT( train_time, '%Y-%m' )
        ORDER BY
            DATE_FORMAT( train_time, '%Y-%m' )
    </select>

    <select id="selectDraftListForApproval" resultType="com.casic.missiles.model.meter.MeterTrainPlan">
        SELECT *
        FROM meter_train_plan
        WHERE is_del = 0
        AND approval_status = #{request.approvalStatus}
        AND create_user_id = #{request.createUserId}
        <if test="request.trainStartTime != null and request.trainStartTime !=''">
            and train_time &gt;= #{request.trainStartTime}
        </if>
        <if test="request.trainEndTime != null and request.trainEndTime !=''">
            and train_time &lt;= #{request.trainEndTime}
        </if>
        <if test="request.createStartTime != null and request.createStartTime !=''">
            and create_time &gt;= #{request.createStartTime}
        </if>
        <if test="request.createEndTime != null and request.createEndTime !=''">
            and create_time &lt;= #{request.createEndTime}
        </if>
        <if test="request.deptId != null">
            and dept_id = #{request.deptId}
        </if>
        <if test="request.effectiveCompany != null and request.effectiveCompany !=''">
            and effective_company like concat('%',#{request.effectiveCompany},'%')
        </if>
        <if test="request.director != null and request.director !=''">
            and director like concat('%',#{request.director},'%')
        </if>
    </select>

    <select id="selectBatchForApproval" resultType="com.casic.missiles.model.meter.MeterTrainPlan">
        SELECT *
        FROM meter_train_plan
        WHERE is_del = 0
        <if test="request.trainStartTime != null and request.trainStartTime !=''">
            and train_time &gt;= #{request.trainStartTime}
        </if>
        <if test="request.trainEndTime != null and request.trainEndTime !=''">
            and train_time &lt;= #{request.trainEndTime}
        </if>
        <if test="request.createStartTime != null and request.createStartTime !=''">
            and create_time &gt;= #{request.createStartTime}
        </if>
        <if test="request.createEndTime != null and request.createEndTime !=''">
            and create_time &lt;= #{request.createEndTime}
        </if>
        <if test="request.deptId != null">
            and dept_id = #{request.deptId}
        </if>
        <if test="request.effectiveCompany != null and request.effectiveCompany !=''">
            and effective_company like concat('%',#{request.effectiveCompany},'%')
        </if>
        <if test="request.director != null and request.director !=''">
            and director like concat('%',#{request.director},'%')
        </if>
        and id in
        <foreach collection="businessKey" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
</mapper>
