<?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.caseinfo.dao.CaseInfoDao">
    
	<sql id="caseInfoColumns">
		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.class_id AS "caseCategory.id",
		a.name AS "name",
		a.source AS "source",
		a.companyName AS "companyName",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.area_id AS "area.id",
		area.name AS "area.name",
		a.price AS "price",
		project.project_name AS "project.projectName",
		a.project_id AS "project.id",
		a.feature AS "feature",
		a.record AS "record",
		a.ext AS "ext",
		a.contractName AS "contractName",
		a.peculiarity AS "peculiarity",
		a.unit AS "unit",
		a.status AS "status",
		a.userids AS "userids",
		a.share_status AS "shareStatus",
		a.company_id AS "companyId",
		a.process_instance_id AS "processInstanceId",
		a.office_id AS "officeId",
		u.name AS "createBy.name",
		u2.name AS "updateBy.name",
		o.name AS "officeName",
		c.name AS "caseCategory.name"
	</sql>
	
	<sql id="caseInfoJoins">
		LEFT JOIN case_category c ON c.id = a.class_id
		left join sys_user u on a.create_by = u.id
		left join sys_user u2 on a.update_by = u2.id
		LEFT JOIN sys_area area ON area.id = a.area_id
		LEFT JOIN project_records project ON project.id = a.project_id
		JOIN sys_office o ON o.id = a.office_id
		JOIN sys_office s ON s.id = a.company_id
	</sql>
	
    
	<select id="get" resultType="CaseInfo" >
		SELECT 
			<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findListme" resultType="CaseInfo" >
		SELECT
		<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by = #{createBy.id}
			</if>
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</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="findListoffice" resultType="CaseInfo" >
		SELECT
		<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and a.status in('2','5')
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</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="findListoffice2" resultType="CaseInfo" >
		SELECT
		<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and a.status =5
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</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="findList" resultType="CaseInfo" >
		SELECT 
			<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		<where>
			(a.share_status = '1' and
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</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="id != null and id != ''">
				AND a.id = #{id}
			</if>
			) or
			(
			(a.del_flag = #{DEL_FLAG_NORMAL} AND a.status = '4'
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			${sqlMap.dsf})
			or(
			(a.status != '4'
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by = #{createBy.id}
			</if>
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			) or (a.status in('2','3','4')
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				and find_in_set(#{createBy.id},a.userids)
			</if>
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</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="findByAllList" resultType="CaseInfo" >
		SELECT
		<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		<where>
			a.status = '5'
			AND
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</if>
			<if test="shareStatus != null and shareStatus != ''">
				AND a.share_status = #{shareStatus}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</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="findByAllLists" resultType="CaseInfo" >
		SELECT
		<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		<where>
			a.status = '5' and a.share_status is not null
			AND
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</if>
			<if test="shareStatus != null and shareStatus != ''">
				AND a.share_status = #{shareStatus}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
		</where>
		<choose>
			<when test="dateOrder == 1">
				order by a.start_date  desc
			</when>
			<when test="dateOrder == 2">
				order by a.start_date  asc
			</when>
			<!--<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="findBuyCase" resultType="CaseInfo" >
		SELECT
		DISTINCT
		<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		LEFT join case_member cm on cm.case_id = a.id
		<where>
			a.status = '5' and a.share_status is not null
			AND
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</if>
			<if test="memberId != null and memberId != ''">
				AND cm.member_id = #{memberId}
			</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="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
		</where>
		<choose>
			<when test="dateOrder == 1">
				order by a.start_date  desc
			</when>
			<when test="dateOrder == 2">
				order by a.start_date  asc
			</when>
			<!--<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="findCompanyList" resultType="CaseInfo" >
		SELECT
		<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} AND a.status = '5'
			<if test="caseCategory != null and caseCategory.id != null and caseCategory.id != ''">
				AND a.class_id = #{caseCategory.id}
			</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="createBy != null and createBy != null and createBy.name != ''">
				AND u.name LIKE
				<if test="dbName == 'oracle'">'%'||#{createBy.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{createBy.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{createBy.name},'%')</if>
			</if>
			<if test="area != null and area.id != null and area.id != ''">
				AND a.area_id = #{area.id}
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.start_date = #{startDate}
			</if>
			<if test="shareStatus != null and shareStatus != ''">
				AND a.share_status = #{shareStatus}
			</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="companyId != null and companyId != ''">
				AND find_in_set(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>

	<select id="findAllList" resultType="CaseInfo" >
		SELECT 
			<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		<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 case_info(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			class_id,
			name,
			start_date,
			end_date,
			area_id,
			price,
			project_id,
			feature,
			source,
			companyName,
			ext,
			contractName,
			peculiarity,
			unit,
			status,
			userids,
			share_status,
			company_id,
			office_id,
			record
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{caseCategory.id},
			#{name},
			#{startDate},
			#{endDate},
			#{area.id},
			#{price},
			#{project.id},
			#{feature},
			#{source},
			#{companyName},
			#{ext},
			#{contractName},
			#{peculiarity},
			#{unit},
			#{status},
			#{userids},
			#{shareStatus},
			#{companyId},
			#{officeId},
			#{record}
		)
	</insert>
	
	<update id="update">
		UPDATE case_info SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			class_id = #{caseCategory.id},
			name = #{name},
			start_date = #{startDate},
			end_date = #{endDate},
			area_id = #{area.id},
			price = #{price},
			project_id = #{project.id},
			feature = #{feature},
			source = #{source},
			ext = #{ext},
			contractName = #{contractName},
			peculiarity = #{peculiarity},
			unit = #{unit},
			status = #{status},
			userids = #{userids},
			share_status = #{shareStatus},
			company_id = #{companyId},
			office_id = #{officeId},
			record = #{record}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM case_info
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE case_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="CaseInfo" statementType="STATEMENT">
		select * FROM case_info  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByproject" resultType="WorkProject">
		SELECT 
			*
		FROM work_project 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 case_info SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<select id="getByProcessInstanceId" resultType="CaseInfo">
		SELECT
		<include refid="caseInfoColumns"/>
		FROM case_info a
		<include refid="caseInfoJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>
</mapper>