<?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.exampleeconomics.dao.ExampleEconomicsDao">
    
	<sql id="exampleEconomicsColumns">
		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.example_id AS "exampleId",
		a.exampleindices_id AS "exampleindicesId",
		a.index_id AS "indexId",
		a.unit AS "unit",
		a.digits AS "digits",
		a.name AS "name",
		a.money AS "money",
		a.cost AS "cost",
		a.proportion AS "proportion"
	</sql>
	
	<sql id="exampleEconomicsJoins">
	</sql>
	
    
	<select id="get" resultType="ExampleEconomics" >
		SELECT 
			<include refid="exampleEconomicsColumns"/>
		FROM example_economics a
		<include refid="exampleEconomicsJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getCount" resultType="ExampleEconomics" >
		SELECT
			i.name AS "name",
			count(a.index_id) AS counts,
			sum(a.money) AS sums,
			max(a.cost) AS maxs,
			min(a.cost) AS mins,
			e.build_scale AS avgs
		FROM
			example_economics a
		INNER JOIN work_example e ON e.id = a.example_id
		LEFT JOIN work_example_indices i ON i.id = a.exampleindices_id
		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 = e.type
		<if test="areaId != null and areaId != ''">
			INNER JOIN (
			select id from sys_area where (FIND_IN_SET(#{areaId},parent_ids) OR id = #{areaId})
			) r ON r.id = e.address
		</if>
		WHERE
			a.del_flag = '0'
			AND e.company_id =  #{companyId}
			AND i.is_disable = '0' AND i.one_items = '2'
			AND a.index_id = #{indexId}
			AND e.status = '5'
		GROUP BY a.exampleindices_id;
	</select>
	
	<select id="findList" resultType="ExampleEconomics" >
		SELECT 
			<include refid="exampleEconomicsColumns"/>
		FROM example_economics a
		<include refid="exampleEconomicsJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="exampleId != null and exampleId != ''">
				AND a.example_id = #{exampleId}
			</if>
			<if test="exampleindicesId != null and exampleindicesId != ''">
				AND a.exampleindices_id = #{exampleindicesId}
			</if>
			<if test="indexId != null and indexId != ''">
				AND a.index_id = #{indexId}
			</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="ExampleEconomics" >
		SELECT 
			<include refid="exampleEconomicsColumns"/>
		FROM example_economics a
		<include refid="exampleEconomicsJoins"/>
		<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 example_economics(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			example_id,
			exampleindices_id,
			index_id,
			unit,
			digits,
			name,
			money,
			cost,
			proportion
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{exampleId},
			#{exampleindicesId},
			#{indexId},
			#{unit},
			#{digits},
			#{name},
			#{money},
			#{cost},
			#{proportion}
		)
	</insert>
	
	<update id="update">
		UPDATE example_economics SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			example_id = #{exampleId},
			exampleindices_id = #{exampleindicesId},
			index_id = #{indexId},
			unit = #{unit},
			digits = #{digits},
			name = #{name},
			money = #{money},
			cost = #{cost},
			proportion = #{proportion}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM example_economics
		WHERE id = #{id}
	</update>
	<update id="deleteByEId">
		DELETE FROM example_economics
		WHERE example_id = #{eId}
	</update>
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE example_economics SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	<update id="delteByIndexId">
		DELETE FROM example_economics
		WHERE example_id = #{eId} and index_id = #{indexId}
	</update>
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="ExampleEconomics" statementType="STATEMENT">
		select * FROM example_economics  where ${propertyName} = '${value}'
	</select>

	<select id="findEconomics" resultType="ExampleEconomics" >
		SELECT
		<include refid="exampleEconomicsColumns"/>
		FROM example_economics a
		<include refid="exampleEconomicsJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="exampleId != null and exampleId != ''">
				AND a.example_id = #{exampleId}
			</if>
			<if test="exampleindicesId != null and exampleindicesId != ''">
				AND a.exampleindices_id = #{exampleindicesId}
			</if>
			<if test="indexId != null and indexId != ''">
				AND a.index_id = #{indexId}
			</if>
		</where>
	</select>
</mapper>