<?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="MdeDepartmentMapper">
    <resultMap type="com.wechat.model.dbEntity.MdeDepartment" id="MdeDepartment">
		<id column="depd_id" property="depdId"/>
		<result column="depd_name_cn" property="depdNameCn"/>
		<result column="depd_name_en" property="depdNameEn"/>
		<result column="sort" property="sort"/>
		<result column="version_no" property="versionNo"/>
		<result column="create_by" property="createBy"/>
		<result column="create_date" property="createDate"/>
		<result column="update_by" property="updateBy"/>
		<result column="update_date" property="updateDate"/>
		<result column="remarks" property="remarks"/>
		<result column="del_flag" property="delFlag"/>
	</resultMap>
	<sql id="mdeDepartmentColumns">
		a.depd_id AS "depdId",
		a.depd_name_cn AS "depdNameCn",
		a.depd_name_en AS "depdNameEn",
		a.sort AS "sort",
		a.version_no AS "versionNo",
		a.create_by AS "createBy",
		a.create_date AS "createDate",
		a.update_by AS "updateBy",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
	</sql>
	
	<sql id="mdeDepartmentJoins">
	</sql>
    
	<select id="get" resultMap="MdeDepartment">
		SELECT 
			<include refid="mdeDepartmentColumns"/>
		FROM mde_department a
		<include refid="mdeDepartmentJoins"/>
		WHERE a = #{id}
	</select>
	
	<select id="getAllDepartments" resultMap="MdeDepartment">
		SELECT 
			<include refid="mdeDepartmentColumns"/>
		FROM mde_department a
		<where>
			a.del_flag = 0
		</where>
		ORDER BY a.sort ASC
	</select>
	<select id="findList" resultMap="MdeDepartment">
		SELECT 
			<include refid="mdeDepartmentColumns"/>
		FROM mde_department a
		<include refid="mdeDepartmentJoins"/>
		<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="MdeDepartment">
		SELECT 
			<include refid="mdeDepartmentColumns"/>
		FROM mde_department a
		<include refid="mdeDepartmentJoins"/>
		<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_department(
			depd_id,
			depd_name_cn,
			depd_name_en,
			sort,
			version_no,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{depdId},
			#{depdNameCn},
			#{depdNameEn},
			#{sort},
			#{versionNo},
			#{createBy},
			#{createDate},
			#{updateBy},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE mde_department SET 	
			depd_id = #{depdId},
			depd_name_cn = #{depdNameCn},
			depd_name_en = #{depdNameEn},
			sort = #{sort},
			version_no = version_no +1,
			update_by = #{updateBy},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE depd_id = #{depdId} and version_no = #{versionNo}
	</update>
	
	<update id="delete">
		UPDATE mde_department SET 
			version_no = version_no +1,
			del_flag = #{DEL_FLAG_DELETE}
		WHERE depd_id = #{depdId} and version_no = #{versionNo}
	</update>
	
</mapper>