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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostStandardDetail;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostStandardResult;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostStandardDetailMapper;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostStandardResultMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostMiningMaterialService;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostStandardDetailService;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostStandardResultService;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: app_scene_cost_standard_detail
 * @Author: jeecg-boot
 * @Date:   2024-07-31
 * @Version: V1.0
 */
@Service
public class AppSceneCostStandardResultServiceImpl extends ServiceImpl<AppSceneCostStandardResultMapper, AppSceneCostStandardResult> implements IAppSceneCostStandardResultService {


    @Autowired
    IAppSceneCostMiningMaterialService iAppSceneCostMiningMaterialService;


    public ModelAndView exportXls2(HttpServletRequest request, AppSceneCostStandardResult object, Class<AppSceneCostStandardResult> clazz, String title) {

       //return iAppSceneCostMiningMaterialService.exportMaterialDiffXls(request,object,AppSceneCostStandardResult.class,"test");
        QueryWrapper<AppSceneCostStandardResult> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
        String selections = request.getParameter("selections");
        List<AppSceneCostStandardResult> exportList;
        List<AppSceneCostStandardResult> exportList2=new ArrayList<>();
//        if (oConvertUtils.isNotEmpty(selections)) {
//            exportList = Arrays.asList(selections.split(","));
//            queryWrapper.in("id", exportList);
//        }



        exportList = list(queryWrapper);

        BigDecimal equiphour = new BigDecimal(0);
        BigDecimal laborhour = new BigDecimal(0);
        BigDecimal fuelhour = new BigDecimal(0);
        BigDecimal auxihour = new BigDecimal(0);
        BigDecimal otherhour=new BigDecimal(0);
        BigDecimal materialcost = new BigDecimal(0);
        BigDecimal laborcost = new BigDecimal(0);
        BigDecimal equipcost = new BigDecimal(0);
        BigDecimal fuelcost = new BigDecimal(0);
        BigDecimal auxicost = new BigDecimal(0);
        BigDecimal othercost = new BigDecimal(0);
        BigDecimal standardcost = new BigDecimal(0);
        for (AppSceneCostStandardResult ascs:exportList){
            equiphour=equiphour.add(ascs.getEquipHour());
            laborhour=laborhour.add(ascs.getLaborHour());
            fuelhour=fuelhour.add(ascs.getFuelHour());
            auxihour=auxihour.add(ascs.getAuxiHour());
            otherhour=otherhour.add(ascs.getOtherHour());
            if(ascs.getMaterialCost()!=null) {
                materialcost = materialcost.add(ascs.getMaterialCost());
            }
            if(ascs.getLaborCost()!=null) {
                laborcost = laborcost.add(ascs.getLaborCost());
            }
            if(ascs.getEquipCost()!=null) {
                equipcost = equipcost.add(ascs.getEquipCost());
            }
            if(ascs.getFuelCost()!=null) {
                fuelcost = fuelcost.add(ascs.getFuelCost());
            }
            if(ascs.getAuxiliaryCost()!=null) {
                auxicost = auxicost.add(ascs.getAuxiliaryCost());
            }
            if(ascs.getOtherCost()!=null) {
                othercost = othercost.add(ascs.getOtherCost());
            }
            if(ascs.getStandardCost()!=null) {
                standardcost = standardcost.add(ascs.getStandardCost());
            }

        }
        if(exportList.size()>0){
            AppSceneCostStandardResult first = exportList.get(0);
            AppSceneCostStandardResult top = AppSceneCostStandardResult.builder()
                    .ymonth(first.getYmonth())
                    .versionNumber(first.getVersionNumber())
                    .stage(first.getStage())
                    .factory(first.getFactory())
                    .parentMatnr(first.getParentMatnr())
                    .lowOrderCode("")
                    .materialNumber("合计")
                    .equipHour(equiphour)
                    .laborHour(laborhour)
                    .fuelHour(fuelhour)
                    .auxiHour(auxihour)
                    .otherHour(otherhour)
                    .laborCost(laborcost)
                    .equipCost(equipcost)
                    .fuelCost(fuelcost)
                    .auxiliaryCost(auxicost)
                    .otherCost(othercost)
                    .materialCost(materialcost)
                    .standardCost(standardcost)
                .build();
            exportList2.add(top);
            exportList2.addAll(exportList);
        }


        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject("fileName", title);
        mv.addObject("entity", clazz);
        ExportParams exportParams = new ExportParams();
        exportParams.setType(ExcelType.XSSF);
        //exportParams.setImageBasePath(this.jeecgBaseConfig.getPath().getUpload());
        mv.addObject("params", exportParams);
        mv.addObject("data", exportList2);
        return mv;
    }
}
