<?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.workexample.dao.WorkExampleDao">
    
	<sql id="workExampleColumns">
		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.company_id AS "companyId",
		a.office_id AS "officeId",
		o.name AS "officeName",
		a.status AS "status",
		a.name AS "name",
		a.type AS "mainDictDetail.id",
		md.detail_name AS "mainDictDetail.detailName",
		a.address AS "area.id",
		area.name AS "area.name",
		a.build_scale AS "buildScale",
		a.modus AS "modus",
		a.basic_type AS "basicType",
		a.scale AS "scale",
		a.safe_level AS "safeLevel",
		a.stage AS "stage",
		a.investment_type AS "investmentType",
		a.valuation_type AS "valuationType",
		a.valuation_according AS "valuationAccording",
		a.bidding_price AS "biddingPrice",
		a.tax_model AS "taxModel",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.aboveground_area AS "abovegroundArea",
		a.underground_area AS "undergroundArea",
		a.civil_area AS "civilArea",
		a.garage_area AS "garageArea",
		a.aboveground_layer AS "abovegroundLayer",
		a.underground_layer AS "undergroundLayer",
		a.aboveground_height AS "abovegroundHeight",
		a.underground_height AS "undergroundHeight",
		a.total_height AS "totalHeight",
		a.eaves_height AS "eavesHeight",
		a.inside_decoration AS "insideDecoration",
		a.outside_decoration AS "outsideDecoration",
		a.skylight_decoration AS "skylightDecoration",
		a.floor_decoration AS "floorDecoration",
		a.door AS "door",
		a.window AS "window",
		a.instruction AS "instruction",
		a.index_id AS "indexId",
		a.item_id AS "itemId",
		u.name AS "createBy.name",
		a.process_instance_id AS "processInstanceId"
	</sql>
	
	<sql id="workExampleJoins">
		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 u on u.id = a.create_by
		LEFT JOIN sys_area area ON area.id = a.address
		LEFT  JOIN  main_dict_detail md on md.id = a.type
	</sql>
	
	<select id="get" resultType="WorkExample" >
		SELECT
			<include refid="workExampleColumns"/>
		FROM work_example a
		<include refid="workExampleJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByTypes" resultType="WorkExample" >
		SELECT
			count(a.build_scale) AS counts,
			sum(a.build_scale) AS sums,
			max(a.build_scale) AS maxs,
			min(a.build_scale) AS mins,
			avg(a.build_scale) AS avgs
		FROM
			work_example a
		INNER JOIN (
				select id FROM main_dict_detail where (FIND_IN_SET(#{dictId},parent_ids) OR id = #{dictId}) AND end_flag = '1' AND company_id =  #{companyId}
			) d ON d.id = a.type
		<if test="area != null and area.id != null and area.id != ''">
			INNER JOIN (
				select id from sys_area where (FIND_IN_SET(#{area.id},parent_ids) OR id = #{area.id})
				) r ON r.id =  a.address
		</if>
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.company_id = #{companyId}
			AND a.status = '5'
	</select>

    <select id="getSumBuildScale" resultType="java.math.BigDecimal" >
        SELECT
        SUM(a.build_scale)
        FROM
        work_example a
		INNER JOIN (
		select id FROM main_dict_detail where (FIND_IN_SET(#{dictId},parent_ids) OR id = #{dictId}) AND end_flag = '1' AND company_id =  #{companyId}
		) d ON d.id = a.type
		<if test="area != null and area.id != null and area.id != ''">
			INNER JOIN (
			select id from sys_area where (FIND_IN_SET(#{area.id},parent_ids) OR id = #{area.id})
			) r ON r.id =  a.address
		</if>
        WHERE
        EXISTS (
        SELECT
        1
        FROM
        example_single s
        WHERE
        s.example_id = a.id
        )
        AND a.company_id = #{companyId}
        AND a.status = '5'
    </select>

	<select id="findList" resultType="WorkExample" >
		SELECT 
			<include refid="workExampleColumns"/>
		FROM work_example a
		<include refid="workExampleJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</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="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.end_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<if test="mainDictDetail !=null and mainDictDetail.id != null and mainDictDetail.id != ''">
				AND a.type = #{mainDictDetail.id}
			</if>
			<if test="type !=null and type != ''">
				AND (md.id = #{type} OR find_in_set(#{type},md.parent_ids))
			</if>
			<if test="area != null and area.id != null and area.id != ''">
				AND (area.id = #{area.id} or find_in_set(#{area.id},area.parent_ids))
			</if>
			<if test="buildScale != null and buildScale != ''">
				AND a.build_scale = #{buildScale}
			</if>
			<if test="modus != null and modus != ''">
				AND a.modus LIKE
				<if test="dbName == 'oracle'">'%'||#{modus}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{modus}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{modus},'%')</if>
			</if>
			<if test="basicType != null and basicType != ''">
				AND a.basic_type = #{basicType}
			</if>
			<if test="scale != null and scale != ''">
				AND a.scale = #{scale}
			</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.end_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkExample" >
		SELECT 
			<include refid="workExampleColumns"/>
		FROM work_example a
		<include refid="workExampleJoins"/>
		<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.end_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_example(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			status,
			name,
			type,
			address,
			build_scale,
			modus,
			basic_type,
			scale,
			safe_level,
			stage,
			investment_type,
			valuation_type,
			valuation_according,
			bidding_price,
			tax_model,
			start_date,
			end_date,
			aboveground_area,
			underground_area,
			civil_area,
			garage_area,
			aboveground_layer,
			underground_layer,
			aboveground_height,
			underground_height,
			total_height,
			eaves_height,
			inside_decoration,
			outside_decoration,
			skylight_decoration,
			floor_decoration,
			door,
			window,
			instruction,
			index_id,
			item_id,
			process_instance_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{status},
			#{name},
			#{mainDictDetail.id},
			#{area.id},
			#{buildScale},
			#{modus},
			#{basicType},
			#{scale},
			#{safeLevel},
			#{stage},
			#{investmentType},
			#{valuationType},
			#{valuationAccording},
			#{biddingPrice},
			#{taxModel},
			#{startDate},
			#{endDate},
			#{abovegroundArea},
			#{undergroundArea},
			#{civilArea},
			#{garageArea},
			#{abovegroundLayer},
			#{undergroundLayer},
			#{abovegroundHeight},
			#{undergroundHeight},
			#{totalHeight},
			#{eavesHeight},
			#{insideDecoration},
			#{outsideDecoration},
			#{skylightDecoration},
			#{floorDecoration},
			#{door},
			#{window},
			#{instruction},
			#{indexId},
			#{itemId},
			#{processInstanceId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_example SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			status = #{status},
			name = #{name},
			type = #{mainDictDetail.id},
			address = #{area.id},
			build_scale = #{buildScale},
			modus = #{modus},
			basic_type = #{basicType},
			scale = #{scale},
			safe_level = #{safeLevel},
			stage = #{stage},
			investment_type = #{investmentType},
			valuation_type = #{valuationType},
			valuation_according = #{valuationAccording},
			bidding_price = #{biddingPrice},
			tax_model = #{taxModel},
			start_date = #{startDate},
			end_date = #{endDate},
			aboveground_area = #{abovegroundArea},
			underground_area = #{undergroundArea},
			civil_area = #{civilArea},
			garage_area = #{garageArea},
			aboveground_layer = #{abovegroundLayer},
			underground_layer = #{undergroundLayer},
			aboveground_height = #{abovegroundHeight},
			underground_height = #{undergroundHeight},
			total_height = #{totalHeight},
			eaves_height = #{eavesHeight},
			inside_decoration = #{insideDecoration},
			outside_decoration = #{outsideDecoration},
			skylight_decoration = #{skylightDecoration},
			floor_decoration = #{floorDecoration},
			door = #{door},
			window = #{window},
			instruction = #{instruction},
			index_id = #{indexId},
			item_id = #{itemId},
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_example
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_example SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkExample" statementType="STATEMENT">
		select * FROM work_example  where ${propertyName} = '${value}'
	</select>


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

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkExample">
		SELECT
		<include refid="workExampleColumns"/>
		FROM work_example a
		<include refid="workExampleJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>
</mapper>