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.AppSceneCostMiningMaterial;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostStandardResult;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostMiningMaterialMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostMiningMaterialService;
import com.zzsmart.qomo.kn.cost.manage.vo.MaterialDiff;
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_material
 * @Author: jeecg-boot
 * @Date:   2024-10-15
 * @Version: V1.0
 */
@Service
public class AppSceneCostMiningMaterialServiceImpl extends ServiceImpl<AppSceneCostMiningMaterialMapper, AppSceneCostMiningMaterial> implements IAppSceneCostMiningMaterialService {

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

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

    public ModelAndView exportXls2(HttpServletRequest request, AppSceneCostMiningMaterial object, Class<AppSceneCostMiningMaterial> clazz, String title) {
        List<MaterialDiff> materialDiffs=  getByResultid(object.getResultid());
//        List<MaterialDiffVO> list = new ArrayList<>();
//        for (MaterialDiff materialDiff:materialDiffs){
//            MaterialDiffVO vo = MaterialDiffVO.builder()
//                    .matnr(materialDiff.getMatnr())
//                    .productname(materialDiff.getProductname())
//                    .layer(materialDiff.getLayer())
//                    //.version_number(materialDiff.getVersion_number())
//                    .build();
//            list.add(vo);
//        }
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject("fileName", title);
        mv.addObject("entity", MaterialDiff.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", materialDiffs);
        return mv;
    }


//    @Override
//    public ModelAndView exportMaterialDiffXls(HttpServletRequest request,  AppSceneCostMiningMaterial object, Class<AppSceneCostMiningMaterial> clazz, String title) {
//        List<MaterialDiff> materialDiffs=  getByResultid(object.getResultid());
//        List<MaterialDiffVO> list = new ArrayList<>();
//        List<AppSceneCostStandardResult> results=new ArrayList<>();
//        for (MaterialDiff materialDiff:materialDiffs){
//            MaterialDiffVO vo = MaterialDiffVO.builder()
//                    .matnr(materialDiff.getMatnr())
//                    .productname(materialDiff.getProductname())
//                    .layer(materialDiff.getLayer())
//                    //.version_number(materialDiff.getVersion_number())
//                    .build();
//            AppSceneCostStandardResult result=AppSceneCostStandardResult.builder()
//                    .materialNumber(materialDiff.getMatnr())
//                    .maktx(materialDiff.getProductname())
//                    .versionNumber(materialDiff.getVersion_number())
//                    .build();
//            results.add(result);
//            list.add(vo);
//        }
//        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//        mv.addObject("fileName", title);
//        mv.addObject("entity", AppSceneCostStandardResult.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", results);
//        return mv;
//    }
}
