package	com.sw.mes.fineReport.service.impl;


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.fineReport.dao.FineReportDataFbDao;
import com.sw.mes.fineReport.entity.FineReportDataFb;
import com.sw.mes.fineReport.service.FineReportDataFbService;
import com.sw.mes.utils.ExcelUtil;
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 FineReportDataFbServiceImpl extends BaseServiceImpl<FineReportDataFb> implements FineReportDataFbService {
    FineReportDataFbDao dao;
	@Autowired
    public void setDao(FineReportDataFbDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }


    @Override
    public void readExcelFile(MultipartFile file) throws Exception {
        Map<String, List<Map<String, Object>>> excelMap = null;
        List<FineReportDataFb> fineReportDataFbs = new ArrayList<FineReportDataFb>();
        Date date = new Date();
        try {
            excelMap = ExcelUtil.getExcelInfo(file);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("文件导入异常！" + e.getMessage());
        }
        if (excelMap != null && !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();
                if (list.size() > 1000) {
                    throw new Exception("每批次导入限制[1000]，该批次为：" + list.size());
                }
                for (Map<String, Object> map : list) {
                    FineReportDataFb fineReportDataFb = new FineReportDataFb();

                    String id = String.valueOf(map.get("序号")).trim();
                    String dateTime = String.valueOf(map.get("时间")).trim();
                    String module = String.valueOf(map.get("模块")).trim();
                    String equipmentType = String.valueOf(map.get("设备类型")).trim();
                    String dataType = String.valueOf(map.get("数据类型")).trim();
                    String dataVal = String.valueOf(map.get("数值")).trim();

                    if ("null".equals(dateTime) || StringUtils.isBlank(dateTime)
                            || "null".equals(module) || StringUtils.isBlank(module)
                            || "null".equals(equipmentType) || StringUtils.isBlank(equipmentType)
                            || "null".equals(dataType) || StringUtils.isBlank(dataType)
                            || "null".equals(dataVal) || StringUtils.isBlank(dataVal)
                    ) {
                        continue;
                    }

                    //时间
                    if (!StringUtils.isBlank(dateTime)) {
                        try {
                            Date checkTime1 = DateUtil.toDate(dateTime, "yyyy-MM-dd");
                            fineReportDataFb.setDateTime(checkTime1);
                        } catch (Exception e) {
                            throw new Exception("序号：" + id + " 时间格式错误！");
                        }
                    }

                    //模块
                    if (!StringUtils.isBlank(module)) {
                        if (!"一模块,二模块,三模块,四模块".contains(module)){
                            throw new Exception("序号：" + id + " 模块格式错误！");
                        }
                        fineReportDataFb.setModule(module);
                    }

                    //设备类型
                    if (!StringUtils.isBlank(equipmentType)) {
                        if (!"开方机,磨削机,方棒设备".contains(equipmentType)){
                            throw new Exception("序号：" + id + " 设备类型格式错误！");
                        }
                        fineReportDataFb.setEquipmentType(equipmentType);
                    }

                    //数据类型
                    if (!StringUtils.isBlank(dataType)) {
                        fineReportDataFb.setDataType(dataType);
                    }

                    //数值
                    if (!StringUtils.isBlank(dataVal)) {
                        try {
                            BigDecimal cBorderContents = new BigDecimal(dataVal);
                            fineReportDataFb.setDataVal(cBorderContents);
                        } catch (Exception e) {
                            throw new Exception("序号：" + id + " 数值异常！");
                        }

                    }
                    fineReportDataFb.setCreateTime(new Date());
                    List<FineReportDataFb> info = this.findByModuleAndDateTimeAndEquipmentTypeAndDataType(fineReportDataFb.getModule(), fineReportDataFb.getDateTime(), fineReportDataFb.getEquipmentType(), fineReportDataFb.getDataType());
                    if (info != null && info.size() > 0){
                        throw new Exception("序号：" + id + " 数据重复！");
                    }
                    fineReportDataFbs.add(fineReportDataFb);
                }
            }
            //对晶棒数据表，和样片检验表数据进行保存以及评级
            if (fineReportDataFbs.size() > 0) {
                saveAll(fineReportDataFbs);
            }

        }
    }

    @Override
    public List<FineReportDataFb> findByModuleAndDateTimeAndEquipmentTypeAndDataType(String module, Date dateTime, String equipmentType, String dataType) {
        return dao.findByModuleAndDateTimeAndEquipmentTypeAndDataType(module, dateTime, equipmentType, dataType);
    }
}
