<?php

/**
 * @name EOLINKER ams open source，EOLINKER open source version
 * @link https://global.eolinker.com/
 * @package EOLINKER
 * @author www.eolinker.com eoLinker Ltd.co 2015-2018
 * 
 * eoLinker is the world's leading and domestic largest online API interface management platform, providing functions such as automatic generation of API documents, API automated testing, Mock testing, team collaboration, etc., aiming to solve the problem of low development efficiency caused by separation of front and rear ends.
 * If you have any problems during the process of use, please join the user discussion group for feedback, we will solve the problem for you with the fastest speed and best service attitude.
 *
 * 
 *
 * Website：https://global.eolinker.com/
 * Slack：eolinker.slack.com
 * facebook：@EoLinker
 * twitter：@eoLinker
 */
class AutoGenerateDao
{
    /**
     * Import Data
     * @param $data array json data
     * @param $project_id int projectID
     * @return bool
     */
    public function importApi(&$data, &$project_id)
    {
        $db = getDatabase();
        try {
            // Start transaction
            $db->beginTransaction();
            $db->prepareExecuteAll('DELETE FROM eo_ams_api_header WHERE eo_ams_api_header.apiID IN (SELECT eo_ams_api.apiID FROM eo_ams_api WHERE eo_ams_api.projectID = ?);', array($project_id));
            $db->prepareExecuteAll('DELETE FROM eo_ams_api_request_value WHERE eo_ams_api_request_value.paramID IN (SELECT eo_ams_api_request_param.paramID FROM eo_ams_api_request_param LEFT JOIN eo_ams_api ON eo_ams_api_request_param.apiID = eo_ams_api.apiID WHERE eo_ams_api.projectID = ?);', array($project_id));
            $db->prepareExecuteAll('DELETE FROM eo_ams_api_request_param WHERE eo_ams_api_request_param.apiID IN (SELECT eo_ams_api.apiID FROM eo_ams_api WHERE eo_ams_api.projectID = ?)', array($project_id));
            $db->prepareExecuteAll('DELETE FROM eo_ams_api_result_value WHERE eo_ams_api_result_value.paramID IN (SELECT eo_ams_api_result_param.paramID FROM eo_ams_api_result_param LEFT JOIN eo_ams_api ON eo_ams_api_result_param.apiID = eo_ams_api.apiID WHERE eo_ams_api.projectID = ?);', array($project_id));
            $db->prepareExecuteAll('DELETE FROM eo_ams_api_result_param WHERE eo_ams_api_result_param.apiID IN (SELECT eo_ams_api.apiID FROM eo_ams_api WHERE eo_ams_api.projectID = ?)', array($project_id));
            $db->prepareExecuteAll('DELETE FROM eo_ams_api_group WHERE eo_ams_api_group.projectID = ?;', array($project_id));
            $db->prepareExecuteAll('DELETE FROM eo_ams_api WHERE eo_ams_api.projectID = ?;', array($project_id));
            $db->prepareExecuteAll('DELETE FROM eo_ams_api_cache WHERE eo_ams_api_cache.projectID = ?;', array($project_id));

            // insert API group Info
            foreach ($data as $api_group) {
                $db->prepareExecute('INSERT INTO eo_ams_api_group (eo_ams_api_group.groupName,eo_ams_api_group.projectID) VALUES (?,?);', array(
                    $api_group['groupName'],
                    $project_id
                ));

                if ($db->getAffectRow() < 1)
                    throw new \PDOException("addGroup error");

                $group_id = $db->getLastInsertID();
                if ($api_group['apiList']) {
                    foreach ($api_group['apiList'] as $api) {
                        $db->prepareExecute('INSERT INTO eo_ams_api (eo_ams_api.apiName,eo_ams_api.apiURI,eo_ams_api.apiProtocol,eo_ams_api.apiSuccessMock,eo_ams_api.apiFailureMock,eo_ams_api.apiRequestType,eo_ams_api.apiStatus,eo_ams_api.groupID,eo_ams_api.projectID,eo_ams_api.starred,eo_ams_api.apiNoteType,eo_ams_api.apiNoteRaw,eo_ams_api.apiNote,eo_ams_api.apiRequestParamType,eo_ams_api.apiRequestRaw,eo_ams_api.apiUpdateTime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);', array(
                            $api['baseInfo']['apiName'],
                            $api['baseInfo']['apiURI'],
                            $api['baseInfo']['apiProtocol'],
                            $api['baseInfo']['apiSuccessMock'],
                            $api['baseInfo']['apiFailureMock'],
                            $api['baseInfo']['apiRequestType'],
                            $api['baseInfo']['apiStatus'],
                            $group_id,
                            $project_id,
                            $api['baseInfo']['starred'],
                            $api['baseInfo']['apiNoteType'],
                            $api['baseInfo']['apiNoteRaw'],
                            $api['baseInfo']['apiNote'],
                            $api['baseInfo']['apiRequestParamType'],
                            $api['baseInfo']['apiRequestRaw'],
                            $api['baseInfo']['apiUpdateTime']
                        ));

                        if ($db->getAffectRow() < 1)
                            throw new \PDOException("addApi error");

                        $api_id = $db->getLastInsertID();

                        if ($api['headerInfo']) {
                            foreach ($api['headerInfo'] as $header) {
                                $db->prepareExecute('INSERT INTO eo_ams_api_header (eo_ams_api_header.headerName,eo_ams_api_header.headerValue,eo_ams_api_header.apiID) VALUES (?,?,?);', array(
                                    $header['headerName'],
                                    $header['headerValue'],
                                    $api_id
                                ));

                                if ($db->getAffectRow() < 1)
                                    throw new \PDOException("addHeader error");
                            }
                        }

                        if ($api['requestInfo']) {
                            foreach ($api['requestInfo'] as $request) {
                                $db->prepareExecute('INSERT INTO eo_ams_api_request_param (eo_ams_api_request_param.apiID,eo_ams_api_request_param.paramName,eo_ams_api_request_param.paramKey,eo_ams_api_request_param.paramValue,eo_ams_api_request_param.paramLimit,eo_ams_api_request_param.paramNotNull,eo_ams_api_request_param.paramType) VALUES (?,?,?,?,?,?,?);', array(
                                    $api_id,
                                    $request['paramName'],
                                    $request['paramKey'],
                                    $request['paramValue'],
                                    $request['paramLimit'],
                                    $request['paramNotNull'],
                                    $request['paramType']
                                ));

                                if ($db->getAffectRow() < 1)
                                    throw new \PDOException("addRequestParam error");

                                $param_id = $db->getLastInsertID();

                                if ($request['paramValueList']) {
                                    foreach ($request['paramValueList'] as $value) {
                                        $db->prepareExecute('INSERT INTO eo_ams_api_request_value (eo_ams_api_request_value.paramID,eo_ams_api_request_value.`value`,eo_ams_api_request_value.valueDescription) VALUES (?,?,?);', array(
                                            $param_id,
                                            $value['value'],
                                            $value['valueDescription']
                                        ));

                                        if ($db->getAffectRow() < 1)
                                            throw new \PDOException("addApi error");
                                    };
                                }
                            };
                        }

                        if ($api['resultInfo']) {
                            foreach ($api['resultInfo'] as $result) {
                                $db->prepareExecute('INSERT INTO eo_ams_api_result_param (eo_ams_api_result_param.apiID,eo_ams_api_result_param.paramName,eo_ams_api_result_param.paramKey,eo_ams_api_result_param.paramNotNull) VALUES (?,?,?,?);', array(
                                    $api_id,
                                    $result['paramName'],
                                    $result['paramKey'],
                                    $result['paramNotNull']
                                ));

                                if ($db->getAffectRow() < 1)
                                    throw new \PDOException("addResultParam error");

                                $param_id = $db->getLastInsertID();

                                if ($result['paramValueList']) {
                                    foreach ($result['paramValueList'] as $value) {
                                        $db->prepareExecute('INSERT INTO eo_ams_api_result_value (eo_ams_api_result_value.paramID,eo_ams_api_result_value.`value`,eo_ams_api_result_value.valueDescription) VALUES (?,?,?);;', array(
                                            $param_id,
                                            $value['value'],
                                            $value['valueDescription']
                                        ));

                                        if ($db->getAffectRow() < 1)
                                            throw new \PDOException("addApi error");
                                    };
                                }
                            };
                        }


                        $db->prepareExecute("INSERT INTO eo_ams_api_cache (eo_ams_api_cache.projectID,eo_ams_api_cache.groupID,eo_ams_api_cache.apiID,eo_ams_api_cache.apiJson,eo_ams_api_cache.starred) VALUES (?,?,?,?,?);", array(
                            $project_id,
                            $group_id,
                            $api_id,
                            json_encode($api),
                            $api['baseInfo']['starred']
                        ));

                        if ($db->getAffectRow() < 1) {
                            throw new \PDOException("addApiCache error");
                        }
                    }
                }
                if (is_array($api_group['apiGroupChildList'])) {

                    $group_parent_id = $group_id;
                    foreach ($api_group['apiGroupChildList'] as $api_group_child) {
                        $db->prepareExecute('INSERT INTO eo_ams_api_group (eo_ams_api_group.groupName,eo_ams_api_group.projectID,eo_ams_api_group.parentGroupID, eo_ams_api_group.isChild) VALUES (?,?,?,?);', array(
                            $api_group_child['groupName'],
                            $project_id,
                            $group_parent_id,
                            1
                        ));

                        if ($db->getAffectRow() < 1)
                            throw new \PDOException("addChildGroup error");

                        $group_id = $db->getLastInsertID();

                        if (empty($api_group_child['apiList']))
                            continue;

                        foreach ($api_group_child['apiList'] as $api) {
                            $db->prepareExecute('INSERT INTO eo_ams_api (eo_ams_api.apiName,eo_ams_api.apiURI,eo_ams_api.apiProtocol,eo_ams_api.apiSuccessMock,eo_ams_api.apiFailureMock,eo_ams_api.apiRequestType,eo_ams_api.apiStatus,eo_ams_api.groupID,eo_ams_api.projectID,eo_ams_api.starred,eo_ams_api.apiNoteType,eo_ams_api.apiNoteRaw,eo_ams_api.apiNote,eo_ams_api.apiRequestParamType,eo_ams_api.apiRequestRaw,eo_ams_api.apiUpdateTime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);', array(
                                $api['baseInfo']['apiName'],
                                $api['baseInfo']['apiURI'],
                                $api['baseInfo']['apiProtocol'],
                                $api['baseInfo']['apiSuccessMock'],
                                $api['baseInfo']['apiFailureMock'],
                                $api['baseInfo']['apiRequestType'],
                                $api['baseInfo']['apiStatus'],
                                $group_id,
                                $project_id,
                                $api['baseInfo']['starred'],
                                $api['baseInfo']['apiNoteType'],
                                $api['baseInfo']['apiNoteRaw'],
                                $api['baseInfo']['apiNote'],
                                $api['baseInfo']['apiRequestParamType'],
                                $api['baseInfo']['apiRequestRaw'],
                                $api['baseInfo']['apiUpdateTime']
                            ));

                            if ($db->getAffectRow() < 1)
                                throw new \PDOException("addChildApi error");

                            $api_id = $db->getLastInsertID();

                            if ($api['headerInfo']) {
                                foreach ($api['headerInfo'] as $header) {
                                    $db->prepareExecute('INSERT INTO eo_ams_api_header (eo_ams_api_header.headerName,eo_ams_api_header.headerValue,eo_ams_api_header.apiID) VALUES (?,?,?);', array(
                                        $header['headerName'],
                                        $header['headerValue'],
                                        $api_id
                                    ));

                                    if ($db->getAffectRow() < 1)
                                        throw new \PDOException("addChildHeader error");
                                }
                            }

                            if ($api['requestInfo']) {
                                foreach ($api['requestInfo'] as $request) {
                                    $db->prepareExecute('INSERT INTO eo_ams_api_request_param (eo_ams_api_request_param.apiID,eo_ams_api_request_param.paramName,eo_ams_api_request_param.paramKey,eo_ams_api_request_param.paramValue,eo_ams_api_request_param.paramLimit,eo_ams_api_request_param.paramNotNull,eo_ams_api_request_param.paramType) VALUES (?,?,?,?,?,?,?);', array(
                                        $api_id,
                                        $request['paramName'],
                                        $request['paramKey'],
                                        $request['paramValue'],
                                        $request['paramLimit'],
                                        $request['paramNotNull'],
                                        $request['paramType']
                                    ));

                                    if ($db->getAffectRow() < 1)
                                        throw new \PDOException("addChildRequestParam error");

                                    $param_id = $db->getLastInsertID();

                                    if ($request['paramValueList']) {
                                        foreach ($request['paramValueList'] as $value) {
                                            $db->prepareExecute('INSERT INTO eo_ams_api_request_value (eo_ams_api_request_value.paramID,eo_ams_api_request_value.`value`,eo_ams_api_request_value.valueDescription) VALUES (?,?,?);', array(
                                                $param_id,
                                                $value['value'],
                                                $value['valueDescription']
                                            ));

                                            if ($db->getAffectRow() < 1)
                                                throw new \PDOException("addChildApi error");
                                        };
                                    }
                                };
                            }

                            if ($api['resultInfo']) {
                                foreach ($api['resultInfo'] as $result) {
                                    $db->prepareExecute('INSERT INTO eo_ams_api_result_param (eo_ams_api_result_param.apiID,eo_ams_api_result_param.paramName,eo_ams_api_result_param.paramKey,eo_ams_api_result_param.paramNotNull) VALUES (?,?,?,?);', array(
                                        $api_id,
                                        $result['paramName'],
                                        $result['paramKey'],
                                        $result['paramNotNull']
                                    ));

                                    if ($db->getAffectRow() < 1)
                                        throw new \PDOException("addChildResultParam error");

                                    $param_id = $db->getLastInsertID();

                                    if ($result['paramValueList']) {
                                        foreach ($result['paramValueList'] as $value) {
                                            $db->prepareExecute('INSERT INTO eo_ams_api_result_value (eo_ams_api_result_value.paramID,eo_ams_api_result_value.`value`,eo_ams_api_result_value.valueDescription) VALUES (?,?,?);;', array(
                                                $param_id,
                                                $value['value'],
                                                $value['valueDescription']
                                            ));

                                            if ($db->getAffectRow() < 1)
                                                throw new \PDOException("addChildParamValue error");
                                        };
                                    }
                                };
                            }

                            $db->prepareExecute("INSERT INTO eo_ams_api_cache (eo_ams_api_cache.projectID,eo_ams_api_cache.groupID,eo_ams_api_cache.apiID,eo_ams_api_cache.apiJson,eo_ams_api_cache.starred) VALUES (?,?,?,?,?);", array(
                                $project_id,
                                $group_id,
                                $api_id,
                                json_encode($api),
                                $api['baseInfo']['starred']
                            ));

                            if ($db->getAffectRow() < 1) {
                                throw new \PDOException("addChildApiCache error");
                            }
                        }

                    }
                }
            }
        } catch (\PDOException $e) {
            $db->rollBack();
            return FALSE;
        }
        $db->commit();
        return TRUE;
    }
}