<?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.workprocesstype.dao.WorkProcessDetailDao">
    
	<sql id="workProcessDetailColumns">
		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.process_number AS "processNumber",
		a.process_name AS "processName",
		b.state AS "state",
		b.id AS "typeId"
	</sql>
	
	<sql id="workProcessDetailJoins">
		LEFT JOIN (select id,state_id,state from work_process_type where company_id = #{companyId}) b on a.id = b.state_id
	</sql>
	
    
	<select id="get" resultType="WorkProcessDetail" >
		SELECT 
			<include refid="workProcessDetailColumns"/>
		FROM work_process_detail a
		<include refid="workProcessDetailJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkProcessDetail" >
		SELECT
			<include refid="workProcessDetailColumns"/>
		FROM work_process_detail a
		<include refid="workProcessDetailJoins"/>
		<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.process_number,a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkProcessDetail" >
		SELECT 
			<include refid="workProcessDetailColumns"/>
		FROM work_process_detail a
		<include refid="workProcessDetailJoins"/>
		<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_process_detail(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_number,
			process_name
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processNumber},
			#{processName}
		)
	</insert>
	
	<update id="update">
		UPDATE work_process_detail SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_number = #{processNumber},
			process_name = #{processName}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_process_detail
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_process_detail SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkProcessDetail" statementType="STATEMENT">
		select * FROM work_process_detail  where ${propertyName} = '${value}'
	</select>


	<select id="findListByState" resultType="WorkProcessDetail">
		SELECT
		<include refid="workProcessDetailColumns"/>
		FROM work_process_detail a
		LEFT JOIN (select id,state_id,state from work_process_type where company_id = #{companyId}) b on a.id = b.state_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			and (b.state = "0" or b.state is null)
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.process_number,a.update_date DESC
			</otherwise>
		</choose>
	</select>
</mapper>