<?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.mca.map.mapper.UseReqeustMapper">

	<select id="selectUseRequestList" parameterType="useRequestSearchVO" resultType="useRequestVO" >
		SELECT
			idx,
			title,
			a.userid,
			b.name,
            b.company,
			status,
			s_date,
			e_date,
			r_date,
			places,
			layers,
			tile_id,
			user_msg,
			admin_msg
		FROM map_use_request a LEFT JOIN t_user b ON a.userid=b.userid
		WHERE 1 = 1
		<if test='userid != null and userid != ""'>
			AND a.userid = #{userid}
		</if>
		<if test='status != null and status != ""'>
			AND status = #{status}
		</if>
		<if test='searchKeyword != null and searchKeyword != ""'>
			<choose>
				<when test='searchCondition eq "all"'>
						AND (title LIKE CONCAT('%',#{searchKeyword},'%')
						OR b.name LIKE CONCAT('%',#{searchKeyword},'%')
						OR b.company LIKE CONCAT('%',#{searchKeyword},'%'))
				</when>
				<otherwise>
						AND ${searchCondition} LIKE CONCAT('%',#{searchKeyword},'%')
				</otherwise>
			</choose>
		</if>
		ORDER BY idx DESC
		LIMIT #{recordCountPerPage} OFFSET #{firstIndex}
	</select>
	
	<select id="selectUseRequestCnt" parameterType="useRequestSearchVO" resultType="int" >
		SELECT
			COUNT(*)
		FROM map_use_request a LEFT JOIN t_user b ON a.userid=b.userid
		WHERE 1 = 1 
		<if test='userid != null and userid != ""'>
			AND a.userid = #{userid}
		</if>
		<if test='status != null and status != ""'>
			AND status = #{status}
		</if>
		<if test='searchKeyword != null and searchKeyword != ""'>
			<choose>
				<when test='searchCondition eq "all"'>
						AND (title LIKE CONCAT('%',#{searchKeyword},'%')
						OR b.name LIKE CONCAT('%',#{searchKeyword},'%')
						OR b.company LIKE CONCAT('%',#{searchKeyword},'%'))
				</when>
				<otherwise>
						AND ${searchCondition} LIKE CONCAT('%',#{searchKeyword},'%')
				</otherwise>
			</choose>
		</if>
	</select>
	
	<select id="selectUseRequest" parameterType="int" resultType="useRequestVO" >
		SELECT
			*
		FROM
			map_use_request
		WHERE
			idx = #{idx}
	</select>
	
	<select id="selectUseRequeststatusCnt" resultType="int" >
		SELECT
			COUNT(*)
		FROM
			map_use_request
		WHERE
			status = 99
	</select>
	
	<insert id="insertReq" parameterType="useRequestVO">
		INSERT INTO map_use_request
		(
			title,
			s_date,
			e_date,
			r_date,
			places,
			layers,
			userid,
			user_msg
		) 
		value
		(
			#{title},
			#{s_date},
			#{e_date},
			NOW(),
			#{places},
			#{layers},
			#{userid},
			#{user_msg}
		);
	</insert>
	
	<update id="updateReq" parameterType="useRequestVO">
		UPDATE map_use_request SET
			title = #{title},
			s_date = #{s_date},
			e_date = #{e_date},
			r_date = now(),
			places = #{places},
			layers = #{layers},
			user_msg = #{user_msg}
		WHERE
			idx = #{idx}
	</update>
	
	<update id="updateReqConfirm" parameterType="useRequestVO">
		UPDATE map_use_request SET
			status = #{status},
			admin_id = #{admin_id},
			admin_msg = #{admin_msg}
		WHERE
			idx = #{idx}
	</update>
	
	<delete id="deleteReq" parameterType="useRequestVO">
		DELETE FROM map_use_request WHERE idx = #{idx}
	</delete>
	
</mapper>