<?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.xinelu.applet.mapper.patientcenter.PatientCenterMapper">

    <!-- 商品信息标签映射-->
    <resultMap type="com.xinelu.applet.vo.goodinfo.GoodInfoVO" id="GoodsInfoVOResult">
        <result property="goodsInfoId" column="goodsInfoId"/>
        <result property="nurseStationId" column="nurse_station_id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsPictureUrl" column="goods_picture_url"/>
        <result property="goodsUnit" column="goods_unit"/>
        <collection property="goodsAttributeDetailsList" javaType="java.util.List"
                    resultMap="goodsAttributeDetailsList"/>
    </resultMap>

    <!-- 商品属性明细信息标签映射-->
    <resultMap type="com.xinelu.manage.vo.goodsInfo.GetGoodAttributeDetailsList"
               id="goodsAttributeDetailsList">
        <result property="goodsAttributeId" column="goods_attribute_id"/>
        <result property="attributeDetailsId" column="attributeDetailsId"/>
        <result property="attributeDetailsName" column="attribute_details_name"/>
        <result property="goodsStock" column="goods_stock"/>
        <result property="attributePitureUrl" column="attribute_piture_url"/>
        <result property="integralExchangeFlag" column="integral_exchange_flag"/>
        <result property="integralExchangeSill" column="integral_exchange_sill"/>
        <result property="integralExchangeCount" column="integral_exchange_count"/>
    </resultMap>

    <select id="selectCouponByUseStatus" resultType="com.xinelu.applet.vo.coupon.CouponVO">
        select
        pcr.coupon_id couponId,
        pcr.coupon_title,
        pcr.coupon_price,
        pcr.coupon_consume_price,
        pcr.coupon_description,
        pcr.id patientCouponReceiveId,
        pcr.receive_source,
        pcr.expiration_start_time,
        pcr.expiration_end_time,
        pcr.receive_time,
        pcr.use_status
        from patient_coupon_receive pcr
        <where>
            <if test="patientId != null ">
                and pcr.patient_id = #{patientId}
            </if>
            <if test="receiveSource != null and receiveSource != ''">
                AND pcr.receive_source = #{receiveSource}
            </if>
            <if test="useStatus != null and useStatus != ''">
                AND pcr.use_status = #{useStatus}
            </if>
            <if test="useStatusList != null and useStatusList.size() > 0 ">
                and pcr.use_status in
                <foreach item="useStatus" collection="useStatusList" open="(" separator="," close=")">
                    #{useStatus}
                </foreach>
            </if>
        </where>
        order by pcr.coupon_price asc, pcr.coupon_consume_price asc
    </select>

        <select id="selectGoodsByExchangeFlag" resultType="com.xinelu.applet.dto.nursingstationgoods.GoodsList">
        select
        gi.id goodsInfoId,
        gi.nurse_station_id,
        gi.goods_name,
        gi.goods_picture_url,
        gi.goods_unit,
        gad.goods_attribute_id,
        gad.id attributeDetailsId,
        gad.attribute_details_name,
        gad.goods_stock,
        gad.goods_price,
        gad.attribute_piture_url,
        gad.integral_exchange_flag,
        gad.integral_exchange_sill,
        gad.integral_exchange_count
        from goods_info gi
        LEFT JOIN goods_attribute ga ON ga.goods_id = gi.id
        LEFT JOIN goods_attribute_details gad ON gad.goods_attribute_id = ga.id
        <where>
            <if test="integralExchangeFlag != null ">
                and gad.integral_exchange_flag = #{integralExchangeFlag}
            </if>
            <if test="whetherShelf != null ">
                and gi.whether_shelf = #{whetherShelf}
            </if>
            <if test="goodsPurpose != null and goodsPurpose != ''">
                AND gi.goods_purpose = #{goodsPurpose}
            </if>
        </where>
        ORDER BY gi.sort
    </select>
    <select id="selectCouponId" resultType="java.lang.Long">
        SELECT id
        from coupon
    </select>

    <select id="getPatientSignInInfo" resultType="com.xinelu.applet.vo.patientcenter.PatientSignInVO">
        SELECT pi.id                                                                patientId,
               pi.total_sign_in_days                                                totalSignInDays,
               pi.integral,
               (select count(1) from patient_sign_in_info where patient_id = pi.id) signInCount,
               (select count(1)
                from patient_sign_in_info
                where patient_id = pi.id
                  and sign_in_time = #{nowDate})                                    todaySignInCount
        FROM patient_info pi
        where pi.id = #{patientId}
    </select>

    <update id="updateTotalSignInDays" parameterType="long">
        update patient_info
        set total_sign_in_days = IFNULL(total_sign_in_days, 0) + 1,
            update_time        = now()
        where id = #{patientId}
          and del_flag = 0
    </update>

    <update id="updatePatientIntegral">
        update patient_info
        set integral    = IFNULL(integral, 0) + #{integral},
            update_time = now()
        where id = #{patientId}
          and del_flag = 0
    </update>

    <update id="clearTotalSignInDays">
        update patient_info
        set total_sign_in_days = 0,
            update_time        = now()
        where id = #{patientId}
          and del_flag = 0
    </update>

    <select id="selectPatientSignIn" resultType="com.xinelu.applet.vo.patientcenter.PatientSignInfoVO">
        SELECT
        id,
        settings_name,
        settings_content,
        settings_type,
        total_sign_in_days,
        integral_count
        FROM
        system_settings_info
        WHERE
        settings_type in
        <foreach item="rule" collection="ruleList" open="(" separator="," close=")">
            #{rule}
        </foreach>
    </select>

    <select id="getCouponNewPeople" resultType="com.xinelu.applet.vo.coupon.CouponVO">
        select id couponId,
        coupon_title,
        coupon_code,
        coupon_type,
        coupon_reduction_days,
        coupon_price,
        coupon_consume_price,
        coupon_description,
        receive_type,
        receive_count,
        receive_limit_count,
        use_scope,
        show_sort,
        "WAIT_RECEIVE" useStatus
        from coupon
        <where>
            <if test="receiveType != null  and receiveType != ''">
                and receive_type = #{receiveType}
            </if>
        </where>
        order by coupon_price asc, coupon_consume_price asc
    </select>

    <select id="getCouponByUseStatus" resultType="com.xinelu.applet.vo.coupon.CouponVO">
        select
        cp.use_scope,
        pcr.coupon_reduction_days,
        pcr.coupon_id couponId,
        pcr.coupon_price,
        pcr.coupon_consume_price,
        pcr.coupon_description,
        pcr.coupon_title,
        pcr.id patientCouponReceiveId,
        pcr.receive_source,
        pcr.expiration_start_time,
        pcr.expiration_end_time,
        pcr.receive_time,
        pcr.use_status
        from patient_coupon_receive pcr
        LEFT JOIN coupon cp ON cp.id = pcr.coupon_id
        <where>
            <if test="patientId != null ">
                and pcr.patient_id = #{patientId}
            </if>
            <if test="receiveType != null  and receiveType != ''">
                AND cp.receive_type = #{receiveType}
            </if>
            <if test="receiveSource != null and receiveSource != ''">
                AND pcr.receive_source = #{receiveSource}
            </if>
            <if test="useStatus != null and useStatus != ''">
                AND pcr.use_status = #{useStatus}
            </if>
        </where>
        order by pcr.coupon_price asc, pcr.coupon_consume_price asc
    </select>

    <select id="getPatientSignInfo" resultType="com.xinelu.applet.vo.patientcenter.PatientSignInfoVO">
        SELECT total_sign_in_days                patientSignInCount,
               integral,
               (SELECT count(1)
                FROM patient_sign_in_info
                WHERE patient_id = #{patientId}
                  AND sign_in_time = #{nowDate}) todaySignInCount
        FROM patient_info
        WHERE id = #{patientId}
          AND del_flag = 0
    </select>
</mapper>
