package com.zzsmart.qomo.kn.cost.manage.service.impl;


import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostActual;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostMiningMaterial;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostMiningResult;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostSapZcorc;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostActualMapper;
import com.zzsmart.qomo.kn.cost.manage.plugin.CostEffectiveService;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostActualService;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostMiningResultService;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostSapZcorcService;
import com.zzsmart.qomo.kn.cost.manage.vo.CostEffectiveVO;
import com.zzsmart.qomo.kn.cost.manage.vo.RealLaborVO;
import com.zzsmart.qomo.kn.cost.manage.vo.RealProduceVO;
import com.zzsmart.qomo.kn.cost.manage.vo.ZcorcMatnr;
import org.apache.commons.beanutils.BeanUtils;
import org.jeecgframework.poi.excel.def.MapExcelConstants;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecgframework.poi.excel.view.JeecgMapExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.Oneway;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;

/**
 * @Description: app_scene_cost_actual
 * @Author: jeecg-boot
 * @Date:   2024-10-15
 * @Version: V1.0
 */
@Service
public class AppSceneCostActualServiceImpl extends ServiceImpl<AppSceneCostActualMapper, AppSceneCostActual> implements IAppSceneCostActualService {

    private static final Logger logger = LoggerFactory.getLogger(AppSceneCostActualServiceImpl.class);

    @Autowired
    CostEffectiveService costEffectiveService;

    @Autowired
    IAppSceneCostSapZcorcService iAppSceneCostSapZcorcService;

    @Autowired
    IAppSceneCostMiningResultService iAppSceneCostMiningResultService;


    @Override
    public void addCostActual(CostEffectiveVO costEffectiveVO){
        //删除原有记录
        QueryWrapper<AppSceneCostActual> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("matnr",costEffectiveVO.getMatnr());
        queryWrapper.eq("werks",costEffectiveVO.getWerks());
        queryWrapper.eq("ymonth",costEffectiveVO.getYmonth());
        List<AppSceneCostActual> actuals = this.list(queryWrapper);
        if(actuals.size()>0){
            List<String> deleteversions = new ArrayList<>();
            for (AppSceneCostActual appSceneCostActual:actuals){
                deleteversions.add(appSceneCostActual.getVersion());
            }
            QueryWrapper<AppSceneCostSapZcorc> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("version",deleteversions);
            iAppSceneCostSapZcorcService.remove(queryWrapper1);
            this.remove(queryWrapper);
        }

        List<AppSceneCostSapZcorc> versions = costEffectiveService.loadsapservice(costEffectiveVO.getWerks(),costEffectiveVO.getMatnr(),"",costEffectiveVO.getYmonth());


        for (AppSceneCostSapZcorc zcorc:versions){
            AppSceneCostActual appSceneCostActual=AppSceneCostActual.builder()
                    .matnr(costEffectiveVO.getMatnr())
                    .werks(costEffectiveVO.getWerks())
                    .version(zcorc.getVersion())
                    .ymonth(costEffectiveVO.getYmonth())
                    .createTime(new Date())
                    .aufnr(zcorc.getAufnr())
                    .build();
            this.save(appSceneCostActual);
        }

        //this.save(appSceneCostActual);
    }

