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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostMiningAufnr;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostMiningMaterial;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostMiningAufnrMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostMiningAufnrService;
import com.zzsmart.qomo.kn.cost.manage.vo.*;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: app_scene_cost_mining_aufnr
 * @Author: jeecg-boot
 * @Date:   2024-10-15
 * @Version: V1.0
 */
@Service
public class AppSceneCostMiningAufnrServiceImpl extends ServiceImpl<AppSceneCostMiningAufnrMapper, AppSceneCostMiningAufnr> implements IAppSceneCostMiningAufnrService {

    private static final Logger logger = LoggerFactory.getLogger(AppSceneCostMiningAufnrServiceImpl.class);
    @Override
    public List<AufnrDiff> getByResultid(int id){
        List<AufnrDiff> diffTotalVOS=new ArrayList<>();
        try {
            Gson gson = new Gson();
            QueryWrapper<AppSceneCostMiningAufnr> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("resultid",id);
            List<AppSceneCostMiningAufnr> materials=this.list(queryWrapper);

            //List<MaterialDiff> materialDiffList = new ArrayList<>();
            for (AppSceneCostMiningAufnr miningMaterial : materials) {
                diffTotalVOS.add(gson.fromJson(miningMaterial.getJsondata(), AufnrDiff.class));
            }
            //return gson.toJson(materialDiffList);
            // return diffTotalVOS;
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        return diffTotalVOS;
    }

    @Override
    public List<LaborDiffVO> getLaborByResultid(int id){
        List<LaborDiffVO> laborDiffVOS=new ArrayList<>();
        try {
            List<AufnrDiff> diffTotalVOS=new ArrayList<>();
            Gson gson = new Gson();
            QueryWrapper<AppSceneCostMiningAufnr> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("resultid",id);
            List<AppSceneCostMiningAufnr> materials=this.list(queryWrapper);

            //List<MaterialDiff> materialDiffList = new ArrayList<>();
            for (AppSceneCostMiningAufnr miningMaterial : materials) {
                diffTotalVOS.add(gson.fromJson(miningMaterial.getJsondata(), AufnrDiff.class));
            }
            for (AufnrDiff aufnrDiff:diffTotalVOS){
                LaborDiffVO laborDiffVO=LaborDiffVO.builder()
                        .matnr(aufnrDiff.matnr)
                        .materialname(aufnrDiff.getMaterialname())
                        .budat(aufnrDiff.budat)
                        .costcenter(aufnrDiff.getCostcenter())
                        .costcentername(aufnrDiff.costcentername)
                        .aufnruse(aufnrDiff.laborCalculate.aufnruse)
                        .bggs(0)
                        .realuse(aufnrDiff.laborCalculate.realuse)
                        .realcost(aufnrDiff.laborCalculate.realcost)
                        .totalbomuse(aufnrDiff.totalbomuse)
                        .bzgs(0)
                        .standarduse(aufnrDiff.laborCalculate.standarduse)
                        .standardcost(aufnrDiff.laborCalculate.standardcost)
                        .pricediff(aufnrDiff.laborCalculate.pricediff)
                        .numdiff(aufnrDiff.laborCalculate.numdiff)
                        .totaldiff(aufnrDiff.laborCalculate.totaldiff)
                        .laborreason(aufnrDiff.getLaborreason())
                        .reallabor(aufnrDiff.reallabor)
                        .stdlabor(aufnrDiff.stdlabor)
                        .totalproductuse(aufnrDiff.totalproductuse)
                        .bomuse(aufnrDiff.bomuse)
                        .reallabor(aufnrDiff.totalcostlabor)
                        .stdlabor(aufnrDiff.laborCalculate.standardcost* aufnrDiff.laborCalculate.standarduse)
                        .build();
                if(laborDiffVO.getTotalproductuse()!=0){
                    laborDiffVO.setBggs(laborDiffVO.getRealuse()/laborDiffVO.getTotalproductuse());
                }
                if(laborDiffVO.getBomuse()!=0){
                    laborDiffVO.setBzgs(laborDiffVO.getStandarduse()/laborDiffVO.getBomuse());
                }
                laborDiffVOS.add(laborDiffVO);
            }
            //return gson.toJson(materialDiffList);
            // return diffTotalVOS;
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        return laborDiffVOS;
    }

    @Override
    public List<ProduceDiffVO> getProduceByResultid(int id){
        List<ProduceDiffVO> produceDiffVOS=new ArrayList<>();
        try {
            List<AufnrDiff> diffTotalVOS=new ArrayList<>();
            Gson gson = new Gson();
            QueryWrapper<AppSceneCostMiningAufnr> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("resultid",id);
            List<AppSceneCostMiningAufnr> materials=this.list(queryWrapper);

            //List<MaterialDiff> materialDiffList = new ArrayList<>();
            for (AppSceneCostMiningAufnr miningMaterial : materials) {
                diffTotalVOS.add(gson.fromJson(miningMaterial.getJsondata(), AufnrDiff.class));
            }
            for (AufnrDiff ad:diffTotalVOS){
                ProduceDiffVO produceDiffVO=ProduceDiffVO.builder()
                        .matnr(ad.matnr)
                        .materialname(ad.getMaterialname())
                        .budat(ad.budat)
                        .costcenter(ad.getCostcenter())
                        .costcentername(ad.costcentername)
                        .productnumdiff(ad.getProductNumDiff())
                        .productpricediff(ad.getProductPriceDiff())
                        .producttotaldiff(ad.getProductNumDiff()+ad.getProductPriceDiff())
                        .productreason(ad.productreason)
                        .zj_aufnrnum(ad.zjCalculate.aufnrnum)
                        .zj_realuse(ad.zjCalculate.realuse)
                        .zj_realcost(ad.zjCalculate.realcost)
                        .zj_stdnum(ad.zjCalculate.stdnum)
                        .zj_stduse(ad.zjCalculate.standarduse)
                        .zj_stdcost(ad.zjCalculate.standardcost)
                        .zj_pricediff(ad.zjCalculate.pricediff)
                        .zj_numdiff(ad.zjCalculate.numdiff)
                        .zj_totaldiff(ad.zjCalculate.totaldiff)

                        .rd_aufnrnum(ad.rdCalculate.aufnrnum)
                        .rd_realuse(ad.rdCalculate.realuse)
                        .rd_realcost(ad.rdCalculate.realcost)
                        .rd_stdnum(ad.rdCalculate.stdnum)
                        .rd_stduse(ad.rdCalculate.standarduse)
                        .rd_stdcost(ad.rdCalculate.standardcost)
                        .rd_pricediff(ad.rdCalculate.pricediff)
                        .rd_numdiff(ad.rdCalculate.numdiff)
                        .rd_totaldiff(ad.rdCalculate.totaldiff)

                        .fl_aufnrnum(ad.flCalculate.aufnrnum)
                        .fl_realuse(ad.flCalculate.realuse)
                        .fl_realcost(ad.flCalculate.realcost)
                        .fl_stdnum(ad.flCalculate.stdnum)
                        .fl_stduse(ad.flCalculate.standarduse)
                        .fl_stdcost(ad.flCalculate.standardcost)
                        .fl_pricediff(ad.flCalculate.pricediff)
                        .fl_numdiff(ad.flCalculate.numdiff)
                        .fl_totaldiff(ad.flCalculate.totaldiff)

                        .qt_aufnrnum(ad.qtCalculate.aufnrnum)
                        .qt_realuse(ad.qtCalculate.realuse)
                        .qt_realcost(ad.qtCalculate.realcost)
                        .qt_stdnum(ad.qtCalculate.stdnum)
                        .qt_stduse(ad.qtCalculate.standarduse)
                        .qt_stdcost(ad.qtCalculate.standardcost)
                        .qt_pricediff(ad.qtCalculate.pricediff)
                        .qt_numdiff(ad.qtCalculate.numdiff)
                        .qt_totaldiff(ad.qtCalculate.totaldiff)
                        .build();
                produceDiffVOS.add(produceDiffVO);
            }
            //return gson.toJson(materialDiffList);
            // return diffTotalVOS;
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        return produceDiffVOS;
    }


    @Override
    public ModelAndView exportXls2(HttpServletRequest request, AufnrExportVO object, Class<AppSceneCostMiningAufnr> clazz, String title) {

        if(object.getType().equals("labor")) {
            List<LaborDiffVO> laborDiffVOS=getLaborByResultid(object.getResultid());
            ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
            mv.addObject("fileName", title);
            mv.addObject("entity", LaborDiffVO.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", laborDiffVOS);
            return mv;
        }else {
            List<ProduceDiffVO> produceDiffVOS=getProduceByResultid(object.getResultid());

            ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
            mv.addObject("fileName", title);
            mv.addObject("entity", ProduceDiffVO.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", produceDiffVOS);
            return mv;
        }
    }
}
