package com.persagy.server.syn.utils

import com.alibaba.fastjson.annotation.JSONField
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.persagy.server.datacenter.models.entities.assistant.AttPic
import com.persagy.server.datacenter.models.entities.assistant.TaskPic
import java.io.Serializable

/**
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/22 22:11
 */
open class ObjectBaseRequestTwo: Serializable {

    /** 对象列表 */
    var listObject = ArrayList<HashMap<String,Any?>>()
    /** 对象 */
    val objMap = LinkedHashMap<String,Any?>()
    //需要忽略的属性，数据平台中没有的信息点
    @JSONField(serialize=false)
    @Transient
    var ignoreKeySet = hashSetOf("LastUpdate", "CreateTime")

    /**
     * 添加信息点到infos
     * @param key   信息点
     * @param value 信息点值
     * @param ignoreNull    是否忽略value为null的情况，true value为null时不添加到infos
     */
    open fun addInfo(key: String, value: Any?, ignoreNull: Boolean) {
        if (!ignoreKeySet.contains(key)) {
            if (key == "pic"||key == "installDrawing"||key == "installPic"||key == "checkReport"||
                key == "insuranceFile"||key == "nameplate" || key == "drawing" || key == "archive"||
                key == "operationManual" || key == "productCertification"||key == "testReport"
                ||key == "originalCertificate"||key == "acceptanceReport"||key == "approachingAcceptance"
                ||key == "supplierContract"||key == "maintainManual"||key == "insuranceFile"){

                if (value.toString() != "{}"||value.toString()!="[]"){
                    try {
                        val gson = Gson()
                        val type = object : TypeToken<ArrayList<TaskPic>>() {
                        }.type
                        val attPicList = gson.fromJson<ArrayList<TaskPic>>(value.toString(),type)
//                        val attPicList = SJsonUtil.fromJson<ArrayList<TaskPic>>(value.toString())
                        if (attPicList.isNotEmpty()){
                            val listPic =ArrayList<AttPic>()
                            for (attPic in attPicList){
                                val taskPic = AttPic()
                                taskPic.key = attPic.key
                                taskPic.name = attPic.name
                                taskPic.systemId = attPic.systemId
                                taskPic.type = attPic.type
                                listPic.add(taskPic)
                            }
                            objMap[key] = listPic
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }

                }

            }else{
                objMap[key] = value
            }
        }

    }









}