package cn.muchinfo.rma.business.commodity.adapter

import cn.muchinfo.rma.business.global.MessageHeadModel
import cn.muchinfo.rma.global.ErrorMessageUtils
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.netcore.packet.Packet50
import cn.muchinfo.rma.protobuf.funcode.FunCode
import cn.muchinfo.rma.protobuf.protoclasses.ManageServiceMI2
import java.io.ByteArrayOutputStream
import java.lang.Exception

/**
 * 商品信息相关装解箱
 */
object CommodityAdapter {

    /**
     * 现货品种申请请求报文装箱
     * @param deliverygoodscode String 交割商品代码(新增时有值)
     * @param deliverygoodsname String 交割商品名称(新增时有值)
     * @param wrstandardid Long 现货商品ID(修改时有值)
     * @param unitid Long 单位ID
     * @param type Int 类型 1 新增 2 修改
     * @param remark String 备注
     * @param spotgoodsmodels List<SpotGoodsModelEx> SpotGoodsModelEx 现货商品型号数据
     * @param spotgoodsbrands List<SpotGoodsBrandEx> SpotGoodsBrandEx 现货商品品牌数据
     * @param wrsconvertdetails List<WRSConvertDetailEx> WRSConvertDetailEx 现货商品折算配置明细数据
     */
    fun getGoodsApplyReqInfo(
        deliverygoodscode: String = "",
        deliverygoodsname: String = "",
        wrstandardid: Long = 0,
        unitid: Long = 0,
        type: Int = 0,
        remark: String = "",
        spotgoodsmodels: List<ManageServiceMI2.SpotGoodsModelEx> = arrayListOf(),
        spotgoodsbrands: List<ManageServiceMI2.SpotGoodsBrandEx> = arrayListOf(),
        wrsconvertdetails: List<ManageServiceMI2.WRSConvertDetailEx> = arrayListOf()
    ): Packet50 {
        val builder = ManageServiceMI2.DeliveryGoodsApplyReq.newBuilder()
        if (type == 1){
            builder.deliverygoodscode = deliverygoodscode
            builder.deliverygoodsname = deliverygoodsname
        }
        builder.wrstandardid = wrstandardid
        builder.type = type
        builder.unitid = unitid
        val loginInfo = GlobalDataCollection.instance?.loginRsp!!
        // FIXME: - 250000000005
        builder.setHeader(
            MessageHeadModel.getHead(
                FunCode.DeliveryGoodsApplyReq,
                loginInfo.userID,
                GlobalDataCollection.instance?.accountId ?: 0,
                0,
                18
            )
        )
        builder.loginid = loginInfo.loginID
        builder.userid = loginInfo.userID.toLong()
        builder.remark = remark
        builder.addAllSpotgoodsbrands(spotgoodsbrands)
        builder.addAllSpotgoodsmodels(spotgoodsmodels)
        builder.addAllWrsconvertdetails(wrsconvertdetails)

        val arrayOutputStream = ByteArrayOutputStream();
        builder.build().writeTo(arrayOutputStream)
        return Packet50(FunCode.DeliveryGoodsApplyReq, arrayOutputStream.toByteArray())
    }

    /**
     * 现货品种申请请求回应报文解析
     * @param packet50 Packet50
     * @return Triple<Boolean, Error?, ManageServiceMI2.DeliveryGoodsApplyRsp?>
     */
    fun analysisGoodsApplyRsq(packet50: Packet50): Triple<Boolean, Error?, ManageServiceMI2.DeliveryGoodsApplyRsp?> {
        return try {
            val loginRsp = ManageServiceMI2.DeliveryGoodsApplyRsp.parseFrom(packet50.content)
            if (loginRsp.retCode == 0) {
                // 操作成功
                Triple(true, null, loginRsp)
            } else {
                // 操作失败
                if (loginRsp.retCode == -1){
                    Triple(false, Error(loginRsp.retDesc), null)
                }else{
                    Triple(false, Error(ErrorMessageUtils.getErrorString(loginRsp.retCode)), null)
                }
            }
        } catch (e: Exception) {
            // 操作失败
            Triple(false, Error("装箱失败"), null)
        }
    }

