def do_get_all_versions_url(self):
     """
     获取所有的版本信息
     :return:
     """
     res_json = VersionInfoTable().get_versions()
     return res_json
    def deal_noinput_res(self):
        """
        没有传入res的,补全全部的latest version的res资源
        :return:
        """
        res_json = {"resInfos": []}

        if self.auto_fill:
            for k, v in self.res_latest_version.items():
                result = VersionInfoTable().get_versions(appID=self.app_id,
                                                         resID=k,
                                                         resVersion=v)

                for one_result in result:
                    one_item = {}
                    one_item[VersionInfoTable.resID] = one_result[
                        VersionInfoTable.resID]
                    one_item[VersionInfoTable.resVersion] = one_result[
                        VersionInfoTable.resVersion]
                    one_item[VersionInfoTable.fullUrl] = one_result[
                        VersionInfoTable.fullUrl]
                    one_item[VersionInfoTable.fullMd5] = one_result[
                        VersionInfoTable.fullMd5]

                    one_item["state"] = 3
                    domains = one_result[VersionInfoTable.domain].split(",")
                    one_item["userData"] = json.dumps({"domains": domains})

                    res_json["resInfos"].append(one_item)
        else:
            pass

        return res_json
    def run(self):
        """
        启动服务并执行
        :return:
        """
        self.get_args()

        if self.is_clear:
            VersionInfoTable().drop_table()

        VersionInfoTable().create_table()

        http_server = socketserver.TCPServer((self.host, self.port),
                                             PackageHttpHandler)

        http_server.serve_forever()

        return
    def all_res_version(self):
        """
        检查appid是否存在
        """
        result = VersionInfoTable().get_versions("resID",
                                                 "resVersion",
                                                 appID=self.app_id)
        if len(result) == 0:
            return False
        else:
            for one in result:
                try:
                    if self.res_latest_version[
                            one["resID"]] < one["resVersion"]:
                        self.res_latest_version[
                            one["resID"]] = one["resVersion"]
                except:
                    self.res_latest_version[one["resID"]] = one[
                        "resVersion"]  # 第一次,那么就讲version赋值过去作为key的value

            return True
    def get_latest_version_url(self):
        """
        /api/get_latest_version
        :return:
        """
        content_json = self.get_content_json()
        latest_versions = []

        try:
            appID = content_json["appID"]
            resID = content_json["resID"]

            result = VersionInfoTable().get_latest_version(
                appID=appID,
                resID=resID,
                ORDER_KEY="resVersion",
                ORDER_TYPE="DESC",
                LIMIT=1)
            for one in result:
                one_item = {}
                one_item[VersionInfoTable.resID] = one[VersionInfoTable.resID]
                one_item[VersionInfoTable.resVersion] = one[
                    VersionInfoTable.resVersion]
                one_item[VersionInfoTable.fullUrl] = one[
                    VersionInfoTable.fullUrl]
                one_item[VersionInfoTable.fullMd5] = one[
                    VersionInfoTable.fullMd5]
                one_item[VersionInfoTable.diffUrl] = one[
                    VersionInfoTable.diffUrl]
                one_item[VersionInfoTable.diffMd5] = one[
                    VersionInfoTable.diffMd5]
                latest_versions.append(one_item)

        except Exception as e:
            print("get_latest_version_url: " + str(e))
            self.do_response(CODE_OTHER_ERROR, "get latest version error.")
            return

        self.do_response(CODE_OK, "ok", latest_versions)
        return
    def do_upload_version_url(self):
        """
        /api/upload_version
        """
        content_json = self.get_content_json()
        new_versions = []
        for one in content_json:
            try:
                one_version = {}
                one_version[VersionInfoTable.appID] = one[
                    VersionInfoTable.appID]
                one_version[VersionInfoTable.appVersion] = one[
                    VersionInfoTable.appVersion]
                one_version[VersionInfoTable.resID] = one[
                    VersionInfoTable.resID]
                one_version[VersionInfoTable.resVersion] = one[
                    VersionInfoTable.resVersion]
                one_version[VersionInfoTable.diffUrl] = one[
                    VersionInfoTable.diffUrl]
                one_version[VersionInfoTable.diffMd5] = one[
                    VersionInfoTable.diffMd5]
                one_version[VersionInfoTable.fullUrl] = one[
                    VersionInfoTable.fullUrl]
                one_version[VersionInfoTable.fullMd5] = one[
                    VersionInfoTable.fullMd5]
                one_version[VersionInfoTable.domain] = one[
                    VersionInfoTable.domain]

                new_versions.append(one_version)
            except Exception as e:
                new_versions = []
                print("do_upload_version: " + str(e))

        VersionInfoTable().add_new_versions(new_versions)

        res_json = {"insert_cnt": len(new_versions)}

        self.do_response(CODE_OK, "ok", res_json)
        return
    def deal_withinput_res(self):
        """
        有传入res的处理方式
        :return:
        """
        code = CODE_OK
        code_msg = "OK"
        res_json = {"resInfos": []}
        no_the_res = False

        # 先查找传入res的
        for input_one in self.res_infos:
            try:
                tmp_resID = input_one[self.resID]
                input_one[self.resVersion]

                if tmp_resID in self.res_latest_version:
                    self.res_latest_version.pop(tmp_resID)
                else:
                    no_the_res = True

            except Exception as e:
                continue

            one_item = {}

            result = VersionInfoTable().get_latest_version(
                appID=self.app_id,
                resID=input_one[self.resID],
                ORDER_KEY="resVersion",
                ORDER_TYPE="DESC",
                LIMIT=1)
            if len(result) == 0:
                one_item["state"] = 2
                one_item[VersionInfoTable.resID] = input_one[self.resID]
            else:
                for result_one in result:
                    if result_one[VersionInfoTable.resVersion] == input_one[
                            self.resVersion]:
                        one_item["state"] = 0
                        one_item[VersionInfoTable.resID] = input_one[
                            self.resID]
                        break

                    elif result_one[VersionInfoTable.resVersion] > input_one[
                            self.resVersion]:
                        # 否则返回服务端最新的
                        one_item[VersionInfoTable.resID] = result_one[
                            VersionInfoTable.resID]
                        one_item[VersionInfoTable.resVersion] = result_one[
                            VersionInfoTable.resVersion]
                        one_item[VersionInfoTable.fullUrl] = result_one[
                            VersionInfoTable.fullUrl]
                        one_item[VersionInfoTable.fullMd5] = result_one[
                            VersionInfoTable.fullMd5]
                        one_item[VersionInfoTable.diffUrl] = result_one[
                            VersionInfoTable.diffUrl]
                        one_item[VersionInfoTable.diffMd5] = result_one[
                            VersionInfoTable.diffMd5]

                        one_item["state"] = 1
                        domains = result_one[VersionInfoTable.domain].split(
                            ",")
                        one_item["userData"] = json.dumps({"domains": domains})
                        break

                    else:
                        # 说明传入的版本是更新的,暂时回复不用更新处理即可
                        one_item["state"] = 0
                        one_item[VersionInfoTable.resID] = input_one[
                            self.resID]
                        break

            res_json["resInfos"].append(one_item)

        # 然后执行需要补全的
        if self.auto_fill:
            for k, v in self.res_latest_version.items():
                result = VersionInfoTable().get_versions(appID=self.app_id,
                                                         resID=k,
                                                         resVersion=v)

                for one_result in result:
                    one_item = {}
                    one_item[VersionInfoTable.resID] = one_result[
                        VersionInfoTable.resID]
                    one_item[VersionInfoTable.resVersion] = one_result[
                        VersionInfoTable.resVersion]
                    one_item[VersionInfoTable.fullUrl] = one_result[
                        VersionInfoTable.fullUrl]
                    one_item[VersionInfoTable.fullMd5] = one_result[
                        VersionInfoTable.fullMd5]
                    one_item[VersionInfoTable.diffUrl] = result_one[
                        VersionInfoTable.diffUrl]
                    one_item[VersionInfoTable.diffMd5] = result_one[
                        VersionInfoTable.diffMd5]

                    one_item["state"] = 3
                    domains = one_result[VersionInfoTable.domain].split(",")
                    one_item["userData"] = json.dumps({"domains": domains})

                    res_json["resInfos"].append(one_item)
        else:
            # if no_the_res:
            #     code = CODE_OTHER_ERROR
            #     code_msg = "The Input Res is No In Server"
            pass

        return res_json, code, code_msg