    @Override
    public List<String> queryParent(int resultid, String matnr){
        List<String> items = new ArrayList<>();
        AppSceneCostMiningResult result=iAppSceneCostMiningResultService.getById(resultid);
        if(result!=null){
            try {
                QueryWrapper<AppSceneCostActual> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("matnr", result.getMatnr());
                queryWrapper.eq("aufnr", result.getAufnr());
                List<AppSceneCostActual> actuals = this.list(queryWrapper);
                if (actuals.size() > 0) {
                    AppSceneCostActual appSceneCostActual = actuals.get(0);
                    QueryWrapper<AppSceneCostSapZcorc> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("version", appSceneCostActual.getVersion());
                    queryWrapper1.eq("idnrk", matnr);
                    List<AppSceneCostSapZcorc> list = iAppSceneCostSapZcorcService.list(queryWrapper1);
                    Set<String> matnrSet = new HashSet<>();
                    for (AppSceneCostSapZcorc zcorc:list){
                        if(!zcorc.getMatnr().equals(result.getMatnr())&&!matnrSet.contains(zcorc.getMatnr())&&!zcorc.getMatnr().equals(matnr)){
                            matnrSet.add(zcorc.getMatnr());
                        }
                    }
                    items.add(matnr);
                    for (String pmatnr:matnrSet){
                        //items.add(pmatnr);
                        List<String> parents=queryParent(resultid,pmatnr);
                        items.addAll(parents);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return items;
    }

    @Override
    public List<AppSceneCostSapZcorc> queryMatnrDetail(int resultid, String matnr){
        if(matnr.equals("41030050003000")){
            logger.info(matnr);
        }
        List<AppSceneCostSapZcorc> zcorcs=new ArrayList<>();
        AppSceneCostMiningResult result=iAppSceneCostMiningResultService.getById(resultid);
        if(result!=null){
            try {
                QueryWrapper<AppSceneCostActual> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("matnr", result.getMatnr());
                queryWrapper.eq("aufnr", result.getAufnr());
                List<AppSceneCostActual> actuals = this.list(queryWrapper);
                if (actuals.size() > 0) {
                    AppSceneCostActual appSceneCostActual = actuals.get(0);
                    QueryWrapper<AppSceneCostSapZcorc> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("version", appSceneCostActual.getVersion());
                    queryWrapper1.eq("idnrk", matnr);
                    queryWrapper1.ne("matnr",matnr);
                    List<AppSceneCostSapZcorc> list = iAppSceneCostSapZcorcService.list(queryWrapper1);
                    Map<String, List<AppSceneCostSapZcorc>> pmatnrMap = new HashMap<>();
                    for (AppSceneCostSapZcorc zcorc : list) {
                        String pmatnr = zcorc.getMatnr();
                        if (pmatnrMap.containsKey(pmatnr)) {
                            List<AppSceneCostSapZcorc> items = pmatnrMap.get(pmatnr);
                            items.add(zcorc);
                            pmatnrMap.put(pmatnr, items);
                        } else {
                            List<AppSceneCostSapZcorc> items = new ArrayList<>();
                            items.add(zcorc);
                            pmatnrMap.put(pmatnr, items);
                        }
                    }
                    List<AppSceneCostSapZcorc> newlist = new ArrayList<>();
                    for (String pmatnr : pmatnrMap.keySet()) {
                        List<AppSceneCostSapZcorc> items = pmatnrMap.get(pmatnr);
                        if (items.size() > 0) {
                            double aufnrmng = 0;
                            double aufnrnum = 0;
                            AppSceneCostSapZcorc topitem = items.get(0);
                            for (AppSceneCostSapZcorc zcorc : items) {
                                AppSceneCostSapZcorc newitem = JSON.parseObject(JSON.toJSONString(zcorc),AppSceneCostSapZcorc.class);
                                //BeanUtils.copyProperties(zcorc,newitem);
                                //newitem.setAufnrnum(zcorc.getMenge());
                                newitem.setPnum(null);
                                newitem.setAvgnum(null);
                                newitem.setTotalprice(null);
                                newitem.setAvgprice(zcorc.getRkst001());
                                if(zcorc.getAufnrmenge()!=null){
                                    //newitem.setAufnrmenge(new BigDecimal(0));
                                    newitem.setAufnrnum(zcorc.getAufnrmenge());
                                    aufnrnum+=zcorc.getAufnrmenge().doubleValue();
                                }else{
                                    newitem.setAufnrnum(new BigDecimal(0));
                                }
                                zcorcs.add(newitem);
                                aufnrmng+=zcorc.getAufnrmng().doubleValue();
                                //aufnrnum+=zcorc.getMenge().doubleValue();
                            }
                            AppSceneCostSapZcorc total = new AppSceneCostSapZcorc();
                            total.setIdnrk("小计:");
                            total.setAufnrmng(new BigDecimal(aufnrmng));
                            total.setAufnrnum(new BigDecimal(aufnrnum).setScale(4,RoundingMode.HALF_UP));
                            total.setAvgnum(topitem.getAufnrtotal());
                            total.setAvgprice(topitem.getAvgprice());
                            total.setTotalprice(new BigDecimal(total.getAvgprice().doubleValue()/total.getAufnrnum().doubleValue(),new MathContext(4, RoundingMode.HALF_UP)));
                            //total.setAufnrnum(topitem.getAufnrnum());
                            total.setPnum(topitem.getPnum());
                            total.setAvgnum(topitem.getAvgnum());
                            //total.set
                            zcorcs.add(total);
                        }
                    }
                    //如果有多个父物料，最后再增加一行合计
                    if (pmatnrMap.size() > 1 && list.size() > 0) {
                        AppSceneCostSapZcorc topitem = list.get(0);
                        AppSceneCostSapZcorc total = new AppSceneCostSapZcorc();
                        total.setIdnrk("合计:");
                        total.setAvgnum(topitem.getTotalnum());
                        total.setTotalnum(topitem.getTotalnum());
                        total.setTotalprice(topitem.getTotalprice());
                        zcorcs.add(total);
                    }
                    //return newlist;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return zcorcs;
    }

    public String getParentLayer(String layer){
        //String layer = zcorc1.getLayer();
        String[] names = layer.split("-");
        if(names.length>3){
            String toplayer="";
            for (int i=0;i<names.length-2;++i){
                toplayer+=names[i];
                toplayer+="-";
            }
            return toplayer;
        }else{
            return layer;
        }
    }

    @Override
    public List<RealProduceVO> queryProduceDetail(int resultid, String matnr){
        if(matnr.equals("41030050003000")){
            logger.info(matnr);
        }
        List<AppSceneCostSapZcorc> list1 = queryMatnrDetail(resultid,matnr);
        double pnum=1;
        if(list1.size()>0){
            AppSceneCostSapZcorc zcorc=list1.get(list1.size()-1);
            if(zcorc.getAvgnum()!=null){
                pnum=zcorc.getAvgnum().doubleValue();
            }
        }
        List<RealProduceVO> zcorcs=new ArrayList<>();
        AppSceneCostMiningResult result=iAppSceneCostMiningResultService.getById(resultid);
        if(result!=null){
            try {
                QueryWrapper<AppSceneCostActual> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("matnr", result.getMatnr());
                queryWrapper.eq("aufnr", result.getAufnr());
                List<AppSceneCostActual> actuals = this.list(queryWrapper);
                if (actuals.size() > 0) {
                    AppSceneCostActual appSceneCostActual = actuals.get(0);
                    QueryWrapper<AppSceneCostSapZcorc> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("version", appSceneCostActual.getVersion());
                    queryWrapper1.eq("idnrk", matnr);
                    queryWrapper1.eq("matnr",matnr);
                    List<AppSceneCostSapZcorc> list = iAppSceneCostSapZcorcService.list(queryWrapper1);
                    Map<String, List<AppSceneCostSapZcorc>> pmatnrMap = new HashMap<>();
                    String toplayer = "";
                    for (AppSceneCostSapZcorc zcorc : list) {
                        String pmatnr = getParentLayer(zcorc.getLayer());
                        if(toplayer.equals("")){
                            toplayer=pmatnr;
                        }
                        if (pmatnrMap.containsKey(pmatnr)) {
                            List<AppSceneCostSapZcorc> items = pmatnrMap.get(pmatnr);
                            items.add(zcorc);
                            pmatnrMap.put(pmatnr, items);
                        } else {
                            List<AppSceneCostSapZcorc> items = new ArrayList<>();
                            items.add(zcorc);
                            pmatnrMap.put(pmatnr, items);
                        }
                    }
                    List<AppSceneCostSapZcorc> newlist = new ArrayList<>();
                    for (String pmatnr : pmatnrMap.keySet()) {
                        if(!pmatnr.equals(toplayer)){
                            continue;
                        }
                        List<AppSceneCostSapZcorc> items = pmatnrMap.get(pmatnr);
                        if (items.size() > 0) {
                            //订单数量
                            double aufnrmng = 0;
                            //实际工时
                            double sbgs = 0;
                            double rdgs=0;
                            double flgs=0;
                            double qtgs=0;
                            double sbgf=0;
                            double rdgf=0;
                            double flgf=0;
                            double qtgf=0;
                            AppSceneCostSapZcorc topitem = items.get(0);
                            for (AppSceneCostSapZcorc zcorc : items) {
                                RealProduceVO newitem = JSON.parseObject(JSON.toJSONString(zcorc),RealProduceVO.class);
                                //BeanUtils.copyProperties(zcorc,newitem);
                                //newitem.setAufnrnum(zcorc.getMenge());
                                newitem.setPnum(null);
                                newitem.setAvgnum(null);
                                newitem.setTotalprice(null);
                                newitem.setAvgprice(zcorc.getRkst001());
                                newitem.setPnum(new BigDecimal(pnum));
                                if(pmatnr.equals(toplayer)) {
                                    BigDecimal six = new BigDecimal(60);
                                    newitem.setAufnrmng(newitem.getMenge());
                                    newitem.setSbgf(newitem.getSbgs().multiply(newitem.getSbfv()).divide(six,RoundingMode.HALF_UP));
                                    newitem.setRdgf(newitem.getRdgs().multiply(newitem.getRdfv()).divide(six,RoundingMode.HALF_UP));
                                    newitem.setFlgf(newitem.getFlgs().multiply(newitem.getFlfv()).divide(six,RoundingMode.HALF_UP));
                                    newitem.setQtgf(newitem.getQtgs().multiply(newitem.getQtfv()).divide(six,RoundingMode.HALF_UP));
                                    newitem.setAvgsbgs(newitem.getSbgs().divide(newitem.getMenge(),RoundingMode.HALF_UP));
                                    newitem.setAvgrdgs(newitem.getRdgs().divide(newitem.getMenge(),RoundingMode.HALF_UP));
                                    newitem.setAvgflgs(newitem.getFlgs().divide(newitem.getMenge(),RoundingMode.HALF_UP));
                                    newitem.setAvgqtgs(newitem.getQtgs().divide(newitem.getMenge(),RoundingMode.HALF_UP));
                                    newitem.setAvgsbfv(newitem.getSbfv().divide(six,RoundingMode.HALF_UP));
                                    newitem.setAvgrdfv(newitem.getRdfv().divide(six,RoundingMode.HALF_UP));
                                    newitem.setAvgflfv(newitem.getFlfv().divide(six,RoundingMode.HALF_UP));
                                    newitem.setAvgqtfv(newitem.getQtfv().divide(six,RoundingMode.HALF_UP));
                                    aufnrmng+=newitem.getAufnrmng().doubleValue();
                                    sbgs+=newitem.getSbgs().doubleValue();
                                    rdgs+=newitem.getRdgs().doubleValue();
                                    flgs+=newitem.getFlgs().doubleValue();
                                    qtgs+=newitem.getQtgs().doubleValue();
                                    sbgf+= newitem.getSbgf().doubleValue();
                                    rdgf+=newitem.getRdgf().doubleValue();
                                    flgf+=newitem.getFlgf().doubleValue();
                                    qtgf+=newitem.getQtgf().doubleValue();
                                }
                                zcorcs.add(newitem);
                            }
                            RealProduceVO total = new RealProduceVO();
                            total.setIdnrk("小计:");
                            total.setAufnrmng(new BigDecimal(aufnrmng));
                            total.setSbgs(new BigDecimal(sbgs));
                            total.setRdgs(new BigDecimal(rdgs));
                            total.setFlgs(new BigDecimal(flgs));
                            total.setQtgs(new BigDecimal(qtgs));
                            total.setPnum(new BigDecimal(pnum));
                            total.setAvgsbgs(total.getSbgs().divide(total.getAufnrmng(),RoundingMode.HALF_UP));
                            total.setAvgflgs(total.getFlgs().divide(total.getAufnrmng(),RoundingMode.HALF_UP));
                            total.setAvgrdgs(total.getRdgs().divide(total.getAufnrmng(),RoundingMode.HALF_UP));
                            total.setAvgqtgs(total.getQtgs().divide(total.getAufnrmng(),RoundingMode.HALF_UP));
                            total.setSbgf(new BigDecimal(sbgf));
                            total.setRdgf(new BigDecimal(rdgf));
                            total.setFlgf(new BigDecimal(flgf));
                            total.setQtgf(new BigDecimal(qtgf));
                            total.setAvgsbfv(total.getSbgf().divide(total.getSbgs(),RoundingMode.HALF_UP));
                            total.setAvgrdfv(total.getRdgf().divide(total.getRdgs(),RoundingMode.HALF_UP));
                            total.setAvgflfv(total.getFlgf().divide(total.getFlgs(),RoundingMode.HALF_UP));
                            total.setAvgqtfv(total.getQtgf().divide(total.getQtgs(),RoundingMode.HALF_UP));

                            zcorcs.add(total);
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return zcorcs;
    }
    @Override
    public List<RealLaborVO> queryAufnrDetail(int resultid, String matnr){
        if(matnr.equals("41030050003000")){
            logger.info(matnr);
        }
        List<AppSceneCostSapZcorc> list1 = queryMatnrDetail(resultid,matnr);
        double pnum=1;
        if(list1.size()>0){
            AppSceneCostSapZcorc zcorc=list1.get(list1.size()-1);
            if(zcorc.getAvgnum()!=null){
                pnum=zcorc.getAvgnum().doubleValue();
            }
        }
        List<RealLaborVO> zcorcs=new ArrayList<>();
        AppSceneCostMiningResult result=iAppSceneCostMiningResultService.getById(resultid);
        if(result!=null){
            try {
                QueryWrapper<AppSceneCostActual> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("matnr", result.getMatnr());
                queryWrapper.eq("aufnr", result.getAufnr());
                List<AppSceneCostActual> actuals = this.list(queryWrapper);
                if (actuals.size() > 0) {
                    AppSceneCostActual appSceneCostActual = actuals.get(0);
                    QueryWrapper<AppSceneCostSapZcorc> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("version", appSceneCostActual.getVersion());
                    queryWrapper1.eq("idnrk", matnr);
                    queryWrapper1.eq("matnr",matnr);
                    List<AppSceneCostSapZcorc> list = iAppSceneCostSapZcorcService.list(queryWrapper1);
                    Map<String, List<AppSceneCostSapZcorc>> pmatnrMap = new HashMap<>();
                    String toplayer = "";
                    for (AppSceneCostSapZcorc zcorc : list) {
                        String pmatnr = getParentLayer(zcorc.getLayer());
                        if(toplayer.equals("")){
                            toplayer=pmatnr;
                        }
                        if (pmatnrMap.containsKey(pmatnr)) {
                            List<AppSceneCostSapZcorc> items = pmatnrMap.get(pmatnr);
                            items.add(zcorc);
                            pmatnrMap.put(pmatnr, items);
                        } else {
                            List<AppSceneCostSapZcorc> items = new ArrayList<>();
                            items.add(zcorc);
                            pmatnrMap.put(pmatnr, items);
                        }
                    }
                    List<AppSceneCostSapZcorc> newlist = new ArrayList<>();
                    for (String pmatnr : pmatnrMap.keySet()) {
                        if(!pmatnr.equals(toplayer)){
                            continue;
                        }
                        List<AppSceneCostSapZcorc> items = pmatnrMap.get(pmatnr);
                        if (items.size() > 0) {
                            double aufnrmng = 0;
                            double rggs = 0;
                            double aufnrnum=0;
                            double avgrggs=0;
                            double avgprice=0;
                            double rggf = 0;
                            AppSceneCostSapZcorc topitem = items.get(0);
                            for (AppSceneCostSapZcorc zcorc : items) {
                                RealLaborVO newitem = JSON.parseObject(JSON.toJSONString(zcorc),RealLaborVO.class);
                                //BeanUtils.copyProperties(zcorc,newitem);
                                //newitem.setAufnrnum(zcorc.getMenge());
                                newitem.setPnum(new BigDecimal(pnum));
                                //newitem.setAvgnum(null);
                                //newitem.setTotalprice(null);
                                //newitem.setAvgprice(zcorc.getRkst001());
                                BigDecimal six = new BigDecimal(60.0);
                                if(pmatnr.equals(toplayer)) {
                                    //if(zcorc.getAufnrmenge()!=null){
                                    //newitem.setAufnrmenge(new BigDecimal(0));
                                    newitem.setAufnrmng(zcorc.getPsmng());
                                    aufnrmng+=newitem.getAufnrmng().doubleValue();
                                    //newitem.setAufnrnum(zcorc.getAufnrmenge());
                                    newitem.setFixedrggs(newitem.getRggs());
                                    if (zcorc.getRggs() != null) {
                                        rggs += zcorc.getRggs().doubleValue();
                                    }
                                    //double price = zcorc.getMenge().doubleValue()*zcorc.getKst003().doubleValue();
                                    //newitem.setAvgprice(new BigDecimal(price));
                                    //avgprice+=price;
                                    BigDecimal gf = newitem.getRggs().multiply(newitem.getRgfv()).divide(six,RoundingMode.HALF_UP);
                                    newitem.setRggf(gf);
                                    newitem.setUrggs(newitem.getRggs().divide(newitem.getAufnrmng(),RoundingMode.HALF_UP));
                                    rggf+=gf.doubleValue();
                                    newitem.setAvgrggs(newitem.getRggs().divide(newitem.getMenge(),RoundingMode.HALF_UP));
                                    newitem.setAvgrgfv(newitem.getRgfv().divide(six,RoundingMode.HALF_UP));
                                    //aufnrnum+=zcorc.getRggs().doubleValue();
                                    //newitem.setAufnrnum(zcorc.getRggs());
                                    //double totalprice = price/zcorc.getRggs().doubleValue();
                                    //newitem.setTotalprice(new BigDecimal(totalprice));
                                    //newitem.setAvgnum(newitem.getAufnrnum().divide(newitem.getAufnrmng(),RoundingMode.HALF_UP));
                                    //aufnrmng+=zcorc.getAufnrmng().doubleValue();
                                }
                                //}else{
                                //    newitem.setAufnrnum(new BigDecimal(0));
                                //}
                                zcorcs.add(newitem);
                            }
                            if(aufnrmng!=0){
                                avgrggs = rggs/aufnrmng;
                            }
                            RealLaborVO total = new RealLaborVO();
                            total.setIdnrk("小计:");
                            total.setAufnrmng(new BigDecimal(aufnrmng));
                            total.setPnum(new BigDecimal(pnum));
                            total.setRggs(new BigDecimal(rggs));
                            total.setFixedrggs(new BigDecimal(rggs));
                            total.setAvgrggs(total.getRggs().multiply(total.getPnum()).divide(total.getAufnrmng(),RoundingMode.HALF_UP));
                            total.setRggf(new BigDecimal(rggf*pnum));
                            total.setAvgrgfv(total.getRggf().divide(total.getRggs(),RoundingMode.HALF_UP));
                            zcorcs.add(total);
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return zcorcs;
    }
    @Override
    public ModelAndView exportMatnrDetail(int resultid, String matnr){
        String title="材料明细";
        String fields="layer,matnr,werks,aufnr,ppsmng,idnrk,maktx,kmonth,unmng,kst001,rkst000,aufnrmng,aufnrnum,pnum,avgnum,avgprice,totalprice";

        List<String> parents = queryParent(resultid,matnr);
        //Map<String,List<AppSceneCostSapZcorc>> data=new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for(String idnrk:parents){
            List<AppSceneCostSapZcorc> produceDiffVOS=queryMatnrDetail(resultid,idnrk);
            Map<String, Object> map1 = new HashMap<>();
            map1.put(NormalExcelConstants.CLASS, AppSceneCostSapZcorc.class);
            map1.put(NormalExcelConstants.PARAMS, new ExportParams(title+"报表", idnrk));
            map1.put(NormalExcelConstants.DATA_LIST, produceDiffVOS);
            map1.put(NormalExcelConstants.EXPORT_FIELDS, fields);
            list.add(map1);
        }
        //List<AppSceneCostSapZcorc> produceDiffVOS=queryMatnrDetail(resultid,matnr);

        //String fields="layer,matnr,werks,aufnr,ppsmng,idnrk,maktx,kmonth,unmng,kst001,rkst000,aufnrmng,aufnrnum,pnum,avgnum,avgprice,totalprice";

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject("fileName", title);
        mv.addObject(NormalExcelConstants.MAP_LIST, list);
        //mv.addObject("entity", AppSceneCostSapZcorc.class);
        ExportParams exportParams = new ExportParams(title + "报表", title + "报表", title);
        exportParams.setType(ExcelType.XSSF);
        //exportParams.setImageBasePath(this.jeecgBaseConfig.getPath().getUpload());
        mv.addObject("params", exportParams);
        //mv.addObject("data", data);
        mv.addObject(NormalExcelConstants.EXPORT_FIELDS, fields);
        return mv;
    }



    @Override
    public ModelAndView exportLaborDetail(int resultid, String matnr){
        String title="人工明细";
        String fields="layer,matnr,werks,aufnr,ppsmng,idnrk,maktx,menge,pnum,kst003,rggs,aufnrmng,fixedrggs,avgrggs,rggf,avgrgfv";

        List<String> parents = queryParent(resultid,matnr);
        //Map<String,List<AppSceneCostSapZcorc>> data=new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for(String idnrk:parents){
            List<RealLaborVO> produceDiffVOS=queryAufnrDetail(resultid,idnrk);
            Map<String, Object> map1 = new HashMap<>();
            map1.put(NormalExcelConstants.CLASS, RealLaborVO.class);
            ExportParams exportParams = new ExportParams(title + "报表", idnrk);
            exportParams.setType(ExcelType.XSSF);
            map1.put(NormalExcelConstants.PARAMS,exportParams);
            //map1.put(NormalExcelConstants.PARAMS, new ExportParams(title+"报表", idnrk));
            map1.put(NormalExcelConstants.DATA_LIST, produceDiffVOS);
            map1.put(NormalExcelConstants.EXPORT_FIELDS, fields);
            list.add(map1);
        }
        //List<AppSceneCostSapZcorc> produceDiffVOS=queryMatnrDetail(resultid,matnr);

        //String fields="layer,matnr,werks,aufnr,ppsmng,idnrk,maktx,kmonth,unmng,kst001,rkst000,aufnrmng,aufnrnum,pnum,avgnum,avgprice,totalprice";

        //制作表头
        List<ExcelExportEntity> entityList = new ArrayList<>();
        ExcelExportEntity entity = new ExcelExportEntity("层级", "layer");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("物料", "matnr");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("工厂", "werks");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("订单", "aufnr");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("订单数量", "ppsmng");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("组件", "idnrk");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("物料描述", "maktx");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("数量", "menge");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("单位用量", "pnum");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("人工费用", "kst003");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("实际工时(人工工时)", "rggs");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("订单数量(取订单项数量)", "aufnrmng");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("实际工时", "fixedrggs");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("加权平均单位人工工时", "avgrggs");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("人工费用总成本", "rggf");
        entity.setWidth(15);
        entityList.add(entity);
        entity = new ExcelExportEntity("加权平均单位人工费率", "avgrgfv");
        entity.setWidth(15);
        entityList.add(entity);

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject("fileName", title);
        mv.addObject(NormalExcelConstants.MAP_LIST, list);
        //mv.addObject(MapExcelConstants.ENTITY_LIST, entityList);
        //mv.addObject("entity", AppSceneCostSapZcorc.class);
        //ExportParams exportParams = new ExportParams(title + "报表", title + "报表", title);
        //exportParams.setType(ExcelType.XSSF);
        //exportParams.setImageBasePath(this.jeecgBaseConfig.getPath().getUpload());
        //mv.addObject("params", exportParams);
        //mv.addObject("data", data);
        //mv.addObject(NormalExcelConstants.EXPORT_FIELDS, fields);
        return mv;
    }

    @Override
    public ModelAndView exportProduceDetail(int resultid, String matnr){
        String title="制费明细";
        String fields="layer,matnr,werks,aufnr,ppsmng,idnrk,maktx,menge,unmng,kst005,rkst301,rkst302,rkst303,rkst304,aufnrmng,sbgs,rdgs,flgs,qtgs,avgsbgs,avgrdgs,avgflgs,avgqtgs,sbgf,rdgf,flgf,qtgf,avgsbfv,avgrdfv,avgflfv,avgqtfv";

        List<String> parents = queryParent(resultid,matnr);
        //Map<String,List<AppSceneCostSapZcorc>> data=new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for(String idnrk:parents){
            List<RealProduceVO> produceDiffVOS=queryProduceDetail(resultid,idnrk);
            Map<String, Object> map1 = new HashMap<>();
            map1.put(NormalExcelConstants.CLASS, RealProduceVO.class);
            ExportParams exportParams = new ExportParams(title + "报表", idnrk);
            exportParams.setType(ExcelType.XSSF);
            map1.put(NormalExcelConstants.PARAMS,exportParams);
            //map1.put(NormalExcelConstants.PARAMS, new ExportParams(title+"报表", idnrk));
            map1.put(NormalExcelConstants.DATA_LIST, produceDiffVOS);
            map1.put(NormalExcelConstants.EXPORT_FIELDS, fields);
            list.add(map1);
        }
        //List<AppSceneCostSapZcorc> produceDiffVOS=queryMatnrDetail(resultid,matnr);

        //String fields="layer,matnr,werks,aufnr,ppsmng,idnrk,maktx,kmonth,unmng,kst001,rkst000,aufnrmng,aufnrnum,pnum,avgnum,avgprice,totalprice";

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject("fileName", title);
        mv.addObject(NormalExcelConstants.MAP_LIST, list);
        //mv.addObject("entity", AppSceneCostSapZcorc.class);
        //ExportParams exportParams = new ExportParams(title + "报表", title + "报表", title);
        //exportParams.setType(ExcelType.XSSF);
        //exportParams.setImageBasePath(this.jeecgBaseConfig.getPath().getUpload());
        //mv.addObject("params", exportParams);
        //mv.addObject("data", data);
        //mv.addObject(NormalExcelConstants.EXPORT_FIELDS, fields);
        return mv;
    }
}