    /**
     * 现货品种停用请求报文装箱
     * @param wrstandardid Long 现货商品ID
     * @param isvalid Int 是否有效 - 0:无效 1:有效
     */
    fun getGoodsCancelReqInfo(
        wrstandardid: Long = 0,
        isvalid: Int = 0
    ): Packet50 {
        val builder = ManageServiceMI2.DeliveryGoodsCancelReq.newBuilder()
        builder.wrstandardid = wrstandardid
        builder.isvalid = isvalid
        val loginInfo = GlobalDataCollection.instance?.loginRsp!!
        // FIXME: - 250000000005
        builder.setHeader(
            MessageHeadModel.getHead(
                FunCode.DeliveryGoodsCancelReq,
                loginInfo.userID,
                GlobalDataCollection.instance?.accountId ?: 0,
                0,
                18
            )
        )
        val arrayOutputStream = ByteArrayOutputStream();
        builder.build().writeTo(arrayOutputStream)
        return Packet50(FunCode.DeliveryGoodsCancelReq, arrayOutputStream.toByteArray())
    }

    /**
     * 现货品种停用请求回应报文解析
     * @param packet50 Packet50
     * @return Triple<Boolean, Error?, ManageServiceMI2.DeliveryGoodsCancelRsp?>
     */
    fun analysisGoodsCancelRsq(packet50: Packet50): Triple<Boolean, Error?, ManageServiceMI2.DeliveryGoodsCancelRsp?> {
        return try {
            val loginRsp = ManageServiceMI2.DeliveryGoodsCancelRsp.parseFrom(packet50.content)
            if (loginRsp.retCode == 0) {
                // 操作成功
                Triple(true, null, loginRsp)
            } else {
                // 操作失败
                if (loginRsp.retCode == -1){
                    Triple(false, Error(loginRsp.retDesc), null)
                }else{
                    Triple(false, Error(ErrorMessageUtils.getErrorString(loginRsp.retCode)), null)
                }
            }
        } catch (e: Exception) {
            // 操作失败
            Triple(false, Error("装箱失败"), null)
        }
    }

    /**
     * 套保品种新增请求报文装箱
     * @param middlegoodsname String 套保品种名称
     * @param middlegoodscode String 套保品种代码
     * @param goodsunitid Int 单位ID
     * @param relatedgoodsid Long 关联交易商品ID
     * @param evaluateratio Double 估价系数
     * @param qtydecimalplace Int 数量小数位
     * @param creatorid Long 创建人
     * @param modifierid Long 修改人
     * @param relatedgoodstype Int 关联商品类型 - 1:期货合约 2:现货品种
     * @param needhedgeratio Double 套保比率
     * @param areauserid Long 机构用户ID
     * @param goodsgroupid Long 关联期货品种ID
     * @param remark String 备注
     */
    fun getErmsMiddelGoodsReqInfo(
        middlegoodsname: String = "",
        middlegoodscode: String = "",
        goodsunitid: Int = 0,
        relatedgoodsid: Long = 0,
        evaluateratio: Double = 0.0,
        qtydecimalplace: Int = 0,
        creatorid: Long = 0,
        modifierid: Long = 0,
        relatedgoodstype: Int = 0,
        needhedgeratio: Double = 0.0,
        areauserid: Long = 0,
        goodsgroupid: Long = 0,
        remark: String = ""
    ): Packet50 {
        val builder = ManageServiceMI2.ErmsMiddelGoodsReq.newBuilder()
        if (middlegoodscode.isNotEmpty()) {
            builder.middlegoodscode = middlegoodscode
        }
        if (middlegoodsname.isNotEmpty()) {
            builder.middlegoodsname = middlegoodsname
        }
        if (goodsunitid != 0) {
            builder.goodsunitid = goodsunitid
        }
        if (relatedgoodsid != 0L) {
            builder.relatedgoodsid = relatedgoodsid
        }
        if (evaluateratio != 0.0) {
            builder.evaluateratio = evaluateratio
        }
        if (qtydecimalplace != 0) {
            builder.qtydecimalplace = qtydecimalplace
        }
        if (creatorid != 0L) {
            builder.creatorid = creatorid
        }
        if (modifierid != 0L) {
            builder.modifierid = modifierid
        }
        if (relatedgoodstype != 0) {
            builder.relatedgoodstype = relatedgoodstype
        }
        if (needhedgeratio != 0.0) {
            builder.needhedgeratio = needhedgeratio
        }
        if (areauserid != 0L) {
            builder.areauserid = areauserid
        }
        if (goodsgroupid != 0L) {
            builder.goodsgroupid = goodsgroupid
        }
        builder.remark = remark

        val loginInfo = GlobalDataCollection.instance?.loginRsp!!
        // FIXME: - 250000000005
        builder.setHeader(
            MessageHeadModel.getHead(
                FunCode.ErmsMiddelGoodsReq,
                loginInfo.userID,
                GlobalDataCollection.instance?.accountId ?: 0,
                0,
                18
            )
        )
        val arrayOutputStream = ByteArrayOutputStream();
        builder.build().writeTo(arrayOutputStream)
        return Packet50(FunCode.ErmsMiddelGoodsReq, arrayOutputStream.toByteArray())
    }

