<?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.sv.mapper.VenueMapper">

    <resultMap id="VenueMap" type="com.sv.entity.Venue">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" property="name"/>
        <result column="address" property="address"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="type" property="type"/>
        <result column="list_image" property="listImage"/>
        <result column="business_time" property="businessTime"/>
        <result column="business_start_time" property="businessStartTime"></result>
        <result column="business_end_time" property="businessEndTime"></result>
        <result column="business_start_date" property="businessStartDate"></result>
        <result column="business_end_date" property="businessEndDate"></result>
        <result column="price" property="price"/>
        <result column="contact" property="contact"/>
        <result column="phone" property="phone"/>
        <result column="status" property="status"></result>
        <result column="description" property="description"/>
        <result column="platform_id" property="platformId"/>
        <result column="card_content" property="cardContent"/>
        <result column="created_id" property="createdId"/>
        <result column="modified_id" property="modifiedId"/>
        <result column="created_time" property="createdTime"/>
        <result column="modified_time" property="modifiedTime"/>
        <result column="code_url" property="codeUrl"></result>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <resultMap id="VenueDtoMap" type="com.sv.dto.api.VenueDTO">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" property="name"/>
        <result column="address" property="address"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="type" property="type"/>
        <result column="list_image" property="listImage"/>
        <result column="business_time" property="businessTime"/>
        <result column="price" property="price"/>
        <result column="contact" property="contact"/>
        <result column="phone" property="phone"/>
        <result column="description" property="description"/>
        <result column="platform_id" property="platformId"/>
        <result column="card_content" property="cardContent"/>
        <result column="created_id" property="createdId"/>
        <result column="modified_id" property="modifiedId"/>
        <result column="created_time" property="createdTime"/>
        <result column="modified_time" property="modifiedTime"/>
        <result column="deleted" property="deleted"/>
        <result column="code_url" property="codeUrl"></result>
        <association property="cards" select="com.sv.mapper.VenueCardMapper.findByVenueId"
                     column="{venueId = id,venueType=type}"></association>
        <association property="images" select="com.sv.mapper.VenueImageMapper.findByVenueId"
                     column="{venueId = id,venueType=type}"></association>
        <association property="venuePriceList" select="com.sv.mapper.VenuePriceMapper.findByVenueId"
                     column="{venueId = id,venueType=type}"></association>
    </resultMap>

    <resultMap id="MemberCardVenueDtoMap" type="com.sv.dto.api.MemberCardVenuesDTO" extends="VenueMap">
        <result column="memberId" property="memberId" jdbcType="INTEGER"></result>
        <association property="memberCards" select="com.sv.mapper.MemberCardMapper.findAllCards"
                     column="{memberId=memberId,venueId = id}"></association>
    </resultMap>

    <resultMap id="VenuesDTOMap" type="com.sv.dto.api.VenueDTO" extends="VenueMap">

    </resultMap>

    <!--表名 -->
    <resultMap id="VenueOMSDTOMap" type="com.sv.dto.oms.VenueOmsDTO" extends="VenueMap">
        <collection property="images" select="findImagesById" column="id"></collection>
    </resultMap>
    <!--表名 -->
    <sql id="tableName">
		sv_venue
	</sql>

    <!-- 字段 -->
    <sql id="Field">
		id,
		number,
		name,
		address,
		longitude,
		latitude,
		type,
		status,
		list_image,
		business_time,
		business_start_time,
		business_end_time,
		business_start_date,
		business_end_date,
		price,
		contact,
		phone,
		description,
		platform_id,
		card_content,
		created_id,
		modified_id,
		created_time,
		modified_time,
		code_url,
		deleted
	</sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{name, jdbcType=VARCHAR},
		#{address, jdbcType=VARCHAR},
		#{longitude, jdbcType=VARCHAR},
		#{latitude, jdbcType=VARCHAR},
		#{type, jdbcType=TINYINT},
		#{listImage, jdbcType=VARCHAR},
		#{businessTime, jdbcType=VARCHAR},
		#{price, jdbcType=DECIMAL},
		#{contact, jdbcType=VARCHAR},
		#{phone, jdbcType=VARCHAR},
		#{description, jdbcType=LONGTEXT},
		#{platformId, jdbcType=INTEGER},
		#{cardContent, jdbcType=INTEGER},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT}
	</sql>

    <!-- 查询全部记录 -->
    <select id="findAll" resultMap="VenueMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE
        deleted = 0
    </select>

    <!-- 查询全部记录 -->
    <select id="findAlls" resultMap="VenueMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE
        deleted = 0
        AND status = 0
    </select>

    <!-- api查询全部记录 -->
    <select id="findAllVenues" resultMap="VenuesDTOMap">
        SELECT
        id,
        list_image,
        `name`,
        address,
        longitude,
        latitude

        FROM
        sv_venue
        WHERE
        `type` = #{type}
        <if test="geohash != null">
            AND geo_hash LIKE CONCAT(#{geohash, jdbcType=VARCHAR}, '%')
        </if>
        AND deleted = 0
        AND `status` = 0
    </select>

    <!-- 新增-->
    <insert id="insert" parameterType="com.sv.entity.Venue" useGeneratedKeys="true" keyProperty="id">

        INSERT INTO
        <include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="address != null">
                address,
            </if>
            <if test="longitude != null">
                longitude,
            </if>
            <if test="latitude != null">
                latitude,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="listImage != null">
                list_image,
            </if>
            <if test="businessTime != null">
                business_time,
            </if>
            <if test="price != null">
                price,
            </if>
            <if test="contact != null">
                contact,
            </if>
            <if test="phone != null">
                phone,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="geoHash != null">
                geo_hash,
            </if>
            <if test="platformId != null">
                platform_id,
            </if>
            <if test="cardContent != null">
                card_content,
            </if>
            <if test="businessStartTime != null">
                business_start_time ,
            </if>
            <if test="businessEndTime != null">
                business_end_time,
            </if>
            <if test="businessStartDate != null">
                business_start_date ,
            </if>
            <if test="businessEndDate != null">
                business_end_date,
            </if>
            <if test="createdId != null">
                created_id,
            </if>
            <if test="modifiedId != null">
                modified_id,
            </if>
            <if test="createdTime != null">
                created_time,
            </if>
            <if test="modifiedTime != null">
                modified_time,
            </if>
            <if test="deleted != null">
                deleted,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="address != null">
                #{address},
            </if>
            <if test="longitude != null">
                #{longitude},
            </if>
            <if test="latitude != null">
                #{latitude},
            </if>
            <if test="type != null">
                #{type},
            </if>
            <if test="listImage != null">
                #{listImage},
            </if>
            <if test="businessTime != null">
                #{businessTime},
            </if>
            <if test="price != null">
                #{price},
            </if>
            <if test="contact != null">
                #{contact},
            </if>
            <if test="phone != null">
                #{phone},
            </if>
            <if test="description != null">
                #{description},
            </if>
            <if test="geoHash != null">
                #{geoHash},
            </if>
            <if test="platformId != null">
                #{platformId},
            </if>
            <if test="cardContent != null">
                #{cardContent},
            </if>
            <if test="businessStartTime != null">
                #{businessStartTime} ,
            </if>
            <if test="businessEndTime != null">
                #{businessEndTime},
            </if>
            <if test="businessStartDate != null">
                #{businessStartDate} ,
            </if>
            <if test="businessEndDate != null">
                #{businessEndDate},
            </if>
            <if test="createdId != null">
                #{createdId},
            </if>
            <if test="modifiedId != null">
                #{modifiedId},
            </if>
            <if test="createdTime != null">
                #{createdTime},
            </if>
            <if test="modifiedTime != null">
                #{modifiedTime},
            </if>
            <if test="deleted != null">
                #{deleted},
            </if>
        </trim>
    </insert>


    <!-- 删除-->
    <delete id="delete" parameterType="java.lang.Integer">
        UPDATE
        <include refid="tableName"></include>
        SET
        deleted = 1
        WHERE
        id = #{id}
    </delete>

    <!-- 批量删除 -->
    <delete id="deleteByIds">
        UPDATE
        <include refid="tableName"></include>
        SET
        deleted = 1
        WHERE
        id IN
        <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>


    <!-- 更新记录 -->
    <update id="update" parameterType="com.sv.entity.Venue">
        UPDATE
        <include refid="tableName"></include>
        <set>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="address != null">
                address = #{address},
            </if>
            <if test="longitude != null">
                longitude = #{longitude},
            </if>
            <if test="latitude != null">
                latitude = #{latitude},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="listImage != null">
                list_image = #{listImage},
            </if>
            <if test="businessTime != null">
                business_time = #{businessTime},
            </if>
            <if test="businessStartTime != null">
                business_start_time = #{businessStartTime},
            </if>
            <if test="businessEndTime != null">
                business_end_time = #{businessEndTime},
            </if>
            <if test="businessStartDate != null">
                business_start_date = #{businessStartDate},
            </if>
            <if test="businessEndDate != null">
                business_end_date = #{businessEndDate},
            </if>
            <if test="price != null">
                price = #{price},
            </if>
            <if test="contact != null">
                contact = #{contact},
            </if>
            <if test="phone != null">
                phone = #{phone},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <if test="geoHash != null">
                geo_hash = #{geoHash},
            </if>
            <if test="platformId != null">
                platform_id = #{platformId},
            </if>
            <if test="cardContent != null">
                card_content = #{cardContent},
            </if>
            <if test="createdId != null">
                created_id = #{createdId},
            </if>
            <if test="modifiedId != null">
                modified_id = #{modifiedId},
            </if>
            <if test="createdTime != null">
                created_time = #{createdTime},
            </if>
            <if test="modifiedTime != null">
                modified_time = #{modifiedTime},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="codeUrl != null">
                code_url = #{codeUrl},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 通过编号查询 -->
    <select id="findById" parameterType="java.lang.Integer" resultMap="VenueMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE id = #{id} and deleted = 0
    </select>

    <select id="findOmsDTOById" parameterType="java.lang.Integer" resultMap="VenueOMSDTOMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE id = #{id}
    </select>

    <!-- api 通过编号查询 -->
    <select id="findByVenueId" parameterType="java.lang.Integer" resultMap="VenueDtoMap">
		SELECT
			id,
			`name`,
			address,
			`type`,
			business_time,
			price,
			contact,
			phone,
			description,
			longitude,
			latitude,
			card_content
		FROM
			sv_venue
		WHERE
			id = #{id}
		AND deleted = 0
	</select>

    <select id="findImagesById" resultType="java.lang.String">
        SELECT url from sv_venue_image
        where venue_id = #{id} and deleted = 0
    </select>

    <!-- api 查询我的会员卡 -->
    <select id="findByMemberCard" resultMap="MemberCardVenueDtoMap">
       SELECT
            ve.`name`,
            ve.id,
            ve.card_content,
            sc.member_id AS memberId,
            ve.card_content as cardContent
        FROM
            sv_member_card AS sc
        JOIN sv_venue AS ve ON sc.venue_id = ve.id
        AND ve.platform_id = #{platformId}
        AND sc.member_id = #{memberId}
        AND sc.deleted = 0
        GROUP BY ve.id
    </select>

    <update id="updateStatus">
        UPDATE sv_venue
        SET status = #{status}
        where id = #{id}
    </update>

    <!-- api 查询地址-->
    <select id="findAddress" resultMap="VenueMap">
        SELECT
            address,
            name,
            longitude,
            latitude
         from sv_venue WHERE id = #{id}
    </select>

    <select id="findByType" resultMap="VenueMap">
        SELECT * from sv_venue
        WHERE type = #{venueType} and deleted = 0
    </select>

    <select id="countByMember" resultType="java.lang.Integer">
      select count(0) from sv_venue_member where member_id = #{memberId} and venue_id = #{venueId}
    </select>

    <insert id="addNumber">
        update sv_venue set number = number + #{number} where id = #{venueId}
    </insert>

    <insert id="updateNumber">
        update sv_venue set number = #{number} where id = #{venueId}
    </insert>

    <insert id="addMember">
        INSERT INTO sv_venue_member (`member_id`, `venue_id`) VALUES (#{memberId},#{venueId})
    </insert>

    <delete id="deleteMember">
        delete  from sv_venue_member where member_id = #{memberId} and venue_id = #{venueId}
    </delete>
</mapper>