<?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.workstockcontract.dao.WorkStockContractDao">
    
	<sql id="workStockContractColumns">
		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",
		o.top_company AS "officeName",
		a.company_id AS "companyId",
		a.status AS "status",
		a.name AS "name",
		a.number AS "number",
		a.supplier_id AS "supplier.id",
		a.link_name AS "linkName",
		a.money AS "money",
		a.sign_date AS "signDate",
		supplier.name AS "supplier.name",
		supplier.name AS "supplierName",
		a.complete_status AS "completeStatus"
	</sql>
	
	<sql id="workStockContractJoins">
		LEFT JOIN work_supplier supplier ON supplier.id = a.supplier_id
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
	</sql>
	
    
	<select id="get" resultType="WorkStockContract" >
		SELECT 
			<include refid="workStockContractColumns"/>
		FROM work_stock_contract a
		<include refid="workStockContractJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkStockContract" >
		SELECT 
			<include refid="workStockContractColumns"/>
		FROM work_stock_contract a
		<include refid="workStockContractJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="number != null and number != ''">
				AND a.number = #{number}
			</if>
			<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>
			<if test="linkName != null and linkName != ''">
				AND a.link_name LIKE
				<if test="dbName == 'oracle'">'%'||#{linkName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{linkName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{linkName},'%')</if>
			</if>
			<if test="supplier != null and supplier.name != null and supplier.name != ''">
				AND supplier.name LIKE
				<if test="dbName == 'oracle'">'%'||#{supplier.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{supplier.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{supplier.name},'%')</if>
			</if>
			<if test="completeStatus != null and completeStatus != ''">
				AND a.complete_status = #{completeStatus}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.sign_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</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.sign_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkStockContract" >
		SELECT 
			<include refid="workStockContractColumns"/>
		FROM work_stock_contract a
		<include refid="workStockContractJoins"/>
		<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_stock_contract(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_instance_id,
			office_id,
			company_id,
			status,
			name,
			number,
			supplier_id,
			link_name,
			money,
			sign_date,
			complete_status
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processInstanceId},
			#{officeId},
			#{companyId},
			#{status},
			#{name},
			#{number},
			#{supplier.id},
			#{linkName},
			#{money},
			#{signDate},
			#{completeStatus}
		)
	</insert>
	
	<update id="update">
		UPDATE work_stock_contract SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_instance_id = #{processInstanceId},
			office_id = #{officeId},
			company_id = #{companyId},
			status = #{status},
			name = #{name},
			number = #{number},
			supplier_id = #{supplier.id},
			link_name = #{linkName},
			money = #{money},
			sign_date = #{signDate},
			complete_status = #{completeStatus}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_stock_contract
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_stock_contract SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkStockContract" statementType="STATEMENT">
		select * FROM work_stock_contract  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListBysupplier" resultType="WorkSupplier">
		SELECT 
			*
		FROM work_supplier a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id LIKE 
				<if test="dbName == 'oracle'">#{id}||'%'</if>
				<if test="dbName == 'mssql'">#{id}+'%'</if>
				<if test="dbName == 'mysql'">concat(#{id},'%')</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>


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

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkStockContract">
		SELECT
		<include refid="workStockContractColumns"/>
		FROM work_stock_contract a
		<include refid="workStockContractJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<update id="updateCompleteStatus">
		UPDATE work_stock_contract SET
		complete_status = #{completeStatus}
		WHERE id = #{id}
	</update>
</mapper>