<?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.workborrowmangement.dao.WorkBorrowMangementDao">

	<sql id="workBorrowMangementColumns">
		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.borrower AS "borrower",
		a.number AS "number",
		a.borrow_date AS "borrowDate",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.borrow_for AS "borrowFor",
		a.state AS "state",
		a.fixed_assets_number AS "fixedAssetsNumber",
		a.item_name AS "itemName",
		a.item_number AS "itemNumber",
		a.item_type AS "itemType",
		a.item_model AS "itemModel",
		a.item_primary_unit AS "itemPrimaryUnit",
		a.back_state AS "backState",
		a.process_instance_id AS "processInstanceId",
		d.name as "userName",
		o.top_company AS "officeName",
		i.number AS assetsNumber
	</sql>

	<sql id="workBorrowMangementJoins">
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_user d on d.id = a.borrower
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		left join work_item i on i.id=a.fixed_assets_number
	</sql>


	<select id="get" resultType="WorkBorrowMangement" >
		SELECT
		<include refid="workBorrowMangementColumns"/>
		FROM work_borrow_management a
		<include refid="workBorrowMangementJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findList" resultType="WorkBorrowMangement" >
		SELECT
		<include refid="workBorrowMangementColumns"/>
		FROM work_borrow_management a
		<include refid="workBorrowMangementJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="borrower != null and borrower != ''">
				AND d.name LIKE
				<if test="dbName == 'oracle'">'%'||#{borrower}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{borrower}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{borrower},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.borrow_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			<if test="state != null and state != ''">
				AND a.state =#{state}
			</if>

			<if test="assetsNumber != null and assetsNumber != ''">
				AND  i.number=LIKE
				<if test="dbName == 'mysql'">concat('%',#{assetsNumber},'%')</if>
			</if>
			<if test="itemName != null and itemName != ''">
				AND a.item_name LIKE
				<if test="dbName == 'mysql'">concat('%',#{itemName},'%')</if>
			</if>
			<if test="itemModel != null and itemModel != ''">
				AND a.item_model LIKE
				<if test="dbName == 'mysql'">concat('%',#{itemModel},'%')</if>
			</if><if test="backState != null and backState != ''">
			AND a.back_state =#{backState}
		</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id =#{officeId}
			</if>
			<if test="sqlMap.dsf !=null and sqlMap.dsf!=''">
				${sqlMap.dsf}
			</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="queryCount" resultType="int">
		SELECT
		count(1)
		FROM work_borrow_management a
		<include refid="workBorrowMangementJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="borrower != null and borrower != ''">
				AND d.name LIKE
				<if test="dbName == 'oracle'">'%'||#{borrower}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{borrower}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{borrower},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.borrow_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			<if test="state != null and state != ''">
				AND a.state =#{state}
			</if>

			<if test="assetsNumber != null and assetsNumber != ''">
				AND  i.number=LIKE
				<if test="dbName == 'mysql'">concat('%',#{assetsNumber},'%')</if>
			</if>
			<if test="itemName != null and itemName != ''">
				AND a.item_name LIKE
				<if test="dbName == 'mysql'">concat('%',#{itemName},'%')</if>
			</if>
			<if test="itemModel != null and itemModel != ''">
				AND a.item_model LIKE
				<if test="dbName == 'mysql'">concat('%',#{itemModel},'%')</if>
			</if><if test="backState != null and backState != ''">
			AND a.back_state =#{backState}
		</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id =#{officeId}
			</if>
			<if test="sqlMap.dsf !=null and sqlMap.dsf!=''">
				${sqlMap.dsf}
			</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="findAllList" resultType="WorkBorrowMangement" >
		SELECT
		<include refid="workBorrowMangementColumns"/>
		FROM work_borrow_management a
		<include refid="workBorrowMangementJoins"/>
		<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_borrow_management(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			borrower,
			number,
			borrow_date,
			office_id,
			company_id,
			borrow_for,
			state,
			fixed_assets_number,
			item_name,
			item_number,
			item_type,
			item_model,
			item_primary_unit,
			back_state,
			process_instance_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{borrower},
			#{number},
			#{borrowDate},
			#{officeId},
			#{companyId},
			#{borrowFor},
			#{state},
			#{fixedAssetsNumber},
			#{itemName},
			#{itemNumber},
			#{itemType},
			#{itemModel},
			#{itemPrimaryUnit},
			#{backState},
			#{processInstanceId}
		)
	</insert>

	<update id="update">
		UPDATE work_borrow_management SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			borrower = #{borrower},
			number = #{number},
			borrow_date = #{borrowDate},
			office_id = #{officeId},
			company_id = #{companyId},
			borrow_for = #{borrowFor},
			state = #{state},
			fixed_assets_number = #{fixedAssetsNumber},
			item_name = #{itemName},
			item_number = #{itemNumber},
			item_type = #{itemType},
			item_model = #{itemModel},
			item_primary_unit = #{itemPrimaryUnit},
			back_state = #{backState},
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>


	<!--物理删除-->
	<update id="delete">
		UPDATE work_borrow_management SET
			del_flag ='1'
		WHERE id = #{id}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_borrow_management SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>


	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkBorrowMangement" statementType="STATEMENT">
		select * FROM work_borrow_management  where ${propertyName} = '${value}'
	</select>


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

	<update id="updateBackStateById" >
        UPDATE  work_borrow_management SET
        back_state ='2'
        WHERE id = #{borrowMangementId}
    </update>

</mapper>