<?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.lyms.platform.permission.dao.master.FolviteReceiveMapper">
    <resultMap id="folviteReceiveResultMap" type="com.lyms.platform.pojo.FolviteReceiveRecordModel">
        <result column="id" property="id"/>
        <result column="folvite_id" property="folviteId"/>
        <result column="receive_number" property="receiveNumber"/>
        <result column="receive_week" property="receiveWeek"/>
        <result column="receive_week" property="receiveWeek"/>
        <result column="highrisk" property="highrisk"/>
        <result column="provide_unit" property="provideUnit"/>
        <result column="issuer" property="issuer"/>
        <result column="receive_date" property="receiveDate"/>
        <result column="creation_date" property="creationDate"/>
        <result column="modified" property="modified"/>
        <result column="hospitalid" property="hospitalId"/>
    </resultMap>
    <insert id="saveFolviteReceive" parameterType="com.lyms.platform.pojo.FolviteReceiveRecordModel">
INSERT INTO lyms_folvite_provide(
id,
folvite_id,
receive_number,
receive_week,
highrisk,
provide_unit,
issuer,
receive_date,
creation_date,
modified,
isconceive,
hospitalid,
end_pregnancy,
yn
) VALUES (
#{id,jdbcType=VARCHAR},
#{folviteId,jdbcType=VARCHAR},
#{receiveNumber,jdbcType=INTEGER},
#{receiveWeek,jdbcType=INTEGER},
#{highrisk,jdbcType=VARCHAR},
#{provideUnit,jdbcType=VARCHAR},
#{issuer,jdbcType=VARCHAR},
#{receiveDate,jdbcType=TIMESTAMP},
#{creationDate,jdbcType=TIMESTAMP},
#{modified,jdbcType=TIMESTAMP},
#{isconceive,jdbcType=INTEGER},
#{hospitalId,jdbcType=VARCHAR},
#{endPregnancy,jdbcType=INTEGER},
#{yn,jdbcType=INTEGER})
    </insert>
    <select id="queryFolviteReceiveList" parameterType="com.lyms.platform.permission.model.FolviteRecordQuery"
            resultMap="folviteReceiveResultMap">
        SELECT * FROM lyms_folvite_provide
        <include refid="whereSql"/>
        <include refid="orderAndLimit"/>
    </select>
    <select id="list" parameterType="com.lyms.platform.permission.model.FolviteRecordQuery" resultType="map">
        SELECT
        f.`name` as name,f.birthday,f.card_no,f.vc_card,f.isconceive,f.receivesum,
        SUM(p.receive_number) as sumNumber,f.drawTime,f.domicile_address,f.domicile_area,
        f.domicile_city,f.domicile_country,f.domicile_province,p.folvite_id,p.issuer,f.phone,f.certificate_type
        FROM
        lyms_folvite_provide AS p
        LEFT JOIN
        lyms_folvite_Info f ON p.folvite_id = f.id
        <where>
            1=1
            <if test="yn != null">
                and p.yn=#{yn}
            </if>
            <if test="queryNo != null and queryNo != ''">
                and f.`name` =#{queryNo} or f.phone=#{queryNo} or f.card_no =#{queryNo} or f.vc_card=#{queryNo}
            </if>
            <if test="isconceive != null">
                and f.isconceive=#{isconceive}
            </if>
           <if test="hospitalId != null">
                and f.hospitalId=#{hospitalId}
            </if>
            <if test="startReceiveDate != null ">
                AND  <![CDATA[ p.receive_date >= date_format(#{startReceiveDate},'%Y-%m-%d %H:%i:%S')  ]]>
            </if>
            <if test="endReceiveDate != null">
                AND  <![CDATA[ p.receive_date <= date_format(#{endReceiveDate},'%Y-%m-%d %H:%i:%S')  ]]>
            </if>
            <if test="startDrawTime != null">
                AND  <![CDATA[ f.drawTime >= date_format(#{startDrawTime},'%Y-%m-%d %H:%i:%S')  ]]>
            </if>
            <if test="endDrawTime != null">
                AND  <![CDATA[ f.drawTime <= date_format(#{endDrawTime},'%Y-%m-%d %H:%i:%S')  ]]>
            </if>
        </where>
        GROUP BY p.folvite_id
        <include refid="orderAndLimit"/>
    </select>
    <select id="listCount" parameterType="com.lyms.platform.permission.model.FolviteRecordQuery"
            resultType="java.lang.Integer">
        select count(1) from (
        SELECT
        count(1)
        FROM
        lyms_folvite_provide AS p
        LEFT JOIN
        lyms_folvite_Info f ON p.folvite_id = f.id
        <where>
            1=1
            <if test="yn != null">
                and p.yn=#{yn}
            </if>
            <if test="queryNo != null and queryNo != ''">
                and f.`name` =#{queryNo} or f.phone=#{queryNo} or f.card_no =#{queryNo} or f.vc_card=#{queryNo}
            </if>
            <if test="isconceive !=null">
                and f.isconceive=#{isconceive}
            </if>
            <if test="startReceiveDate != null ">
                AND  <![CDATA[ p.receive_date >= date_format(#{startReceiveDate},'%Y-%m-%d %H:%i:%S')  ]]>
            </if>
            <if test="endReceiveDate != null">
                AND  <![CDATA[ p.receive_date <= date_format(#{endReceiveDate},'%Y-%m-%d %H:%i:%S')  ]]>
            </if>
            <if test="startDrawTime != null">
                AND  <![CDATA[ f.drawTime >= date_format(#{startDrawTime},'%Y-%m-%d %H:%i:%S')  ]]>
            </if>
            <if test="endDrawTime != null">
                AND  <![CDATA[ f.drawTime <= date_format(#{endDrawTime},'%Y-%m-%d %H:%i:%S')  ]]>
            </if>
        </where>
        GROUP BY p.folvite_id
        ) t
    </select>
    <select id="queyFolviteReceiveCount" parameterType="com.lyms.platform.permission.model.FolviteRecordQuery"
            resultType="java.lang.Integer">
        select count(id) from lyms_folvite_provide
        <include refid="whereSql"/>
    </select>
    <sql id="whereSql">
        <where>
            1=1
            <if test="folviteId != null and folviteId != ''">
                and folvite_id=#{folviteId}
            </if>
            <if test="yn != null">
                and yn=#{yn}
            </if>
        </where>
    </sql>
    <update id="updaetFolviteReceive" parameterType="com.lyms.platform.pojo.FolviteReceiveRecordModel">
        UPDATE lyms_folvite_provide
        <include refid="columnUpdate"/>
        where id=#{id,jdbcType=VARCHAR}
    </update>
    <sql id="columnUpdate">
        <set>
            <if test="id != null">
                id=#{id,jdbcType=VARCHAR},
            </if>
            <if test="folviteId != null">
                folvite_id=#{folviteId,jdbcType=VARCHAR},
            </if>
            <if test="receiveNumber != null">
                receive_number=#{receiveNumber,jdbcType=INTEGER},
            </if>
            <if test="receiveWeek != null">
                receive_week=#{receiveWeek,jdbcType=INTEGER},
            </if>
            <if test="highrisk != null">
                highrisk=#{highrisk,jdbcType=VARCHAR},
            </if>
            <if test="provideUnit != null">
                provide_unit=#{provideUnit,jdbcType=VARCHAR},
            </if>
            <if test="issuer != null">
                issuer=#{issuer,jdbcType=VARCHAR},
            </if>
            <if test="receiveDate != null">
                receive_date=#{receiveDate,jdbcType=TIMESTAMP},
            </if>
            <if test="creationDate != null">
                reation_date=#{creationDate,jdbcType=TIMESTAMP},
            </if>
            <if test="modified != null">
                modified=#{modified,jdbcType=TIMESTAMP},
            </if>
            <if test="isconceive != null">
                isconceive=#{isconceive,jdbcType=INTEGER},
            </if>
            <if test="endPregnancy != null ">
                end_pregnancy=#{endPregnancy,jdbcType=INTEGER},
            </if>
            <if test="yn != null">
                yn=#{yn}
            </if>
        </set>
    </sql>
    <select id="queryOneReceive" parameterType="java.lang.String" resultMap="folviteReceiveResultMap">
SELECT * FROM lyms_folvite_provide
where id=#{id,jdbcType=VARCHAR}
    </select>
    <sql id="orderAndLimit">
        <if test="sort != null and sort != '' ">
            order by ${sort}
            <if test="need != null">
                limit #{offset, jdbcType=INTEGER} , #{limit, jdbcType=INTEGER}
            </if>
        </if>
    </sql>
</mapper>