import arcpy
import sqlite3
 
aprx = arcpy.mp.ArcGISProject(r"CURRENT")
m = aprx.activeMap
groupLayer = arcpy.GetParameterAsText(0)
lyrs = m.listLayers()
 
stylx_path = arcpy.GetParameterAsText(1)
 
stylxDB = sqlite3.connect(stylx_path)
 
cursor = stylxDB.cursor()
 
category_switch = {4: "一级类",
                   5: "二级类",
                   6: "三级类", }
 
class_switch = {"Point": 3,
                "Polyline": 4,
                "Polygon": 5}
 
 
for lyr in lyrs:
    longName = lyr.longName
    if longName.startswith(groupLayer) and lyr.isGroupLayer == False:
        type = arcpy.Describe(lyr).featureClass.shapeType
        classValue = class_switch.get(type)
 
        sym = lyr.symbology
        if sym.renderer.type == 'SimpleRenderer':
            try:
                cim = lyr.getDefinition("V2").renderer.symbol.symbol
                content = arcpy.cim.GetJSONForCIMObject(cim, "V2")
                name = lyr.name
                key = lyr.name
                category = category_switch.get(len(name))
 
                new_row = (classValue, category, name, content, key)
 
                cursor.execute(
                    'INSERT INTO ITEMS(CLASS, CATEGORY, NAME, CONTENT, KEY) VALUES(?,?,?,?)', new_row)
 
                stylxDB.commit()
                arcpy.AddMessage("√ "+lyr.name)
            except:
                arcpy.AddMessage("× "+lyr.name)
 
stylxDB.close()