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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.utils.ExcelUtil;
import com.sw.mes.yb.report.dao.MaterialPreparationDao;
import com.sw.mes.yb.report.dao.RecycledMaterialPlanDao;
import com.sw.mes.yb.report.entity.MaterialPreparation;
import com.sw.mes.yb.report.entity.RecycledMaterialPlan;
import com.sw.mes.yb.report.service.MaterialPreparationService;
import com.sw.mes.yb.report.service.RecycledMaterialPlanService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
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.math.BigDecimal;
import java.util.*;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class MaterialPreparationServiceImpl extends BaseServiceImpl<MaterialPreparation> implements MaterialPreparationService {
    MaterialPreparationDao dao;
	@Autowired
    public void setDao(MaterialPreparationDao 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) {
                    MaterialPreparation materialPreparation = new MaterialPreparation();
                    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");
                        materialPreparation.setDate(checkTime1);
                    } catch (Exception e) {
                        stringBuilder.append("第"+ row +"行  时间格式错误！");
                    }
                    MaterialPreparation materialPreparation1 = dao.findByDate(checkTime1);
                    if(materialPreparation1!=null){
                        stringBuilder.append("第"+ row +"行 日期已存在！");
                    }

                    if (StringUtils.isEmpty(String.valueOf(map.get("管控目标")).trim())) {
                        stringBuilder.append("第" + row + "行 管控目标不能为空！");
                    }
                    materialPreparation.setTargetValue(new BigDecimal(String.valueOf(map.get("管控目标")).trim()));
                    this.saveOrUpdate(materialPreparation);
                }
            }
        }
        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;
    }
}
