<?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.workloan.dao.WorkLoanDao">
    
	<sql id="workLoanColumns">
		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_instance_id AS "processInstanceId",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.status AS "status",
		a.staff_achives_id AS "workStaffBasicInfo.id",
		sa.user_id AS "workStaffBasicInfo.userId",
		a.num AS "num",
		a.type AS "type",
		a.project_record_id AS "projectRecords.id",
		a.money AS "money",
		a.loan_date AS "loanDate",
		sa.name AS "workStaffBasicInfo.name",
		oa.top_company AS "workStaffBasicInfo.office.name",
		sa.bank_card AS "workStaffBasicInfo.bankCard",
		p.project_name AS "projectRecords.projectName",
		su.name AS "createBy.name",
		wci.contract_price AS "projectRecords.workContractInfo.contractPrice",
		wci.id AS "projectRecords.workContractInfo.id",
		a.repay_state AS "repayState"
	</sql>
	
	<sql id="workLoanJoins">
		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 su on su.id = a.create_by
		LEFT JOIN work_staff_achives sa on sa.id = a.staff_achives_id
		LEFT JOIN sys_office oa ON oa.id = sa.office_id
		LEFT JOIN project_records p ON p.id = a.project_record_id
		LEFT JOIN work_contract_info wci on p.contract_id = wci.id
	</sql>

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

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkLoan">
		SELECT
		<include refid="workLoanColumns"/>
		FROM work_loan a
		<include refid="workLoanJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<select id="get" resultType="WorkLoan" >
		SELECT 
			<include refid="workLoanColumns"/>
		FROM work_loan a
		<include refid="workLoanJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkLoan" >
		SELECT 
			<include refid="workLoanColumns"/>
		FROM work_loan a
		<include refid="workLoanJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="num != null and num != ''">
				AND a.num = #{num}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.loan_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.name !=null and workStaffBasicInfo.name != ''">
				AND sa.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workStaffBasicInfo.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workStaffBasicInfo.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workStaffBasicInfo.name},'%')</if>
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.office !=null and workStaffBasicInfo.office.name != null and workStaffBasicInfo.office.name != ''">
				AND oa.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workStaffBasicInfo.office.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workStaffBasicInfo.office.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workStaffBasicInfo.office.name},'%')</if>
			</if>
			<if test="projectRecords != null  and projectRecords.projectName != null and projectRecords.projectName != ''">
				AND p.project_name LIKE
				<if test="dbName == 'oracle'">'%'||#{projectRecords.projectName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{projectRecords.projectName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{projectRecords.projectName},'%')</if>
			</if>
			${sqlMap.dsf} or (sa.user_id = #{currentUser.id} and a.create_by != #{currentUser.id})
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.loan_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkLoan" >
		SELECT 
			<include refid="workLoanColumns"/>
		FROM work_loan a
		<include refid="workLoanJoins"/>
		<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>

	<select id="findListByContract" resultType="WorkLoan" >
		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.process_instance_id AS "processInstanceId",
			a.office_id AS "officeId",
			a.company_id AS "companyId",
			a.status AS "status",
			a.staff_achives_id AS "workStaffBasicInfo.id",
			a.num AS "num",
			a.type AS "type",
			a.project_record_id AS "projectRecords.id",
			a.money AS "money",
			a.loan_date AS "loanDate",
			wci.contract_price AS "projectRecords.workContractInfo.contractPrice",
			a.repay_state AS "repayState"
		FROM work_loan a
			LEFT JOIN project_records p ON p.id = a.project_record_id
			LEFT JOIN work_contract_info wci on p.contract_id = wci.id
		<where>
			a.del_flag = '0'
			and a.status = '5'
			and wci.id = #{contractId}
			<if test="repayState != null and repayState != ''">
				AND a.repay_state = #{repayState}
			</if>
		</where>
			group by a.id
	</select>

	<select id="findListByUser" resultType="WorkLoan" >
		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.process_instance_id AS "processInstanceId",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.status AS "status",
		a.staff_achives_id AS "workStaffBasicInfo.id",
		a.num AS "num",
		a.type AS "type",
		a.project_record_id AS "projectRecords.id",
		a.money AS "money",
		a.loan_date AS "loanDate",
		a.repay_state AS "repayState"
		FROM work_loan a
		LEFT JOIN work_staff_achives sa on sa.id = a.staff_achives_id
		<where>
			a.del_flag = '0'
			and a.status = '5'
			and sa.user_id = #{userId}
			<if test="repayState != null and repayState != ''">
				AND a.repay_state = #{repayState}
			</if>
		</where>
		group by a.id
	</select>

	<insert id="insert">
		INSERT INTO work_loan(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_instance_id,
			office_id,
			company_id,
			status,
			staff_achives_id,
			num,
			type,
			project_record_id,
			money,
			loan_date,
			repay_state
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processInstanceId},
			#{officeId},
			#{companyId},
			#{status},
			#{workStaffBasicInfo.id},
			#{num},
			#{type},
			#{projectRecords.id},
			#{money},
			#{loanDate},
			#{repayState}
		)
	</insert>
	
	<update id="update">
		UPDATE work_loan SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_instance_id = #{processInstanceId},
			office_id = #{officeId},
			company_id = #{companyId},
			status = #{status},
			staff_achives_id = #{workStaffBasicInfo.id},
			num = #{num},
			type = #{type},
			project_record_id = #{projectRecords.id},
			money = #{money},
			loan_date = #{loanDate},
			repay_state = #{repayState}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_loan
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_loan SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkLoan" statementType="STATEMENT">
		select * FROM work_loan  where ${propertyName} = '${value}'
	</select>
	<update id="updateReay">
		UPDATE work_loan SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			repay_state = #{repayState}
		WHERE id = #{id}
	</update>
</mapper>