<?xml version="1.0" encoding="utf-8"?>
<sqltoy xmlns="http://www.sagframe.com/schema/sqltoy"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sagframe.com/schema/sqltoy
        http://www.sagframe.com/schema/sqltoy/sqltoy.xsd"
>

    <!-- 依据页面给定查询条件返回板卡故障列表信息 -->
    <sql id="atv_faultStatic">
        <value>
            <![CDATA[
            SELECT count(a.id) as total
            ,b.name AS countryName,c.name AS cityName
            ,d.name AS lineName,e.sn AS trainName
            FROM atv_board_card_fault a
            LEFT JOIN atv_country b ON b.id = a.country_id
            LEFT JOIN atv_area c ON c.id = a.area_id
        	LEFT JOIN atv_line d ON d.id = a.line_id
 			LEFT JOIN atv_train e ON e.id = a.train_id
 			WHERE 1=1
 			    #[AND a.country_id = :countryId]
 			    #[AND a.area_id = :areaId]
 			    #[AND a.line_id = :lineId]
 			    #[AND a.fault_status = :faultStatus]
 			    #[AND a.worker_user_id = :workerUserId]
 			    #[AND a.fault_source = :faultSource]
 			    #[AND a.create_time >= :beginTime]
 			    #[AND a.create_time <= :endTime]
            ]]>
        </value>
    </sql>
    <!-- 更新在线状态-->
    <sql id="atv_trainOnlineStatus">
        <value>
            <![CDATA[
                UPDATE atv_train atn
                    INNER JOIN atv_line al ON atn.line_id=al.id
                    INNER JOIN atv_area ar ON atn.area_id=ar.id
                    INNER JOIN atv_country ac ON atn.country_id=ac.id
                    SET atn.online_status=@value(:onlineStatus)
                WHERE
                     #[atn.sn = :trainSn]
                     #[AND al.sn = :lineSn]
                     #[AND ar.area_code = :areaCode]
                     #[AND ac.country_code = :countryCode]
                ]]>
        </value>
    </sql>
    <!-- 更新在线状态-->
    <sql id="atv_equipmentOnlineStatus">
        <value>
            <![CDATA[
              UPDATE atv_equipment ae
                  INNER JOIN atv_train atn ON ae.train_id=atn.id
                  INNER JOIN atv_line al ON ae.line_id=al.id
                  INNER JOIN atv_area ar ON ae.area_id=ar.id
                  INNER JOIN atv_country ac ON ae.country_id=ac.id
                  SET ae.online_status=@value(:onlineStatus)
              WHERE
                  #[atn.sn = :trainSn]
                  #[AND al.sn = :lineSn]
                  #[AND ar.area_code = :areaCode]
                  #[AND ac.country_code = :countryCode]
            ]]>
        </value>
    </sql>
    <!--暂时不用-->
    <sql id="atv_getEquipmentLog">
        <value>
            <![CDATA[
			SELECT *
        	FROM atv_equipment_log a
	        WHERE 1=1
    		    #[AND a.equipment_id = :equipmentId]
        		#[AND a.equipment_log_type = :equipmentLogType]
        		#[AND a.start_time = :start_time]
         		#[AND a.end_time = :end_time]
           ]]>
        </value>
    </sql>
    <sql id="atv_getTrainList">
        <value>
            <![CDATA[
            SELECT atn.*,MIN(abcf.fault_status) fault_status
        	FROM atv_train atn
        	LEFT JOIN atv_board_card_fault abcf ON atn.id=abcf.train_id
        	WHERE 1=1
            	#[AND atn.country_id = :countryId]
            	#[AND atn.area_id = :areaId]
            	#[AND atn.line_id = :lineId]
        	GROUP BY atn.id
        	ORDER BY atn.sn ASC
            ]]>
        </value>
    </sql>
    <sql id="atv_getEquipWarnInfo">
        <value>
            <![CDATA[
            SELECT a.*,
                round(a.life / 60 / 60,0) AS lifeH,
                CONCAT(IFNULL(a.voltage_min3v3,''),'~',IFNULL(a.voltage_max3v3,'')) AS voltageMin3v3H,
                CONCAT(IFNULL(a.voltage_min5,''),'~',IFNULL(a.voltage_max5,'')) AS voltageMin5vH,
                CONCAT(IFNULL(a.voltage_min12,''),'~',IFNULL(a.voltage_max12,'')) AS voltageMin12H,
                CONCAT(IFNULL(a.current_min3v3,''),'~',IFNULL(a.current_max3v3,'')) AS currentMin3v3H,
                CONCAT(IFNULL(a.current_min5,''),'~',IFNULL(a.current_max5,'')) AS currentMin5vH,
                CONCAT(IFNULL(a.current_min12,''),'~',IFNULL(a.current_max12,'')) AS currentMin12vH
            FROM atv_threshold a
 			WHERE 1=1
			     #[AND a.board_card_type = :boardCardType] 
 			]]>
        </value>
    </sql>
    <!-- 全部知识List(APP)  -->
    <sql id="atv_findKnowledgeListApp">
        <value>
            <![CDATA[
            SELECT *
            FROM atv_knowledge ak
            WHERE ak.is_release=1
                #[@if(@value(:keyword) != null AND @value(:keyword) !='')
                   AND (ak.keyword like CONCAT('%',:keyword,'%')
                   or ak.title like CONCAT('%',:keyword,'%') )
            	]
 			    #[@if(@value(:status)==1) AND ak.knowledge_type=4]
 			    #[@if(@value(:status)==2) AND ak.knowledge_type=1]
                #[@if(@value(:status)==3) AND ak.knowledge_type=3]
 			    #[@if(@value(:status)==4) AND ak.knowledge_type=2]
             ORDER  BY ak.create_time DESC
            ]]>
        </value>
    </sql>
    <!-- 板卡下拉框数据(APP)  -->
    <sql id="atv_boardCardListApp">
        <translate cache="dictBoardCardType" columns="boardCardType_dictText" />
        <value>
            <![CDATA[
            SELECT a.id,a.sn,a.board_card_type  AS boardCardType_dictText
            FROM atv_board_card a
 			WHERE 1=1
 			    #[AND a.equipment_id =:equipmentId]
            ]]>
        </value>
    </sql>
    <!--获取故障详细信息(APP) -->
    <sql id="atv_getDetailList4BoardCardFault">
        <value>
            <![CDATA[
            SELECT a.*
            FROM atv_board_card_fault a
            WHERE 1=1
                #[@if(@value(:status)==0) AND (a.line_id IN (:atvLineIds) AND a.fault_status=1)
                OR (a.fault_status=1 AND a.submit_user_id=:id
                OR a.worker_user_id =:id)
             ]
            #[@if(@value(:status)==1) AND (a.line_id IN (:atvLineIds) AND a.fault_status=1)
               OR (a.fault_status=4 AND a.worker_user_id=:id)
                 ]
                #[@if(@value(:status)==2) AND (a.fault_status=2 or a.fault_status=3) AND a.worker_user_id =:id]
            #[@if(@value(:status)==3) AND a.fault_status=5 AND a.worker_user_id =:id]
            ORDER BY a.create_time DESC
            ]]>
        </value>
    </sql>
    <!--根据车辆查询开始事件前设备最后在线状态-->
    <sql id="atv_findOnlineStatus">
        <value>
            <![CDATA[
            SELECT MAX(atwl.create_time) create_time,atn.sn train_sn,
                   ac.sn carriage_sn,ae.sn equipment_sn,atwl.online_status,
            	   atwl.version,atwl.play_state
            FROM atv_train_working_log atwl
            INNER JOIN atv_equipment ae ON atwl.equipment_id=ae.id
            INNER JOIN atv_carriage ac ON atwl.carriage_id=ac.id
            INNER JOIN atv_train atn ON atwl.train_id=atn.id
            WHERE atwl.train_working_log_type=5
			    #[AND atwl.train_id =:trainId]
 			    #[AND atwl.create_time < :startTime]
            GROUP BY atwl.equipment_id
            ORDER BY atwl.create_time DESC
 			]]>
        </value>
    </sql>
    <!-- 根据车辆查询当天的相同故障的数据
    trainId车辆;faultSource 故障来源;address故障未知;phenomenon故障现象
 -->
    <sql id="atv_findByNowBoardCardFault">
        <value>
            <![CDATA[
            SELECT abcf.*
            FROM atv_board_card_fault abcf
            WHERE abcf.fault_status != 5
			    #[AND abcf.address= :address]
 			    #[AND abcf.fault_source = :faultSource]
 			    #[AND abcf.train_id = :trainId]
 			    #[AND abcf.phenomenon LIKE :phenomenon]
            	#[AND abcf.create_time = :nowDate]
            	#[AND abcf.is_finish = :isFinish]
            limit 1
 			]]>
        </value>
    </sql>
    <!-- 获取当天的相同的预警(预警日志表) nowDate(DATE(awl.create_time)=DATE(NOW())
      lineId 线路ID;trainId列车ID;carriageId车厢ID;equipmentId设备ID
         boardCardId板卡ID;warnType预警类型;faultType故障类型;faultNo故障编码
      自动获取当前时间？#[AND awl.create_time = DATE(NOW())]

        -->
    <sql id="atv_findByNowWarnLog">
        <page-optimize alive-max="100" alive-seconds="120" />
        <value>
            <![CDATA[
            SELECT awl.*
            FROM atv_warn_log awl
            WHERE 1=1
			    #[AND awl.warn_type= :warnType]
 			    #[AND awl.line_id = :lineId]
 			    #[AND awl.train_id = :trainId]
 			    #[AND awl.carriage_id = :carriageId]
 			    #[AND awl.equipment_id= :equipmentId]
 			    #[AND awl.board_card_id= :boardCardId]
            	#[AND awl.fault_type = :faultType]
 			    #[AND awl.fault_no= :faultNo]
            	#[AND awl.create_time = :nowDate]
                ORDER BY awl.create_time DESC
 			]]>
        </value>
    </sql>
    <!-- 获取设备日志列表信息
      平铺不成功 待验证
       #[AND a.create_time >= :dateRange[0]]
       #[AND a.create_time <= :dateRange[1]]
    -->
    <sql id="atv_equipLogList">
        <translate cache="dictEquipLog" columns="equipmentLogType" />
        <value>
            <![CDATA[
            SELECT a.*,
               CONCAT(IFNULL(a.start_time,''),'~',IFNULL(a.end_time,'')) AS dataRange,
               a.equipment_log_type as equipmentLogType,
               CONCAT(b.sn,'-',c.sn,'-',d.sn) AS trainIdName,
               CONCAT(e.name,'-',f.name,'-',g.name) AS countryIdName
            FROM atv_equipment_log a
            LEFT JOIN atv_train b ON b.id = a.train_id
            LEFT JOIN atv_carriage c ON c.id = a.carriage_id
        	LEFT JOIN atv_equipment d ON d.id = a.equipment_id
 			LEFT JOIN atv_country e ON e.id = a.country_id
            LEFT JOIN atv_area f ON f.id = a.area_id
        	LEFT JOIN atv_line g ON g.id = a.line_id
 			WHERE 1=1
			    #[AND a.country_id = :countryId]
 			    #[AND a.area_id = :areaId]
 			    #[AND a.line_id = :lineId]
 			    #[AND a.train_id = :trainId]
 			    #[AND a.equipment_type = :equipmentType]
 			    #[AND a.create_time >= :beginTime]
 		        #[AND a.create_time <= :endTime]
 		        ORDER BY a.create_time DESC
 			]]>
        </value>
    </sql>
    <!-- 获取设Tts日志列表   -->
    <sql id="atv_getTtsLogList">
        <translate cache="dictTurnStatus" columns="turnStatusText" />
        <value>
            <![CDATA[
            SELECT a.*,b.sn as trainIdName
                ,a. turn_status as turnStatusText
               ,CONCAT(e.name,'-',f.name,'-',g.name) AS countryIdName
            FROM atv_tts a
            LEFT JOIN atv_train b ON b.id = a.train_id
            LEFT JOIN atv_country e ON e.id = a.country_id
            LEFT JOIN atv_area f ON f.id = a.area_id
        	LEFT JOIN atv_line g ON g.id = a.line_id
 			WHERE 1=1
			    #[AND a.create_time >= :beginTime]
 		        #[AND a.create_time <= :endTime]
 		        ORDER BY a.create_time DESC
 			]]>
        </value>
    </sql>
    <sql id="atv_warnLogList">
        <translate cache="dictBoardCardType" columns="boardCardType_dictText" />
        <translate cache="dictWarnType" columns="warnType_dictText" />
        <translate cache="dictFaultType" columns="faultType_dictText" />
        <value>
            <![CDATA[
            SELECT a.create_time,a.board_card_id,a.train_working_log_id,a.fault_no
                  ,a.fault_type as faultType_dictText,a.warn_type as warnType_dictText
                  ,a.board_card_type as boardCardType_dictText
                  ,d.sn as equipSn,c.sn as carriageSn,g.name as lineName,b.sn as trainSn
            FROM atv_warn_log a
                     LEFT JOIN atv_train b ON b.id = a.train_id
                     LEFT JOIN atv_carriage c ON c.id = a.carriage_id
                     LEFT JOIN atv_equipment d ON d.id = a.equipment_id
                     LEFT JOIN atv_line g ON g.id = a.line_id
            WHERE 1=1
                #[AND a.line_id = :lineId]
 			    #[AND a.train_id = :trainId]
 			    #[AND a.equipment_type = :equipmentType]
 			    #[AND a.create_time >= :beginTime]
 		        #[AND a.create_time <= :endTime]
 		        ORDER BY a.create_time DESC
            ]]>
        </value>
    </sql>
    <sql id="atv_trainLogList">
        <translate cache="dictFaultType" columns="faultType_dictText" />
        <translate cache="dictCarDirection" columns="carDirection_dictText" />
        <translate cache="dictBoardCardType" columns="boardCardType_dictText" />
        <translate cache="dictEquipType" columns="equipType_dictText" />
        <translate cache="dictAlarmStatus" columns="alarmStatus_dictText" />
        <translate cache="dictDoorDirection" columns="openDoorDirection_dictText" />
        <translate cache="dictPlayState" columns="playState_dictText" />
        <translate cache="dictTrainWorkingLogType" columns="trainWorkingLogType_dictText" />
        <translate cache="dictOnlineStatus" columns="onlineStatu_dictText" />
        <value>
            <![CDATA[
            SELECT a.create_time,a.worktime,a.voltage12,a.voltage5
                  ,a.voltage3v3,a.current12,a.current5,a.current3v3,a.temperature
                  ,a.equipment_type as equipType_dictText,a.board_card_type as boardCardType_dictText
                  ,a.pa_start_flag,a.start_site_sn,a.emergent_pano,a.section_sn
                  ,a.play_state as playState_dictText,a.alarm_status as alarmStatus_dictText
                  ,a.emergent_paid,a.play_file_name,a.pa_state,a.version
                  ,a.open_door_direction as openDoorDirection_dictText,a.pa_model,a.now_site_sn
                  ,a.train_working_log_type as trainWorkingLogType_dictText
                  ,a.end_site_sn,a.pa_carms,a.direction as carDirection_dictText
                  ,a.online_status as onlineStatu_dictText
                  ,a.mic_model,a.car_direction,a.fault_description
                  ,a.fault_type as faultType_dictText,a.fault_no,a.board_card_no
                  ,CONCAT(b.sn,'-',c.sn,'-',d.sn) AS trainIdName
                  ,CONCAT(e.name,'-',f.name,'-',g.name) AS countryIdName
            FROM atv_train_working_log a
                     LEFT JOIN atv_country e ON e.id = a.country_id
                     LEFT JOIN atv_area f ON f.id = a.area_id
                     LEFT JOIN atv_line g ON g.id = a.line_id
                     LEFT JOIN atv_train b ON b.id = a.train_id
                     LEFT JOIN atv_carriage c ON c.id = a.carriage_id
                     LEFT JOIN atv_equipment d ON d.id = a.equipment_id
            WHERE 1=1
                #[AND a.country_id = :countryId]
 			    #[AND a.area_id = :areaId]
 			    #[AND a.line_id = :lineId]
 			    #[AND a.train_id = :trainId]
 			    #[AND a.equipment_type = :equipmentType]
 			    #[AND a.create_time >= :beginTime]
 		        #[AND a.create_time <= :endTime]
 		        ORDER BY a.create_time DESC
            ]]>
        </value>
    </sql>
    <!-- 知识库列表信息 -->
    <sql id="atv_knowledgeList">
        <translate cache="dictSendStatus" columns="is_release" />
        <translate cache="dictKnowledgeType" columns="knowledgeType_dictText" />
        <value>
            <![CDATA[
            SELECT a.*
             	  ,a.knowledge_type as knowledgeType_dictText
            FROM atv_knowledge a
            WHERE 1=1
 			    #[AND a.create_time >= :beginTime]
 			    #[AND a.create_time <= :endTime] 
 			    #[AND (a.title LIKE :keyWord OR a.keyword LIKE :keyWord)]
 			    ORDER BY a.create_time DESC
            ]]>
        </value>
    </sql>
    <!-- 板卡故障分析 -->
    <sql id="atv_faultAnalysis">
        <value>
            <![CDATA[
             SELECT a.carriage_sn AS carriageId_dictText,
             a.equipment_sn AS equipmentId_dictText,
             a.board_card_sn AS boardCardId_dictText,
             a.board_card_type AS boardCardType_dictText
            FROM atv_board_card_fault_analyse A
 			WHERE 1=1
 			    #[AND a.board_card_fault_id = :id]
            ]]>
        </value>
    </sql>
    <!-- 删除: 板卡故障及板卡故障文件(一对多) -->
    <sql id="atv_delBoardCardFaultAndFile">
        <value>
            <![CDATA[
		    DELETE p.*, pp.* 
			FROM atv_board_card_fault p 
        	LEFT JOIN atv_board_card_fault_file pp ON p.id = pp.board_card_fault_id
          	WHERE #[p.id IN (:id)]
           ]]>
        </value>
    </sql>
    <!-- 依据页面给定查询条件返回板卡故障列表信息 -->
    <sql id="atv_faultList">
        <value>
            <![CDATA[
            SELECT a.*,
            b.name AS countryIdName,c.name AS areaIdName,
            d.name AS lineIdName,e.sn AS trainIdName,
            f.phonenumber AS submitUserId_phone
            FROM atv_board_card_fault a
            LEFT JOIN atv_country b ON b.id = a.country_id
            LEFT JOIN atv_area c ON c.id = a.area_id
        	LEFT JOIN atv_line d ON d.id = a.line_id
 			LEFT JOIN atv_train e ON e.id = a.train_id
 			LEFT JOIN t_sys_user f ON f.user_name = a.submit_user_id
 			WHERE 1=1
 			    #[AND a.country_id = :countryId]
 			    #[AND a.area_id = :areaId]
 			    #[AND a.line_id = :lineId]
 			    #[AND a.fault_status = :faultStatus]
 			    #[AND a.worker_user_id = :workerUserId]
 			    #[AND a.sn = :sn]
 			    #[AND a.fault_source = :faultSource]
 			    #[AND a.create_time >= :beginTime]
 			    #[AND a.create_time <= :endTime]
 			ORDER BY a.is_finish asc,a.create_time desc
            ]]>
        </value>
    </sql>
    <!-- 依据车辆Id获取国家、地区、线路的编码信息 -->
    <sql id="atv_getCounrtyAreaLineCodeByTrainId">
        <value>
            <![CDATA[
            SELECT a.sn AS trainSn,b.sn AS lineSn,c.area_code,d.country_code
 			FROM atv_train a
 			LEFT JOIN atv_line b ON b.id = a.line_id
            LEFT JOIN atv_area c ON c.id = a.area_id
            LEFT JOIN atv_country d ON d.id = a.country_id
 			WHERE #[a.id = :trainId]
            ]]>
        </value>
    </sql>
    <!-- 远程广播 -->
    <sql id="atv_ttsList">
        <translate cache="dictTurnStatus" columns="turnStatusText" />
        <value>
            <![CDATA[
            SELECT a.*,
                   b.name AS countryIdName,c.name AS areaIdName,
                   d.name AS lineIdName,e.sn AS trainIdName,
                   a.turn_status as turnStatusText
            FROM atv_tts a
                LEFT JOIN atv_country b ON b.id = a.country_id
                LEFT JOIN atv_area c ON c.id = a.area_id
                LEFT JOIN atv_line d ON d.id = a.line_id
                LEFT JOIN atv_train e ON e.id = a.train_id
            WHERE 1=1 AND a.del_flag = '0'
                #[AND a.country_id = :countryId]
            	#[AND a.area_id = :areaId]
			    #[AND a.line_id = :lineId]
			    #[AND a.train_id = :trainId]
            ]]>
        </value>
    </sql>
    <!-- 设备信息列表 -->
    <sql id="atv_equipInfoList">
        <value>
            <![CDATA[
            SELECT a.*,b.sn AS carriageName,c.sn AS trainName
 			FROM atv_equipment a
 			LEFT JOIN atv_carriage b ON b.id = a.carriage_id
 			LEFT JOIN atv_train c ON c.id = a.train_id
 			WHERE 1=1
 			    #[AND a.country_id = :countryId]
            	#[AND a.area_id = :areaId]
			    #[AND a.line_id = :lineId]
			    #[AND a.train_id = :trainId]
			    #[AND a.equipment_type = :equipmentType]
			    #[AND a.online_status = :onlineStatus]  
            ]]>
        </value>
    </sql>
    <!-- 进销存报表 -->
    <sql id="atv_stockInOutReport">
        <translate cache="dictWarehouse" columns="warehouse_id" />
        <value>
            <![CDATA[
            SELECT  b.name AS productIdName,c.name AS productTypeIdName, 
            		a.warehouse_id,a.product_sn,a.specifications,a.unit,a.quantity,
            		a.initial_quantity,a.finish_quantity,
            	    CASE WHEN a.out_in=1 THEN a.quantity END AS inNum,
    			    CASE WHEN a.out_in =2 THEN a.quantity END AS outNum
            FROM atv_output_and_input_stock_item A
 			LEFT JOIN atv_product b ON b.id = a.product_id
            LEFT JOIN atv_product_type C ON c.id = a.product_type_id
           WHERE 1=1
 			    #[AND a.warehouse_id = :warehouseId]
 			    #[AND a.create_time >= :beginTime]
 			    #[AND a.create_time <= :endTime] 
 			    #[AND (a.product_sn LIKE :keyWord OR b.name LIKE :keyWord)]
            ]]>
        </value>
    </sql>
    <!-- 出入库单编码查询出入库单明细 -->
    <sql id="atv_stockInOutDetail">
        <translate cache="dictStockOutIn" columns="out_in" />
        <translate cache="dictWarehouse" columns="warehouse_id" />
        <translate cache="dictSupplier" columns="supplier_id" />
        <translate cache="dictUser" columns="user_id" />
        <value>
            <![CDATA[
            SELECT b.name AS productIdName,c.name AS productTypeIdName, 
            	a.warehouse_id,a.product_sn,a.specifications,a.unit,a.quantity,
            	a.supplier_id,a.out_in,a.create_time,a.user_id
            FROM atv_output_and_input_stock_item A
 			LEFT JOIN atv_product b ON b.id = a.product_id
            LEFT JOIN atv_product_type C ON c.id = a.product_type_id
           WHERE 1=1
 			    #[AND a.user_id = :userId]
 			    #[AND a.out_in = :outIn]
 			    #[AND a.warehouse_id = :warehouseId]
 			    #[AND a.supplier_id = :supplierId]
 			    #[AND a.product_type_id = :productTypeId]
 			    #[AND a.create_time >= :beginTime]
 			    #[AND a.create_time <= :endTime] 
   		        #[AND (a.product_sn LIKE :keyWord OR b.name LIKE :keyWord)]
            ]]>
        </value>
    </sql>
    <!-- 依据出入库单编码查询出入库单明细:   -->
    <sql id="atv_stockInOutDetailById">
        <value>
            <![CDATA[
            SELECT a.*,b.sn,b.name AS productIdName,c.name AS productTypeIdName
            FROM atv_output_and_input_stock_item a
 			LEFT JOIN atv_product b ON b.id = a.product_id
            LEFT JOIN atv_product_type c ON c.id = a.product_type_id
            WHERE 1=1
 			    #[AND a.output_and_input_stock_id = :id]
            ]]>
        </value>
    </sql>
    <!-- 查询仓库出入库单:  #[AND date_format(a.create_time,'%Y-%m-%d') >= :beginTime]  -->
    <sql id="atv_stockInOutList">
        <value>
            <![CDATA[
            SELECT a.id,a.warehouse_id,a.out_in,a.user_phone,a.sn,a.user_id,a.create_time
 			FROM atv_output_and_input_stock a
 			WHERE 1=1
 			    #[AND a.user_id = :userId]
 			    #[AND a.out_in = :outIn]
 			    #[AND a.sn = :sn]
 			    #[AND a.warehouse_id = :warehouseId]
 			    #[AND a.create_time >= :beginTime]
 			    #[AND a.create_time <= :endTime] 			    
            ]]>
        </value>
    </sql>
    <!-- 查询库存商品信息: IFNULL(b.name,a.warehouse_id) AS warehouseName,空的化不翻译  -->
    <sql id="atv_inventoryList">
        <value>
            <![CDATA[
            SELECT a.id,a.quantity,a.product_sn,
                   a.product_name ,a.unit,a.specifications,
                   IFNULL(b.name,a.warehouse_id) AS warehouseName,
                   IFNULL(d.name,a.product_type_id) AS productTypeName
            FROM atv_inventory a
                     LEFT JOIN atv_warehouse b ON b.id = a.warehouse_id
                     LEFT JOIN atv_product_type d ON d.id = a.product_type_id
            WHERE 1=1
                #[AND a.warehouse_id = :warehouseId]
 			    #[AND a.product_type_id = :productTypeId]
 			    #[AND (a.product_sn LIKE :keyWord OR a.product_name LIKE :keyWord)]
 			    #[AND a.quantity @value(:threshold) :thresholdValue]
            ]]>
        </value>
    </sql>
    <!-- 删除: 车辆及车厢(一对多) -->
    <sql id="atv_delTrainAndCarriage">
        <value>
            <![CDATA[
		    DELETE p.*, pp.* 
			FROM atv_train p 
        	LEFT JOIN atv_carriage pp ON p.id = pp.train_id
          	WHERE #[p.id IN (:id)]
           ]]>
        </value>
    </sql>
    <!-- 查询: 车辆详情1 -->
    <sql id="atv_trainDetail">
        <value>
            <![CDATA[
            SELECT a.remarks,a.sn AS trainIdName,
            b.name AS countryIdName,c.name AS areaIdName,
            d.name AS lineIdName
            FROM atv_train A
            LEFT JOIN atv_country b ON b.id = a.country_id
            LEFT JOIN atv_area c ON c.id = a.area_id
        	LEFT JOIN atv_line d ON d.id = a.line_id
 			WHERE 1=1
 			    #[AND a.country_id = :countryId]
 			    #[AND a.area_id = :areaId]
 			    #[AND a.line_id = :lineId]
 			    #[AND a.id = :id]
 			]]>
        </value>
    </sql>
    <!--2.28 -->
    <!-- 板卡档案 之 故障档案：获取板卡故障分析信息 -->
    <sql id="atv_getBoardCardFaultAnalyseById">
        <translate cache="dictBoardCardType" columns="boardCardType_dictText" />
        <translate cache="dictEquipType" columns="equipType_dictText" />
        <value>
            <![CDATA[
            SELECT a.*
                 ,a.board_card_type as boardCardType_dictText
                 ,a.equipment_type as equipType_dictText
            FROM atv_board_card_fault_analyse a
            WHERE 1=1
                #[AND a.board_card_fault_id = :id]
                #[AND a.create_time >= :dateRange[0]]
 			    #[AND a.create_time <= :dateRange[1]]
            ORDER BY a.create_time DESC
            ]]>
        </value>
    </sql>
    <!-- 板卡档案 之 故障档案：获取板卡故障处理信息 -->
    <sql id="atv_getBoardCardFaultHandleById">
        <translate cache="dictFaultStatus" columns="faultStatus_dictText" />
        <value>
            <![CDATA[
            SELECT a.*
                 ,a.fault_status as faultStatus_dictText
            FROM atv_board_card_fault_handle a
            WHERE 1=1
                #[AND a.board_card_fault_id = :id]
                #[AND a.create_time >= :dateRange[0]]
 			    #[AND a.create_time <= :dateRange[1]]
            ORDER BY a.create_time DESC
            ]]>
        </value>
    </sql>
    <!-- 板卡档案 之 故障档案：获取板卡故障信息 -->
    <sql id="atv_getBoardCardFaultInfoById">
        <translate cache="dictFaultStatus" columns="faultStatus_dictText" />
        <value>
            <![CDATA[
            SELECT a.*
                ,a.fault_status as faultStatus_dictText
                ,b.name as countryIdText,c.name as areaIdText,d.name as lineIdText
            FROM atv_board_card_fault a
                LEFT JOIN atv_country b ON b.id = a.country_id
                LEFT JOIN atv_area c ON c.id = a.area_id
                LEFT JOIN atv_line d ON d.id = a.line_id
            WHERE 1=1
                #[AND a.country_id = :countryId]
                #[AND a.area_id = :areaId]
                #[AND a.line_id = :lineId]
                #[AND a.train_id = :trainId]
                #[AND a.create_time >= :dateRange[0]]
 			    #[AND a.create_time <= :dateRange[1]]
            ORDER BY a.create_time DESC
            ]]>
        </value>
    </sql>
    <!-- 板卡档案 之 板卡测试档案：获取给定板卡号某一次测试详细信息 -->
    <sql id="atv_getBoardCardTestItemById">
        <value>
            <![CDATA[
            SELECT a.item,a.test_result,a.unit,a.reference_value
            FROM atv_board_card_test_item a
            WHERE 1=1
                #[AND a.board_card_test_id = :id]
            ORDER BY a.create_time DESC
            ]]>
        </value>
    </sql>
    <!-- 板卡档案 之 板卡测试档案：获取给定板卡号的测试历史信息 -->
    <sql id="atv_getBoardCardTestInfoById">
        <value>
            <![CDATA[
            SELECT a.id,a.tester,a.test_time,a.serial_no
                 ,b.sn
            FROM atv_board_card_test a
                     LEFT JOIN atv_board_card b ON b.id = a.board_card_id
            WHERE 1=1
                #[AND a.board_card_id=:id]
                #[AND a.tester = :tester]
                #[AND DATE_FORMAT(a.test_time,'%Y-%m-%d') >= :dateRange[0]]
                #[AND DATE_FORMAT(a.test_time,'%Y-%m-%d') <= :dateRange[1]]
            ORDER BY a.test_time DESC
            ]]>
        </value>
    </sql>
    <!-- 板卡档案 之 板卡测试档案：获取给定板卡号的特定信息 -->
    <sql id="atv_getBoardCardInfoById">
        <translate cache="dictBoardCardType" columns="boardCardType_dictText" />
        <value>
            <![CDATA[
            SELECT a.sn,a.board_card_type
                 ,a.board_card_type as boardCardType_dictText
                 ,c.name AS lineIdName
            FROM atv_board_card a
                     LEFT JOIN atv_board_card_test b ON a.id = b.board_card_id
                     LEFT JOIN atv_line c ON c.id = a.line_id
            WHERE 1=1
                #[AND a.id=:id]
            ]]>
        </value>
    </sql>

    <!--2.28 -->
    <!-- 查询: 某车辆拥有的所有板卡信息
         1）子SELECT查询某车辆所拥有的全部车厢的Id；
         2）查询满足条件全部板卡信息(板卡表中车厢的Id即a.carriage_id IN(子SELECT查询的结果))
     -->
    <sql id="atv_trainBoardCard">
        <value>
            <![CDATA[
            SELECT a.id,a.sn AS boardCardId,a.working_hours,a.board_card_type, 
                   d.sn AS equipmentIdName,
                   a.country_id,a.area_id,a.line_id, a.train_id
            FROM atv_board_card a
            LEFT JOIN atv_equipment d ON d.id = a.equipment_id 
            WHERE a.carriage_id IN (SELECT b.id
	      		FROM atv_carriage b
                LEFT JOIN atv_train c ON b.train_id = c.id
            	WHERE 1=1 #[AND c.id=:id])
 			]]>
        </value>
    </sql>
    <!-- 查询: 板卡档案 -->
    <sql id="atv_boardList">
        <value>
            <![CDATA[
            SELECT a.id,a.sn,a.working_hours,a.test_time,a.board_card_type,
              a.country_id,a.area_id,a.line_id, a.train_id,a.equipment_id,
              a.carriage_id,
            c.name AS areaIdName,
            d.name AS lineIdName, e.sn AS trainIdName,
            f.sn AS carriageIdName,g.sn AS equipmentIdName
            FROM atv_board_card a
 			LEFT JOIN atv_area c ON c.id = a.area_id
 			LEFT JOIN atv_line d ON d.id = a.line_id
 			LEFT JOIN atv_train e ON e.id = a.train_id
 			LEFT JOIN atv_carriage f ON f.id = a.carriage_id
 			LEFT JOIN atv_equipment g ON g.id = a.equipment_id
 			WHERE 1=1
 			    #[AND a.country_id = :countryId]
 			    #[AND a.area_id = :areaId]
 			    #[AND a.line_id = :lineId]
 			    #[AND a.sn = :sn]
 			    #[AND a.board_card_type = :boardCardType]
 			    #[AND a.test_time >= :beginTime]
 			    #[AND a.test_time <= :endTime]
 			ORDER BY a.test_time DESC
            ]]>
        </value>
    </sql>
    <!-- 查询: 版本测试贡藕给你信息 -->
    <sql id="atv_boardCardTest_COM">
        <translate cache="dictBoardCardType" columns="boardCardType_dictText" />
        <value>
            <![CDATA[
            SELECT b.sn,b.board_card_type
                 ,b.board_card_type as boardCardType_dictText
                 ,e.name AS lineIdName
            FROM atv_board_card b
            LEFT JOIN atv_board_card_test a ON b.id = a.board_card_id
            LEFT JOIN atv_line e ON e.id = a.line_id
            WHERE 1=1
                #[AND b.id=:id]
 		   ]]>
        </value>
    </sql>
    <!-- 查询: 板卡测试信息 -->
    <sql id="atv_boardCardTest_new">
        <value>
            <![CDATA[
            SELECT a.id,a.tester,a.test_time,a.serial_no
                    ,c.sn
            FROM atv_board_card_test a
            LEFT JOIN atv_board_card c ON c.id = a.board_card_id
            WHERE 1=1
                #[AND a.board_card_id=:id]
                #[AND a.tester = :tester]
                #[AND DATE_FORMAT(a.test_time,'%Y-%m-%d') >= :beginTime]
                #[AND DATE_FORMAT(a.test_time,'%Y-%m-%d') <= :endTime]
                ORDER BY a.test_time DESC
 		   ]]>
        </value>
    </sql>
    <sql id="atv_boardCardTestItem_new">
        <value>
            <![CDATA[
            SELECT a.item,a.test_result,a.unit,a.reference_value
              <!-- ,b.serial_no,c.sn -->
            FROM atv_board_card_test_item a
            <!-- LEFT JOIN atv_board_card_test b ON  b.id= a.board_card_test_id
            LEFT JOIN atv_board_card c ON c.id = b.board_card_id -->
            WHERE 1=1
               #[AND a.board_card_test_id = :id]
               ORDER BY a.create_time DESC
 			]]>
        </value>
    </sql>
    <!-- 查询: 板卡测试信息 原来的 -->
    <sql id="atv_boardCardTest">
        <translate cache="dictBoardCardType" columns="boardCardType_dictText" />
        <value>
            <![CDATA[
            SELECT a.tester,a.test_time,a.serial_no
                ,b.sn,b.board_card_type,b.board_card_type as boardCardType_dictText
                ,c.name AS countryIdName,d.name AS areaIdName,e.name AS lineIdName
            FROM atv_board_card b
            LEFT JOIN atv_board_card_test a ON b.id = a.board_card_id
            LEFT JOIN atv_country c ON c.id = b.country_id
            LEFT JOIN atv_area d ON d.id = b.area_id
        	LEFT JOIN atv_line e ON e.id = a.line_id
            WHERE 1=1
                #[AND b.id=:id]
 		   ]]>
        </value>
    </sql>
    <!-- 原来额  -->
    <sql id="atv_boardCardTestItem">
        <filters>
            <date-format params="testTime" format="yyyy-MM-dd"/>
        </filters>
        <value>
            <![CDATA[
            SELECT a.item,a.test_result,a.unit,a.reference_value
                 ,b.serial_no,b.tester,b.test_time
                 ,c.sn
            FROM atv_board_card_test_item a
            LEFT JOIN atv_board_card_test b ON  b.id= a.board_card_test_id
            LEFT JOIN atv_board_card c ON c.id = b.board_card_id
            WHERE 1=1
               #[AND b.board_card_id = :id]
               #[AND b.tester = :tester]
               #[AND DATE_FORMAT(b.test_time,'%Y-%m-%d') >= :beginTime]
               #[AND DATE_FORMAT(b.test_time,'%Y-%m-%d') <= :endTime]
               ORDER BY b.test_time DESC
 			]]>
        </value>
    </sql>
    <!-- 查询: 板卡测试详情23.2.17 新的用上边的替换  -->
    <sql id="atv_boardCardTestItem_OLD">
        <value>
            <![CDATA[
            SELECT a.serial_no,a.item,a.test_result,a.unit,a.reference_value
                  ,b.sn
            FROM atv_board_card_test_item a join atv_board_card_test b
            ON b.board_card_id=:id AND a.board_card_test_id = b.board_card_id
<!--            WHERE a.board_card_test_id IN (SELECT b.id-->
<!--	      		FROM atv_board_card_test b-->
<!--                WHERE 1=1 #[AND b.board_card_id=:id])-->
 			]]>
        </value>
    </sql>
    <!-- 根据列车号查询上一次报站时间-->
    <sql id="atv_LastTrainWorkingLogTime">
        <value>
            <![CDATA[
            SELECT a.*
            FROM atv_train_working_log a
            WHERE a.train_id = :trainId and a.pa_start_flag = 1
            ORDER BY a.create_time DESC
            LIMIT 1
            ]]>
        </value>
    </sql>
</sqltoy>