<?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.iformall.mapper.WxCarCmdLogMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCarCmdLog">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="vendor_type" jdbcType="INTEGER" property="vendorType"/>
		<result column="cmd_type" jdbcType="INTEGER" property="cmdType"/>
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
		<result column="cmd_json" jdbcType="VARCHAR" property="cmdJson"/>
	</resultMap>

	<sql id="allColumns">
	    `id`,`tenant_id`,`vendor_type`,`cmd_type`,`create_date`,`update_date`,`cmd_json` 
	</sql>
	
	<sql id="dynamicWhereConditions">
		where 1 = 1
		
		<if test=" null != id ">
			and `id` = #{id}
		
		</if>
		
		<if test=" null != tenantId ">
			and `tenant_id` = #{tenantId}
		
		</if>
		
		<if test=" null != vendorType ">
			and `vendor_type` = #{vendorType}
		
		</if>
		
		<if test=" null != cmdType ">
			and `cmd_type` = #{cmdType}
		
		</if>
		
		<if test=" null != createDate ">
			and `create_date` = #{createDate}
		
		</if>
		
		<if test=" null != updateDate ">
			and `update_date` = #{updateDate}
		
		</if>
		
		<if test=" null != cmdJson ">
			and `cmd_json` like concat('%', #{cmdJson},'%')
		
		</if>
		<if test=" null != ids ">
			and id in
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test=" null != sortColumns">order by ${sortColumns}</if>
	</sql>
	
	<select id="findList" parameterType="com.iformall.domain.po.WxCarCmdLog" resultMap="BaseResultMap">
		select
		<include refid="allColumns"/>
		from wx_car_cmd_log
		<include refid="dynamicWhereConditions"/>
	</select>
	
	<select id="queryHistory" resultType="hashmap" parameterType="hashmap">
		select count(c.id) carcount,c.create_date from (
		select id,DATE_FORMAT(create_date,'%Y-%m-%d') create_date
		 from wx_car_cmd_log where tenant_id = #{tenantId} and cmd_type=#{cmdType}
		 and  create_date BETWEEN #{startdate} and #{enddate}
		) c
		group by c.create_date
	</select>
	
	<select id="queryTodayCar" resultType="hashmap" parameterType="hashmap">
		select count(c.id) carcount,c.create_time from (
		select id,DATE_FORMAT(DATE_ADD(create_date,INTERVAL 1 HOUR),'%H:00') create_time
		from wx_car_cmd_log where tenant_id = #{tenantId} and cmd_type=#{cmdType}
		and  create_date BETWEEN #{startdate} and #{enddate}
		) c group by c.create_time order by c.create_time
	</select>

	<select id="queryWeekCarPayCount" resultType="Long" parameterType="hashmap">
		select count(id)
		from wx_car_cmd_log where tenant_id = #{tenantId} and cmd_type=#{cmdType}
		and create_date BETWEEN #{startdate} and #{enddate}
	</select>

	<select id="queryWeekCarPay" resultType="hashmap" parameterType="hashmap">
		select sum(fee) fee,c.create_time from (
		select id,DATE_FORMAT(create_date,'%m/%d') create_time,CONVERT(JSON_EXTRACT(cmd_json,'$.fee'), Decimal(16,2)) fee
		from wx_car_cmd_log where tenant_id = #{tenantId} and cmd_type=#{cmdType}
		and  create_date BETWEEN #{startdate} and #{enddate}
		) c group by c.create_time order by c.create_time
	</select>

	<select id="queryForSceneRepotyHistoryCar" resultType="com.iformall.domain.vo.MarkingSceneDataVo" parameterType="hashmap">
		select DATE_FORMAT(create_date,'%Y-%m-%d') xTime, count(c.id) triggerCount
		from wx_car_cmd_log c where tenant_id = #{tenantId} and cmd_type=#{cmdType}
		<if test=" null != startTime and null != endTime">
		and  create_date BETWEEN #{startTime} and #{endTime}
		</if>
		 group by xTime
	</select>
	
	
	<select id="queryPlateNumberList" parameterType="hashmap" resultType="hashmap">
		select json_unquote(JSON_EXTRACT(cmd_json,'$.plateNumber')) plateNumber from wx_car_cmd_log where tenant_id=#{tenantId}
		<if test=" null != startdate and null != enddate">
			and  create_date BETWEEN #{startdate} and #{enddate}
		</if>
		and json_unquote(JSON_EXTRACT(cmd_json,'$.plateNumber')) is not null
		group by plateNumber
	</select>

	<select id="queryTenantIdBySynId" parameterType="hashmap" resultType="String">
		select `tenant_id` from wx_car_cmd_log
		where cmd_type = 601 and json_extract(cmd_json, "$.synId") = #{synId}
	</select>
	

</mapper>
