<?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="MdePositionMapper">
    <resultMap type="com.wechat.model.dbEntity.MdePosition"
               id="MdePositionMap">
        <id column="position_id" property="positionId"/>
        <result column="depd_id" property="depdId"/>
        <result column="position_name_cn" property="positionNameCn"/>
        <result column="position_name_en" property="positionNameEn"/>
        <result column="position_grade" property="positionGrade"/>
        <result column="requirement" property="requirement"/>
        <result column="describe" property="describe"/>
        <result column="depdName" property="depdName"/>
        <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="mdePositionColumns">
        a.position_id AS "positionId",
		a.depd_id AS "depdId",
		a.position_name_cn AS "positionNameCn",
		a.position_name_en AS "positionNameEn",
		a.position_grade AS "positionGrade",
		a.requirement AS "requirement",
		a.describe AS "describe",
		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="mdePositionJoins">
        INNER JOIN mde_department d ON a.DEPD_ID = d.DEPD_ID
    </sql>

    <select id="get" resultMap="MdePositionMap">
        SELECT
        <include refid="mdePositionColumns"/>
        FROM mde_position a
        <include refid="mdePositionJoins"/>
        WHERE a.id = #{id}
    </select>

    <select id="getId" resultMap="MdePositionMap">
        SELECT
        <include refid="mdePositionColumns"/>
        ,d.DEPD_NAME_CN as 'depdName'
        FROM mde_position a
        <include refid="mdePositionJoins"/>
        WHERE a.position_id = #{id}
    </select>

    <select id="getAllPositions" resultMap="MdePositionMap">
        SELECT
        <include refid="mdePositionColumns"/>
        FROM mde_position a
        <where>
            a.del_flag = 0
        </where>
        order by if(a.depd_id='0','990',a.depd_id) asc , a.POSITION_GRADE desc, a.position_id asc
    </select>
    <select id="findList" resultMap="MdePositionMap">
        SELECT
        <include refid="mdePositionColumns"/>
        ,d.DEPD_NAME_CN as 'depdName'
        FROM mde_position a
        <include refid="mdePositionJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="positionNameCn != null and positionNameCn != ''">
                AND a.POSITION_NAME_CN like concat('%',#{positionNameCn},'%')
            </if>
            <if test="positionNameEn != null and positionNameEn != ''">
                AND a.POSITION_NAME_EN like concat('%',#{positionNameEn},'%')
            </if>
            <if test="positionGrade != null and positionGrade != ''">
                AND a.POSITION_GRADE = #{positionGrade}
            </if>
            <if test="depdId != null and depdId != ''">
                AND a.DEPD_ID = #{depdId}
            </if>
        </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="MdePositionMap">
        SELECT
        <include refid="mdePositionColumns"/>
        FROM mde_position a
        <include refid="mdePositionJoins"/>
        <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_position(position_id,
                                 depd_id,
                                 position_name_cn,
                                 position_name_en,
                                 position_grade,
                                 requirement,
                                 `describe`,
                                 create_by,
                                 create_date,
                                 update_by,
                                 update_date,
                                 remarks,
                                 del_flag)
        VALUES (#{positionId},
                #{depdId},
                #{positionNameCn},
                #{positionNameEn},
                #{positionGrade},
                #{requirement},
                #{describe},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{remarks},
                #{delFlag})
    </insert>

    <update id="update">
        UPDATE mde_position
        SET depd_id          = #{depdId},
            position_name_cn = #{positionNameCn},
            position_name_en = #{positionNameEn},
            position_grade   = #{positionGrade},
            requirement      = #{requirement},
            `describe`       = #{describe},
            version_no       =version_no + 1,
            update_by        = #{updateBy.id},
            update_date      = #{updateDate},
            remarks          = #{remarks}
        WHERE position_id = #{positionId}
    </update>

    <update id="delete">
        UPDATE mde_position
        SET version_no = version_no + 1,
            del_flag   = #{delFlag}
        WHERE position_id = #{positionId}
          and version_no =
              #{versionNo}
    </update>

    <select id="getPvByJobId" resultType="java.lang.Integer">
        SELECT
        PAGE_VIEW as pageView
        FROM MDE_VIEW_JOB_PUV_ALL
        <where>
            JOB_ID = #{jobId}
        </where>
    </select>
</mapper>