<?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.user.mapper.UserMapper">

	<select id="selectUserList" parameterType="userSearchVO" resultType="userVO" >
		SELECT
			userid,
			name,
			company,
			email,
			phonenum,
			regdate,
			auth
		FROM t_user
		WHERE 1 = 1
		<choose>
			<when test='auth != null and auth != ""'>
				AND auth = #{auth}
			</when>
			<otherwise>
				AND auth != 99
			</otherwise>
		</choose>
		<if test='searchKeyword != null and searchKeyword != ""'>
			<choose>
				<when test='searchCondition eq "all"'>
						AND (name LIKE CONCAT('%',#{searchKeyword},'%')
						OR company LIKE CONCAT('%',#{searchKeyword},'%')
						OR email LIKE CONCAT('%',#{searchKeyword},'%')
						OR phonenum LIKE CONCAT('%',#{searchKeyword},'%'))
				</when>
				<otherwise>
						AND ${searchCondition} LIKE CONCAT('%',#{searchKeyword},'%')
				</otherwise>
			</choose>
		</if>
		ORDER BY regdate DESC
		LIMIT #{recordCountPerPage} OFFSET #{firstIndex}
	</select>
	
	<select id="selectUserCount" parameterType="useRequestSearchVO" resultType="int" >
		SELECT
			COUNT(*)
		FROM t_user
		WHERE 1 = 1
		<if test='auth != null and auth != ""'>
			AND auth = #{auth}
		</if>
		<if test='searchKeyword != null and searchKeyword != ""'>
			<choose>
				<when test='searchCondition eq "all"'>
						AND (name LIKE CONCAT('%',#{searchKeyword},'%')
						OR company LIKE CONCAT('%',#{searchKeyword},'%')
						OR email LIKE CONCAT('%',#{searchKeyword},'%')
						OR phonenum LIKE CONCAT('%',#{searchKeyword},'%'))
				</when>
				<otherwise>
						AND ${searchCondition} LIKE CONCAT('%',#{searchKeyword},'%')
				</otherwise>
			</choose>
		</if>
	</select>
	
	<select id="selectUserStandByCount" resultType="int" >
		SELECT
			COUNT(*)
		FROM
			t_user
		WHERE
			auth = 99
	</select>

	<!-- 아이디 중복체크 -->	
	<select id="selectUserIdCheck" parameterType="String" resultType="int" >
		SELECT
			COUNT(userid)
		FROM
			T_USER
		WHERE
			userid = #{checkId}
	</select>
	
	<insert id="insertUser" parameterType="userVO">
		INSERT INTO	T_USER(
			userid,
		    password,
		    name,
		    company,
		    email,
		    phonenum,
		    regdate,
		    auth
		)
		VALUES(
			#{userid},
		    #{password},
		    #{name},
		    #{company},
		    #{email},
		    #{phonenum},
		    NOW(),
		    99
		)
	</insert>
	
	<select id="selectUserInfo" resultType="userVO" parameterType="String">
		SELECT
			*
		FROM
			t_user
		WHERE
			userid = #{userid}
	</select>
	
	<update id="updateUserSign" parameterType="String">
		UPDATE t_user SET 
			auth = 2
		WHERE userid = #{userid}
	</update>
	
	<delete id="updateUserDelete" parameterType="String">
		DELETE FROM
			t_user
		WHERE
			userid = #{userid}
	</delete>
	
	<update id="updateUser" parameterType="userVO">
		UPDATE t_user SET
		<if test='password != null and password != ""'>
			password = #{password},
		</if>
		<if test='auth != null and auth != ""'>
			auth = #{auth},
		</if>
			name = #{name},
			company = #{company},
			email = #{email},
			phonenum = #{phonenum}
		WHERE userid = #{userid}
	</update>

 <!--아이디 비밀번호 찾기-->
	<select id="findId" resultType="userVO">
		select userid
		from t_user
		where
			name=#{name} and company=#{company} and email=#{email}
	</select>

	<select id="findPwd" resultType="userVO">
		select userid, email, name
		from t_user
		where userid=#{userid}
		  and email=#{email}
	</select>

	<update id="updatePwd" parameterType="userVO">
		update t_user set password=#{password} where userid=#{userid}
	</update>
</mapper>