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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.base.factory.utils.PageUtils;
import com.sw.mes.utils.ExcelUtil;
import com.sw.mes.utils.StringUtils;
import com.sw.mes.yb.check.dao.ExceptManageDao;
import com.sw.mes.yb.check.entity.ExceptManage;
import com.sw.mes.yb.check.service.ExceptManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional
public class ExceptManageServiceImpl extends BaseServiceImpl<ExceptManage> implements ExceptManageService {

    ExceptManageDao exceptManageDao;

    @Autowired
    public void setDao(ExceptManageDao exceptManageDAO) {
        this.exceptManageDao = exceptManageDAO;
        super.setBaseDao(exceptManageDAO);
    }


    @Override
    public List<ExceptManage> findByExample(Specification<ExceptManage> specification, RequestPage page) {
        org.springframework.data.domain.Page<ExceptManage> springDataPage = exceptManageDao.findAll(specification, PageUtils.createPageable(page));
        page.setTotal(springDataPage.getTotalElements());
        return springDataPage.getContent();
    }

    @Override
    public Integer getCountByGTETime(String startTime) {
        return exceptManageDao.getCountByGTETime(startTime);
    }

    @Override
    public void readInfoImport(MultipartFile file) throws Exception {
        Map<String, List<Map<String, Object>>> excelMap;
        try {
            excelMap = ExcelUtil.getExcelInfo(file);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("文件导入异常！" + e.getMessage());
        }
        if (!excelMap.isEmpty()) {

            List<ExceptManage> exceptManage = new ArrayList<>();

            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();

                //导入数量限制
                if (list.size() > 10000) {
                    throw new Exception("每批次导入限制[10000]，该批次为：" + list.size());
                }

                for (Map<String, Object> map : list) {
                    ExceptManage exceptManage2 = new ExceptManage();
                    String recordStatus = StringUtils.valueOf(map.get("异常记录状态"));
                    if ("停炉".equals(recordStatus))
                        exceptManage2.setRecordStatus(2);
                    else
                        exceptManage2.setRecordStatus(0);
                    exceptManage2.setDeviceNo(StringUtils.valueOf(map.get("设备编号")));
                    exceptManage2.setLotNo(StringUtils.valueOf(map.get("炉次号")));
                    exceptManage2.setWorkNo(StringUtils.valueOf(map.get("工号")));
                    exceptManage2.setOperateDate(new Date());
                    exceptManage2.setUserName(BaseContextHandler.getUsername());
                    exceptManage2.setUserId(BaseContextHandler.getUserID().toString());
                    exceptManage2.setExceptionType(StringUtils.valueOf(map.get("异常类型")));
                    exceptManage2.setDetailType(StringUtils.valueOf(map.get("具体类型")));
                    exceptManage2.setRemark(StringUtils.valueOf(map.get("拉晶情况备注")));
                    exceptManage2.setBeiZhu(StringUtils.valueOf(map.get("备注")));
                    exceptManage2.setExceptionGrade(StringUtils.valueOf(map.get("二级")));
                    exceptManage2.setCauseClassification(StringUtils.valueOf(map.get("原因分类")));
                    exceptManage2.setDutyProcess(StringUtils.valueOf(map.get("责任工序")));
                    exceptManage2.setArea(StringUtils.valueOf(map.get("区域")));
                    exceptManage2.setAreaOne(StringUtils.valueOf(map.get("区域1")));
                    exceptManage2.setSeries(StringUtils.valueOf(map.get("系列")));
                    exceptManage2.setClassesName(StringUtils.valueOf(map.get("班次")));
                    exceptManage2.setTeamName(StringUtils.valueOf(map.get("班组")));
                    exceptManage2.setPersonLiable(StringUtils.valueOf(map.get("直接责任人")));
                    exceptManage2.setResponsiblePerson(StringUtils.valueOf(map.get("负责人")));
                    exceptManage2.setQuartzCrucibleManufacturer(StringUtils.valueOf(map.get("石英坩埚厂家")));
                    exceptManage2.setCrucibleMaterial(StringUtils.valueOf(map.get("坩埚材质")));
                    if (StringUtils.isNotBlank(StringUtils.valueOf(map.get("运行时间")))) {
                        try {
                            BigDecimal runTime = new BigDecimal(StringUtils.valueOf(map.get("运行时间")));
                            exceptManage2.setRunTime(runTime);
                        } catch (Exception e) {
                        }
                    }
                    if (StringUtils.isNotBlank(StringUtils.valueOf(map.get("剩料重量")))) {
                        try {
                            BigDecimal residualWeight = new BigDecimal(StringUtils.valueOf(map.get("剩料重量")));
                            exceptManage2.setResidualWeight(residualWeight);
                        } catch (Exception e) {

                        }
                    }

                    exceptManage2.setCrystalInfo(StringUtils.valueOf(map.get("拉晶情况")));
                    exceptManage2.setOperationExceptionDescription(StringUtils.valueOf(map.get("运行异常描述")));
                    exceptManage2.setReasonAnalysis(StringUtils.valueOf(map.get("原因分析")));
                    exceptManage2.setMeasure(StringUtils.valueOf(map.get("措施")));
                    exceptManage2.setAnalysisReport(StringUtils.valueOf(map.get("分析报告")));
                    exceptManage2.setAnalyst(StringUtils.valueOf(map.get("分析人")));
                    if (StringUtils.isNotBlank(StringUtils.valueOf(map.get("用料配比")))) {
                        try {
                            BigDecimal ratioMaterials = new BigDecimal(StringUtils.valueOf(map.get("用料配比")));
                            exceptManage2.setRatioMaterials(ratioMaterials);
                        } catch (Exception e) {

                        }
                    }
                    if (StringUtils.isNotBlank(StringUtils.valueOf(map.get("目标投料量")))) {
                        try {
                            BigDecimal targetMaterialsInput = new BigDecimal(StringUtils.valueOf(map.get("目标投料量")));
                            exceptManage2.setTargetMaterialsInput(targetMaterialsInput);
                        } catch (Exception e) {

                        }
                    }
                    if (StringUtils.isNotBlank(StringUtils.valueOf(map.get("实际投料量")))) {
                        try {
                            BigDecimal actualMaterialsInput = new BigDecimal(StringUtils.valueOf(map.get("实际投料量")));
                            exceptManage2.setActualMaterialsInput(actualMaterialsInput);
                        } catch (Exception e) {

                        }
                    }
                    exceptManage2.setOccurredTime(new Date());

                    exceptManage.add(exceptManage2);
                }

                this.saveAll(exceptManage);
            }
        }
    }
}
