<?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.workvisa.dao.WorkVisaDao">
    
	<sql id="workVisaColumns">
		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.initiate_client AS "initiateClient",
		a.company_id AS "companyId",
		a.cproject_name AS "cprojectName",
		a.construction_example AS "constructionExample",
		a.visa_number AS "visaNumber",
		a.visa_area AS "visaArea",
		a.theme AS "theme",
		a.visa_content AS "visaContent",
		a.reply_suggest AS "replySuggest",
		a.full_manage,fullM.id AS "fullManage.id",
		a.status AS "status",
		/*fullM.id AS "fullManage.id",*/
		a.process_instance_id AS "processInstanceId",
		a.office_id AS "officeId"
	</sql>
	
	<sql id="workVisaJoins">
		LEFT JOIN work_full_manage fullM ON fullM.id = a.full_manage
		JOIN sys_user u ON u.id = a.create_by
		JOIN sys_office o ON o.id = a.office_id
		JOIN sys_office s ON s.id = a.company_id
	</sql>
	
    
	<select id="get" resultType="WorkVisa" >
		SELECT 
			<include refid="workVisaColumns"/>
		FROM work_visa a
		<include refid="workVisaJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkVisa" >
		SELECT 
			<include refid="workVisaColumns"/>
		FROM work_visa a
		<include refid="workVisaJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="status != null and status != ''">
				AND a.status LIKE 
					<if test="dbName == 'oracle'">'%'||#{status}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{status}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{status},'%')</if>
			</if>
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			${sqlMap.dsf}
		</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" resultType="WorkVisa" >
		SELECT 
			<include refid="workVisaColumns"/>
		FROM work_visa a
		<include refid="workVisaJoins"/>
		<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_visa(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			initiate_client,
			cproject_name,
			construction_example,
			visa_number,
			visa_area,
			theme,
			visa_content,
			reply_suggest,
			full_manage,
			office_id,
			status
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{initiateClient},
			#{cprojectName},
			#{constructionExample},
			#{visaNumber},
			#{visaArea},
			#{theme},
			#{visaContent},
			#{replySuggest},
			#{fullManage.id},
			#{officeId},
			#{status}
		)
	</insert>
	
	<update id="update">
		UPDATE work_visa SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			initiate_client = #{initiateClient},
			cproject_name = #{cprojectName},
			construction_example = #{constructionExample},
			visa_number = #{visaNumber},
			company_id = #{companyId},
			visa_area = #{visaArea},
			theme = #{theme},
			visa_content = #{visaContent},
			reply_suggest = #{replySuggest},
			full_manage = #{fullManage.id},
			office_id = #{officeId},
			status = #{status}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_visa
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_visa SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkVisa" statementType="STATEMENT">
		select * FROM work_visa  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByfullManage" resultType="WorkFullManage">
		SELECT 
			*
		FROM work_full_manage a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id LIKE 
				<if test="dbName == 'oracle'">#{id}||'%'</if>
				<if test="dbName == 'mssql'">#{id}+'%'</if>
				<if test="dbName == 'mysql'">concat(#{id},'%')</if>
			</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="getCount" resultType="java.lang.Integer">
		select count(*) from work_visa w where w.full_manage =#{fullManage.id}
	</select>

	<select id="findListByWF" resultType="WorkVisa">
		select
		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.initiate_client AS "initiateClient",
		a.cproject_name AS "cprojectName",
		a.construction_example AS "constructionExample",
		a.visa_number AS "visaNumber",
		a.visa_area AS "visaArea",
		a.theme AS "theme",
		a.visa_content AS "visaContent",
		a.reply_suggest AS "replySuggest",
		a.process_instance_id AS "processInstanceId",
		a.full_manage AS "fullManage.id",
		a.status AS "status"
		FROM work_visa a LEFT JOIN work_full_manage	w on w.id = a.full_manage
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="fullManage != null  and fullManage.id != null and fullManage.id != ''">
				AND a.full_manage =  #{fullManage.id}
			</if>
			<!--<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>-->
		</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="getListOfAttachment" resultType="com.jeeplus.modules.sys.entity.Workattachment">
		SELECT
		t.id AS "id",
		t.create_by AS "createBy.id",
		t.create_date AS "createDate",
		t.update_by AS "updateBy.id",
		t.update_date AS "updateDate",
		t.remarks AS "remarks",
		t.del_flag AS "delFlag",
		t.url AS "url",
		t.type AS "type",
		t.attachment_id AS "attachmentId",
		u.name AS "attachmentUser",
		t.attachment_name AS "attachmentName",
		t.attachment_flag AS "attachmentFlag"
		FROM work_attachment t left JOIN  sys_user u on t.attachment_user = u.id
		WHERE t.attachment_id = #{attachmentId}
	</select>

	<update id="updateProcessInstanceId">
		UPDATE work_visa SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<select id="getByProcessInstanceId" resultType="WorkVisa">
		SELECT
		<include refid="workVisaColumns"/>
		FROM work_visa a
		<include refid="workVisaJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>
</mapper>