<?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.bonus.canteen.core.nutrition.common.mapper.HealthBodyRecordMapper">

    <select id="listHealthMonitoring" resultType="com.bonus.canteen.core.nutrition.common.vo.HealthBodyRecordPageVO">
        SELECT
        h.crtime register_date,
        h.height,
        h.weight,
        h.bmi,
        h.shape,
        h.blood_pressure_high,
        h.blood_pressure_low,
        h.heart_rate,
        c.cust_num,
        h.cust_id,
        c.cust_name,
        c.mobile,
        co.org_full_name,
        h.health_info_source,
        c.sex
        FROM
        health_body_record h
        LEFT JOIN cust_info c ON c.cust_id = h.cust_id
        LEFT JOIN cust_org co ON c.org_id = co.org_id
        WHERE 1=1
        <if test="param.healthInfoSource != null">
            AND h.health_info_source = #{param.healthInfoSource}
        </if>
        <if test="param.custId != null">
            AND h.cust_id = #{param.custId}
        </if>
        <if test="param.keyword != null and param.keyword != ''">
            AND (
            c.cust_num = #{param.keyword}
            OR c.cust_name = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            OR c.mobile = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            or c.mobile_suffix = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            )
        </if>
        <if test="param.queryDate != null">
            <![CDATA[AND date_format( h.crtime, '%Y-%m' ) = date_format(#{param.queryDate},'%Y-%m')]]>
        </if>
        <if test="param.orgIdList != null and param.orgIdList.size() > 0">
            AND c.org_id IN
            <foreach collection="param.orgIdList" item="orgId" separator="," open="("
                     close=")">
                #{orgId}
            </foreach>
        </if>
        ORDER BY h.crtime DESC
    </select>


    <!--KingBase适配-->
    <select id="listHealthMonitoring" resultType="com.bonus.canteen.core.nutrition.common.vo.HealthBodyRecordPageVO"
            databaseId="kb">
        SELECT
        h.crtime register_date,
        h.height,
        h.weight,
        h.bmi,
        h.shape,
        h.blood_pressure_high,
        h.blood_pressure_low,
        h.heart_rate,
        c.cust_num,
        h.cust_id,
        c.cust_name,
        c.mobile,
        co.org_full_name,
        h.health_info_source,
        c.sex
        FROM
        health_body_record h
        LEFT JOIN cust_info c ON c.cust_id = h.cust_id
        LEFT JOIN cust_org co ON c.org_id = co.org_id
        WHERE 1=1
        <if test="param.healthInfoSource != null">
            AND h.health_info_source = #{param.healthInfoSource}
        </if>
        <if test="param.custId != null">
            AND h.cust_id = #{param.custId}
        </if>
        <if test="param.keyword != null and param.keyword != ''">
            AND (
            c.cust_num = #{param.keyword}
            OR c.cust_name = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            OR c.mobile = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            OR c.mobile_suffix = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            )
        </if>
        <if test="param.queryDate != null">
            <![CDATA[AND TO_CHAR( h.crtime, 'yyyy-mm' ) = TO_CHAR(#{param.queryDate},'yyyy-mm')]]>
        </if>
        <if test="param.orgIdList != null and param.orgIdList.size() > 0">
            AND c.org_id IN
            <foreach collection="param.orgIdList" item="orgId" separator="," open="("
                     close=")">
                #{orgId}
            </foreach>
        </if>
        ORDER BY h.crtime DESC
    </select>
</mapper>
