<?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="cn.com.energy.mapper.DvMachineryDataMapper">

	<resultMap type="DvMachineryData" id="DvMachineryDataResult">
		<id     property="dataId"         column="data_id"        />
		<result property="machineryCode"          column="machinery_code"          />
		<result property="machineryName"   column="machinery_name"  />
		<result property="runData"         column="run_data"         />
		<result property="parserData"  column="parser_data" />
		<result property="attr1"   column="attr1"  />
		<result property="attr2"       column="attr2"      />
		<result property="attr3"       column="attr3"      />
		<result property="remark"        column="remark"       />
		<result property="createTime"       column="create_time"      />
	</resultMap>

	<sql id="selectDvMachineryData">
        select data_id, machinery_code, machinery_name, run_data, parser_data, attr1, attr2, attr3, oper_url, remark, create_time from dv_machinery_data
    </sql>

	<select id="selectElectric" parameterType="DvMachineryData" resultType="java.util.HashMap">
		select min(parser_data) as min_data, max(parser_data) as max_data, max(parser_data)-min(parser_data) as parser_data from dv_machinery_data
		<where>
			<if test="machineryCode != null  and machineryCode != ''"> and machinery_code = #{machineryCode}</if>
			<if test="machineryName != null  and machineryName != ''"> and machinery_name like concat('%', #{machineryName}, '%')</if>
			<if test="attr1 != null  and attr1 != ''"> and attr1 = #{attr1}</if>
			<if test="attr2 != null  and attr2 != ''"> and attr2 = #{attr2}</if>
			<if test="attr3 != null  and attr3 != ''"> and attr3 = #{attr3}</if>
			<if test="createTimeStr != null and createTimeStr != ''"> and create_time like concat('%', #{createTimeStr}, '%')</if>
		</where>
		order by create_time desc
	</select>

	<select id="selectElectricByWeek" resultType="java.util.Map">
		SELECT
			DATE(create_time) AS day,
			COALESCE(MAX(parser_data) - MIN(parser_data), 0) AS parser_data
		FROM
			dv_machinery_data
		WHERE
			create_time BETWEEN CURDATE() - INTERVAL 7 DAY AND NOW()
		GROUP BY
			DATE(create_time)
		ORDER BY
			day ASC ;
	</select>
</mapper> 