    /**
     * 套保品种新增请求回应报文解析
     * @param packet50 Packet50
     * @return Triple<Boolean, Error?, ManageServiceMI2.DeliveryGoodsCancelRsp?>
     */
    fun analysisErmsMiddelGoodsRsq(packet50: Packet50): Triple<Boolean, Error?, ManageServiceMI2.ErmsMiddelGoodsRsp?> {
        return try {
            val loginRsp = ManageServiceMI2.ErmsMiddelGoodsRsp.parseFrom(packet50.content)
            if (loginRsp.retCode == 0) {
                // 操作成功
                Triple(true, null, loginRsp)
            } else {
                // 操作失败
                if (loginRsp.retCode == -1){
                    Triple(false, Error(loginRsp.retDesc), null)
                }else{
                    Triple(false, Error(ErrorMessageUtils.getErrorString(loginRsp.retCode)), null)
                }
            }
        } catch (e: Exception) {
            // 操作失败
            Triple(false, Error("装箱失败"), null)
        }
    }

    /**
     * 套保品种修改请求报文装箱
     * @param middlegoodsid Long 套保品种ID
     * @param goodsunitid Int 单位ID
     * @param relatedgoodsid Long 关联交易商品ID
     * @param evaluateratio Double 估价系数
     * @param qtydecimalplace Int 数量小数位
     * @param modifierid Long 修改人
     * @param relatedgoodstype Int 关联商品类型 - 1:期货合约 2:现货品种
     * @param needhedgeratio Double 套保比率
     * @param areauserid Long 机构用户ID
     * @param goodsgroupid Long 关联期货品种ID
     * @param remark String 备注
     * @return Packet50
     */
    fun getErmsMiddelGoodsEditReqinfo(
        middlegoodsid: Long = 0,
        goodsunitid: Int = 0,
        relatedgoodsid: Long = 0,
        evaluateratio: Double = 0.0,
        qtydecimalplace: Int = 0,
        modifierid: Long = 0,
        relatedgoodstype: Int = 0,
        needhedgeratio: Double = 0.0,
        areauserid: Long = 0,
        goodsgroupid: Long = 0,
        remark: String = ""
    ): Packet50 {
        val builder = ManageServiceMI2.ErmsMiddelGoodsEditReq.newBuilder()
        if (middlegoodsid != 0L) {
            builder.middlegoodsid = middlegoodsid
        }
        if (goodsunitid != 0) {
            builder.goodsunitid = goodsunitid
        }
        if (relatedgoodsid != 0L) {
            builder.relatedgoodsid = relatedgoodsid
        }
        if (evaluateratio != 0.0) {
            builder.evaluateratio = evaluateratio
        }
        if (qtydecimalplace != 0) {
            builder.qtydecimalplace = qtydecimalplace
        }
        if (modifierid != 0L) {
            builder.modifierid = modifierid
        }
        if (relatedgoodstype != 0) {
            builder.relatedgoodstype = relatedgoodstype
        }
        if (needhedgeratio != 0.0) {
            builder.needhedgeratio = needhedgeratio
        }
        if (areauserid != 0L) {
            builder.areauserid = areauserid
        }
        if (goodsgroupid != 0L) {
            builder.goodsgroupid = goodsgroupid
        }
        builder.remark = remark
        val loginInfo = GlobalDataCollection.instance?.loginRsp!!
        // FIXME: - 250000000005
        builder.setHeader(
            MessageHeadModel.getHead(
                FunCode.ErmsMiddelGoodsEditReq,
                loginInfo.userID,
                GlobalDataCollection.instance?.accountId ?: 0,
                0,
                18
            )
        )
        val arrayOutputStream = ByteArrayOutputStream();
        builder.build().writeTo(arrayOutputStream)
        return Packet50(FunCode.ErmsMiddelGoodsEditReq, arrayOutputStream.toByteArray())
    }

