#!/bin/bash
source $HOME/.gamerc

[ "$1" == "" ] && exec echo "Usage: $0 <Environment (TEST => 0, LIVE => 1)>"

env=$1
output_filename_1="/tmp/itemmall_iap_name_platform_monitor.log"
correct_filename_2="/tmp/itemmall_item_price_iap_name_mapping_correct_monitor.log"
output_filename_2="/tmp/itemmall_item_price_iap_name_monitor.log"

COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT iap_name_android, iap_name_ios, iap_name_xpay_h5, substring(iap_name_android from '([0-9]+_[0-9]+)$') AS android, substring(iap_name_ios from '([0-9]+_[0-9]+)$') AS ios, substring(iap_name_xpay_h5 from '([0-9]+_[0-9]+)$') AS xpay_h5 FROM itemmall_item WHERE (iap_name_android <> '' OR iap_name_ios <> '' OR iap_name_xpay_h5 <> '') AND (substring(iap_name_android from '([0-9]+_[0-9]+)$') IS DISTINCT FROM substring(iap_name_ios from '([0-9]+_[0-9]+)$') OR substring(iap_name_ios from '([0-9]+_[0-9]+)$') IS DISTINCT FROM substring(iap_name_xpay_h5 from '([0-9]+_[0-9]+)$'))) SELECT COUNT(*) FROM t1) TO STDOUT;")

if [ "${COUNT}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (SELECT iap_name_android, iap_name_ios, iap_name_xpay_h5, substring(iap_name_android from '([0-9]+_[0-9]+)$') AS android, substring(iap_name_ios from '([0-9]+_[0-9]+)$') AS ios, substring(iap_name_xpay_h5 from '([0-9]+_[0-9]+)$') AS xpay_h5 FROM itemmall_item WHERE (iap_name_android <> '' OR iap_name_ios <> '' OR iap_name_xpay_h5 <> '') AND (substring(iap_name_android from '([0-9]+_[0-9]+)$') IS DISTINCT FROM substring(iap_name_ios from '([0-9]+_[0-9]+)$') OR substring(iap_name_ios from '([0-9]+_[0-9]+)$') IS DISTINCT FROM substring(iap_name_xpay_h5 from '([0-9]+_[0-9]+)$'))) TO '${output_filename_1}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR itemmall_iap_name_platform "${ENV_TAG} 上架表 IAP_NAME 跟平台對應有誤, 請確認!"
fi

psql -U postgres ${ACCOUNT_DB_NAME} -c "DROP TABLE IF EXISTS xl_iap_name_price_mapping; CREATE TABLE xl_iap_name_price_mapping AS (WITH t1 AS (SELECT id, iap_name_android, (string_to_array(iap_name_android, '_'))[array_length(string_to_array(iap_name_android, '_'), 1)] AS last_value, price FROM itemmall_item WHERE iap_name_android <> '' ORDER BY id), t2 AS (SELECT last_value, price, COUNT(*) FROM t1 GROUP BY last_value, price ORDER BY last_value::INTEGER, count DESC) SELECT DISTINCT ON (last_value::INTEGER) last_value, price FROM t2 ORDER BY last_value::INTEGER, count DESC);"

COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, iap_name_android, (string_to_array(iap_name_android, '_'))[array_length(string_to_array(iap_name_android, '_'), 1)] AS last_value, price FROM itemmall_item WHERE iap_name_android <> '' ORDER BY id) SELECT COUNT(*) FROM t1 WHERE (last_value, price) NOT IN (SELECT last_value, price FROM xl_iap_name_price_mapping)) TO STDOUT;")

if [ "${COUNT}" -ne 0 ]; then
    CORRECT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (SELECT * FROM xl_iap_name_price_mapping ORDER BY last_value::INTEGER) TO '${correct_filename_2}' CSV HEADER")
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, iap_name_android, iap_name_ios, iap_name_xpay_h5, iap_name_other, (string_to_array(iap_name_android, '_'))[array_length(string_to_array(iap_name_android, '_'), 1)] AS last_value, price FROM itemmall_item WHERE iap_name_android <> '' ORDER BY id) SELECT * FROM t1 WHERE (last_value, price) NOT IN (SELECT last_value, price FROM xl_iap_name_price_mapping) ORDER BY last_value::INTEGER, price) TO '${output_filename_2}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages REFERENCE itemmall_item_price_iap_name_mapping_correct "${ENV_TAG} 上架表 IAP_NAME 跟價錢對應, 正確版本如下"
    send_messages ERROR itemmall_item_price_iap_name "${ENV_TAG} 上架表 IAP_NAME 跟價錢對應有誤, 請確認!"
fi
