<?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.elephant.framework.galaxy.aroad.module.roadcondition.mapper.RoadConditionEventMapper">

	<resultMap id="roadConditionEventMap"
	           type="com.elephant.framework.galaxy.aroad.module.roadcondition.entity.RoadConditionEvent">
		<id property="id" column="id"/>
		<result property="roadCode" column="road_code"/>
		<result property="roadName" column="road_name"/>
		<result property="startm" column="startM"/>
		<result property="endm" column="endM"/>
		<result property="eventType" column="event_type"/>
		<result property="eventTitle" column="event_title"/>
		<result property="eventContent" column="event_content"/>
		<result property="influence" column="influence"/>
		<result property="carsLength" column="cars_length"/>
		<result property="solution" column="solution"/>
		<result property="startTime" column="start_time"/>
		<result property="startUnit" column="start_unit"/>
		<result property="startPerson" column="start_person"/>
		<result property="preEndTime" column="pre_end_time"/>
		<result property="endTime" column="end_time"/>
		<result property="endUnit" column="end_unit"/>
		<result property="endReason" column="end_reason"/>
	</resultMap>

	<resultMap id="staticByEventTypeMap"
	           type="com.elephant.framework.galaxy.aroad.module.roadcondition.dto.staticByEventTypeDto">
		<result property="count" column="count"/>
		<result property="eventType" column="event_type"/>
	</resultMap>

	<resultMap id="staticByInfluenceMap"
	           type="com.elephant.framework.galaxy.aroad.module.roadcondition.dto.staticByInfluenceDto">
		<result property="count" column="count"/>
		<result property="influence" column="influence"/>
	</resultMap>

	<resultMap id="staticByMonthMap"
	           type="com.elephant.framework.galaxy.aroad.module.roadcondition.dto.staticByMonthDto">
		<result property="count" column="count"/>
		<result property="year" column="year"/>
		<result property="month" column="month"/>
	</resultMap>

	<resultMap id="staticByRoadCodeMap"
	           type="com.elephant.framework.galaxy.aroad.module.roadcondition.dto.staticByRoadCodeDto">
		<result property="count" column="count"/>
		<result property="roadcode" column="roadcode"/>
	</resultMap>

	<resultMap id="staticByCityMap"
	           type="com.elephant.framework.galaxy.aroad.module.roadcondition.dto.staticByCityDto">
		<result property="count" column="count"/>
		<result property="city" column="city"/>
	</resultMap>

	<resultMap id="staticByYHCityMap"
	           type="com.elephant.framework.galaxy.aroad.module.roadcondition.dto.staticByYHCityDto">
		<result property="count" column="count"/>
		<result property="startunit" column="startunit"/>
	</resultMap>

	<resultMap id="citysWithOrganizesMap"
	           type="com.elephant.framework.galaxy.aroad.module.roadcondition.dto.CityNameDto">
		<result property="cityName" column="city"/>
		<result property="organizeName" column="organize"/>
	</resultMap>

	<select id="getRoadConditionEventPage" resultMap="roadConditionEventMap"
	        parameterType="com.elephant.framework.galaxy.aroad.module.roadcondition.entity.RoadConditionEvent">
		SELECT
		id,road_code,concat(road_code,'·',road_name) as
		road_name,startm,endm,event_type,event_title,event_content,influence,cars_length,solution,start_time,start_unit,start_person,pre_end_time,end_time,end_unit,end_reason
		FROM road_condition_event
		<where>
			<if test="roadConditionEvent.startUnit != null">start_unit = #{roadConditionEvent.startUnit} and</if>
			<if test="roadConditionEvent.eventType != null">event_type = #{roadConditionEvent.eventType} and</if>
			<if test="roadConditionEvent.influence != null">influence = #{roadConditionEvent.influence} and</if>
			<if test="roadConditionEvent.carsLength != null">cars_length = #{roadConditionEvent.carsLength} and</if>
			<if test="roadConditionEvent.startTime != null">start_time &gt; #{roadConditionEvent.startTime} and</if>
			<if test="roadConditionEvent.endTime != null">end_time &lt; #{roadConditionEvent.endTime} and</if>
			road_name is not null
		</where>
	</select>

	<select id="getById" resultMap="roadConditionEventMap">
		SELECT *
		FROM road_condition_event
		where id = #{id}
	</select>

	<select id="staticByEventType" resultMap="staticByEventTypeMap">
		SELECT count(*) as count,
		       event_type
		FROM road_condition_event
		GROUP BY event_type
	</select>

	<select id="staticByInfluence" resultMap="staticByInfluenceMap">
		SELECT count(*) AS count,
		       influence
		FROM road_condition_event
		GROUP BY influence
	</select>

	<select id="staticByMonth" resultMap="staticByMonthMap">
		SELECT count(*)          AS count,
		       YEAR(start_time)  AS year,
		       MONTH(start_time) AS month
		FROM road_condition_event
		WHERE event_type = '养护施工'
		GROUP BY YEAR(start_time),
		         MONTH(start_time)
	</select>

	<select id="staticByRoadCode" resultMap="staticByRoadCodeMap">
		SELECT count(*)  AS count,
		       road_code as roadcode
		FROM road_condition_event
		WHERE event_type = '养护施工'
		GROUP BY road_code
	</select>

	<select id="staticByCity" resultMap="staticByCityMap">
		SELECT count(shortName) as count,
		       city
		FROM base_organize
		WHERE city IS NOT NULL
		  AND city != '山东'
		GROUP BY city
	</select>

	<select id="staticByYHCity" resultMap="staticByYHCityMap">
		SELECT count(*)   as count,
		       start_unit as startunit
		FROM road_condition_event
		WHERE event_type = '养护施工'
		GROUP BY start_unit
	</select>

	<select id="getTree" resultMap="citysWithOrganizesMap">
		SELECT city,
		       fullname as organize
		FROM base_organize
		WHERE city IS NOT NULL
		  AND city != '山东'
		GROUP BY city,
		         fullname
	</select>

</mapper>
