-- 일상기록 삭제 - 전체
delete from tbl_bf_daily_nutrition;
delete from tbl_bf_daily_nutrition_1;
delete from tbl_bf_daily_nutrition_2;
delete from tbl_bf_daily_nutrition_3;
delete from tbl_bf_daily_nutrition_4;
delete from tbl_bf_daily_nutrition_5;
delete from tbl_bf_daily_nutrition_6;
delete from tbl_bf_daily_nutrition_7;
delete from tbl_bf_daily_nutrition_8;
delete from tbl_bf_daily_nutrition_9;
delete from tbl_bf_daily_nutrition_10;


-- 동기화 관련
select * from tbl_bf_family where member_id = 'renamaestro';
select smc_no, member_id from tbl_bf_family_baby where member_id = 'renamaestro';
select * from tbl_bf_family_baby_disease where smc_no in(2,3);
select * from tbl_bf_family_baby_order where smc_no in(2,3);


-- 회원정보 삭제
truncate table tbl_bf_family_baby_disease;
truncate table tbl_bf_family_disease;
truncate table tbl_member_confirm;
truncate tbl_bf_family_match;
delete from tbl_bf_family_baby;
delete from tbl_bf_family;
delete from tbl_member;


-- 메모리 - 일간분석
SELECT
    DATE_FORMAT(a.reg_dt, '%H') as hour,
    count(*) as cnt
FROM tbl_bf_family_match a
INNER JOIN tbl_bf_daily_nutrition b on a.smc_no = b.smc_no
INNER JOIN tbl_bf_family_match c on a.smc_no = c.smc_no
WHERE
    a.member_id = '1642861413918'
    and DATE_FORMAT(b.reg_dt, '%Y-%m-%d') = DATE_FORMAT(now(), '%Y-%m-%d')
GROUP BY DATE_FORMAT(b.reg_dt, '%H');


-- 메모리 - 주간분석
SELECT
    DATE_FORMAT(b.reg_dt, '%Y-%m-%d %H') as day,
    count(*) as cnt
FROM tbl_bf_family_match a
INNER JOIN tbl_bf_daily_nutrition b on a.smc_no = b.smc_no
INNER JOIN tbl_bf_family_match c on a.smc_no = c.smc_no
WHERE
    a.member_id = '1642861413918'
  and b.reg_dt between STR_TO_DATE('2022-02-15 00:00:00','%Y-%m-%d %H:%i:%s') and STR_TO_DATE('2022-02-15 23:59:59','%Y-%m-%d %H:%i:%s')
group by DATE_FORMAT(b.reg_dt, '%Y-%m-%d %H')
order by DATE_FORMAT(b.reg_dt, '%Y-%m-%d %H') asc;


-- 메모리 - 타임라인(모유, 각 카테고리별 수치 및 시간간격 계산해야됨.)
SELECT
    DATE_FORMAT(b.reg_dt, '%Y-%m-%d %H:%i:%s')                  as reg_date,
    TIMEDIFF(b.reg_dt, LAG(b.reg_dt) OVER (ORDER BY b.reg_dt) ) AS time_diff,
    d.title,
    b.dn_no,
    d.cd_no,
    CASE
        when d.cd_no = 'BF_NU_TP_1' THEN
            (select ifnull(emo_pre - emo_post,0)
             from tbl_bf_daily_nutrition_1 tn1
             where tn1.dn_no = b.dn_no
            ) -- 모유
        when d.cd_no = 'BF_NU_TP_2' THEN
            (select eat_qty
             from tbl_bf_daily_nutrition_2 tn1
             where tn1.dn_no = b.dn_no
            ) -- 분유(젖병)
        when d.cd_no = 'BF_NU_TP_3' THEN
            (select
                 concat(ifnull(tn.prd_nm, ''),' ', tn1.weight, ' 섭취')
             from tbl_bf_daily_nutrition_3 tn1
                      left outer join tbl_db_nutrient tn on tn1.db_dn_no = tn.dn_no
             where tn1.dn_no = b.dn_no
            ) -- 간식
        when d.cd_no = 'BF_NU_TP_5' THEN
            (select concat( (sleep_h - sleep_s_h), '시 ', (sleep_s_m - sleep_m), '분')
             from tbl_bf_daily_nutrition_5 tn1
             where tn1.dn_no = b.dn_no
            ) -- 수면시간
        when d.cd_no = 'BF_NU_TP_6' THEN
            (select tn2.title
             from tbl_bf_daily_nutrition_6 tn1
                      inner join tbl_code_item_str tn2 on tn2.cg_cd = 'BF_FECES_QTY' and tn1.rest_qty = tn2.val_1
             where tn1.dn_no = b.dn_no
            ) -- 대소변
        when d.cd_no = 'BF_NU_TP_7' THEN
            (select
                 tv.dv_disease
             from tbl_bf_daily_nutrition_7 tn1
                      inner join tbl_db_vac tv on tn1.db_dvs_no = tv.dv_no
             where tn1.dn_no = b.dn_no
            ) -- 예방접종
        when d.cd_no = 'BF_NU_TP_9' THEN
            (select
                 concat(ifnull(tb.name, ''),' ', tn1.weight, ' 섭취')
             from tbl_bf_daily_nutrition_9 tn1
                      left outer join tbl_db_home_bf tb on tn1.db_dhb_no = tb.dhb_no
             where tn1.dn_no = b.dn_no
            ) -- 홈메이드 이유식
        when d.cd_no = 'BF_NU_TP_10' THEN
            (select
                 concat(ifnull(tc.prd_nm, ''),' ', tn1.bm, '시간 섭취')
             from tbl_bf_daily_nutrition_10 tn1
                      left outer join tbl_db_bf_commerce tc on tc.dbc_no = tn1.db_dbc_no
             where tn1.dn_no = b.dn_no
            ) -- 시판이유식
        when d.cd_no = 'BF_NU_TP_11' THEN
            (select
                 hosp_nm
             from tbl_bf_daily_nutrition_11 tn1
             where tn1.dn_no = b.dn_no
            ) -- 병원
        when d.cd_no = 'BF_NU_TP_12' THEN
            (select
                 daily_life
             from tbl_bf_daily_nutrition_12 tn1
             where tn1.dn_no = b.dn_no
            ) -- 일상생활
        when d.cd_no = 'BF_NU_TP_13' THEN
            (select
                 achievement
             from tbl_bf_daily_nutrition_13 tn1
             where tn1.dn_no = b.dn_no
            ) -- 발달과정 성취
        when d.cd_no = 'BF_NU_TP_14' THEN
            (select
                 ifnull(blq + brq,0)
             from tbl_bf_daily_nutrition_14 tn1
             where tn1.dn_no = b.dn_no
            ) -- 유축
        when d.cd_no = 'BF_NU_TP_15' THEN
            (select
                 temperature
             from tbl_bf_daily_nutrition_15 tn1
             where tn1.dn_no = b.dn_no
            ) -- 체온
        END AS time_val
FROM tbl_bf_family_match a
         INNER JOIN tbl_bf_daily_nutrition b on a.smc_no = b.smc_no
         INNER JOIN tbl_bf_family_match c on a.smc_no = c.smc_no
         INNER JOIN tbl_code_item_str d on b.nutirition_cd_no = d.cd_no
WHERE
        a.member_id = '1645342677758'
  and DATE_FORMAT(b.reg_dt,'%Y-%m-%d') = STR_TO_DATE('2022-02-20','%Y-%m-%d');
