<?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.workreimbursement.dao.WorkReimbursementDao">
    
	<sql id="workReimbursementColumns">
		a.id AS "id",
		a.process_instance_id AS "processInstanceId",
		a.number AS "number",
		a.business_type AS "businessType",
		a.submitter_id AS "submitterId",
		a.submitter AS "submitter",
		a.submitter_date AS "submitterDate",
		a.project_id AS "project.id",
		a.money AS "money",
		a.backmoney as "backmoney",
		a.backmoneys as "backmoneys",
		a.checkMoneys as "checkMoneys",
		a.status AS "status",
		a.attachment AS "attachment",
		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.is_public AS "isPublic",
		a.comname AS "comname",
		a.achives_id as "achivesId",
		a.bank AS "bank",
		a.bank_no AS "bankNo",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		p.project_name AS "project.projectName",
		a.project_name AS "projectName",
		a.bank_card AS "bankCard",
		a.ext AS "ext",
		a.create_office as "createOffice",
		a.type as "type"
	</sql>
	
	<sql id="workReimbursementJoins">
        LEFT JOIN project_records p on p.id = a.project_id
	</sql>
	
    
	<select id="get" resultType="WorkReimbursement" >
		SELECT 
			<include refid="workReimbursementColumns"/>
		FROM work_reimbursement a
		<include refid="workReimbursementJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReimbursement" >
		SELECT 
			<include refid="workReimbursementColumns"/>
		FROM work_reimbursement a
		<include refid="workReimbursementJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="project != null and project.id != null and project.id != ''">
				AND a.project_id = #{project.id}
			</if>
			<if test="status != '' and status != null">
				AND a.status = #{status}
			</if>
			<if test="officeId != '' and officeId != null">
				AND a.office_id = #{officeId}
			</if>
			<if test="submitterDate != '' and submitterDate != null">
				AND a.submitter_date = #{submitterDate}
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.submitter_date &gt;= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.submitter_date &lt;= #{endDate}
			</if>
			<if test="submitterId != null and submitterId != ''">
				AND a.submitter_id = #{submitterId}
			</if>
			${sqlMap.dsf} or a.submitter_id=#{currentUser.id}
		</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_reimbursement a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="project != null and project.id != null and project.id != ''">
				AND a.project_id = #{project.id}
			</if>
			<if test="status != '' and status != null">
				AND a.status = #{status}
			</if>
			<if test="officeId != '' and officeId != null">
				AND a.office_id = #{officeId}
			</if>
			<if test="submitterDate != '' and submitterDate != null">
				AND a.submitter_date = #{submitterDate}
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.submitter_date &gt;= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.submitter_date &lt;= #{endDate}
			</if>
			<if test="submitterId != null and submitterId != ''">
				AND a.submitter_id = #{submitterId}
			</if>
			${sqlMap.dsf} or a.submitter_id=#{currentUser.id}
		</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="WorkReimbursement" >
		SELECT 
			<include refid="workReimbursementColumns"/>
		FROM work_reimbursement a
		<include refid="workReimbursementJoins"/>
		<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>
		
	<update id="updateProcessInstanceId">
		UPDATE work_reimbursement SET 
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	<insert id="insert">
		INSERT INTO work_reimbursement(
			id,
			number,
			business_type,
			office_id,
			submitter_id,
			submitter_date,
			project_id,
			money,
			status,
			attachment,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			submitter,
			project_name,
			bank_card,
			ext,
			is_public,
			comname,
			bank,
			bank_no,
			achives_id,
			backmoney,
			backmoneys,
			checkMoneys,
            create_office,
            type
		) VALUES (
			#{id},
			#{number},
			#{businessType},
			#{officeId},
			#{submitterId},
			#{submitterDate},
			#{project.id},
			#{money},
			#{status},
			#{attachment},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{submitter},
			#{projectName},
			#{bankCard},
			#{ext},
			#{isPublic},
			#{comname},
			#{bank},
			#{bankNo},
			#{achivesId},
			#{backmoney},
			#{backmoneys},
			#{checkMoneys},
			#{createOffice},
			#{type}

		)
	</insert>
	
	<update id="update">
		UPDATE work_reimbursement SET 	
			number = #{number},
			business_type = #{businessType},
			office_id = #{officeId},
			<if test="limitCond != 1">
				submitter_date = #{submitterDate},
			</if>
			project_id = #{project.id},
			money = #{money},
			status = #{status},
			attachment = #{attachment},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			submitter = #{submitter},
		    submitter_id = #{submitterId},
		    bank_card = #{bankCard},
			ext = #{ext},
		    is_public = #{isPublic},
		    comname = #{comname},
		    bank = #{bank},
		    bank_no = #{bankNo},
		    achives_id =#{achivesId},
		    backmoney = #{backmoney},
		    backmoneys =#{backmoneys},
		    checkMoneys = #{checkMoneys},
		    type = #{type}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_reimbursement
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_reimbursement SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReimbursement" statementType="STATEMENT">
		select * FROM work_reimbursement  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByproject" resultType="WorkBidProject">
		select
		a.id AS "id",
		a.project_number AS "projectNumber",
		a.project_name AS "projectName",
		a.project_type AS "projectType",
		a.type_explain AS "typeExplain",
		a.project_source AS "projectSource",
		a.source_explain AS "sourceExplain",
		a.is_ext AS "isExt",
		a.bid_company AS "bidCompany.id",
		a.ext_company AS "extCompany",
		a.contacts AS "contacts",
		a.registration_date AS "registrationDate",
		a.bid_date AS "bidDate",
		a.trial_date AS "trialDate",
		a.trial_type AS "trialType",
		a.document_cost AS "documentCost",
		a.purchase_date AS "purchaseDate",
		a.bind_cost AS "bindCost",
		a.other_cost AS "otherCost",
		a.bid_bond AS "bidBond",
		a.bond_style AS "bondStyle",
		a.refund_date AS "refundDate",
		a.offer AS "offer",
		a.is_bid AS "isBid",
		a.implementing_dept AS "implementingDept.id",
		a.is_subcontract AS "isSubcontract",
		a.subcontract_company AS "subcontractCompany",
		a.project_situation AS "projectSituation",
		a.status AS "status",
		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",
		bidCompany.name AS "bidCompany.name",
		(select GROUP_CONCAT(user_id)
			from work_project_user
			where project_id = a.id and is_master = 1 GROUP BY project_id) AS "projectMaster",
		a.project_member AS "projectMember",
		u.name AS "createBy.name",
		a.company_id AS "companyId",
		a.process_instance_id AS "processInstanceId"

		FROM work_bid_project a
		LEFT JOIN work_client_info bidCompany ON bidCompany.id = a.bid_company
		LEFT JOIN sys_office implementingDept ON implementingDept.id = a.implementing_dept
		JOIN sys_user u ON u.id = a.create_by
		JOIN sys_office o ON o.id = u.office_id
		<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>
		</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="findListByprojectt" resultType="projectRecords">
		SELECT
		a.*
		FROM project_records a
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = a.create_by
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} AND a.status in ('5','9')
			<if test="projectName != null and projectName != ''">
				AND a.project_name LIKE
				<if test="dbName == 'oracle'">#{projectName}||'%'</if>
				<if test="dbName == 'mssql'">#{projectName}+'%'</if>
				<if test="dbName == 'mysql'">concat(#{projectName},'%')</if>
			</if>
			<if test="company!= null and company.id != null and company.id != ''">
				AND a.company_id = #{company.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>

    <!-- 获取流程ID -->
    <select id="getByProcessInstanceId" resultType="WorkReimbursement">
        SELECT
        <include refid="workReimbursementColumns"/>
        FROM work_reimbursement a
        <include refid="workReimbursementJoins"/>
        WHERE a.process_instance_id = #{processInstanceId}
    </select>



</mapper>