#encoding:UTF8

from BaseDataMaintenance.model.ots.BaseModel import BaseModel
from datetime import datetime
from tablestore import *
from BaseDataMaintenance.common.Utils import *


designed_project_partitionkey = 'partitionkey'
designed_project_id = 'id'
designed_project_area = 'area'
designed_project_begintime = 'begintime'
designed_project_city = 'city'
designed_project_construct_install_fee = 'construct_install_fee'
designed_project_contacts = 'contacts'
designed_project_covered_area = 'covered_area'
designed_project_crtime = 'crtime'
designed_project_des_project_type = 'des_project_type'
designed_project_district = 'district'
designed_project_docids = 'docids'
designed_project_elevator = 'elevator'
designed_project_endtime = 'endtime'
designed_project_engineer_cost = 'engineer_cost'
designed_project_facade_type = 'facade_type'
designed_project_facade_type2 = 'facade_type2'
designed_project_floor_space = 'floor_space'
designed_project_floors_num = 'floors_num'
designed_project_follow_number = 'follow_number'
designed_project_follows = 'follows'
designed_project_full_text = 'full_text'
designed_project_has_steel = 'has_steel'
designed_project_high_project_name = 'high_project_name'
designed_project_json_list_group = 'json_list_group'
designed_project_latest_service_time = 'latest_service_time'
designed_project_ordinary_name = 'ordinary_name'
designed_project_page_time = 'page_time'
designed_project_progress = 'progress'
designed_project_project_address = 'project_address'
designed_project_project_code = 'project_code'
designed_project_project_description = 'project_description'
designed_project_project_follow = 'project_follow'
designed_project_project_investment = 'project_investment'
designed_project_project_name = 'project_name'
designed_project_project_nature = 'project_nature'
designed_project_project_structure = 'project_structure'
designed_project_project_type = 'project_type'
designed_project_proportion_unit = 'proportion_unit'
designed_project_province = 'province'
designed_project_spids = 'spids'
designed_project_status = 'status'
designed_project_update_status = 'update_status'
designed_project_update_time = 'update_time'
designed_project_approval_list = 'approval_list'
designed_project_bid_info_list = 'bid_info_list'
designed_project_contract_register_list = 'contract_register_list'
designed_project_construct_pic_check_list = 'construct_pic_check_list'
designed_project_construct_permit_list = 'construct_permit_list'
designed_project_completion_accpet_list = 'completion_accpet_list'


class designed_project(BaseModel):

    def __init__(self,_dict):
        for k,v in _dict.items():
            self.setValue(k,v,True)
        self.table_name = "designed_project"

    def getPrimary_keys(self):
        return ["partitionkey","id"]

    def search_by_docids(self,ots_client,docids,spids):
        should_q = []
        for _docid in docids.split(","):
            should_q.append(TermQuery("docids",_docid))
        for _spid in spids.split(","):
            should_q.append(TermQuery("spids",_spid))

        _begin = 0
        _step = 20
        list_dict = []
        while 1:
            _end = _begin +_step
            bool_query = BoolQuery(should_queries=should_q[_begin:_end])
            columns = ["status"]
            rows, next_token, total_count, is_all_succeed = ots_client.search("designed_project", "designed_project_index",
                                                                              SearchQuery(bool_query, limit=100,get_total_count=True),
                                                                              ColumnsToGet(columns,return_type=ColumnReturnType.SPECIFIED))
            list_dict.extend(getRow_ots(rows))
            _begin = _end
            if _begin>=len(should_q):
                break
        list_dict_final = []
        set_id = set()
        for _dict in list_dict:
            if _dict.get("id","") in set_id:
                continue
            list_dict_final.append(_dict)
            set_id.add(_dict.get("id",""))
        return list_dict_final

    def getAttribute_turple(self):
        _list = []
        for _key in self.getAttribute_keys():
            if _key=="all_columns":
                continue
            _v = self.getProperties().get(_key)
            if _v is not None:
                if isinstance(_v,list):
                    _v = json.dumps(_v)
                _list.append((_key,_v))
        return _list

    def update_row(self,ots_client,retrytimes=3, delete=1):
        primary_key = self.getPrimaryKey_turple()
        update_of_attribute_columns = {
            'PUT' : self.getAttribute_turple()
        }
        row = Row(primary_key,update_of_attribute_columns)
        condition = Condition('IGNORE')
        for i in range(retrytimes):
            try:
                if self.exists_row(ots_client) and delete:
                    self.delete_row(ots_client)
                consumed, return_row = ots_client.update_row(self.table_name, row, condition)
                return True
            # 客户端异常，一般为参数错误或者网络异常。
            except OTSClientError as e:
                traceback.print_exc()
                log("update row failed, http_status:%s, error_message:%s" % (str(e.get_http_status()), e.get_error_message()))
                # raise e
            # 服务端异常，一般为参数错误或者流控错误。
            except OTSServiceError as e:
                traceback.print_exc()
                log("update row failed, http_status:%s, error_code:%s, error_message:%s, request_id:%s" % (str(e.get_http_status()), e.get_error_code(), e.get_error_message(), e.get_request_id()))
                # raise e
            except Exception as e:
                traceback.print_exc()
                pass
                # raise e
        return False
        # log ('update succeed, consume %s write cu.' % consumed.write)

    def update_project(self,ots_client):
        docids = self.__dict__.get("docids","")
        spids = self.__dict__.get("spids","")

        #判断是否有存量生成项目,有则更新且删除多余的
        list_dict = self.search_by_docids(ots_client,docids,spids)
        if len(list_dict)>0:
            for _dict in list_dict[1:]:
                _designed_delete = designed_project(_dict)

                _designed_delete.setValue("status", "404", True)
                current_time = datetime.now()
                current_time = current_time.strftime('%Y-%m-%d %H:%M:%S')
                _designed_delete.setValue("update_time", current_time, True)
                _designed_delete.update_project(ots_client)
                # _designed_delete.delete_row(ots_client)

            _designed_update = designed_project(list_dict[0])
            properties = _designed_update.getProperties()
            _partitionkey = properties.get("partitionkey")
            _id = properties.get("id")
            self.setProperties("partitionkey",_partitionkey)
            self.setProperties("id",_id)
        self.update_row(ots_client)


