Advertisement
aadddrr

th-sdp-script-sync-product-catalog_20180629_2

Jun 29th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.24 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. dbhost=192.168.0.100
  4. dbport=5434
  5. dbname=erp_paloma_20180514
  6. dbuser="sts"
  7. dbpass="Awesome123!"
  8. fileNamePrefix="product_"
  9. fileNameSuffix=".csv"
  10. datetime=`date +%Y%m%d%H%M%S`
  11. targetDir="/home/sts/sdp/th-sdp-intgr/productcatalog/sync"
  12. catalogCode="$1"
  13.  
  14. if [ -z "$catalogCode" ]; then
  15.    echo "Please specify catalog code"
  16.    exit 0
  17. fi
  18.  
  19. #TRANSFER DATA CALCULATOR
  20.  
  21. queryTransferCalculatorCatalogPrice=`PGPASSWORD=$dbpass psql -h $dbhost -p $dbport -U $dbuser -d $dbname -c "SELECT f_transfer_calculator_catalog_price( 11, -1, '$datetime', '$catalogCode' )"`
  22.  
  23.  
  24. #GENERATE CSV
  25.  
  26. tempFile=`mktemp`
  27. targetFile="$targetDir/$fileNamePrefix$datetime$fileNameSuffix"
  28.    
  29. PGPASSWORD=$dbpass psql -h $dbhost -p $dbport -U $dbuser -d $dbname -c "Copy (SELECT A.product_code AS product_code, A.product_name, A.ctgr_product_code AS ctgr_code, A.sub_ctgr_product_code AS sub_ctgr_code, A.barcode AS product_barcode, COALESCE(A.weight,0) AS weight, A.dimension_length AS length, A.dimension_width AS width, A.dimension_height AS height, A.available_stock AS available_stock, A.style_product, A.size, A.color, A.class_product, A.category_name_lv0, A.category_name_lv1, A.category_name_lv2, A.category_name_lv3, A.brand_name, REGEXP_REPLACE(ENCODE(CONVERT_TO(A.product_description, 'UTF-8'), 'base64'), E'[\\n\\r]+', '', 'g' ) AS product_description, A.online_price, A.online_sale_prices FROM vw_product_catalog_for_sdp A WHERE A.catalog_code = '$catalogCode' AND (NOT EXISTS( SELECT 1 FROM sdp_product_sync Z WHERE A.product_id = Z.product_id ) OR NOT EXISTS ( SELECT 1 FROM sdp_product_sync Z WHERE A.product_id = Z.product_id AND A.online_price = Z.online_price AND A.online_sale_prices = Z.online_sale_prices) )) To STDOUT With CSV HEADER DELIMITER E'\t' QUOTE '\"' NULL '';" > $tempFile && \
  30.    
  31. mkdir -p "$targetDir"
  32. cp "$tempFile" "$targetFile"
  33. rm -f "$tempFile"
  34.  
  35. csvRowNumberInclHeader=$( cat "$targetFile" | wc -l )
  36. csvRowNumber=$(($csvRowNumberInclHeader-1))
  37.  
  38.  
  39. BASE_DIR="/home/sts/sdp/th-sdp-intgr"
  40. SOURCE_SYNC_DIR="$BASE_DIR/productcatalog/sync"
  41. SOURCE_DONE_DIR="$BASE_DIR/productcatalog/done"
  42.  
  43. TARGET_DIR="/home/sts/sdp/th-sdp-intgr/product/input"
  44. TARGET_HOST="192.168.0.100"
  45. TARGET_PORT="10061"
  46. TARGET_PEM="/home/sts/.ssh/character_land.pem"
  47. TARGET_USERNAME="ubuntu"
  48. TARGET_FILE_OWN="sts:sts"
  49. TARGET_TEMP_DIR="/tmp"
  50.  
  51. FILE=$fileNamePrefix$datetime$fileNameSuffix
  52.  
  53.  
  54. if (( $csvRowNumber > 0 )); then
  55.     echo "$csvRowNumber product data will be processed"
  56.  
  57.     #UPDATE sdp_product_sync
  58.  
  59.     queryUpdateSdpProductSync=`PGPASSWORD=$dbpass psql -h $dbhost -p $dbport -U $dbuser -d $dbname -c "WITH tt_unsynced_product_catalog_for_sdp AS ( SELECT A.product_id, A.tenant_id, A.product_code, A.product_name, A.barcode, A.ctgr_product_code, A.sub_ctgr_product_code, A.weight, A.dimension_length, A.dimension_width, A.dimension_height, A.available_stock, A.style_product, A.size, A.color, A.class_product, A.category_name_lv0, A.category_name_lv1, A.category_name_lv2, A.category_name_lv3, A.brand_name,  A.product_description, A.online_price, A.online_sale_prices FROM vw_product_catalog_for_sdp A WHERE A.catalog_code = '$catalogCode' AND ( NOT EXISTS( SELECT 1 FROM sdp_product_sync Z WHERE A.product_id = Z.product_id ) OR NOT EXISTS ( SELECT 1 FROM sdp_product_sync Z WHERE A.product_id = Z.product_id AND A.online_price = Z.online_price AND A.online_sale_prices = Z.online_sale_prices ) ) ), updated_product_sync AS ( UPDATE sdp_product_sync Z SET online_price = A.online_price, online_sale_prices = A.online_sale_prices, update_datetime = '$datetime', update_user_id = -1, version = Z.version+1 FROM tt_unsynced_product_catalog_for_sdp A WHERE Z.product_id = A.product_id ) INSERT INTO sdp_product_sync( product_id, tenant_id, product_code, product_name, product_barcode, ctgr_product_code, sub_ctgr_product_code, weight, length, width, height, available_stock, style_product, size, color, class_product, category_name_lv0, category_name_lv1, category_name_lv2, category_name_lv3, brand_name, product_description, online_price, online_sale_prices, create_datetime, update_datetime, create_user_id, update_user_id, version) SELECT A.product_id, A.tenant_id, A.product_code, A.product_name, A.barcode, A.ctgr_product_code, A.sub_ctgr_product_code, A.weight, A.dimension_length, A.dimension_width, A.dimension_height, A.available_stock, A.style_product, A.size, A.color, A.class_product, A.category_name_lv0, A.category_name_lv1, A.category_name_lv2, A.category_name_lv3, A.brand_name,  A.product_description, A.online_price, A.online_sale_prices, '$datetime' AS create_datetime, '$datetime' AS update_datetime, -1 AS create_user_id, -1 AS update_user_id, 0 AS version FROM tt_unsynced_product_catalog_for_sdp A WHERE NOT EXISTS ( SELECT 1 FROM sdp_product_sync B WHERE A.product_id = B.product_id )"`
  60.  
  61.     #UPLOAD
  62.  
  63.     echo "================================== "
  64.     echo "* SDP Processed * "
  65.     echo "* Start : "$(date +"%m-%d-%Y %T")
  66.     cd $BASE_DIR
  67.  
  68.     echo ">> Retrieved  $SOURCE_SYNC_DIR/$FILE"
  69.     #MOVED_FILE=$(scp -P $TARGET_PORT -i "$TARGET_PEM" "$SOURCE_SYNC_DIR/$FILE" "$TARGET_USERNAME@$TARGET_HOST:$TARGET_TEMP_DIR" && echo "PROCESS_DONE")
  70.     MOVED_FILE=$(scp -P $TARGET_PORT "$SOURCE_SYNC_DIR/$FILE" "$TARGET_USERNAME@$TARGET_HOST:$TARGET_TEMP_DIR" && echo "PROCESS_DONE")
  71.    
  72.     if [ $MOVED_FILE="PROCESS_DONE" ]; then
  73.     #MOVE_FROM_TEMP=$(ssh -p $TARGET_PORT -i "$TARGET_PEM" "$TARGET_USERNAME@$TARGET_HOST" "sudo chmod 666 \"$TARGET_TEMP_DIR/$FILE\" && sudo chown $TARGET_FILE_OWN \"$TARGET_TEMP_DIR/$FILE\" && sudo mv \"$TARGET_TEMP_DIR/$FILE\" \"$TARGET_DIR/\" && echo \"PROCESS_DONE\"")
  74.     MOVE_FROM_TEMP=$(ssh -p $TARGET_PORT "$TARGET_USERNAME@$TARGET_HOST" "chmod 666 \"$TARGET_TEMP_DIR/$FILE\" &&  chown $TARGET_FILE_OWN \"$TARGET_TEMP_DIR/$FILE\" && mv \"$TARGET_TEMP_DIR/$FILE\" \"$TARGET_DIR/\" && echo \"PROCESS_DONE\"")
  75.     if [ $MOVE_FROM_TEMP="PROCESS_DONE" ]; then
  76.         echo ">> Moving server file to success $FILE"  
  77.         mv $SOURCE_SYNC_DIR/$FILE $SOURCE_DONE_DIR/
  78.     else
  79.         echo ">> Moving file $FILE from temp to failed"    
  80.     fi
  81.     else
  82.     echo ">> Moving server file to failed $FILE"    
  83.     fi
  84.     echo "* End Processed : "$(date +"%m-%d-%Y %T")
  85.  
  86. else
  87.  
  88.     echo "No data to be processed"
  89.     echo ">> Moving file $FILE"    
  90.     mv $SOURCE_SYNC_DIR/$FILE $SOURCE_DONE_DIR/    
  91.  
  92. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement