<?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.trade.admin.customer.mapper.CustomerReferralCodeMapper">
	<resultMap id="referralCodeResult" type="com.trade.admin.customer.domain.CustomerReferralCode">
		<result property="id" column="id"/>
		<result property="userId" column="user_id"/>
		<result property="userName" column="user_name"/>
		<result property="loginName" column="login_name"/>
		<result property="roleId" column="role_id"/>
		<result property="roleName" column="role_name"/>
		<result property="referralCode" column="referral_code"/>
		<result property="referralUrlPrefix" column="referral_url_prefix"/>
		<result property="referralUrlSuffix" column="referral_url_suffix"/>
		<result property="operatorTime" column="operator_time"/>
		<result property="operatorName" column="operator_name"/>
		<result property="operatorId" column="operator_id"/>
        <result property="createBy" column="create_by"/>
		<result property="status" column="status"/>
		<result property="fileId" column="file_id"/>
		<result property="gmtCreate" column="gmt_create"/>
		<result property="gmtModified" column="gmt_modified"/>
	</resultMap>
	
	<resultMap id="searchResultMap" type="com.trade.admin.customer.domain.CustomerReferralCode"  extends="referralCodeResult">
		<result property="referralUrl" column="referral_url"/>
	</resultMap>
	
	<sql id="fields">
		id,
		user_id,
		user_name,
		login_name,
		role_id,
		role_name,
		referral_code,
		referral_url_prefix,
		referral_url_suffix,
		operator_time,
		operator_name,
		operator_id,
        create_by,
		status,
		file_id,
		gmt_create,
		gmt_modified
	</sql>
	
	<insert id="add" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="com.trade.admin.customer.domain.CustomerReferralCode">
		INSERT INTO customer_referral_code(
			user_id,user_name,login_name,role_id,role_name,referral_code,referral_url_prefix,referral_url_suffix,
			operator_time,operator_name,operator_id,status,file_id
		)VALUES(
			#{userId},#{userName},#{loginName},#{roleId},#{roleName},#{referralCode},#{referralUrlPrefix},#{referralUrlSuffix},
			#{operatorTime},#{operatorName},#{operatorId},#{status},#{fileId}
		)
	</insert>

	<select id="findBySearch" parameterType="com.trade.admin.customer.domain.SearchCustomerReferralCode" resultMap="searchResultMap">
		SELECT 
			c.id,
			c.user_id,
			s.user_name as login_name,
			s.nick_name as user_name,
			c.role_id,
			c.role_name,
			c.referral_code,
			c.referral_url_prefix,
			c.referral_url_suffix,
			CONCAT(c.referral_url_prefix,c.referral_url_suffix) AS referral_url,
			c.operator_time,
			c.operator_name,
			c.operator_id,
			c.status,
			c.file_id,
			c.gmt_create,
			c.gmt_modified
		FROM customer_referral_code c
		LEFT JOIN sys_user s on s.id = c.user_id
		<where>
			<if test="userName != null and userName !=''">
				AND (
					s.nick_name LIKE CONCAT( '%',#{userName},'%' ) OR
					to_pinyin(s.nick_name) LIKE CONCAT( '%',#{userName},'%')
					)
			</if>
			<if test="roleName != null and roleName !=''">
				AND c.role_name  = #{roleName}
			</if>
			<if test="currentUserId != null">
				AND c.user_id = #{currentUserId}
			</if>
		</where>
		ORDER BY c.status DESC,c.operator_time DESC
	</select>
	
	<select id="get" parameterType="java.lang.Long" resultMap="referralCodeResult">
		SELECT <include refid="fields"/> FROM customer_referral_code WHERE id = #{value}
	</select>

	<update id="updateStatus" parameterType="com.trade.admin.customer.domain.CustomerReferralCode">
		UPDATE customer_referral_code
		SET operator_time = #{operatorTime},
			operator_name = #{operatorName},
			operator_id   = #{operatorId},
			status        = #{status}
		WHERE id = #{id}
	</update>

	<delete id="delete">
		DELETE
		FROM customer_referral_code
		WHERE id = #{id}
	</delete>

	<update id="update" parameterType="com.trade.admin.customer.domain.CustomerReferralCode">
		UPDATE customer_referral_code
		SET user_id       = #{userId},
			user_name     = #{userName},
			login_name    = #{loginName},
			referral_code = #{referralCode},
			operator_time = #{operatorTime},
			operator_name = #{operatorName},
			operator_id   = #{operatorId}
		WHERE id = #{id}
	</update>
	
	<select id="getByUserId" parameterType="java.lang.Long" resultMap="referralCodeResult">
		SELECT <include refid="fields"/> FROM customer_referral_code WHERE user_id = #{value}
	</select>
	
	<select id="getByReferralCode" parameterType="java.lang.String" resultMap="referralCodeResult">
		SELECT <include refid="fields"/> FROM customer_referral_code WHERE referral_code = #{value}
	</select>
	
	<select id="getByReferralUrlSuffix" parameterType="java.lang.String" resultMap="referralCodeResult">
		SELECT <include refid="fields"/> FROM customer_referral_code WHERE referral_url_suffix = #{value}
	</select>
	
	
	<select id="findNullFileId" resultMap="referralCodeResult">
		SELECT <include refid="fields"/> FROM customer_referral_code WHERE file_id = 0 OR ISNULL(file_id)
	</select>
	
	<update id="updateFileId">
		UPDATE customer_referral_code SET file_id = #{fileId} WHERE id = #{id}
	</update>

	<select id="getByUserIdAndRoleName" resultMap="referralCodeResult">
		SELECT
		<include refid="fields"/>
		FROM customer_referral_code WHERE user_id = #{userId}
		AND role_name = #{roleName}
	</select>
</mapper>
