<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.3//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zw.platform.repository.vas.OilVehicleSettingDao">
	<!-- 查询油箱 -->
	<select id="findOilVehicleList" resultType="com.zw.platform.domain.vas.oilmassmgt.OilVehicleSetting">
		SELECT DISTINCT  ov.*, v.id AS vId, v.brand, vt.vehicle_type, v.group_id ,
		b.type, b.shape, b.box_length, b.width, b.height, b.thickness, b.theory_volume, b.real_volume,
		os.sensor_number,os.measuring_range,os.upper_blind_zone,os.filtering_factor,os.baud_rate,os.odd_even_check,
		os.compensation_can_make, v.create_data_time AS createDataTime FROM 
		zw_m_vehicle_info v
		
		INNER JOIN zw_m_config c ON c.vehicle_id = v.id AND c.flag = 1
		INNER JOIN zw_m_device_info di ON c.device_id = di.id AND di.flag = 1
		INNER JOIN zw_m_assignment_vehicle av ON v.id = av.vehicle_id AND av.flag = 1
		INNER JOIN zw_m_assignment a ON a.id = av.assignment_id AND a.flag = 1
		INNER JOIN zw_m_assignment_user au ON av.assignment_id =  au.assignment_id AND au.flag = 1

		LEFT JOIN zw_m_vehicle_type vt ON vt.id = v.vehicle_type AND vt.flag = 1
		LEFT JOIN zw_m_oil_box_vehicle ov ON ov.vehicle_id = v.id AND ov.flag = 1
		LEFT JOIN zw_m_oil_box b ON  b.id = ov.oil_box_id AND b.flag = 1
		LEFT JOIN zw_m_oil_rod_sensor_info os ON os.id = ov.sensor_type AND os.flag = 1
		WHERE v.flag = 1
		AND au.user_id = #{userId}
		AND v.brand NOT LIKE '扫%'
		AND a.org_id IN
		<foreach item="item" collection="groupList" open="(" separator=","
				close=")">
				#{item}
		</foreach>
		<if test="param != null">
		 	<if test="param.simpleQueryParam != null and param.simpleQueryParam!=''">
	            AND (v.brand LIKE CONCAT('%',#{param.simpleQueryParam},'%')
	            	 OR b.type LIKE CONCAT('%',#{param.simpleQueryParam},'%')
	            	)
			</if>
			<if test="param.groupId != null and param.groupId!=''">
				AND a.org_id = #{param.groupId}
			</if>
			<if test="param.assignmentId != null and param.assignmentId!=''">
				AND a.id = #{param.assignmentId}
			</if>

			<if test="param.protocol != null and param.protocol!=''">
				AND di.device_type = #{param.protocol}
			</if>
			<if test="param.vehicleId != null and param.vehicleId!=''">
				AND v.id = #{param.vehicleId}
			</if>

		</if>
		ORDER BY createDataTime DESC,v.id ASC ,ov.oil_box_type ASC
	</select>

	<select id="findOilVehicleList_COUNT" resultType="Long">
		SELECT count(0) FROM(
		SELECT DISTINCT  v.id AS vId, b.id AS bid
		FROM zw_m_vehicle_info v

		INNER JOIN zw_m_config c ON c.vehicle_id = v.id AND c.flag = 1
		INNER JOIN zw_m_device_info di ON c.device_id = di.id AND di.flag = 1 AND di.device_type = '1'
		INNER JOIN zw_m_assignment_vehicle av ON v.id = av.vehicle_id AND av.flag = 1
		INNER JOIN zw_m_assignment a ON a.id = av.assignment_id AND a.flag = 1
		INNER JOIN zw_m_assignment_user au ON av.assignment_id = au.assignment_id AND au.flag = 1
		AND au.user_id = #{userId}

		LEFT JOIN zw_m_oil_box_vehicle ov ON ov.vehicle_id = v.id AND ov.flag = 1
		LEFT JOIN zw_m_oil_box b ON  b.id = ov.oil_box_id AND b.flag=1
		WHERE v.flag = 1
		<if test="param != null">
			<if test="param.simpleQueryParam != null and param.simpleQueryParam!=''">
				AND (v.brand LIKE CONCAT('%',#{param.simpleQueryParam},'%')
				OR b.type LIKE CONCAT('%',#{param.simpleQueryParam},'%')
				)
			</if>

			<if test="param.assignmentId != null and param.assignmentId!=''">
				AND a.id = #{param.assignmentId}
			</if>

			<if test="param.protocol != null and param.protocol!=''">
				AND di.device_type = #{param.protocol}
			</if>
		</if>) table_count
	</select>

	<!--优化缓存使用方法  -->
	<select id="listOilVehicleByIds" resultType="com.zw.platform.domain.vas.oilmassmgt.OilVehicleSetting">
		SELECT DISTINCT ov.*, v.id AS vId, v.brand,
		b.type, b.shape, b.box_length, b.width, b.height, b.thickness, b.theory_volume, b.real_volume,b.buttom_radius,b.top_radius,
		os.sensor_number,os.measuring_range,os.upper_blind_zone,os.filtering_factor,os.baud_rate,os.odd_even_check,
		os.compensation_can_make, v.create_data_time AS createDataTime

		FROM
		(select id,brand,create_data_time from zw_m_vehicle_info where flag = 1 union all
		select id,people_number as brand,create_data_time from zw_m_people_info where flag = 1 union all
		select id,thing_number,create_data_time from zw_m_thing_info where flag = 1) v
		LEFT JOIN zw_m_oil_box_vehicle ov ON ov.vehicle_id = v.id AND ov.flag = 1
		<if test="tanks.size() > 0">
			AND ov.id IN
			<foreach collection="tanks" item="item" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
		LEFT JOIN zw_m_oil_box b ON b.id = ov.oil_box_id AND b.flag = 1
		LEFT JOIN zw_m_oil_rod_sensor_info os ON os.id = ov.sensor_type AND os.flag = 1
		WHERE 1 = 1
		<if test="list != null and list.size() > 0">
			AND v.id IN
			<foreach item="item" collection="list" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
	</select>
	
	<!--用于启动时，车辆油箱缓存加载  -->
	<select id="findBindingOilBoxList"  resultType="com.zw.platform.domain.vas.oilmassmgt.OilVehicleSetting">

 		SELECT  v.id, CONCAT(b.type, "#@!@#", ov.id) as type
		FROM zw_m_vehicle_info v
	    INNER JOIN zw_m_oil_box_vehicle ov ON ov.vehicle_id = v.id AND ov.flag = 1
        INNER JOIN zw_m_oil_box b ON  b.id = ov.oil_box_id AND b.flag = 1
	    WHERE v.flag =1 AND v.brand NOT LIKE '扫%'
      ORDER BY  ov.vehicle_id, ov.oil_box_type
	</select>

	<!-- 查询油箱车辆设置 -->
	<select id="findOilBoxVehicle"
		resultType="com.zw.platform.domain.vas.oilmassmgt.OilVehicleSetting">
		SELECT DISTINCT  ov.*, v.id AS vId, v.brand,
		b.type, b.shape, b.box_length, b.width, b.height, b.thickness, b.buttom_radius, b.top_radius, b.theory_volume, b.real_volume,
		os.sensor_number,os.sensor_length,os.measuring_range,os.upper_blind_zone,os.filtering_factor,os.baud_rate,os.odd_even_check,
		os.compensation_can_make FROM zw_m_oil_box_vehicle ov
		INNER JOIN zw_m_vehicle_info v ON v.id = ov.vehicle_id AND v.flag = 1
		INNER JOIN zw_m_oil_box b ON b.id = ov.oil_box_id AND b.flag = 1
		INNER JOIN zw_m_oil_rod_sensor_info os ON os.id = ov.sensor_type AND os.flag = 1
		INNER JOIN zw_m_config c ON c.vehicle_id = v.id AND c.flag = 1
		INNER JOIN zw_m_assignment_vehicle av ON v.id = av.vehicle_id AND av.flag = 1
		INNER JOIN zw_m_assignment a ON a.id = av.assignment_id AND a.flag = 1
		INNER JOIN zw_m_assignment_user au ON av.assignment_id =  au.assignment_id AND au.flag = 1
		WHERE ov.flag = 1
		AND au.user_id = #{userId}
		AND a.org_id IN
		<foreach item="item" collection="groupList" open="(" separator=","
				close=")">
				#{item}
		</foreach>
	</select>
	
	<select id="findVehicleSetting"
		resultType="com.zw.platform.domain.vas.oilmassmgt.OilVehicleSetting">
		SELECT DISTINCT ov.vehicle_id, v.brand
		FROM zw_m_oil_box_vehicle ov
		INNER JOIN
		(select id,brand from zw_m_vehicle_info where flag = 1 union all
		select id,people_number as brand from zw_m_people_info where flag = 1 union all
		select id,thing_number from zw_m_thing_info where flag = 1) v ON v.id = ov.vehicle_id
		INNER JOIN zw_m_oil_box b ON b.id = ov.oil_box_id AND b.flag = 1
		INNER JOIN zw_m_oil_rod_sensor_info os ON os.id = ov.sensor_type AND os.flag = 1
		INNER JOIN zw_m_config c ON c.vehicle_id = v.id AND c.flag = 1
		INNER JOIN zw_m_assignment_vehicle av ON v.id = av.vehicle_id AND av.flag = 1
		INNER JOIN zw_m_assignment a ON a.id = av.assignment_id AND a.flag = 1
		INNER JOIN zw_m_assignment_user au ON av.assignment_id =  au.assignment_id AND au.flag = 1
		WHERE ov.flag = 1
		AND au.user_id = #{userId}
	</select>

	<!-- 查询油箱车辆设置 -->
	<select id="findFuelTankList"
		resultType="com.zw.platform.domain.vas.oilmassmgt.FuelTank">
		SELECT DISTINCT ob.* FROM zw_m_oil_box ob
		WHERE ob.flag = 1 ORDER BY ob.type asc
	</select>

	<!-- 新增车辆油箱设置 -->
	  <insert id="addOilSetting" parameterType="com.zw.platform.domain.vas.oilmassmgt.form.OilVehicleSettingForm" >
	    insert into zw_m_oil_box_vehicle (id, oil_box_type, oil_box_id, vehicle_id, automatic_upload_time,
	      output_correction_coefficient_k, output_correction_coefficient_b,
	      add_oil_time_threshold, add_oil_amount_threshol,seep_oil_time_threshold,
	      seep_oil_amount_threshol,sensor_type,calibration_sets,
	      flag, create_data_time, create_data_username
	      )
	    values (#{id}, #{oilBoxType}, #{oilBoxId}, #{vehicleId}, #{automaticUploadTime},
	      #{outputCorrectionCoefficientK}, #{outputCorrectionCoefficientB},
	      #{addOilTimeThreshold}, #{addOilAmountThreshol}, #{seepOilTimeThreshold},
	      #{seepOilAmountThreshol}, #{sensorType}, #{calibrationSets},
	      #{flag}, #{createDataTime}, #{createDataUsername}
	      )
	  </insert>

	  <!-- 根据id删除 -->
	   <update id="deleteFuelTankBindById" parameterType="String" >
	    update zw_m_oil_box_vehicle
	    set flag = 0
	    where id = #{id}
	    and flag = 1
	  </update>

	  <!-- 查询油箱车辆设置 -->
	 <select id="findOilBoxVehicleByVid"
		resultType="com.zw.platform.domain.vas.oilmassmgt.OilVehicleSetting">
		SELECT DISTINCT  ov.*, v.id AS vId, v.brand,
		b.type, b.shape, b.box_length, b.width, b.height, b.thickness, b.theory_volume, b.real_volume, b.buttom_radius, b.top_radius,
		os.sensor_number,os.sensor_length,os.measuring_range,os.upper_blind_zone,os.filtering_factor,os.baud_rate,os.odd_even_check,
		os.compensation_can_make, v.fuel_type AS fuelOil
		FROM zw_m_oil_box_vehicle ov
		INNER JOIN
		(select id,brand,fuel_type from zw_m_vehicle_info where flag = 1 union all
		 select id,people_number as brand,'' as fuel_type from zw_m_people_info where flag = 1 union all
		 select id,thing_number,'' as fuel_type from zw_m_thing_info where flag = 1) v ON v.id = ov.vehicle_id
		INNER JOIN zw_m_oil_box b ON b.id = ov.oil_box_id AND b.flag = 1
		INNER JOIN zw_m_oil_rod_sensor_info os ON os.id = ov.sensor_type AND os.flag = 1
		WHERE ov.flag = 1
		AND v.id = #{vId}
		ORDER BY ov.oil_box_type desc
	 </select>

	 <!-- 查询油箱车辆设置 -->
	 <select id="findOilBoxVehicleByBindId"
		resultType="com.zw.platform.domain.vas.oilmassmgt.OilVehicleSetting">
				SELECT DISTINCT  ov.*, v.id AS vId, v.brand,
		b.type, b.shape, b.box_length, b.width, b.height, b.thickness, b.theory_volume, b.real_volume,
		os.sensor_number,os.sensor_length,os.measuring_range,os.upper_blind_zone,os.filtering_factor,os.baud_rate,os.odd_even_check,
		os.compensation_can_make, ft.fuel_category AS fuelOil
		FROM zw_m_oil_box_vehicle ov
		INNER JOIN
		(select id,brand,fuel_type from zw_m_vehicle_info where flag = 1 union all
		select id,people_number as brand,'' as fuel_type from zw_m_people_info where flag = 1 union all
		select id,thing_number,'' as fuel_type from zw_m_thing_info where flag = 1) v ON v.id = ov.vehicle_id
		INNER JOIN zw_m_oil_box b ON b.id = ov.oil_box_id AND b.flag = 1
		INNER JOIN zw_m_oil_rod_sensor_info os ON os.id = ov.sensor_type AND os.flag = 1
		left join zw_m_fuel_type ft on ft.id = v.fuel_type and ft.flag = 1
		WHERE ov.flag = 1
		AND ov.id = #{id}
	 </select>

	   <update id="updateOilSetting" parameterType="com.zw.platform.domain.vas.oilmassmgt.form.OilVehicleSettingForm" >
		    update zw_m_oil_box_vehicle
		    set
		      oil_box_type = #{oilBoxType},
		      oil_box_id = #{oilBoxId},
		      vehicle_id = #{vehicleId},
		      sensor_type = #{sensorType},
		      calibration_sets = #{calibrationSets},
		      calibration_status = '0',
		      automatic_upload_time = #{automaticUploadTime},
		      output_correction_coefficient_k = #{outputCorrectionCoefficientK},
		      output_correction_coefficient_b = #{outputCorrectionCoefficientB},
		      add_oil_time_threshold = #{addOilTimeThreshold},
		      add_oil_amount_threshol = #{addOilAmountThreshol},
		      seep_oil_time_threshold = #{seepOilTimeThreshold},
		      seep_oil_amount_threshol = #{seepOilAmountThreshol},
		      flag = #{flag},
		      update_data_time = #{updateDataTime},
		      update_data_username = #{updateDataUsername}
		      where id =  #{id}
		</update>

	<update id="updateParamOilSetting" parameterType="com.zw.platform.domain.vas.oilmassmgt.form.OilVehicleSettingForm" >
		update zw_m_oil_box_vehicle
		set
		automatic_upload_time = #{automaticUploadTime},
		output_correction_coefficient_k = #{outputCorrectionCoefficientK},
		output_correction_coefficient_b = #{outputCorrectionCoefficientB},
		add_oil_time_threshold = #{addOilTimeThreshold},
		add_oil_amount_threshol = #{addOilAmountThreshol},
		seep_oil_time_threshold = #{seepOilTimeThreshold},
		seep_oil_amount_threshol = #{seepOilAmountThreshol},
		update_data_time = #{updateDataTime},
		update_data_username = #{updateDataUsername}
		where flag = 1 and id =  #{id}
	</update>


		<select id="selectOilVehicleById" parameterType="String"
		resultType="com.zw.platform.domain.vas.oilmassmgt.OilVehicleSetting">
			SELECT * from zw_m_oil_box_vehicle WHERE flag = 1 AND id = #{id}
		</select>

		<!-- 修改油箱2 to 油箱1 -->
	   <update id="updateOil2ToOil1" parameterType="String" >
	    UPDATE zw_m_oil_box_vehicle
	    SET oil_box_type = 1
	    WHERE id = #{id}
	    AND flag = 1
	  </update>

	   <!-- 根据油箱id删除 -->
	   <update id="deleteByOilTankId" parameterType="String" >
	    update zw_m_oil_box_vehicle
	    set flag = 0
	    where oil_box_id = #{oilBoxId}
	  </update>

	  <!-- 根据车辆id删除油箱与车辆的关联 -->
	   <update id="deleteOilSettingByVid" parameterType="String" >
	    update zw_m_oil_box_vehicle
	    set flag = 0
	    where vehicle_id = #{vehicleId}
	  </update>

	<update id="deleteBatchOilSettingByVid" >
	    update zw_m_oil_box_vehicle
	    set flag = 0
	    where vehicle_id in
		<foreach collection="monitorIds" item="monitorId" open="(" separator="," close=")">
			#{monitorId}
		</foreach>
	  </update>
</mapper>