if __name__=="__main__":
    s = '{"project_name": "\u6587\u5b89\u53bf\u57ce\u5e02\u5efa\u8bbe\u53d1\u5c55\u6709\u9650\u516c\u53f8\u7efc\u5408\u4fbf\u6c11\u670d\u52a1\u4e2d\u5fc3\u9879\u76ee", "full_text": "\u6587\u5b89\u53bf\u57ce\u5e02\u5efa\u8bbe\u53d1\u5c55\u6709\u9650\u516c\u53f8\u7efc\u5408\u4fbf\u6c11\u670d\u52a1\u4e2d\u5fc3\u9879\u76ee\u9879\u76ee\u6982\u51b5\u4e0e\u62db\u6807\u8303\u56f4\uff0c2.1\u9879\u76ee\u6982\u51b5\uff1a2.1\u5de5\u7a0b\u540d\u79f0\uff1a\u6587\u5b89\u53bf\u57ce\u5e02\u5efa\u8bbe\u53d1\u5c55\u6709\u9650\u516c\u53f8\u7efc\u5408\u4fbf\u6c11\u670d\u52a1\u4e2d\u5fc3\u9879\u76ee\u8bbe\u8ba12.2\u5efa\u8bbe\u5730\u5740\uff1a\u6587\u5b89\u53bf\u4e2d\u5fc3\u57ce\u533a\u539f\u6587\u5b89\u53bf\u7b2c\u4e00\u4e2d\u5b66\u51852.3\u5de5\u7a0b\u6982\u51b5\uff1a\u672c\u9879\u76ee\u5360\u5730\u9762\u79ef49794.49\u5e73\u65b9\u7c73\uff0c\u603b\u5efa\u7b51\u9762\u79ef50477.17\u5e73\u65b9\u7c73\uff0c\u5bf9\u539f\u57409\u680b\u5efa\u7b51\u4f53\u8fdb\u884c\u5185\u5916\u90e8\u88c5\u4fee\u63d0\u5347\u6539\u9020\uff0c\u5bf9\u9053\u8def\u5de5\u7a0b\u63d0\u5347\u6539\u9020\u3002\u65b0\u5efa\u9644\u5c5e\u7528\u623f\uff0c\u914d\u5957\u76d1\u63a7\u8bbe\u5907\u53ca\u5176\u4ed6\u5ba4\u5916\u5de5\u7a0b\u7b49\uff0c\u62c6\u9664\u73b0\u6709\u90e8\u5206\u5efa\u7b51\u30022.4\u6807\u6bb5\u5212\u5206\uff1a\u4e00\u4e2a\u6807\u6bb52.5\u8bbe\u8ba1\u670d\u52a1\u671f\u9650\uff1a\u5408\u540c\u7b7e\u8ba2\u4e4b\u65e5\u8d7750\u65e5\u5386\u5929\u5185\u5b8c\u6210\u8bbe\u8ba1\u6210\u679c\u62a5\u544a2.6\u8d28\u91cf\u8981\u6c42\uff1a\u5408\u683c\uff0c\u6ee1\u8db3\u73b0\u884c\u8bbe\u8ba1\u89c4\u8303\u6807\u51c6\uff0c2.2\u62db\u6807\u8303\u56f4\uff1a\u672c\u9879\u76ee\u65bd\u5de5\u56fe\u8bbe\u8ba1\u4ee5\u53ca\u5176\u4ed6\u76f8\u5173\u670d\u52a1\u5de5\u4f5c\uff0c(\u8bf4\u660e\u672c\u6b21\u62db\u6807\u9879\u76ee\u7684\u5efa\u8bbe\u5730\u70b9\u3001\u89c4\u6a21\u3001\u8ba1\u5212\u5de5\u671f\u3001\u62db\u6807\u8303\u56f4\u3001\u6807\u6bb5\u5212\u5206", "high_project_name": "\u6587\u5b89\u53bf\u57ce\u5e02\u5efa\u8bbe\u53d1\u5c55\u6709\u9650\u516c\u53f8\u7efc\u5408\u4fbf\u6c11\u670d\u52a1\u4e2d\u5fc3\u9879\u76ee", "update_time": "2023-05-16", "status": "1", "page_time": "2023-05-15", "docids": "265407137,264467735,323405252,209966750,323337928,215629840,264446259,264577762,209049408,208428670,215595257,258632899", "follow_number": 6, "follows": "[{\"mdate\": \"2022-01-14 00:00:00\", \"id\": 4756009807427665921, \"progress_remark\": \"2021-12-16,\u9879\u76ee\u5904\u4e8e\u8bbe\u8ba1\u9636\u6bb5\u7684\u62db\u6807\u9636\u6bb5\uff1b\u622a\u6b622022-01-14\u786e\u5b9a\u8bbe\u8ba1\u9636\u6bb5\u4e2d\u6807\u4eba\u4e3a\u4e2d\u8054\u5408\u76db\u79d1\u6280\u6709\u9650\u516c\u53f8\", \"version\": \"\u8ddf\u8fdb1\", \"crtime\": \"2023-05-16 10:05:00\", \"docid\": 208428670, \"progress\": \"\u8bbe\u8ba1\u9636\u6bb5\"}, {\"mdate\": \"2022-01-14 00:00:00\", \"id\": 4756009811311591425, \"progress_remark\": \"\u622a\u6b622022-01-14,\u9879\u76ee\u5904\u4e8e\u8bbe\u8ba1\u9636\u6bb5\uff1b\u786e\u5b9a\u8bbe\u8ba1\u9636\u6bb5\u4e2d\u6807\u4eba\u4e3a\u4e2d\u8054\u5408\u76db\u79d1\u6280\u6709\u9650\u516c\u53f8\", \"version\": \"\u8ddf\u8fdb2\", \"crtime\": \"2023-05-16 10:05:01\", \"docid\": 215629840, \"progress\": \"\u8bbe\u8ba1\u9636\u6bb5\"}, {\"mdate\": \"2022-09-02 00:00:00\", \"id\": 4756009811399671809, \"progress_remark\": \"2022-08-08,\u9879\u76ee\u5904\u4e8e\u65bd\u5de5\u5728\u5efa\u7684\u62db\u6807\u9636\u6bb5\uff1b\u622a\u6b622022-09-02\", \"version\": \"\u8ddf\u8fdb3\", \"crtime\": \"2023-05-16 10:05:01\", \"docid\": 258632899, \"progress\": \"\u65bd\u5de5\u5728\u5efa\"}, {\"mdate\": \"2022-09-06 00:00:00\", \"id\": 4756009811647135745, \"progress_remark\": \"\u622a\u6b622022-09-06\", \"version\": \"\u8ddf\u8fdb4\", \"crtime\": \"2023-05-16 10:05:01\", \"docid\": 264577762, \"progress\": \"\u65bd\u5de5\u5728\u5efa\"}, {\"mdate\": \"2022-09-08 00:00:00\", \"id\": 4756009812041400321, \"progress_remark\": \"\u622a\u6b622022-09-08,\u9879\u76ee\u5904\u4e8e\u65bd\u5de5\u5728\u5efa\uff1b\", \"version\": \"\u8ddf\u8fdb5\", \"crtime\": \"2023-05-16 10:05:01\", \"docid\": 265407137, \"progress\": \"\u65bd\u5de5\u5728\u5efa\"}, {\"mdate\": \"2023-05-15 00:00:00\", \"id\": 4756009812372750337, \"progress_remark\": \"2023-05-15,\u9879\u76ee\u5904\u4e8e\u8bbe\u8ba1\u9636\u6bb5\u7684\u62db\u6807\u9636\u6bb5\uff1b\u622a\u6b622023-05-15\u786e\u5b9a\u8bbe\u8ba1\u9636\u6bb5\u4e2d\u6807\u4eba\u4e3a\u6cb3\u5317\u94ed\u9510\u5de5\u7a0b\u54a8\u8be2\u6709\u9650\u516c\u53f8\", \"version\": \"\u8ddf\u8fdb6\", \"crtime\": \"2023-05-16 10:05:01\", \"docid\": 323337928, \"progress\": \"\u8bbe\u8ba1\u9636\u6bb5\"}]", "update_status": "0", "ordinary_name": "\u6587\u5b89\u53bf\u57ce\u5e02\u5efa\u8bbe\u53d1\u5c55\u6709\u9650\u516c\u53f8\u7efc\u5408\u4fbf\u6c11\u670d\u52a1\u4e2d\u5fc3\u9879\u76ee", "floor_space": "\u603b\u5efa\u7b51\u9762\u79ef50477.17\u5e73\u65b9\u7c73", "project_follow": "", "project_address": "", "area": "\u5eca\u574a", "progress": "\u8bbe\u8ba1\u9636\u6bb5", "spids": "", "endtime": "", "crtime": "2023-05-16 10:05:00", "begintime": "", "project_description": "\u9879\u76ee\u6982\u51b5\u4e0e\u62db\u6807\u8303\u56f4\uff0c2.1\u9879\u76ee\u6982\u51b5\uff1a2.1\u5de5\u7a0b\u540d\u79f0\uff1a\u6587\u5b89\u53bf\u57ce\u5e02\u5efa\u8bbe\u53d1\u5c55\u6709\u9650\u516c\u53f8\u7efc\u5408\u4fbf\u6c11\u670d\u52a1\u4e2d\u5fc3\u9879\u76ee\u8bbe\u8ba12.2\u5efa\u8bbe\u5730\u5740\uff1a\u6587\u5b89\u53bf\u4e2d\u5fc3\u57ce\u533a\u539f\u6587\u5b89\u53bf\u7b2c\u4e00\u4e2d\u5b66\u51852.3\u5de5\u7a0b\u6982\u51b5\uff1a\u672c\u9879\u76ee\u5360\u5730\u9762\u79ef49794.49\u5e73\u65b9\u7c73\uff0c\u603b\u5efa\u7b51\u9762\u79ef50477.17\u5e73\u65b9\u7c73\uff0c\u5bf9\u539f\u57409\u680b\u5efa\u7b51\u4f53\u8fdb\u884c\u5185\u5916\u90e8\u88c5\u4fee\u63d0\u5347\u6539\u9020\uff0c\u5bf9\u9053\u8def\u5de5\u7a0b\u63d0\u5347\u6539\u9020\u3002\u65b0\u5efa\u9644\u5c5e\u7528\u623f\uff0c\u914d\u5957\u76d1\u63a7\u8bbe\u5907\u53ca\u5176\u4ed6\u5ba4\u5916\u5de5\u7a0b\u7b49\uff0c\u62c6\u9664\u73b0\u6709\u90e8\u5206\u5efa\u7b51\u30022.4\u6807\u6bb5\u5212\u5206\uff1a\u4e00\u4e2a\u6807\u6bb52.5\u8bbe\u8ba1\u670d\u52a1\u671f\u9650\uff1a\u5408\u540c\u7b7e\u8ba2\u4e4b\u65e5\u8d7750\u65e5\u5386\u5929\u5185\u5b8c\u6210\u8bbe\u8ba1\u6210\u679c\u62a5\u544a2.6\u8d28\u91cf\u8981\u6c42\uff1a\u5408\u683c\uff0c\u6ee1\u8db3\u73b0\u884c\u8bbe\u8ba1\u89c4\u8303\u6807\u51c6\uff0c2.2\u62db\u6807\u8303\u56f4\uff1a\u672c\u9879\u76ee\u65bd\u5de5\u56fe\u8bbe\u8ba1\u4ee5\u53ca\u5176\u4ed6\u76f8\u5173\u670d\u52a1\u5de5\u4f5c\uff0c(\u8bf4\u660e\u672c\u6b21\u62db\u6807\u9879\u76ee\u7684\u5efa\u8bbe\u5730\u70b9\u3001\u89c4\u6a21\u3001\u8ba1\u5212\u5de5\u671f\u3001\u62db\u6807\u8303\u56f4\u3001\u6807\u6bb5\u5212\u5206", "latest_service_time": "", "covered_area": "\u603b\u5efa\u7b51\u9762\u79ef50477.17\u5e73\u65b9\u7c73", "id": 4756009812641185793, "des_project_type": "10", "partitionkey": 194, "project_type": "\u6559\u80b2\u53ca\u7814\u7a76\u5efa\u7b51", "project_code": "ZYSJZ2021-12036", "contacts": "[{\"cellphone\": \"16631127926\", \"id\": 4756009807276670977, \"company_name\": \"\u4e2d\u8054\u5408\u76db\u79d1\u6280\u6709\u9650\u516c\u53f8\", \"type\": \"\u8bbe\u8ba1\u5355\u4f4d\", \"contact_name\": \"\u738b\u7855\u51ef\"}, {\"cellphone\": \"13780515677\", \"id\": 4756009807297642497, \"company_name\": \"\u4e2d\u8054\u5408\u76db\u79d1\u6280\u6709\u9650\u516c\u53f8\", \"type\": \"\u8bbe\u8ba1\u5355\u4f4d\", \"contact_name\": \"\u79e6\u4e49\u582a\"}, {\"cellphone\": \"13931670665\", \"id\": 4756009807394111489, \"company_name\": \"\u6587\u5b89\u53bf\u57ce\u5e02\u5efa\u8bbe\u53d1\u5c55\u6709\u9650\u516c\u53f8\", \"type\": \"\u4e1a\u4e3b\u5355\u4f4d\", \"contact_name\": \"\u5f20\u7acb\u5f3a\"}, {\"cellphone\": \"13833633069\", \"id\": 4756009807415083009, \"company_name\": \"\u6587\u5b89\u53bf\u57ce\u5e02\u5efa\u8bbe\u53d1\u5c55\u6709\u9650\u516c\u53f8\", \"type\": \"\u4e1a\u4e3b\u5355\u4f4d\", \"contact_name\": \"\u738b\u52c7\"}, {\"cellphone\": \"13933924903\", \"id\": 4756009812280475649, \"company_name\": \"\u6cb3\u5317\u94ed\u9510\u5de5\u7a0b\u54a8\u8be2\u6709\u9650\u516c\u53f8\", \"type\": \"\u8bbe\u8ba1\u5355\u4f4d\", \"contact_name\": \"\u8d75\u53cc\u829d\"}, {\"cellphone\": \"13722631316\", \"id\": 4756009812314030081, \"company_name\": \"\u6cb3\u5317\u94ed\u9510\u5de5\u7a0b\u54a8\u8be2\u6709\u9650\u516c\u53f8\", \"type\": \"\u8bbe\u8ba1\u5355\u4f4d\", \"contact_name\": \"\u9ad8\u4fca\u666f\"}, {\"cellphone\": \"13476568963\", \"id\": 4756009812578271233, \"company_name\": \"\u4e2d\u5b87\u4fe1\u8fbe\u9879\u76ee\u7ba1\u7406\u6709\u9650\u516c\u53f8\", \"type\": \"\u4ee3\u7406\u5355\u4f4d\", \"contact_name\": \"\u60e0\u6811\u519b\", \"address\": \"\u77f3\u5bb6\u5e84\u5e02\u6865\u897f\u533a\u6c11\u751f\u8def66\u53f7\u5609\u9ca4\u5c0f\u533a\uff08\u5357\u533a\uff096-3-1801\"}, {\"cellphone\": \"13933026974\", \"id\": 4756009812603437057, \"company_name\": \"\u4e2d\u5b87\u4fe1\u8fbe\u9879\u76ee\u7ba1\u7406\u6709\u9650\u516c\u53f8\", \"type\": \"\u4ee3\u7406\u5355\u4f4d\", \"contact_name\": \"\u674e\u6cbb\u6cc9\", \"address\": \"\u77f3\u5bb6\u5e84\u5e02\u6865\u897f\u533a\u6c11\u751f\u8def66\u53f7\u5609\u9ca4\u5c0f\u533a\uff08\u5357\u533a\uff096-3-1801\"}]"}'

    a = designed_project(json.loads(s))
    print(a.getProperties())
