# -*- coding: utf-8 -*-
import os
import sys
import log
import uuid
import utils
import json
import copy
import io
import gc
# import importlib
import appconfig
import zbmx.modelFlow
import zbmx.arcTools
import zbmx.resultTools
import arcpy
import db.postgresql as db
from arcpy import env
from db.postgresql import Postgresql
import importlib, sys

importlib.reload(sys)


class ModelBuider:
    def __init__(self, data):
        # 数据库
        self.model = data
        self.db = Postgresql(appconfig.DB_CONN)

        self.load()
        time = utils.getNowTimeStr();

        gdbName = "{0}_{1}.gdb".format(time, data["bsm"]);
        self.root = os.path.dirname(os.path.abspath(__file__))
        self.path = os.path.join(self.root, "out");
        self.outGdb = os.path.join(self.path, gdbName)
        if not os.path.exists(self.path):
            os.makedirs(self.path)
        arcpy.CreateFileGDB_management(self.path, gdbName)

        # 输出文件夹里面已经有内容的，就覆盖掉
        env.overwriteOutput = True
        # 设置工作空间
        # env.workspace = appconfig.SDE["KJGH_QY"]

    def load(self):
        '''加载数据-行政区'''
        sql = "SELECT substr(XZQDM,0,6) as \"XZQDM\",XZQMC as \"XZQMC\" FROM KJGH.XZQXS";
        if self.model['xzqbm'] != '' and self.model['xzqbm'] != None:
            sql += " WHERE substr(XZQDM,0,6) LIKE '{0}%'".format(self.model['xzqbm'][0:6])
        xzq = self.db.query(sql)
        if self.model['xzqbm'] == '':
            xzqds = {}
            xzqds["XZQDM"] = "1506"
            xzqds["XZQMC"] = "鄂尔多斯市"
            xzq.append(xzqds)

        self.xzq = xzq

    def initData(self):
        '''清空数据'''
        temp = utils.toJson(self.model["mxcs"])
        self.nodes = temp["nodeDataArray"]  # 节点
        self.links = temp["linkDataArray"]  # 关系

    def run(self):
        '''执行'''
        log.info('开始')
        self.runLog("info", "开始执行模型运算")
        self.xzqbm = '1506'
        self.xzqmc = '鄂尔多斯市'

        # 先把市的算出来，然后在分
        log.info("然后在分")
        self.initData()
        self.runLog("info", "初始化数据")
        log.info("初始化数据")
        self.xzqgl()
        log.info("xzqgl")
        self.analysis()
        log.info("analysis")
        self.result()
        log.info("result")
        self.xzjs()

        log.info('结束')
        print("####OK####" + self.outGdb)
        self.db.close()

    def xzjs(self):
        self.runLog("info", "按行政区进行分别统计结果")
        for x in self.xzq:
            self.xzqbm = x["XZQDM"]
            self.xzqmc = x["XZQMC"]
            self.runLog("info", self.xzqbm + "统计结果")
            log.info("行政区>>>" + self.xzqbm)
            inList = [{
                "key": self.xzqbm,
                "text": "行政区",
                "category": "sjy",
                "sjylx": "KJGH",
                "sjy": "XZQXS"
            }]
            comObj = {
                "key": "xzq",
                "text": "条件筛选",
                "category": "jsfs",
                "tool": "Select_analysis",
                "gltj": "XZQDM LIKE '{0}%'".format(self.xzqbm)
            }
            xzq = arcTools.Select_analysis(inList, comObj, self.outGdb, self.xzqbm)
            xzqObj = {
                "category": "sjy_xzq",
                "key": "xzq",
                "sjy": xzq,
            }
            # 把所有记算的数据切一下
            scfs = filter(modelFlow.filterScfs, self.nodes)
            for comObj in scfs:
                lins = modelFlow.getLink(comObj["key"], self.links)
                inList = []
                for x in lins:
                    node = copy.deepcopy(modelFlow.getKey(self.nodes, x))
                    node["sjy"] = arcTools.Intersect_analysis([node, xzqObj], None, self.outGdb, self.xzqbm)
                    inList.append(node)

                # zb = resultTools.Area_proportion(inList, comObj, self.outGdb)
                zb = eval("resultTools.{0}".format(comObj["tool"]))(inList, comObj, self.outGdb, self.xzqbm)
                gc.collect()
                # 入库
                self.insertdb(zb)

    def xzqgl(self):
        '''行政区过滤'''
        sjy = filter(modelFlow.filterSjy, self.nodes)
        inList = [{
            "key": self.xzqbm,
            "text": "行政区",
            "category": "sjy",
            "sjylx": "KJGH",
            "sjy": "XZQXS"
        }]
        comObj = {
            "key": "xzq",
            "text": "条件筛选",
            "category": "jsfs",
            "tool": "Select_analysis",
            "gltj": "XZQDM LIKE '{0}%'".format(self.xzqbm)
        }
        xzq = arcTools.Select_analysis(inList, comObj, self.outGdb, self.xzqbm)
        for in_node in sjy:
            self.runLog("info", "行政区过滤{0}数据源".format(in_node["text"]))
            # 有条件先过滤
            if in_node["gltj"] != "":
                in_node['sjy'] = arcTools.Select_analysis([in_node], {"gltj": in_node["gltj"]}, self.outGdb, self.xzqbm)
                in_node['category'] = "sjy_gl"

            in2 = [
                in_node,
                {
                    "category": "sjy_xzq",
                    "key": "xzq",
                    "sjy": xzq,
                }
            ]
            in_node['sjy'] = arcTools.Intersect_analysis(in2, None, self.outGdb, self.xzqbm)
            in_node['category'] = "sjy_xzq"
            in_node['xzq'] = xzq

    def analysis(self):
        '''计算分析'''
        jsfs = filter(modelFlow.filterJsfs, self.nodes)
        tempOut = []
        index = 0
        while True:
            if len(jsfs) == 0:
                break
            comObj = jsfs[index]
            lins = modelFlow.getLink(comObj["key"], self.links)
            inList = []
            isRun = True
            indata = ""
            for x in lins:
                node = modelFlow.getKey(self.nodes, x)
                if node['category'] == 'sjy' or node.get('sjy') != None:
                    inList.append(node)
                    indata += node["text"] + "、"
                else:
                    isRun = False
                    break
            if isRun:
                self.runLog("info", "{0}执行{1}分析".format(indata, comObj["text"]))
                data = eval("arcTools.{0}".format(comObj["tool"]))(inList, comObj, self.outGdb, self.xzqbm)  # 空间分析
                comObj["sjy"] = data
                del jsfs[index]
                index = 0
            else:
                index += 1

    def result(self):
        '''结果输出'''
        scfs = filter(modelFlow.filterScfs, self.nodes)
        for comObj in scfs:
            lins = modelFlow.getLink(comObj["key"], self.links)
            inList = []
            for x in lins:
                node = modelFlow.getKey(self.nodes, x)
                inList.append(node)

            self.runLog("info", "模型进行" + comObj["text"])
            zb = eval("resultTools.{0}".format(comObj["tool"]))(inList, comObj, self.outGdb, self.xzqbm)
            # 入库
            self.insertdb(zb)

    def insertdb(self, jcz):
        '''入库'''
        self.runLog("info", "成果数据入库")
        sql = "INSERT INTO onemap.T_ZBMX_ZBJCZ(BSM,ZBBH,XZQDM,XZQMC,JCSJ,JCZ,JCYH,TJSJ,JCN,JCY) VALUES ('{0}','{1}','{2}','{3}',to_date('{4}', 'yyyy-mm-dd hh24:mi:ss'),{5},'{6}',to_date('{7}', 'yyyy-mm-dd hh24:mi:ss'),{8},{9})"
        dtime = utils.getNowTime()
        sql = sql.format('{0}_{1}'.format(self.model["bsm"], utils.getNowTimeStr()),
                         self.model["zbbh"],
                         self.xzqbm.ljust(6, '0'),
                         self.xzqmc,
                         utils.getTimeStr(dtime, "%Y-%m-%d %H:%M:%S"),
                         jcz,
                         'sys',
                         utils.getTimeStr(dtime, "%Y-%m-%d %H:%M:%S"),
                         utils.getTimeStr(dtime, "%Y"),
                         utils.getTimeStr(dtime, "%m")
                         )
        self.db.insert(sql)

    def runLog(self, rzlx, rzlr):
        sql = "INSERT INTO onemap.T_ZBMX_MXJSRWRZ(BSM,MXBSM,MXMC,RWBSM,RZLX,RZSJ,RZLR) VALUES (SEQ_T_ZBMX_MXJSRWRZ.NEXTVAL,'{0}', '{1}', '{2}', '{3}', TO_DATE('{4}', 'SYYYY-MM-DD HH24:MI:SS'), '{5}')"
        sql = sql.format(self.model["mxbsm"],
                         self.model["mxmc"],
                         self.model["bsm"],
                         rzlx,
                         utils.getNowTimeStr("%Y-%m-%d %H:%M:%S"),
                         rzlr
                         )
        log.info(sql)
        self.db.insert(sql)
