<?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.jeeplus.modules.workjobgrade.dao.WorkJobGradeDao">
    
	<sql id="workJobGradeColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.office_id AS "office.id",
		a.company_id AS "companyId",
		a.name AS "name",
		a.no AS "no",
		a.parent_id AS "parent.id",
		a.end_flag AS "endFlag",
		a.parent_ids AS "parentIds",
		a.branch_office AS "branchOffice",
		a.state AS "state",
		a.pay AS "pay",
		a.sort AS "sort",
		o.name AS "office.name",
		p.name AS "parent.name"
	</sql>
	
	<sql id="workJobGradeJoins">
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN work_job_grade p ON p.id = a.parent_id
	</sql>

	<select id="get" resultType="WorkJobGrade" >
		SELECT 
			<include refid="workJobGradeColumns"/>
		FROM work_job_grade a
		<include refid="workJobGradeJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkJobGrade" >
		SELECT 
			<include refid="workJobGradeColumns"/>
		FROM work_job_grade a
		<include refid="workJobGradeJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="no != null and no != ''">
				AND a.no = #{no}
			</if>
			<if test="parent != null and parent.id != null and parent.id != ''">
				AND a.parent_id = #{parent.id}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
            AND a.branch_office = #{currentUser.branchOffice.id}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.parent_ids ASC,a.no ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkJobGrade" >
		SELECT 
			<include refid="workJobGradeColumns"/>
		FROM work_job_grade a
		<include refid="workJobGradeJoins"/>
		<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 work_job_grade(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			name,
			no,
			parent_id,
			end_flag,
			parent_ids,
			branch_office,
			state,
			pay,
			sort
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{office.id},
			#{companyId},
			#{name},
			#{no},
			#{parent.id},
			#{endFlag},
			#{parentIds},
			#{branchOffice},
			#{state},
			#{pay},
			#{sort}
		)
	</insert>
	
	<update id="update">
		UPDATE work_job_grade SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{office.id},
			company_id = #{companyId},
			name = #{name},
			no = #{no},
			parent_id = #{parent.id},
			end_flag = #{endFlag},
			parent_ids = #{parentIds},
			branch_office = #{branchOffice},
			state = #{state},
			pay = #{pay},
			sort = #{sort}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_job_grade
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_job_grade SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkJobGrade" statementType="STATEMENT">
		select * FROM work_job_grade  where ${propertyName} = '${value}'
	</select>

    <select id="findAllListByParentIds" resultType="WorkJobGrade">
        SELECT
        <include refid="workJobGradeColumns"/>
        FROM work_job_grade a
        <include refid="workJobGradeJoins"/>
        WHERE a.del_flag = #{DEL_FLAG_NORMAL}
        AND (find_in_set(#{id}, a.parent_ids) OR a.id = #{id})
        ORDER BY a.no
    </select>

    <select id="findByParentIdsLike" resultType="WorkJobGrade">
        SELECT
        <include refid="workJobGradeColumns"/>
        FROM work_job_grade a
        <include refid="workJobGradeJoins"/>
        WHERE a.del_flag = #{DEL_FLAG_NORMAL} AND (a.parent_ids LIKE
        <if test="dbName == 'mysql'">concat('%,',#{parentIds},',%')</if>
        OR a.id = #{id})
        ORDER BY a.no
    </select>

    <select id="findByParent" resultType="WorkJobGrade">
        SELECT
        <include refid="workJobGradeColumns"/>
        FROM work_job_grade a
        <include refid="workJobGradeJoins"/>
        WHERE a.del_flag = #{DEL_FLAG_NORMAL}
        <if test="parent != null and parent.id != null and parent.id != ''">
            AND a.parent_id = #{parent.id}
        </if>
        OR a.id = #{id}
        ORDER BY a.no
    </select>

    <update id="changeState">
        UPDATE work_job_grade SET state = #{state} WHERE id=#{id}
    </update>

	<select id="findByBranchOffice" resultType="WorkJobGrade" >
		SELECT
		<include refid="workJobGradeColumns"/>
		FROM work_job_grade a
		<include refid="workJobGradeJoins"/>
		WHERE  a.name=#{name} and a.branch_office=#{branchOffice}
	</select>


    <select id="findByName" resultType="WorkJobGrade">
        SELECT * FROM work_job_grade a WHERE a.name = #{name} and a.branch_office = #{branchOffice}
    </select>

</mapper>