<?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.WxCUserCarMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCUserCar">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
		<result column="c_user_id" jdbcType="BIGINT" property="cUserId" />
		<result column="car_number" jdbcType="VARCHAR" property="carNumber" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="vendor_type" jdbcType="INTEGER" property="vendorType" />
		<result column="vendor_params" jdbcType="VARCHAR" property="vendorParams" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`c_user_id`,`car_number`,`create_date`,`update_date`,`vendor_type`,`vendor_params` 
	</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 != cUserId "> 
		and `c_user_id` = #{cUserId} 
       	
		</if>    
	    
		<if test=" null != carNumber "> 
		and `car_number`  like concat('%', #{carNumber},'%')
       	
		</if>    
	    
		<if test=" null != createDate "> 
		and `create_date` = #{createDate} 
       	
		</if>    
	    
		<if test=" null != updateDate "> 
		and `update_date` = #{updateDate} 
       	
		</if>    
	    
		<if test=" null != vendorType "> 
		and `vendor_type` = #{vendorType} 
       	
		</if>    
	    
		<if test=" null != vendorParams "> 
		and `vendor_params`  like concat('%', #{vendorParams},'%')
       	
		</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.WxCUserCar" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_c_user_car
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="countList" parameterType="com.iformall.domain.po.WxCUserCar" resultType="java.lang.Integer">
		select count(*) from wx_c_user_car
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="countUser" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Integer">
		select count(DISTINCT ubi.id) from wx_c_user_car uc, wx_c_user_basic_info ubi
		where uc.c_user_id = ubi.id
		<if test=" null != tenantId ">
			and uc.tenant_id  = #{tenantId}
			and ubi.tenant_id = #{tenantId}
		</if>
		<if test=" null != startTime ">
			and ubi.create_date &gt;= #{startTime}
		</if>
		<if test=" null != endTime">
			and ubi.create_date &lt; #{endTime}
		</if>

	</select>
	
	<select id="queryCarCount" parameterType="hashmap" resultType="long">
		select count(id) from wx_c_user_car where tenant_id=#{tenantId} and create_date between #{startdate} and #{enddate}
	</select>

	<select id="queryCarCountHistory" resultType="hashmap" parameterType="hashmap">
		SELECT count(DISTINCT c.id) count, c.create_time FROM
		(SELECT ubi.id as id, DATE_FORMAT(uc.create_date, '%m/%d') create_time FROM wx_c_user_car uc, wx_c_user_basic_info ubi WHERE uc.c_user_id = ubi.id
		AND uc.tenant_id = #{tenantId} AND ubi.tenant_id = #{tenantId} AND ubi.create_date BETWEEN #{startdate} AND #{enddate}) c
		GROUP BY c.create_time ORDER BY c.create_time
	</select>
</mapper>
