/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

package com.persagy.server.service

import com.alibaba.fastjson.JSONArray
import com.persagy.database.SFilter
import com.persagy.database.SQueryBuilder
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.Opt
import com.persagy.server.models.PropertyEquip
import com.persagy.server.models.requests.EquipUpDateEnclosure
import com.persagy.server.models.requests.EquipUpDateEnclosureRequest
import com.persagy.server.service.base.Service
import com.persagy.server.service.objects.PropertyService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import org.slf4j.LoggerFactory

/**
 * 资产关联设备信息服务
 *
 * @author 张维新
 */
object PropertyEquipService : Service<PropertyEquip>(SMybatisDao(PropertyEquip::class.java)) {
    /** 日志 */
     val logger = LoggerFactory.getLogger(PropertyEquipService::class.java)

    /**
     * 查询之前的操作
     */
    override fun onQueryBefore(queryBuilder: SQueryBuilder<PropertyEquip>) {
        queryBuilder.tableName = "v_property_equip"
        return
    } // Function onQueryBefore()


    /**
     * 修改设备的附件,增量方式
     */
    fun equipUpDateEnclosurs(equipUpDateEnclosureRequest: EquipUpDateEnclosureRequest<EquipUpDateEnclosure>) : SBaseResponse {
        return try {
            val projectId = SPageContext.getHeader("projectId")
            val equipUpDateEnclosureList = equipUpDateEnclosureRequest.content
            if (!equipUpDateEnclosureList.isNullOrEmpty()){
                for (equipUpDateEnclosure in equipUpDateEnclosureList){
                    /** id不能为空 */
                    if (equipUpDateEnclosure.equipId.isNullOrEmpty()){
                        val sBaseResponse = SBaseResponse(SResponseType.failure)
                        sBaseResponse.message = "设备id不能为空"
                        sBaseResponse
                    }
                    val equip = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("id", equipUpDateEnclosure.equipId!!)).entity()

                    if (equip == null){

                    }

                    if (!equipUpDateEnclosure.pic.isNullOrEmpty()){
                        if (!equip!!.infos.isNullOrEmpty()) {
                            val containsKey1 = equip!!.infos!!.containsKey(Opt.PIC)
                            if (containsKey1) {
                                val any = equip!!.infos!![Opt.PIC] as JSONArray
                                any.addAll(equipUpDateEnclosure.pic!!)
                                equip!!.infos!![Opt.PIC] = any
                            } else {
                                equip!!.infos!![Opt.PIC] = equipUpDateEnclosure.pic!!
                            }
                        }
                    }
                    if (!equipUpDateEnclosure.archive.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.ARCHIVE)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.ARCHIVE] as JSONArray
                            any.addAll(equipUpDateEnclosure.archive!!)
                            equip!!.infos!![Opt.ARCHIVE] = any
                        } else {
                            equip!!.infos!![Opt.ARCHIVE] = equipUpDateEnclosure.archive!!
                        }
                    }



