<?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.workproject.dao.WorkProjectDao">
    
	<sql id="workProjectColumns">
		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.del_flag AS "delFlag",
		a.remarks AS "remarks",
		a.name AS "name",
		a.number AS "number",
		a.contract_id AS "contractId.id",
		a.sort AS "sort",
		a.company_id AS "companyId",
		a.state AS "state",
		a.office_id AS "officeId"
	</sql>
	
	<sql id="workProjectJoins">
		LEFT JOIN work_contract_info contractId ON contractId.id = a.contract_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="WorkProject" >
		SELECT 
			<include refid="workProjectColumns"/>
		FROM work_project a
		<include refid="workProjectJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByCId" resultType="WorkProject" >
		SELECT
		<include refid="workProjectColumns"/>
		FROM work_project a
		<include refid="workProjectJoins"/>
		WHERE a.contract_id = #{id}
	</select>


	<update id="updateStatus">
		UPDATE work_project SET
		state = #{state}
		WHERE id = #{id}
	</update>

	<select id="findList" resultType="WorkProject" >
		SELECT 
			<include refid="workProjectColumns"/>
		FROM work_project a
		<include refid="workProjectJoins"/>
		<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>
			<if test="number != null and number != ''">
				AND a.number = #{number}
			</if>
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="sort != null and sort != ''">
				AND a.sort LIKE
				<if test="dbName == 'oracle'">'%'||#{sort}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{sort}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{sort},'%')</if>
			</if>
			<if test="contractId != null and contractId != ''">
				AND a.contract_id = #{contractId.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>
	
	<select id="findAllList" resultType="WorkProject" >
		SELECT 
			<include refid="workProjectColumns"/>
		FROM work_project a
		<include refid="workProjectJoins"/>
		<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_project(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			remarks,
			name,
			number,
			contract_id,
			sort,
			company_id,
			state,
			office_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{remarks},
			#{name},
			#{number},
			#{contractId.id},
			#{sort},
			#{companyId},
			#{state},
			#{officeId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_project SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			name = #{name},
			number = #{number},
			contract_id = #{contractId.id},
			sort = #{sort},
			company_id = #{companyId},
			office_id = #{officeId},
			state  = #{state},
			del_flag = #{delFlag}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_project
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_project SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkProject" statementType="STATEMENT">
		select * FROM work_project  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListBycontractId" resultType="WorkContractInfo">
		SELECT 
			*
		FROM work_contract_info a
		<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>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</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>
</mapper>