<?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="org.wfc.user.mapper.UKycMapper">

	<resultMap type="UKyc" id="UKycResult">
		<id     property="kycId"     column="kyc_id"     />
		<result property="userId"   column="user_id"   />
		<result property="realName" column="real_name" />
		<result property="birthDate"  column="birth_date"   />
		<result property="idType"   column="id_type"   />
		<result property="idFile"   column="id_file"   />
		<result property="identifyPicture"     column="identify_picture"      />
		<result property="status"      column="status"       />
		<result property="description"  column="description"       />
		<result property="delFlag"    column="del_flag"    />		
		<result property="createBy"   column="create_by"   />
		<result property="createTime" column="create_time" />
		<result property="updateBy"   column="update_by"   />
		<result property="updateTime" column="update_time" />
	</resultMap>

	<insert id="insertUserKyc" parameterType="UKyc" useGeneratedKeys="true" keyProperty="kycId">
 		INSERT INTO u_kyc(
 			<if test="userId != null and userId != 0">user_id,</if>
	 		<if test="realName != null and realName != ''">real_name,</if>			
 			<if test="birthDate != null">birth_date,</if>
 			<if test="idType != null and idType != ''">id_type,</if>
 			<if test="idFile != null and idFile != ''">id_file,</if>
 			<if test="identifyPicture != null and identifyPicture != ''">identify_picture,</if>
 			<if test="status != null and status != ''">status,</if>
 			<if test="description != null and description != ''">description,</if>            
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			create_time
 		)VALUES(
 			<if test="userId != null and userId != ''">#{userId},</if>
			<if test="realName != null and realName != ''">#{realName},</if>				
 			<if test="birthDate != null">STR_TO_DATE(#{birthDate}, '%Y-%m-%d'),</if>
 			<if test="idType != null and idType != ''">#{idType},</if>
 			<if test="idFile != null and idFile != ''">#{idFile},</if>
 			<if test="identifyPicture != null and identifyPicture != ''">#{identifyPicture},</if>
 			<if test="status != null and status != ''">#{status},</if>
			<if test="description != null and description != ''">#{description},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			sysdate()
 		)
	</insert>

	<update id="updateUserKyc" parameterType="UKyc">
 		UPDATE u_kyc
 		<set>
 			<if test="realName != null and realName != ''">real_name = #{realName},</if>		
 			<if test="birthDate != null">birth_date = STR_TO_DATE(#{birthDate}, '%Y-%m-%d'),</if>
 			<if test="idType != null and idType != ''">id_type = #{idType},</if>
 			<if test="idFile != null and idFile != ''">id_file = #{idFile},</if>
 			<if test="identifyPicture != null and identifyPicture != ''">identify_picture = #{identifyPicture},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
 			<if test="description != null and description != ''">description = #{description},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
  			update_time = sysdate()
 		</set>
 		WHERE user_id = #{userId}
	</update>

    <select id="isExistUserKyc" parameterType="Long" resultType="int">
		SELECT 
			count(1) 
		FROM 
			u_kyc k 
			LEFT JOIN u_user u ON u.user_id = k.user_id 
		WHERE 
			k.user_id = #{userId} 
			AND k.del_flag = '0'
			AND u.del_flag = '0'
	</select>

    <select id="selectKycByUserId" resultType="org.wfc.user.domain.vo.UKycUserVo">
        SELECT
			k.kyc_id,
            k.user_id,
            u.full_name as real_name,
            k.birth_date,
            k.id_type,
            k.id_file,
            k.identify_picture,
            k.`status`,
            <!-- u.`kyc_status` as user_kyc_status, -->
            k.create_time,
            k.update_time
        FROM
            u_kyc k
            LEFT JOIN u_user u ON u.user_id = k.user_id
        WHERE    
            u.del_flag = 0
            AND k.del_flag = 0
            AND k.user_id = #{userId}
    </select>

</mapper>
