package com.sw.mes.yb.report.service.impl;


import com.sw.mes.base.factory.inspect.entity.RoundSpellRule;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.utils.ExcelUtil;
import com.sw.mes.yb.plan.entity.ProductionSchedule;
import com.sw.mes.yb.production.entity.RecycledMaterialManage;
import com.sw.mes.yb.report.dao.BrokenBractBaseDao;
import com.sw.mes.yb.report.dao.RecycledMaterialPlanDao;
import com.sw.mes.yb.report.dto.RecycledMaterialPlanDto;
import com.sw.mes.yb.report.entity.BrokenBractBase;
import com.sw.mes.yb.report.entity.RecycledMaterialPlan;
import com.sw.mes.yb.report.service.BrokenBractBaseService;
import com.sw.mes.yb.report.service.RecycledMaterialPlanService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class RecycledMaterialPlanServiceImpl extends BaseServiceImpl<RecycledMaterialPlan> implements RecycledMaterialPlanService {
    RecycledMaterialPlanDao dao;
	@Autowired
    public void setDao(RecycledMaterialPlanDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @SneakyThrows
    @Override
    public String importRecycledMaterial(MultipartFile file) {
        Map<String, List<Map<String, Object>>> excelMap = null;
        try {
            excelMap = ExcelUtil.getExcelInfo(file);
        } catch (Exception e) {
            throw new RuntimeException("文件导入异常！" + e.getMessage());
        }
        StringBuilder stringBuilder=new StringBuilder();
        if (!excelMap.isEmpty()) {
            Collection<List<Map<String, Object>>> collection = excelMap.values();
            Iterator<List<Map<String, Object>>> iterator = collection.iterator();
            while (iterator.hasNext()) {
                List<Map<String, Object>> list = iterator.next();
                for (Map<String, Object> map : list) {
                    RecycledMaterialPlan recycledMaterialPlan = new RecycledMaterialPlan();
                    String row = String.valueOf(map.get("序号"));
                    //当行号为空跳过这行
                    if ("null".equals(row) || "".equals(row)) {
                        stringBuilder.append("第"+ row +"行序号不能为空！");
                    }
                    if(StringUtils.isEmpty(String.valueOf(map.get("日期")).trim())){
                        stringBuilder.append("第"+ row +"行 日期不能为空！");
                    }
                    Date checkTime1=null;
                    try{
                        checkTime1 = DateUtil.toDate(String.valueOf(map.get("日期")).trim(), "yyyy-MM-dd");
                        recycledMaterialPlan.setDate(checkTime1);
                    } catch (Exception e) {
                        stringBuilder.append("第"+ row +"行  时间格式错误！");
                    }
                    RecycledMaterialPlan recycledMaterialPlan1 = dao.findByDate(checkTime1);
                    if(recycledMaterialPlan1!=null){
                        stringBuilder.append("第"+ row +"行 日期已存在！");
                    }

                    if (StringUtils.isEmpty(String.valueOf(map.get("计划产出")).trim())) {
                        stringBuilder.append("第" + row + "行 计划产出不能为空！");
                    }
                    recycledMaterialPlan.setPlanValue(new BigDecimal(String.valueOf(map.get("计划产出")).trim()));
                    this.saveOrUpdate(recycledMaterialPlan);
                }
            }
        }
        if(StringUtils.isNotBlank(stringBuilder.toString())){
            throw new RuntimeException(stringBuilder.toString());
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getData() {
        List<Map<String, Object>> mapList=dao.findAllData();
        return mapList;
    }
}
