<?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.stdiet.custom.mapper.SysWxUserLogMapper">

    <resultMap type="SysWxUserLog" id="SysWxUserLogResult">
        <result property="id"    column="id"    />
        <result property="openid" column="openid"/>
        <result property="weight" column="weight"/>
        <result property="appid" column="appid"/>
        <result property="phone" column="phone"/>
        <result property="phone" column="phone"/>
        <result property="logTime" column="log_time"/>
        <result property="wakeupTime" column="wakeup_time"/>
        <result property="sleepTime" column="sleep_time"/>
        <result property="sport" column="sport"/>
        <result property="avatarUrl" column="avatar_url"/>
        <result property="diet" column="diet"/>
        <result property="insomnia" column="insomnia"/>
        <result property="defecation" column="defecation"/>
        <result property="water" column="water"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="emotion"    column="emotion"    />
        <result property="slyEatFood"    column="sly_eat_food"    />
        <result property="constipation"    column="constipation"    />
        <result property="breakfastImages"    column="breakfast_images"    />
        <result property="lunchImages"    column="lunch_images"    />
        <result property="dinnerImages"    column="dinner_images"    />
        <result property="extraMealImages"    column="extra_meal_images"    />
        <result property="bodyImages"    column="body_images"    />
        <result property="suggest"    column="suggest"    />
        <result property="targetWeight"    column="target_weight"    />
        <result property="executionScore"    column="execution_score"    />
        <result property="comment"    column="comment"    />
        <result property="delFlag"    column="del_flag"    />
        <!-- 非持久字段 -->
        <result property="customerName" column="customer_name"></result>
        <!-- 营养师 -->
        <result property="nutritionist" column="nutritionist"></result>
        <result property="afterNutritionist" column="after_nutritionist"></result>
    </resultMap>

    <resultMap type="com.stdiet.custom.page.WxLogInfo" id="WxLogInfo">
        <result property="weight" column="weight"/>
        <result property="sleepTime" column="sleep_time"/>
        <result property="wakeupTime" column="wakeup_time"/>
        <result property="sport" column="sport"/>
        <result property="diet" column="diet"/>
        <result property="insomnia" column="insomnia"/>
        <result property="defecation" column="defecation"/>
        <result property="water" column="water"/>
        <result property="date" column="log_time"/>
        <result property="emotion"    column="emotion"    />
        <result property="slyEatFood"    column="sly_eat_food"    />
        <result property="constipation"    column="constipation"    />
        <result property="breakfastImages"    column="breakfast_images"    />
        <result property="lunchImages"    column="lunch_images"    />
        <result property="dinnerImages"    column="dinner_images"    />
        <result property="extraMealImages"    column="extra_meal_images"    />
        <result property="bodyImages"    column="body_images"    />
        <result property="suggest"    column="suggest"    />
        <result property="targetWeight"    column="target_weight"    />
        <result property="executionScore"    column="execution_score"    />
        <result property="comment"    column="comment"    />
    </resultMap>

    <sql id="selectSysWxUserLogVo">
        select id,openid, weight, appid, phone, log_time, sleep_time, wakeup_time, sport, avatar_url, diet, insomnia, defecation, water, create_by, create_time, update_by, update_time, remark,
         emotion,sly_eat_food,constipation,breakfast_images,lunch_images,dinner_images,extra_meal_images,body_images,suggest,execution_score,comment
         from sys_wx_user_log
    </sql>

    <select id="checkWxLogInfoCount" parameterType="String" resultType="Integer">
        select count(*) from sys_wx_user_log where to_days(log_time) = to_days(now()) and openid = #{openid} and del_flag = 0
    </select>

    <!-- 后台查询 -->
    <select id="selectSysWxUserLogList" parameterType="SysWxUserLog" resultMap="SysWxUserLogResult">
        SELECT wxlog.id,wxinfo.appid,wxinfo.openid,wxinfo.avatar_url,wxinfo.phone,wxlog.weight,wxlog.log_time,wxlog.sleep_time, wxlog.wakeup_time,wxlog.defecation, wxlog.water, wxlog.insomnia,wxlog.sport,wxlog.diet,wxlog.remark,
        wxlog.emotion,wxlog.sly_eat_food,wxlog.constipation,wxlog.breakfast_images,wxlog.lunch_images,wxlog.dinner_images,wxlog.extra_meal_images,wxlog.body_images,
        wxlog.suggest,wxlog.execution_score,wxlog.comment,sc.name as customer_name, su.nick_name as nutritionist, su_atferSale.nick_name as after_nutritionist
        FROM sys_wx_user_log wxlog
        left join sys_wx_user_info wxinfo on wxinfo.openid  = wxlog.openid
        left join sys_customer sc on sc.phone = wxinfo.phone and sc.del_flag = 0
        left join sys_user su on su.user_id = sc.main_dietitian and su.del_flag = '0'
        left join sys_user su_atferSale on su_atferSale.user_id = sc.after_dietitian and su_atferSale.del_flag = '0'
        where wxlog.del_flag = 0 and wxinfo.phone is not null
        <if test="id != null">
            and wxlog.id = #{id}
        </if>
        <if test="customerId != null">
            and sc.id = #{customerId}
        </if>
        <if test="phone != null and phone != ''">and (sc.name like concat('%',#{phone},'%') or wxinfo.phone like  concat('%',#{phone},'%') )</if>
        <if test="appid != null">
            and wxinfo.appid = #{appid}
        </if>
        <if test="nutritionistId != null">
            and su.user_id = #{nutritionistId}
        </if>
        <if test="afterNutritionistId != null">
            and su_atferSale.user_id = #{afterNutritionistId}
        </if>
        <if test="beginTime != null and beginTime != ''">and date_format(wxlog.log_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
        <if test="endTime != null and endTime != ''">and date_format(wxlog.log_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        order by wxlog.log_time desc
    </select>

    <select id="selectWxLogInfoList" parameterType="SysWxUserLog" resultMap="WxLogInfo">
        SELECT * FROM sys_wx_user_log log where log.del_flag = 0
        <choose>
            <when test="phone == null or phone == ''">
                (SELECT phone FROM sys_wx_user_info WHERE openid = #{openid}) = log.phone
            </when>
            <otherwise>
                <if test="openid != null  and openid != ''">and openid = #{openid}</if>
                <if test="phone != null  and phone != ''">or phone = #{phone}</if>
            </otherwise>
        </choose>
        order by log_time asc
    </select>

    <select id="selectSysWxUserLogById" parameterType="String" resultMap="SysWxUserLogResult">
        <include refid="selectSysWxUserLogVo"/>
        where id = #{id} and del_flag = 0
    </select>

    <insert id="insertSysWxUserLog" parameterType="SysWxUserLog">
        insert into sys_wx_user_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="openid != null">openid,</if>
            <if test="weight != null">weight,</if>
            <if test="appid != null">appid,</if>
            <if test="phone != null">phone,</if>
            <if test="logTime != null">log_time,</if>
            <if test="sleepTime != null">sleep_time,</if>
            <if test="wakeupTime != null">wakeup_time,</if>
            <if test="sport != null">sport,</if>
            <if test="avatarUrl != null">avatar_url,</if>
            <if test="diet != null">diet,</if>
            <if test="insomnia != null">insomnia,</if>
            <if test="defecation != null">defecation,</if>
            <if test="water != null">water,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="emotion != null">emotion,</if>
            <if test="slyEatFood != null">sly_eat_food,</if>
            <if test="constipation != null">constipation,</if>
            <if test="breakfastImages != null">breakfast_images,</if>
            <if test="lunchImages != null">lunch_images,</if>
            <if test="dinnerImages != null">dinner_images,</if>
            <if test="extraMealImages != null">extra_meal_images,</if>
            <if test="bodyImages != null">body_images,</if>
            <if test="suggest != null">suggest,</if>
            <if test="targetWeight != null">target_weight,</if>
            <if test="executionScore != null">execution_score,</if>
            <if test="comment != null">comment,</if>
            <if test="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="openid != null">#{openid},</if>
            <if test="weight != null">#{weight},</if>
            <if test="appid != null">#{appid},</if>
            <if test="phone != null">#{phone},</if>
            <if test="logTime != null">#{logTime},</if>
            <if test="sleepTime != null">#{sleepTime},</if>
            <if test="wakeupTime != null">#{wakeupTime},</if>
            <if test="sport != null">#{sport},</if>
            <if test="avatarUrl != null">#{avatarUrl},</if>
            <if test="diet != null">#{diet},</if>
            <if test="insomnia != null">#{insomnia},</if>
            <if test="defecation != null">#{defecation},</if>
            <if test="water != null">#{water},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="emotion != null">#{emotion},</if>
            <if test="slyEatFood != null">#{slyEatFood},</if>
            <if test="constipation != null">#{constipation},</if>
            <if test="breakfastImages != null">#{breakfastImages},</if>
            <if test="lunchImages != null">#{lunchImages},</if>
            <if test="dinnerImages != null">#{dinnerImages},</if>
            <if test="extraMealImages != null">#{extraMealImages},</if>
            <if test="bodyImages != null">#{bodyImages},</if>
            <if test="suggest != null">suggest = #{suggest},</if>
            <if test="targetWeight != null">target_weight = #{targetWeight},</if>
            <if test="executionScore != null">execution_score = #{executionScore},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
    </insert>

    <update id="updateSysWxUserLog" parameterType="SysWxUserLog">
        update sys_wx_user_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="openid != null">openid = #{openid},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="appid != null">appid = #{appid},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="logTime != null">log_time = #{logTime},</if>
            <if test="sleepTime != null">sleep_time = #{sleepTime},</if>
            <if test="wakeupTime != null">wakeup_time = #{wakeupTime},</if>
            <if test="sport != null">sport = #{sport},</if>
            <if test="avatarUrl != null">avatar_url = #{avatarUrl},</if>
            <if test="diet != null">diet = #{diet},</if>
            <if test="insomnia != null">insomnia = #{insomnia},</if>
            <if test="defecation != null">defecation = #{defecation},</if>
            <if test="water != null">water = #{water},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="emotion != null">emotion = #{emotion},</if>
            <if test="slyEatFood != null">sly_eat_food = #{slyEatFood},</if>
            <if test="constipation != null">constipation = #{constipation},</if>
            <if test="breakfastImages != null">breakfast_images = #{breakfastImages},</if>
            <if test="lunchImages != null">lunch_images = #{lunchImages},</if>
            <if test="dinnerImages != null">dinner_images = #{dinnerImages},</if>
            <if test="extraMealImages != null">extra_meal_images = #{extraMealImages},</if>
            <if test="bodyImages != null">body_images = #{bodyImages},</if>
            <if test="suggest != null">suggest = #{suggest},</if>
            <if test="targetWeight != null">target_weight = #{targetWeight},</if>
            <if test="executionScore != null">execution_score = #{executionScore},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteSysWxUserLogById" parameterType="Long">
        update sys_wx_user_log set del_flag = 1 where id = #{id}
    </update>

    <update id="deleteSysWxUserLogByIds" parameterType="String">
        update sys_wx_user_log set del_flag = 1  where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <!-- 根据openid和手机号查询对应打卡记录 -->
    <select id="getWxLogInfoList" parameterType="SysWxUserLog" resultMap="WxLogInfo">
        SELECT wxlog.weight,wxlog.log_time,wxlog.sleep_time, wxlog.wakeup_time,wxlog.defecation, wxlog.water, wxlog.insomnia,wxlog.sport,wxlog.diet,
        wxlog.emotion,wxlog.sly_eat_food,wxlog.constipation,wxlog.breakfast_images,wxlog.lunch_images,wxlog.dinner_images,wxlog.extra_meal_images,wxlog.body_images
        ,wxlog.remark,wxlog.execution_score,wxlog.comment
        FROM sys_wx_user_log wxlog left join sys_wx_user_info wxinfo on wxinfo.openid  = wxlog.openid
        where wxlog.del_flag = 0 and (wxinfo.openid = #{openid} or wxinfo.phone = #{phone})
        order by wxlog.log_time desc
    </select>

    <select id="selectSysWxUserLogByDateAndOpenId"  parameterType="SysWxUserLog" resultMap="SysWxUserLogResult">
        select id from sys_wx_user_log where del_flag = 0 and to_days(log_time) = to_days(#{logTime}) and openid = #{openid} limit 1
    </select>

    <!-- 根据手机号和openid查询打卡连续天数,只查询前两条 -->
    <select id="getContinuity" parameterType="SysWxUserLog" resultType="Map">
        SELECT yearMonth,MIN(log_time) AS minLogTime,MAX(log_time) AS maxLogTime,COUNT(*) AS continuityDayCount FROM
          (
            SELECT log_time,yearMonth,(days-date_rank) AS day_cha FROM
            (
                SELECT *,row_number() over(PARTITION BY yearMonth ORDER BY log_time) date_rank  FROM
                (
                    SELECT log_time,CONCAT(YEAR(log_time),'-',MONTH(log_time)) AS yearMonth,DAY(log_time) AS days
                    FROM sys_wx_user_log wxlog left join sys_wx_user_info wxinfo on wxinfo.openid  = wxlog.openid
                    where wxlog.del_flag = 0 and wxinfo.openid = #{openid} or wxinfo.phone = #{phone}
                ) AS s ORDER BY s.log_time DESC
            ) ss
        ) sss GROUP BY yearMonth,day_cha LIMIT 2
    </select>

    <!-- 后台根据用户ID查询该用户的打卡体重 -->
    <select id="getWxUserLogListByCustomerId" parameterType="SysWxUserLog" resultMap="SysWxUserLogResult">
        SELECT wxlog.id,wxlog.log_time,wxlog.weight FROM sys_wx_user_log wxlog
        left join sys_wx_user_info wxinfo on wxinfo.openid  = wxlog.openid
        left join sys_customer sc on sc.phone = wxinfo.phone and sc.del_flag = 0
        where wxlog.del_flag = 0 and wxinfo.phone is not null and sc.id = #{customerId}
        <if test="beginTime != null and beginTime != ''">and date_format(wxlog.log_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
        <if test="endTime != null and endTime != ''">and date_format(wxlog.log_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        order by wxlog.log_time asc
    </select>

    <!-- 根据openid和手机号查询对应打卡记录 -->
    <select id="getWxLogInfoDetailById" parameterType="SysWxUserLog" resultMap="WxLogInfo">
        SELECT wxlog.weight,wxlog.log_time,wxlog.sleep_time, wxlog.wakeup_time,wxlog.defecation, wxlog.water, wxlog.insomnia,wxlog.sport,wxlog.diet,
        wxlog.emotion,wxlog.sly_eat_food,wxlog.constipation,wxlog.breakfast_images,wxlog.lunch_images,wxlog.dinner_images,wxlog.extra_meal_images,wxlog.body_images
        ,wxlog.remark,wxlog.execution_score,wxlog.comment
        FROM sys_wx_user_log wxlog left join sys_wx_user_info wxinfo on wxinfo.openid  = wxlog.openid
        where wxlog.del_flag = 0 and wxlog.id = #{id}
        order by wxlog.log_time desc
    </select>

</mapper>