<?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.workreceiptssettle.dao.WorkReceiptsSettleDao">
    
	<sql id="workReceiptsSettleColumns">
		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.num AS "num",
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		o.top_company AS "office.name",
		o.top_company AS "office.topCompany",
		u.name AS "createBy.name",
		a.state AS "state",
		a.process_instance_id AS "processInstanceId"
	</sql>
	
	<sql id="workReceiptsSettleJoins">
        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
	</sql>
	
    
	<select id="get" resultType="WorkReceiptsSettle" >
		SELECT 
			<include refid="workReceiptsSettleColumns"/>
		FROM work_receipts_settle a
		<include refid="workReceiptsSettleJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReceiptsSettle" >
		SELECT 
			<include refid="workReceiptsSettleColumns"/>
		FROM work_receipts_settle a
		<include refid="workReceiptsSettleJoins"/>
		<where>
            <if test="office !=null and office.id !=null and office.id !=''">
                AND a.office_id = #{office.id}
            </if>
            <if test="createBy !=null and createBy.id !=null and createBy.id !=''">
                AND a.create_by = #{createBy.id}
            </if>
            <if test="createBy !=null and createBy.name !=null and createBy.name !=''">
                AND u.name LIKE concat('%',#{createBy.name},'%')
            </if>
            <if test="num !=null and num !=''">
                AND a.num = #{num}
            </if>
            <if test="beginDate !=null">
                AND a.create_date >= #{beginDate}
            </if>
            <if test="endDate !=null">
                AND a.create_date &lt;= #{endDate}
            </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="findAllList" resultType="WorkReceiptsSettle" >
		SELECT 
			<include refid="workReceiptsSettleColumns"/>
		FROM work_receipts_settle a
		<include refid="workReceiptsSettleJoins"/>
		<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_receipts_settle(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			num,
			office_id,
			company_id,
			state,
            process_instance_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{num},
			#{office.id},
			#{company.id},
			#{state},
		    #{processInstanceId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_receipts_settle SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			num = #{num},
			office_id = #{office.id},
			company_id = #{company.id},
			state = #{state},
		    process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		UPDATE work_receipts_settle SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_receipts_settle SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReceiptsSettle" statementType="STATEMENT">
		select * FROM work_receipts_settle  where ${propertyName} = '${value}'
	</select>

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

    <select id="countByInvoiceNumAndComp" resultType="int">
        SELECT count(*)
        FROM work_receipts_settle t
        INNER JOIN work_receipts_settle_invoice t1 ON t1.settle_id=t.id
        WHERE t.company_id = #{comId} AND t1.num = #{invoiceNum} AND t.id != #{id}
    </select>
</mapper>