-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -- CORPORATE->TBL_CORPORATE_CUSTOM_SELECT_DATA TBL_CORPORATE_CUSTOM_SELECT_DATA TBL_CORPORATE_CUSTOM_SELECT_DATA TBL_CORPORATE_CUSTOM_SELECT_DATA TBL_CORPORATE_CUSTOM_SELECT_DATA -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ------------------------------------------------------------------------------------------ CREATE OR REPLACE FUNCTION fn_get_all_corporate_custom_select_data() RETURNS SETOF corporate.tbl_corporate_custom_select_data AS $$ DECLARE BEGIN RETURN QUERY SELECT * FROM corporate.tbl_corporate_custom_select_data WHERE int_sys_action_id != -1; END; $$ LANGUAGE plpgsql; ------------------------------------------------------------------------------------------ CREATE OR REPLACE FUNCTION fn_get_all_corporate_custom_select_data_with_action_user() RETURNS TABLE (pk_corporate_custom_select_data_id BIGINT, fk_corporate_custom_data_id BIGINT, fk_customer_id BIGINT, art_select_items TEXT[], int_sys_action_id INT, fk_created_user_id BIGINT, vhr_created_user VARCHAR, dtm_created TIMESTAMP WITH TIME ZONE, fk_last_modified_user_id BIGINT, vhr_last_modified_user VARCHAR, dtm_last_modified TIMESTAMP WITH TIME ZONE, fk_deleted_user_id BIGINT, vhr_deleted_user VARCHAR, dtm_deleted TIMESTAMP WITH TIME ZONE) AS $$ DECLARE BEGIN RETURN QUERY SELECT mt.pk_corporate_custom_select_data_id, mt.fk_corporate_custom_data_id, mt.fk_customer_id, mt.art_select_items, mt.int_sys_action_id, mt.fk_created_user_id, u1.vhr_login_name AS vhr_created_user, mt.dtm_created, mt.fk_last_modified_user_id, u2.vhr_login_name AS vhr_last_modified_user, mt.dtm_last_modified, mt.fk_deleted_user_id, u3.vhr_login_name AS vhr_deleted_user, mt.dtm_deleted FROM corporate.tbl_corporate_custom_select_data mt LEFT JOIN authentication.tbl_user u1 ON mt.fk_created_user_id = u1.pk_user_id LEFT JOIN authentication.tbl_user u2 ON mt.fk_last_modified_user_id = u2.pk_user_id LEFT JOIN authentication.tbl_user u3 ON mt.fk_deleted_user_id = u3.pk_user_id WHERE mt.int_sys_action_id != -1 ORDER BY ; END; $$ LANGUAGE plpgsql; ------------------------------------------------------------------------------------------ CREATE OR REPLACE PROCEDURE sp_insert_or_update_corporate_custom_select_data(jsnCorporateCustomSelectData JSONB, jsnSqlRes OUT JSON) AS $$ DECLARE objSpOutRes TYP_SP_OUT_RES; objFnCheckRes TYP_FN_CHECK_RES; dtmCurrent TIMESTAMP WITH TIME ZONE; BEGIN SELECT NOW() INTO dtmCurrent; -- // Duplication Checking in DB FUCTION(Insert and Update) SELECT * FROM fn_check_data_uniqueness('corporate.tbl_corporate_custom_select_data'::VARCHAR, ''::VARCHAR, ::VARCHAR, 'pk_corporate_custom_select_data_id'::VARCHAR, CAST(jsnCorporateCustomSelectData->>'binCorporateCustomSelectDataId' AS BIGINT)::BIGINT) INTO objFnCheckRes; IF objFnCheckRes.sin_sql_check_status = -1::SMALLINT THEN objSpOutRes.sinSqlCustomStatus := -1::SMALLINT; objSpOutRes.strSqlCustomStatusCode := 'ERROR'; objSpOutRes.strSqlCustomMessage := objFnCheckRes.vhr_sql_check_message; objSpOutRes.strSqlCustomErrorType := 'DATABASE_VALIDATION_ERROR'; SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes; RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes; RETURN; END IF; IF CAST(jsnCorporateCustomSelectData->>'binCorporateCustomSelectDataId' AS BIGINT)::BIGINT = 0::BIGINT THEN -- // Count Exced or other checking License related in DB FUCTION(Insert) --jsnCorporateCustomSelectData.intSysActionId = 0; --jsnCorporateCustomSelectData.binCreatedUserId = jsnCorporateCustomSelectData.linLoginUserId; --jsnCorporateCustomSelectData.dtmCreated = jsnCorporateCustomSelectData.dtmCurrentDateTime; --SELECT NOW(); INSERT INTO corporate.tbl_corporate_custom_select_data (fk_corporate_custom_data_id, fk_customer_id, art_select_items, int_sys_action_id, fk_created_user_id, dtm_created) VALUES(CAST(jsnCorporateCustomSelectData->>'binCorporateCustomDataId' AS BIGINT), CAST(jsnCorporateCustomSelectData->>'binCustomerId' AS BIGINT), STRING_TO_ARRAY(jsnCorporateCustomSelectData->>'strDbArtSelectItems', ',')::TEXT[], 0, CAST(jsnCorporateCustomSelectData->>'binCreatedUserId' AS BIGINT), dtmCurrent) RETURNING pk_corporate_custom_select_data_id INTO objSpOutRes.binSqlPk; ELSE -- // UPDATE Check already update another usersin DB FUCTION(Update and Delete) SELECT * FROM fn_check_already_update_or_delete_another_user('corporate.tbl_corporate_custom_select_data'::VARCHAR, 'pk_corporate_custom_select_data_id'::VARCHAR, CAST(jsnCorporateCustomSelectData->>'binCorporateCustomSelectDataId' AS BIGINT)::BIGINT, CAST(jsnCorporateCustomSelectData->>'intSysActionId' AS INT)::INT) INTO objFnCheckRes; IF objFnCheckRes.sin_sql_check_status = -1::SMALLINT THEN objSpOutRes.sinSqlCustomStatus := -1::SMALLINT; objSpOutRes.strSqlCustomStatusCode := 'ERROR'; objSpOutRes.strSqlCustomMessage := objFnCheckRes.vhr_sql_check_message; objSpOutRes.strSqlCustomErrorType := 'DATABASE_VALIDATION_ERROR'; SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes; RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes; RETURN; END IF; --jsnCorporateCustomSelectData.intSysActionId = jsnCorporateCustomSelectData.intSysActionId + 1; --jsnCorporateCustomSelectData.binLastModifiedUserId = jsnCorporateCustomSelectData.linLoginUserId; --jsnCorporateCustomSelectData.dtmLastModified = jsnCorporateCustomSelectData.dtmCurrentDateTime; --SELECT NOW(); UPDATE corporate.tbl_corporate_custom_select_data SET fk_corporate_custom_data_id = CAST(jsnCorporateCustomSelectData->>'binCorporateCustomDataId' AS BIGINT), fk_customer_id = CAST(jsnCorporateCustomSelectData->>'binCustomerId' AS BIGINT), art_select_items = STRING_TO_ARRAY(jsnCorporateCustomSelectData->>'strDbArtSelectItems', ',')::TEXT[], int_sys_action_id = int_sys_action_id + 1, fk_last_modified_user_id = CAST(jsnCorporateCustomSelectData->>'binLastModifiedUserId' AS BIGINT), dtm_last_modified = dtmCurrent WHERE pk_corporate_custom_select_data_id = CAST(jsnCorporateCustomSelectData->>'binCorporateCustomSelectDataId' AS BIGINT); END IF; --COMMIT; objSpOutRes.sinSqlCustomStatus := 1::SMALLINT; objSpOutRes.strSqlCustomStatusCode := 'SUCCESS'; objSpOutRes.strSqlCustomMessage := 'CorporateCustomSelectData Saved'; objSpOutRes.dtmCurrent = dtmCurrent; SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes; RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes; RETURN; EXCEPTION WHEN OTHERS THEN ROLLBACK; objSpOutRes.sinSqlCustomStatus := -1::SMALLINT; objSpOutRes.strSqlCustomStatusCode := 'ERROR'; objSpOutRes.strSqlCustomMessage := 'CorporateCustomSelectData Save Failed'; objSpOutRes.strSqlCustomErrorType := 'DATABASE_SYSTEM_ERROR'; GET STACKED DIAGNOSTICS objSpOutRes.strSqlSysMessage := MESSAGE_TEXT, objSpOutRes.strSqlSysExceptionDetail := PG_EXCEPTION_DETAIL, objSpOutRes.strSqlSysExceptionHint := PG_EXCEPTION_CONTEXT; SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes; RAISE EXCEPTION 'jsnSqlRes: %', jsnSqlRes; RETURN; END; $$ LANGUAGE plpgsql; ------------------------------------------------------------------------------------------ CREATE OR REPLACE PROCEDURE sp_delete_corporate_custom_select_data(binCorporateCustomSelectDataId BIGINT, intSysActionId INT, binDeletedUserId BIGINT, jsnSqlRes OUT JSON) AS $$ DECLARE objSpOutRes TYP_SP_OUT_RES; objFnCheckRes TYP_FN_CHECK_RES; dtmCurrent TIMESTAMP WITH TIME ZONE; BEGIN SELECT NOW() INTO dtmCurrent; -- // FOREIGN KEY REFERENCES Checking in DB FUCTION(Delete) SELECT * FROM fn_check_foreignkey_referance('tbl_corporate_custom_select_data'::VARCHAR, 'pk_corporate_custom_select_data_id'::VARCHAR, binCorporateCustomSelectDataId::BIGINT, '{}'::VARCHAR ARRAY) INTO objFnCheckRes; IF objFnCheckRes.sin_sql_check_status = -1::SMALLINT THEN objSpOutRes.sinSqlCustomStatus := -1::SMALLINT; objSpOutRes.strSqlCustomStatusCode := 'ERROR'; objSpOutRes.strSqlCustomMessage := objFnCheckRes.vhr_sql_check_message; objSpOutRes.strSqlCustomErrorType := 'DATABASE_VALIDATION_ERROR'; SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes; RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes; RETURN; END IF; -- // UPDATE Check already update another users in DB FUCTION(Update and Delete) SELECT * FROM fn_check_already_update_or_delete_another_user('corporate.tbl_corporate_custom_select_data'::VARCHAR, 'pk_corporate_custom_select_data_id'::VARCHAR, CAST(binCorporateCustomSelectDataId AS BIGINT)::BIGINT, intSysActionId::INT) INTO objFnCheckRes; IF objFnCheckRes.sin_sql_check_status = -1::SMALLINT THEN objSpOutRes.sinSqlCustomStatus := -1::SMALLINT; objSpOutRes.strSqlCustomStatusCode := 'ERROR'; objSpOutRes.strSqlCustomMessage := objFnCheckRes.vhr_sql_check_message; objSpOutRes.strSqlCustomErrorType := 'DATABASE_VALIDATION_ERROR'; SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes; RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes; RETURN; END IF; --dtmDeleted = SELECT NOW(); UPDATE corporate.tbl_corporate_custom_select_data SET int_sys_action_id = -1::INT, fk_deleted_user_id = binDeletedUserId, dtm_deleted = dtmCurrent WHERE pk_corporate_custom_select_data_id = binCorporateCustomSelectDataId; --COMMIT; objSpOutRes.sinSqlCustomStatus := 1::SMALLINT; objSpOutRes.strSqlCustomStatusCode := 'SUCCESS'; objSpOutRes.strSqlCustomMessage := 'CorporateCustomSelectData Deleted'; objSpOutRes.dtmCurrent = dtmCurrent; SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes; RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes; RETURN; EXCEPTION WHEN OTHERS THEN ROLLBACK; objSpOutRes.sinSqlCustomStatus := -1::SMALLINT; objSpOutRes.strSqlCustomStatusCode := 'ERROR'; objSpOutRes.strSqlCustomMessage := 'CorporateCustomSelectData Delete Failed'; objSpOutRes.strSqlCustomErrorType := 'DATABASE_SYSTEM_ERROR'; GET STACKED DIAGNOSTICS objSpOutRes.strSqlSysMessage := MESSAGE_TEXT, objSpOutRes.strSqlSysExceptionDetail := PG_EXCEPTION_DETAIL, objSpOutRes.strSqlSysExceptionHint := PG_EXCEPTION_CONTEXT; SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes; RAISE EXCEPTION 'jsnSqlRes: %', jsnSqlRes; RETURN; END; $$ LANGUAGE plpgsql;