    /**
     * 套保品种修改请求回应报文解析
     * @param packet50 Packet50
     * @return Triple<Boolean, Error?, ManageServiceMI2.DeliveryGoodsCancelRsp?>
     */
    fun analysisErmsMiddelGoodsEditRsq(packet50: Packet50): Triple<Boolean, Error?, ManageServiceMI2.ErmsMiddelGoodsEditRsp?> {
        return try {
            val loginRsp = ManageServiceMI2.ErmsMiddelGoodsEditRsp.parseFrom(packet50.content)
            if (loginRsp.retCode == 0) {
                // 操作成功
                Triple(true, null, loginRsp)
            } else {
                // 操作失败
                Triple(false, Error(ErrorMessageUtils.getErrorString(loginRsp.retCode)), null)
            }
        } catch (e: Exception) {
            // 操作失败
            Triple(false, Error("装箱失败"), null)
        }
    }

    /**
     * 套保品种停用请求报文装箱
     * @param middlegoodsid Long
     * @param isvalid Int
     * @param modifierid Long
     */
    fun getErmsMiddelGoodsStopReqInfo(
        middlegoodsid: Long = 0,
        isvalid: Int = 0,
        modifierid: Long = 0
    ): Packet50 {
        val builder = ManageServiceMI2.ErmsMiddelGoodsStopReq.newBuilder()
        builder.middlegoodsid = middlegoodsid
        builder.isvalid = isvalid
        builder.modifierid = modifierid
        val loginInfo = GlobalDataCollection.instance?.loginRsp!!
        // FIXME: - 250000000005
        builder.setHeader(
            MessageHeadModel.getHead(
                FunCode.ErmsMiddelGoodsStopReq,
                loginInfo.userID,
                GlobalDataCollection.instance?.accountId ?: 0,
                0,
                18
            )
        )
        val arrayOutputStream = ByteArrayOutputStream();
        builder.build().writeTo(arrayOutputStream)
        return Packet50(FunCode.ErmsMiddelGoodsStopReq, arrayOutputStream.toByteArray())
    }


    /**
     * 套保品种停用请求回应报文解析
     * @param packet50 Packet50
     * @return Triple<Boolean, Error?, ManageServiceMI2.DeliveryGoodsCancelRsp?>
     */
    fun analysisErmsMiddelGoodsStopRsq(packet50: Packet50): Triple<Boolean, Error?, ManageServiceMI2.ErmsMiddelGoodsStopRsp?> {
        return try {
            val loginRsp = ManageServiceMI2.ErmsMiddelGoodsStopRsp.parseFrom(packet50.content)
            if (loginRsp.retCode == 0) {
                // 操作成功
                Triple(true, null, loginRsp)
            } else {
                // 操作失败
                Triple(false, Error(ErrorMessageUtils.getErrorString(loginRsp.retCode)), null)
            }
        } catch (e: Exception) {
            // 操作失败
            Triple(false, Error("装箱失败"), null)
        }
    }


}