<?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="MdeHotelCompanyMapper">
    <resultMap type="com.wechat.model.dbEntity.MdeHotelCompany" id="MdeHotelCompany">
    	<id column="hotel_id" property="hotelId"/>
		<result column="subbrand_id" property="subBrandId"/>
		<result column="hotel_name_cn" property="hotelNameCn"/>
		<result column="hotel_name_en" property="hotelNameEn"/>
		<result column="hotel_country" property="hotelCountry"/>
		<result column="hotel_province" property="hotelProvince"/>
		<result column="hotel_city" property="hotelCity"/>
		<result column="hotel_area" property="hotelArea"/>
		<result column="hotel_zipcode" property="hotelZipcode"/>
		
		<result column="hotel_address_cn" property="hotelAddressCn"/>
		<result column="hotel_address_en" property="hotelAddressEn"/>
		<result column="hotel_longitude"  property="hotelLongitude"/>
		<result column="hotel_latitude"   property="hotelLatitude"/>
		
		<result column="sort_id" property="sortId"/>
		<result column="version_no" property="versionNo"/>
		<result column="remarks" property="remarks"/>
		<result column="del_flag" property="delFlag"/>
		<result column="create_by" property="createBy"/>
		<result column="create_date" property="createDate"/>
		<result column="update_by" property="updateBy"/>
		<result column="update_date" property="updateDate"/>
    </resultMap>
	<sql id="mdeHotelCompanyColumns">
		a.hotel_id AS "hotelId",
		a.subbrand_id AS "subBrandId",
		a.hotel_name_cn AS "hotelNameCn",
		a.hotel_name_en AS "hotelNameEn",
		a.hotel_country AS "hotelCountry",
		a.hotel_province AS "hotelProvince",
		a.hotel_city AS "hotelCity",
		a.hotel_area AS "hotelArea",
		a.hotel_zipcode AS "hotelZipcode",
		
		a.hotel_address_cn AS "hotelAddressCn",
		a.hotel_address_en AS "hotelAddressEn",
		a.hotel_longitude  AS "hotelLongitude",
		a.hotel_latitude   AS "hotelLatitude",
		
		a.sort_id AS "sortId",
		a.version_no AS "versionNo",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.create_by AS "createBy",
		a.create_date AS "createDate",
		a.update_by AS "updateBy",
		a.update_date AS "updateDate"
		
		,b.international as "international"
	</sql>
	
	<sql id="mdeHotelCompanyJoins">
		LEFT JOIN mde_hotel_subbrand b ON b.SUBBRAND_ID = a.SUBBRAND_ID 
	</sql>
    
	<select id="get" resultMap="MdeHotelCompany">
		SELECT 
			<include refid="mdeHotelCompanyColumns"/>
		FROM mde_hotel_company a
		<include refid="mdeHotelCompanyJoins"/>
		WHERE a.hotel_id = #{hotelId}
	</select>
	
	<select id="findList" resultMap="MdeHotelCompany">
		SELECT 
			<include refid="mdeHotelCompanyColumns"/>
		FROM mde_hotel_company a
		<include refid="mdeHotelCompanyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultMap="MdeHotelCompany">
		SELECT 
			<include refid="mdeHotelCompanyColumns"/>
		FROM mde_hotel_company a
		<include refid="mdeHotelCompanyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO mde_hotel_company(
			subbrand_id,
			hotel_name_cn,
			hotel_name_en,
			hotel_country,
			hotel_province,
			hotel_city,
			hotel_area,
			hotel_zipcode,
			sort_id,
			version_no,
			remarks,
			del_flag,
			create_by,
			create_date,
			update_by,
			update_date
		) VALUES (
			#{subBrandId},
			#{hotelNameCn},
			#{hotelNameEn},
			#{hotelCountry},
			#{hotelProvince},
			#{hotelCity},
			#{hotelArea},
			#{hotelZipcode},
			#{sortId},
			#{versionNo},
			#{remarks},
			#{delFlag},
			#{createBy},
			#{createDate},
			#{updateBy},
			#{updateDate}
		)
	</insert>
	
	<update id="update">
		UPDATE mde_hotel_company SET 	
			subbrand_id = #{subBrandId},
			hotel_name_cn = #{hotelNameCn},
			hotel_name_en = #{hotelNameEn},
			hotel_country = #{hotelCountry},
			hotel_province = #{hotelProvince},
			hotel_city = #{hotelCity},
			hotel_area = #{hotelArea},
			hotel_zipcode = #{hotelZipcode},
			sort_id = #{sortId},
			version_no = version_no +1,
			remarks = #{remarks},
			update_by = #{updateBy},
			update_date = #{updateDate}
		WHERE hotel_id = #{hotelId} and version_no = #{versionNo}
	</update>
	
	<update id="delete">
		UPDATE mde_hotel_company SET 
			version_no = version_no +1,
			del_flag = #{DEL_FLAG_DELETE}
		WHERE hotel_id = #{id} and version_no = #{versionNo}
	</update>
	<!-- 获取相似酒店 -->
	<select id="getSimilarHotel" resultMap="MdeHotelCompany">
		SELECT 
			<include refid="mdeHotelCompanyColumns"/>
		FROM mde_hotel_company a
		<include refid="mdeHotelCompanyJoins"/>
		<where>
			a.del_flag = 0
			AND a.hotel_id >= 
			((SELECT MAX(hotel_id) FROM mde_hotel_company) - (SELECT MIN(hotel_id) FROM mde_hotel_company)) * RAND() + 
			( SELECT MIN(hotel_id) FROM mde_hotel_company )
		</where>
		limit #{limitSize}
	</select>
	<!-- 获取推荐酒店 -->
	<select id="getRecommentHotels" resultMap="MdeHotelCompany">
		SELECT 
			case #{lang}
				when 'zh' then a.hotel_name_cn
				when 'en' then a.hotel_name_en
				else a.hotel_name_cn
			end as "hotelName",
			
			case #{lang}
				when 'zh' then a.hotel_address_cn
				when 'en' then a.hotel_address_en
				else a.hotel_address_cn
			end as "hotelAddress", 
			
			<include refid="mdeHotelCompanyColumns"/>
		FROM mde_hotel_company a 
		<include refid="mdeHotelCompanyJoins"/>
		<where>
			a.del_flag = 0
			AND a.hotel_id >= 
			((SELECT MAX(hotel_id) FROM mde_hotel_company) - (SELECT MIN(hotel_id) FROM mde_hotel_company)) * RAND() + 
			( SELECT MIN(hotel_id) FROM mde_hotel_company )
		</where>
		limit #{limitSize}
	</select>
	
	<select id="getAllHotelCompanyByLang" resultMap="MdeHotelCompany">
		SELECT 
			a.hotel_id AS "hotelId",
			case #{lang}
				when 'en' then a.hotel_name_en
				else a.hotel_name_cn
				end as "hotelName",
			a.subbrand_id AS "subBrandId",
			b.international as "international"
		FROM mde_hotel_company a 
		LEFT JOIN mde_hotel_subbrand b ON b.SUBBRAND_ID = a.SUBBRAND_ID 
		<where>
			a.del_flag = 0
		</where>
		order by hotelName
	</select>
	
	<select id="getAllHotelCompany" resultType="com.wechat.model.dto.HotelCompanyDto">
       SELECT distinct
		    d.hotelId,
		    d.hotelName,
		    d.subBrandId,
		    b.international
		FROM
		    (
		        (
		            SELECT
		                a.hotel_id AS "hotelId",
		                a.hotel_name_cn AS "hotelName",
		                a.subbrand_id AS "subBrandId"
		            FROM
		                mde_hotel_company a
		            WHERE
		                a.del_flag = 0
		        )
		        UNION ALL
		            (
		                SELECT
		                    c.hotel_id AS "hotelId",
		                    c.hotel_name_en AS "hotelName",
		                    c.subbrand_id AS "subBrandId"
		                FROM
		                    mde_hotel_company c
		                WHERE
		                    c.del_flag = 0
		            )
		    ) d
		INNER JOIN mde_hotel_subbrand b ON b.SUBBRAND_ID = d.subBrandId
        <where>
            b.del_flag = 0
        </where>
        order by hotelName
    </select>
	
</mapper>