<?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="MdeUserCvMapper">
    <resultMap type="com.wechat.model.dbEntity.MdeUserCv" id="MdeUserCv">
    	<id column="cv_id" property="cvId"/>
		<result column="user_code" property="userCode"/>
		<result column="cv_title" property="cvTitle"/>
		<result column="cv_url" property="cvUrl"/>
		<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="mdeUserCvColumns">
		a.cv_id AS "cvId",
		a.user_code AS "userCode",
		a.cv_title AS "cvTitle",
		a.cv_url AS "cvUrl",
		a.version_no AS "versionNo",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.update_by AS "updateBy",
		a.update_date AS "updateDate"
	</sql>
	
	<sql id="mdeUserCvJoins">
	</sql>
    
	<select id="get" resultMap="MdeUserCv">
		SELECT 
			<include refid="mdeUserCvColumns"/>
		FROM mde_user_cv a
		<include refid="mdeUserCvJoins"/>
		WHERE a.cv_id = #{cvId}
			and a.user_code = #{userCode}
	</select>
    
	<select id="getUserCVList" resultMap="MdeUserCv">
		SELECT 
			<include refid="mdeUserCvColumns"/>
		FROM mde_user_cv a
		<include refid="mdeUserCvJoins"/>
		WHERE a.user_code = #{userCode} and a.del_flag=0
	</select>
	
	<select id="findList" resultMap="MdeUserCv">
		SELECT 
			<include refid="mdeUserCvColumns"/>
		FROM mde_user_cv a
		<include refid="mdeUserCvJoins"/>
		<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="MdeUserCv">
		SELECT 
			<include refid="mdeUserCvColumns"/>
		FROM mde_user_cv a
		<include refid="mdeUserCvJoins"/>
		<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_user_cv(
			user_code,
			cv_title,
			cv_url,
			version_no,
			remarks,
			del_flag,
			create_by,
			create_date,
			update_by,
			update_date
		) VALUES (
			#{userCode},
			#{cvTitle},
			#{cvUrl},
			#{versionNo},
			#{remarks},
			#{delFlag},
			#{createBy},
			#{createDate},
			#{updateBy},
			#{updateDate}
		)
	</insert>
	
	<update id="update">
		UPDATE mde_user_cv SET 	
			<if test="userCode!=null"> user_code = #{userCode},      </if>
			<if test="cvTitle!=null"> cv_title = #{cvTitle},        </if>
			<if test="cvUrl!=null"> cv_url = #{cvUrl},            </if>
			<if test="remarks!=null"> remarks = #{remarks},         </if>
			<if test="delFlag!=null"> del_flag = #{delFlag},         </if>
			<if test="updateBy!=null"> update_by = #{updateBy},      </if>
			<if test="updateDate!=null"> update_date = #{updateDate},  </if>
			version_no =  version_no +1
		WHERE cv_id = #{cvId} 
		<if test="versionNo!=null">
			and version_no = #{versionNo}
		</if>
	</update>
	
	<select id="getCvUsed" resultType="com.wechat.model.dto.UserCvInfo">
		SELECT
		  a.CV_ID as cvId,
			a.USER_CODE as userCode,
			a.CV_TITLE as cvTitle,
			a.CV_URL as cvUrl,
			b.CV_URL as cvUrlUsed 
		FROM
			mde_user_cv a
			LEFT JOIN mde_position_cv_rel b ON a.USER_CODE = b.CANDIDATE 
			AND a.CV_URL = b.CV_URL
		WHERE a.cv_id = #{cvId}
			and a.user_code = #{userCode}
	</select>
	
	<!-- 物理删除 -->
	<delete id="delete" >
		DELETE
		FROM
			mde_user_cv
		WHERE cv_id = #{cvId}
          and user_code = #{userCode}
	</delete>
	
</mapper>