package com.zzsmart.qomo.kn.cost.manage.vo;

import com.google.gson.Gson;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelTarget;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MaterialDiff  {
    private static final Logger logger = LoggerFactory.getLogger(MaterialDiff.class);

    @Excel(name = "物料号", width = 15,orderNum = "1")
    public  String matnr;

    @Excel(name = "物料信息", width = 15,orderNum = "2")
    public  String productname="";


    public  String layer;


    public  String version_number;


    public  String code;
    @Excel(name = "量差原因分类", width = 15,orderNum = "10")
    public  String reason;

    public  String detail;

    public  String pricecode;
    @Excel(name = "价差原因分类", width = 15,orderNum = "11")
    public String pricereason;

    public String pricedetail;

    public  String productcategory="";
    public String materialtype="";
    public String graphicnumber="";
    @Excel(name = "基于成品单位标准用量", width = 22,orderNum = "5",numFormat = "0.0000",type = 4)
    public double standarduse=0;
    @Excel(name = "标准成本", width = 15,orderNum = "6",numFormat = "0.0000",type = 4)
    public double standardcost=0;
    public double ppmng=1;//总工单生产数量
    public double meng=1;//总领用数量 单位实际用量等于 领用数量/工单生产数量
    //public double realtotalcost=0;//工单总价 平均单价=工单总价/领用数量
    @Excel(name = "基与成品单位实际用量", width = 22,orderNum = "3",numFormat = "0.0000",type = 4)
    public double realuse=0;
    @Excel(name = "实际成本", width = 15,orderNum = "4",numFormat = "0.0000",type = 4)
    public double realcost=0;
    @Excel(name = "价差", width = 15,orderNum = "7",numFormat = "0.0000",type = 4)
    public double pricediff=0;
    @Excel(name = "量差", width = 15,orderNum = "8",numFormat = "0.0000",type = 4)
    public double numdiff=0;
    @Excel(name = "总差", width = 15,orderNum = "9",numFormat = "0.0000",type = 4)
    public double totaldiff=0;
    public String parent="";
    //public double rate=1;
    //public Map<String,Double> realmap=new HashMap<>();//实际父件用量
    public Map<String,Double> stdmap=new HashMap<>();//标准父件用量

    public HiveSapZcorc hiveSapZcorc;
    //public List<HiveSapZcorc> hiveSapZcorcList=new ArrayList<>();//工单
    //public List<SrmPrice> purchasePriceList=new ArrayList<>();//价格变更记录
    public List<MaterialDiff> childs = new ArrayList<>();

    public String getMatnr() {
        return matnr;
    }

    public void setMatnr(String matnr) {
        this.matnr = matnr;
    }

    public String getProductname() {
        return productname;
    }

    public void setProductname(String productname) {
        this.productname = productname;
    }

    public String getLayer() {
        return layer;
    }

    public void setLayer(String layer) {
        this.layer = layer;
    }

    public String getVersion_number() {
        return version_number;
    }

    public void setVersion_number(String version_number) {
        this.version_number = version_number;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getPricecode() {
        return pricecode;
    }

    public void setPricecode(String pricecode) {
        this.pricecode = pricecode;
    }

    public String getPricereason() {
        return pricereason;
    }

    public void setPricereason(String pricereason) {
        this.pricereason = pricereason;
    }

    public String getPricedetail() {
        return pricedetail;
    }

    public void setPricedetail(String pricedetail) {
        this.pricedetail = pricedetail;
    }

    public String getProductcategory() {
        return productcategory;
    }

    public void setProductcategory(String productcategory) {
        this.productcategory = productcategory;
    }

    public String getMaterialtype() {
        return materialtype;
    }

    public void setMaterialtype(String materialtype) {
        this.materialtype = materialtype;
    }

    public String getGraphicnumber() {
        return graphicnumber;
    }

    public void setGraphicnumber(String graphicnumber) {
        this.graphicnumber = graphicnumber;
    }

    public double getStandarduse() {
        return standarduse;
    }

    public void setStandarduse(double standarduse) {
        this.standarduse = standarduse;
    }

    public double getStandardcost() {
        return standardcost;
    }

    public void setStandardcost(double standardcost) {
        this.standardcost = standardcost;
    }

    public double getPpmng() {
        return ppmng;
    }

    public void setPpmng(double ppmng) {
        this.ppmng = ppmng;
    }

    public double getMeng() {
        return meng;
    }

    public void setMeng(double meng) {
        this.meng = meng;
    }

    public double getRealuse() {
        return realuse;
    }

    public void setRealuse(double realuse) {
        this.realuse = realuse;
    }

    public double getRealcost() {
        return realcost;
    }

    public void setRealcost(double realcost) {
        this.realcost = realcost;
    }

    public double getPricediff() {
        return pricediff;
    }

    public void setPricediff(double pricediff) {
        this.pricediff = pricediff;
    }

    public double getNumdiff() {
        return numdiff;
    }

    public void setNumdiff(double numdiff) {
        this.numdiff = numdiff;
    }

    public double getTotaldiff() {
        return totaldiff;
    }

    public void setTotaldiff(double totaldiff) {
        this.totaldiff = totaldiff;
    }

    public String getParent() {
        return parent;
    }

    public void setParent(String parent) {
        this.parent = parent;
    }

    public Map<String, Double> getStdmap() {
        return stdmap;
    }

    public void setStdmap(Map<String, Double> stdmap) {
        this.stdmap = stdmap;
    }

    public HiveSapZcorc getHiveSapZcorc() {
        return hiveSapZcorc;
    }

    public void setHiveSapZcorc(HiveSapZcorc hiveSapZcorc) {
        this.hiveSapZcorc = hiveSapZcorc;
    }

//    public List<SrmPrice> getPurchasePriceList() {
//        return purchasePriceList;
//    }

//    public void setPurchasePriceList(List<SrmPrice> purchasePriceList) {
//        this.purchasePriceList = purchasePriceList;
//    }

    //public List<MaterialDiff> getChilds() {
    //    return childs;
    //}

    //public void setChilds(List<MaterialDiff> childs) {
    //    this.childs = childs;
   // }

    public void Cal(){
       // if(ppmng!=0){
            //realuse=meng/ppmng;
       // }
        //if(realuse>0){
        //    realcost=realtotalcost/realuse;
       // }
        //realuse=meng/ppmng;
        //realcost=realtotalcost/meng;
        pricediff = realuse * (realcost - standardcost);
        numdiff = standardcost * (realuse - standarduse);
        totaldiff = pricediff + numdiff;
    }

//    public MaterialDiff(MaterialTree materialTree){
//        matnr=materialTree.matnr;
//        layer=materialTree.layer;
//        productname=materialTree.productname;
//        version_number=materialTree.version_number;
//        standardcost=materialTree.standardcost;
//        standarduse=materialTree.standarduse;
//        realcost=materialTree.realcost;
//        realuse=materialTree.realuse;
//        pricediff=materialTree.pricediff;
//        numdiff=materialTree.numdiff;
//        totaldiff=materialTree.totaldiff;
//        code=materialTree.code;
//        reason=materialTree.reason;
//        detail=materialTree.detail;
//        productcategory=materialTree.productcategory;
//        for (NodeMatnr nodeAufnr:materialTree.nodeMatnrList){
//            hiveSapZcorcList.add(nodeAufnr.hiveSapZcorc);
//        }
//    }
    public MaterialDiff(MaterialDiff materialDiff){
        this.matnr=materialDiff.matnr;
        this.productname=materialDiff.productname;
        this.layer=materialDiff.layer;
        this.version_number=materialDiff.version_number;
        this.code=materialDiff.code;
        this.reason=materialDiff.reason;
        this.detail=materialDiff.detail;
        this.pricecode=materialDiff.pricecode;
        this.pricereason=materialDiff.pricereason;
        this.pricedetail=materialDiff.pricedetail;
        this.productcategory=materialDiff.productcategory;
        this.materialtype=materialDiff.materialtype;
        this.graphicnumber=materialDiff.graphicnumber;
        this.standarduse=materialDiff.standarduse;
        this.standardcost=materialDiff.standardcost;
        this.ppmng=materialDiff.ppmng;
        this.meng=materialDiff.meng;
        this.realuse=materialDiff.realuse;
        this.realcost=materialDiff.realcost;
        this.pricediff=materialDiff.pricediff;
        this.numdiff=materialDiff.numdiff;
        this.totaldiff=materialDiff.totaldiff;
        this.parent=materialDiff.parent;
        this.hiveSapZcorc=new HiveSapZcorc();
        //MaterialDiff materialDiff=new MaterialDiff();
        BeanUtils.copyProperties(materialDiff.hiveSapZcorc,this.hiveSapZcorc);

    }

    public MaterialDiff(StandardMatnr standardMatnr,double bomuse){
        matnr=standardMatnr.matnr;
        version_number=standardMatnr.version;
        standarduse=standardMatnr.getParentUse()*bomuse;
        standardcost=standardMatnr.stage.price;
        stdmap.put(standardMatnr.matnr,standarduse);
    }

    public MaterialDiff(NodeMatnr nodeMatnr,double _rate){
        layer=nodeMatnr.hiveSapZcorc.layer;
        matnr=nodeMatnr.hiveSapZcorc.idnrk;
        ppmng=nodeMatnr.hiveSapZcorc.rpsmng;
        meng=nodeMatnr.hiveSapZcorc.menge;
        if(meng==0){
            meng=ppmng;
        }
        //realtotalcost=nodeMatnr.hiveSapZcorc.rkst001;
        realuse=meng*_rate/ppmng;//单位用量
        realcost=nodeMatnr.hiveSapZcorc.rkst001/meng;//单价
        //realtotalcost=nodeMatnr.hiveSapZcorc.rkst001/meng;
        if(nodeMatnr.hiveSapZcorc.e_actual_material_total_cost>0){
            realcost=nodeMatnr.hiveSapZcorc.e_actual_material_total_cost/meng;
        }
        hiveSapZcorc = nodeMatnr.hiveSapZcorc;

        //MaterialDiff materialDiff=new MaterialDiff();
        //BeanUtils.copyProperties(this,materialDiff);
        Gson gson = new Gson();
        MaterialDiff materialDiff=gson.fromJson(gson.toJson(this),MaterialDiff.class);
        childs.add(materialDiff);
       // realcost=realtotalcost*_rate/meng;
        //rate=_rate;
        //realcost=nodeMatnr.hiveSapZcorc.kstsum;
        //realmap.put(nodeMatnr.hiveSapZcorc.matnr,nodeMatnr.hiveSapZcorc.unmng);
        //hiveSapZcorcList.add(nodeMatnr.hiveSapZcorc);
    }

    public MaterialDiff(NodeAufnr nodeAufnr,double _bomuse){
        layer=nodeAufnr.hiveSapZcorc.layer;
        matnr=nodeAufnr.hiveSapZcorc.idnrk;
        ppmng=nodeAufnr.hiveSapZcorc.rpsmng;
        meng=nodeAufnr.hiveSapZcorc.menge;
        if(meng==0){
            meng=ppmng;
        }
        //realtotalcost=nodeMatnr.hiveSapZcorc.rkst001;
        realuse=_bomuse*nodeAufnr.rate;//单位用量
        realcost=nodeAufnr.stageprice/meng;//单价
        hiveSapZcorc = nodeAufnr.hiveSapZcorc;

        //MaterialDiff materialDiff=new MaterialDiff();
        //BeanUtils.copyProperties(this,materialDiff);
        MaterialDiff materialDiff=new MaterialDiff(this);
        //Gson gson = new Gson();
        //MaterialDiff materialDiff=gson.fromJson(gson.toJson(this),MaterialDiff.class);
        childs.add(materialDiff);
    }

    public MaterialDiff(ZcorcMatnr nodeMatnr,double _rate){
        layer=nodeMatnr.hiveSapZcorc.layer;
        matnr=nodeMatnr.hiveSapZcorc.idnrk;
        ppmng=nodeMatnr.hiveSapZcorc.rpsmng;
        meng=nodeMatnr.hiveSapZcorc.menge;
        if(meng==0){
            meng=ppmng;
        }
        //realtotalcost=nodeMatnr.hiveSapZcorc.rkst001;
        realuse=meng*_rate/ppmng;//单位用量
        realcost=nodeMatnr.hiveSapZcorc.rkst001/meng;//单价
        //realtotalcost=nodeMatnr.hiveSapZcorc.rkst001/meng;
        if(nodeMatnr.hiveSapZcorc.e_actual_material_total_cost>0){
            realcost=nodeMatnr.hiveSapZcorc.e_actual_material_total_cost/meng;
        }
        hiveSapZcorc = nodeMatnr.hiveSapZcorc;

        //MaterialDiff materialDiff=new MaterialDiff();
        //BeanUtils.copyProperties(this,materialDiff);
        MaterialDiff materialDiff=new MaterialDiff(this);
        //Gson gson = new Gson();
        //MaterialDiff materialDiff=gson.fromJson(gson.toJson(this),MaterialDiff.class);
        childs.add(materialDiff);
        // realcost=realtotalcost*_rate/meng;
        //rate=_rate;
        //realcost=nodeMatnr.hiveSapZcorc.kstsum;
        //realmap.put(nodeMatnr.hiveSapZcorc.matnr,nodeMatnr.hiveSapZcorc.unmng);
        //hiveSapZcorcList.add(nodeMatnr.hiveSapZcorc);
    }

    public MaterialDiff(ZcorcAufnr nodeAufnr,double _bomuse){
        layer=nodeAufnr.hiveSapZcorc.layer;
        matnr=nodeAufnr.hiveSapZcorc.idnrk;
        ppmng=nodeAufnr.hiveSapZcorc.rpsmng;
        meng=nodeAufnr.hiveSapZcorc.menge;
        if(meng==0){
            meng=ppmng;
        }
        //realtotalcost=nodeMatnr.hiveSapZcorc.rkst001;
        realuse=_bomuse*nodeAufnr.rate;//单位用量
        realcost=nodeAufnr.stageprice/meng;//单价
        hiveSapZcorc = nodeAufnr.hiveSapZcorc;

        //MaterialDiff materialDiff=new MaterialDiff();
        //BeanUtils.copyProperties(this,materialDiff);
        MaterialDiff materialDiff=new MaterialDiff(this);
        //Gson gson = new Gson();
        //MaterialDiff materialDiff=gson.fromJson(gson.toJson(this),MaterialDiff.class);
        childs.add(materialDiff);
    }

}
