<?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.workadministrativeatamp.dao.WorkAdministrativeAtampDao">
    
	<sql id="workAdministrativeAtampColumns">
		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 "company.id",
		a.doc_type AS "docType",
		a.num AS "num",
		a.com_sign AS "comSign.id",
		a.submiter AS "submiter.id",
		a.submit_date AS "submitDate",
		a.seal_type AS "sealType",
		a.state AS "state",
		a.process_instance_id AS "processInstanceId",
		u1.name AS "submiter.name",
		comSign.name AS "comSign.name",
		o.name AS "office.name",
		o.top_company AS "office.topCompany"
	</sql>
	
	<sql id="workAdministrativeAtampJoins">
		LEFT JOIN sys_office comSign ON comSign.id = a.com_sign
		LEFT JOIN sys_office o on a.office_id = o.id
        LEFT JOIN sys_office s ON s.id = a.company_id
        LEFT JOIN sys_user u ON u.id = a.create_by
        LEFT JOIN sys_user u1 ON u1.id = a.submiter
	</sql>
	
    
	<select id="get" resultType="WorkAdministrativeAtamp" >
		SELECT 
			<include refid="workAdministrativeAtampColumns"/>
		FROM work_administrative_stamp a
		<include refid="workAdministrativeAtampJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkAdministrativeAtamp" >
		SELECT 
			<include refid="workAdministrativeAtampColumns"/>
		FROM work_administrative_stamp a
		<include refid="workAdministrativeAtampJoins"/>
		<where>
            <if test="beginDate !=null">
                AND a.submit_date >= #{beginDate}
            </if>
            <if test="endDate !=null">
                AND a.submit_date &lt; #{endDate}
            </if>
            <if test="submiter !=null and submiter.name != null and submiter.name!=''">
                AND u1.name LIKE concat('%',#{submiter.name},'%')
            </if>
            <if test="num !=null and num !=''">
                AND a.num LIKE concat('%',#{num},'%')
            </if>
            <if test="comSign !=null and comSign.name != null and comSign.name!=''">
                AND comSign.name LIKE concat('%',#{comSign.name},'%')
            </if>
            <if test="docType != null and docType != ''">
                AND a.doc_type = #{docType}
            </if>
            <if test="state != null">
                AND a.state = #{state}
            </if>
			AND a.del_flag = #{DEL_FLAG_NORMAL}
            ${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="queryCount" resultType="int" >
        SELECT COUNT(1)
        FROM work_administrative_stamp a
        <if test="comSign !=null and comSign.name != null and comSign.name!=''">
            LEFT JOIN sys_office comSign ON comSign.id = a.com_sign
        </if>
        <if test="submiter !=null and submiter.name != null and submiter.name!=''">
            LEFT JOIN sys_user u1 ON u1.id = a.submiter
        </if>
        <where>
            <if test="beginDate !=null">
                AND a.submit_date >= #{beginDate}
            </if>
            <if test="endDate !=null">
                AND a.submit_date &lt; #{endDate}
            </if>
            <if test="submiter !=null and submiter.name != null and submiter.name!=''">
                AND u1.name LIKE concat('%',#{submiter.name},'%')
            </if>
            <if test="num !=null and num !=''">
                AND a.num LIKE concat('%',#{num},'%')
            </if>
            <if test="comSign !=null and comSign.name != null and comSign.name!=''">
                AND comSign.name LIKE concat('%',#{comSign.name},'%')
            </if>
            <if test="docType != null and docType != ''">
                AND a.doc_type = #{docType}
            </if>
            <if test="state != null">
                AND a.state = #{state}
            </if>
            AND a.del_flag = #{DEL_FLAG_NORMAL}
            ${sqlMap.dsf}
        </where>
    </select>

	<select id="findAllList" resultType="WorkAdministrativeAtamp" >
		SELECT 
			<include refid="workAdministrativeAtampColumns"/>
		FROM work_administrative_stamp a
		<include refid="workAdministrativeAtampJoins"/>
		<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_administrative_stamp(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			doc_type,
			num,
			com_sign,
			submiter,
			submit_date,
			seal_type,
			state,
			process_instance_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{office.id},
			#{company.id},
			#{docType},
			#{num},
			#{comSign.id},
			#{submiter.id},
			#{submitDate},
			#{sealType},
			#{state},
			#{processInstanceId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_administrative_stamp SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{office.id},
			company_id = #{company.id},
			doc_type = #{docType},
			num = #{num},
			com_sign = #{comSign.id},
			submiter = #{submiter.id},
			submit_date = #{submitDate},
			seal_type = #{sealType},
			state = #{state},
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		UPDATE work_administrative_stamp SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_administrative_stamp SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkAdministrativeAtamp" statementType="STATEMENT">
		select * FROM work_administrative_stamp  where ${propertyName} = '${value}'
	</select>

    <update id="updateProcessIdAndStatus" >
        UPDATE  work_administrative_stamp SET
        process_instance_id = #{processInstanceId},
        state = #{state}
        WHERE id = #{id}
    </update>
</mapper>