<?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.ygtx.gxt.mapper.source.GxtMisInfoMapper">

    <resultMap type="com.ygtx.gxt.domain.GxtMisInfo" id="GxtMisInfoResult">
<!--        <result property="misNo"    column="work_order_project_no"    />-->
<!--        <result property="workOrderStatus"    column="work_order_status"    />-->
<!--        <result property="pcsStationName"    column="pcs_stati_name"    />-->
<!--        <result property="planStartTime"    column="plan_start_time"    />-->
<!--        <result property="planEndTime"    column="plan_end_time"    />-->
<!--        <result property="realStartTime"    column="real_start_time"    />-->
<!--        <result property="realEndTime"    column="real_end_time"    />-->
<!--        <result property="content"    column="work_content"    />-->
<!--        <result property="issuerName"    column="issuer_name"    />-->
<!--        <result property="permitterName"    column="permitter_name"    />-->
<!--        <result property="createTime"    column="create_time"    />-->
<!--        <result property="updateTime"    column="update_time"    />-->
<!--        <result property="remark"    column="remark"    />-->
        <result property="misNo"    column="work_order_project_code"    />
        <result property="workOrderStatus"    column="work_order_status"    />
        <result property="pcsStationName"    column="pcs_station_name"    />
        <result property="pcsDeviceName"    column="work_order_device"    />
        <result property="realStartTime"    column="real_start_time"    />
        <result property="realEndTime"    column="real_end_time"    />
        <result property="content"    column="work_content"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="workPermitNum"    column="work_ticket_no"    />
    </resultMap>

    <resultMap type="com.ygtx.gxt.domain.GxtWorkOrderPerson" id="GxtWorkOrderPersonResult">
        <result property="nickName"    column="person_name"    />
        <result property="misNo"    column="work_order_project_code"    />
        <result property="userName"    column="person_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isLeader"    column="is_charge_user"    />
    </resultMap>

    <sql id="selectGxtMisInfoVo">
        select work_order_project_code, work_order_status, pcs_station_name,
               work_order_device, real_start_time, real_end_time,
               work_content, create_time, update_time, remark, work_ticket_no
        from fujian_wo_new_work_item t
    </sql>

    <sql id="selectGxtWorkOrderPersonVo">
        select person_name, work_order_project_code, person_id, create_time, update_time, is_charge_user
        from fujian_wo_new_internal_team_members
    </sql>

    <select id="selectGxtMisInfoList" parameterType="com.ygtx.gxt.domain.GxtMisInfo" resultMap="GxtMisInfoResult">
        <include refid="selectGxtMisInfoVo"/>
        <where>
            work_order_device is not null
            <if test="misNo != null  and misNo != ''"> and work_order_project_code like concat('%', #{misNo}, '%')</if>
            <if test="pcsStationName != null  and pcsStationName != ''"> and pcs_station_name like concat('%', #{pcsStationName}, '%')</if>
            <if test="pcsDeviceName != null  and pcsDeviceName != ''"> and work_order_device = #{pcsDeviceName}</if>
            <if test="workOrderStatus != null  and workOrderStatus != ''"> and work_order_status in
                <foreach collection="workOrderStatus.split(',')" item="status" open="(" separator="," close=")">
                    #{status}
                </foreach>
            </if>
            <if test="beginRealEndTime != null and beginRealEndTime != ''"> and date_format(real_end_time,'%y%m%d') &gt;= date_format(#{beginRealEndTime},'%y%m%d')</if>
            <if test="endRealEndTime != null and endRealEndTime != ''"> and date_format(real_end_time,'%y%m%d') &lt;= date_format(#{endRealEndTime},'%y%m%d')</if>
            <if test="pauseTime != null"> and real_start_time &gt;= #{pauseTime}</if>
            <if test="restartTime != null"> and real_start_time &lt; #{restartTime}</if>
            <if test="workPermitNum != null"> and work_ticket_no like concat('%', #{workPermitNum}, '%')</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAutoGxtMisInfoList" parameterType="com.ygtx.gxt.domain.GxtMisInfo" resultMap="GxtMisInfoResult">
        <include refid="selectGxtMisInfoVo"/>
        WHERE t.work_order_device IS NOT NULL
        AND t.pcs_station_name = #{pcsStationName}
        AND t.work_order_device = #{pcsDeviceName}
        <!-- 维修工单 -->
        <if test="orderType != null and orderType != '' and orderType == 1">
            AND t.real_start_time &gt;= #{pauseTime}
            AND t.real_start_time &lt; #{restartTime}
            AND t.real_end_time &gt; #{restartTime}
        </if>
        <!-- 维保工单 -->
        <if test="orderType != null and orderType != '' and orderType == 2">
            AND t.real_start_time &lt; #{pauseTime}
            <!-- 开始时间与停机时间相差 ≤ 60分钟（分钟级时间差） -->
            AND TIMESTAMPDIFF(MINUTE, t.real_start_time, #{pauseTime}) &lt;= 60
            AND t.real_end_time &gt; #{restartTime}
        </if>
        <if test="misNo != null  and misNo != ''"> and work_order_project_code like concat('%', #{misNo}, '%')</if>
        <if test="workOrderStatus != null  and workOrderStatus != ''"> and work_order_status in
            <foreach collection="workOrderStatus.split(',')" item="status" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>

        order by create_time desc
    </select>

    <select id="selectGxtWorkOrderPersonList" parameterType="com.ygtx.gxt.domain.GxtWorkOrderPerson" resultMap="GxtWorkOrderPersonResult">
        <include refid="selectGxtWorkOrderPersonVo"/>
        <where>
            <if test="nickName != null and nickName != ''"> and person_name = #{nickName}</if>
            <if test="misNo != null and misNo != ''"> and work_order_project_code = #{misNo}</if>
            <if test="userName != null and userName != ''"> and person_id = #{userName}</if>
            <if test="isLeader != null and isLeader != ''"> and is_charge_user = #{isLeader}</if>
        </where>
        order by create_time desc
    </select>

</mapper>