                    if (!equipUpDateEnclosure.checkReport.isNullOrEmpty()){

                        val containsKey1 = equip!!.infos!!.containsKey(Opt.CHECKREPORT)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.CHECKREPORT] as JSONArray
                            any.addAll(equipUpDateEnclosure.checkReport!!)
                            equip!!.infos!![Opt.CHECKREPORT] = any
                        } else {
                            equip!!.infos!![Opt.CHECKREPORT] = equipUpDateEnclosure.checkReport
                        }
                    }
                    if (!equipUpDateEnclosure.drawing.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.DRAWING)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.DRAWING] as JSONArray
                            any.addAll(equipUpDateEnclosure.drawing!!)
                            equip!!.infos!![Opt.DRAWING] = any
                        } else {
                            equip!!.infos!![Opt.DRAWING] = equipUpDateEnclosure.drawing
                        }
                    }
                    if (!equipUpDateEnclosure.installDrawing.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.INSTALLDRAWING)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.INSTALLDRAWING] as JSONArray
                            any.addAll(equipUpDateEnclosure.installDrawing!!)
                            equip!!.infos!![Opt.INSTALLDRAWING] = any
                        } else {
                            equip!!.infos!![Opt.INSTALLDRAWING]
                        }
                    }
                    if (!equipUpDateEnclosure.installPic.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.INSTALLPIC)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.INSTALLPIC] as JSONArray
                            any.addAll(equipUpDateEnclosure.installPic!!)
                            equip!!.infos!![Opt.INSTALLPIC] = any
                        } else {
                            equip!!.infos!![Opt.INSTALLPIC] = equipUpDateEnclosure.installPic
                        }
                    }

                    if (!equipUpDateEnclosure.insuranceFile.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.INSURANCEFILE)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.INSURANCEFILE] as JSONArray
                            any.addAll(equipUpDateEnclosure.insuranceFile!!)
                        } else {
                            equip!!.infos!![Opt.INSURANCEFILE] = equipUpDateEnclosure.insuranceFile
                        }
                    }
                    if (!equipUpDateEnclosure.nameplate.isNullOrEmpty()){

                        val containsKey1 = equip!!.infos!!.containsKey(Opt.NAMEPLATE)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.NAMEPLATE] as JSONArray
                            any.addAll(equipUpDateEnclosure.nameplate!!)
                        } else {
                            equip!!.infos!![Opt.NAMEPLATE] = equipUpDateEnclosure.nameplate
                        }
                    }
                    /** 操作说明书 */
                    if (!equipUpDateEnclosure.operationManual.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.OPERATIONMANUAL)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.OPERATIONMANUAL] as JSONArray
                            any.addAll(equipUpDateEnclosure.operationManual!!)
                        } else {
                            equip!!.infos!![Opt.OPERATIONMANUAL] = equipUpDateEnclosure.operationManual
                        }
                    }
                    /** 产品合格证 */
                    if (!equipUpDateEnclosure.productCertification.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.PRODUCTCERTIFICATION)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.PRODUCTCERTIFICATION] as JSONArray
                            any.addAll(equipUpDateEnclosure.productCertification!!)
                        } else {
                            equip!!.infos!![Opt.PRODUCTCERTIFICATION] = equipUpDateEnclosure.productCertification
                        }
                    }
                    /** 维修保养手册 */
                    if (!equipUpDateEnclosure.maintainManual.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.MAINTAINMANUAL)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.MAINTAINMANUAL] as JSONArray
                            any.addAll(equipUpDateEnclosure.maintainManual!!)
                        } else {
                            equip!!.infos!![Opt.MAINTAINMANUAL] = equipUpDateEnclosure.maintainManual
                        }
                    }
                    /** 安装说明书 */
                    if (!equipUpDateEnclosure.installInstruction.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.INSTALLINSTRUCTION)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.INSTALLINSTRUCTION] as JSONArray
                            any.addAll(equipUpDateEnclosure.installInstruction!!)
                        } else {
                            equip!!.infos!![Opt.INSTALLINSTRUCTION] = equipUpDateEnclosure.installInstruction
                        }
                    }
                    /** 验收报告 */
                    if (!equipUpDateEnclosure.acceptanceReport.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.ACCEPTANCEREPORT)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.ACCEPTANCEREPORT] as JSONArray
                            any.addAll(equipUpDateEnclosure.acceptanceReport!!)
                        } else {
                            equip!!.infos!![Opt.ACCEPTANCEREPORT] = equipUpDateEnclosure.acceptanceReport
                        }
                    }
                    /** 进场验收单 */
                    if (!equipUpDateEnclosure.approachingAcceptance.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.APPROACHINGACCEPTANCE)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.APPROACHINGACCEPTANCE] as JSONArray
                            any.addAll(equipUpDateEnclosure.approachingAcceptance!!)
                        } else {
                            equip!!.infos!![Opt.APPROACHINGACCEPTANCE] = equipUpDateEnclosure.approachingAcceptance
                        }
                    }
                    /** 供应合同 */
                    if (!equipUpDateEnclosure.supplierContract.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.SUPPLIERCONTRACT)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.SUPPLIERCONTRACT] as JSONArray
                            any.addAll(equipUpDateEnclosure.supplierContract!!)
                        } else {
                            equip!!.infos!![Opt.SUPPLIERCONTRACT] = equipUpDateEnclosure.supplierContract
                        }
                    }
                    /** 检测报告 */
                    if (!equipUpDateEnclosure.testReport.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.TESTREPORT)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.TESTREPORT] as JSONArray
                            any.addAll(equipUpDateEnclosure.testReport!!)
                        } else {
                            equip!!.infos!![Opt.TESTREPORT] = equipUpDateEnclosure.testReport
                        }
                    }

                    /** 原厂证明 */
                    if (!equipUpDateEnclosure.originalCertificate.isNullOrEmpty()){
                        val containsKey1 = equip!!.infos!!.containsKey(Opt.ORIGINALCERTIFICATE)
                        if (containsKey1) {
                            val any = equip!!.infos!![Opt.ORIGINALCERTIFICATE] as JSONArray
                            any.addAll(equipUpDateEnclosure.originalCertificate!!)
                        } else {
                            equip!!.infos!![Opt.ORIGINALCERTIFICATE] = equipUpDateEnclosure.originalCertificate
                        }
                    }

                    /** 更新设备 */
                    PropertyService.update(equip!!,null, SFilter.eq("projectId",projectId!!), SFilter.eq("id",equipUpDateEnclosure.equipId!!))
                }

                try {
                    val jsonObject = org.json.JSONObject()
                    jsonObject.put("type", "7")
                    jsonObject.put("projectId", projectId)
                    val jsonArray = org.json.JSONArray()
                    for (entity in equipUpDateEnclosureList) {
                        val jsonInfos = org.json.JSONObject()
                        jsonInfos.put("id", entity.equipId)
                        jsonArray.put(jsonInfos)
                    }
                    jsonObject.put("infos", jsonArray)

                    /** 发送消息 */
                    messageHandler.sendMessage(jsonObject.toString())
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                val sBaseResponse = SBaseResponse(SResponseType.success)
                sBaseResponse
            }else{
                val sBaseResponse = SBaseResponse(SResponseType.failure)
                sBaseResponse.message = "不能传入空值"
                sBaseResponse
            }
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure)
        }
    } //

} // Object PropertyService
