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

import com.alibaba.excel.annotation.ExcelProperty;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.enums.EnumValuesFactory;
import com.sw.mes.core.exception.BusinessException;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.inspect.dao.YbOemDataDao;
import com.sw.mes.inspect.dto.YbInspectDataCUDto;
import com.sw.mes.inspect.entity.YbInspectData;
import com.sw.mes.inspect.entity.YbOemData;
import com.sw.mes.inspect.service.YbOemDataService;
import com.sw.mes.utils.ExcelUtil;
import com.sw.mes.utils.StringUtils;
import com.sw.mes.yb.pad.entity.ProductionPaintingLineDetail;
import com.sw.mes.yb.pad.entity.SectionNoDetail;
import com.sw.mes.yb.production.entity.LongCrystal;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.*;

/**
 *
 */
@Service
@Slf4j
public class YbOemDataServiceImpl extends BaseServiceImpl<YbOemData> implements YbOemDataService {

    YbOemDataDao dao;

    @Autowired
    public void setDao(YbOemDataDao dao) {
        super.setBaseDao(dao);
        this.dao = dao;
    }


    @Override
    public List<YbOemData> getList() {
        return dao.findAll();
    }

    @Override
    @Transactional
    public Result readOemImport(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()) {
            Collection<List<Map<String, Object>>> collection = excelMap.values();
            Iterator<List<Map<String, Object>>> iterator = collection.iterator();
            //遍历excel文件的内容
            ArrayList<YbOemData> ybOemList = new ArrayList<>();
            while (iterator.hasNext()) {
                List<Map<String, Object>> list = iterator.next();
                for (Map<String, Object> map : list) {
                    YbOemData ybOemData = new YbOemData();
                    //1对单晶条码编号的校验
                    String ybSectionNo = StringUtils.valueOf(map.get("单晶编号")).toUpperCase();
                    if (StringUtils.isBlank(ybSectionNo)) {
                        throw new Exception("请输入晶段编号！");
                    }
                    ybOemData.setSectionNo("B" + ybSectionNo);
                    String trayNo = StringUtils.valueOf(map.get("托盘编号")).toUpperCase();
                    if (StringUtils.isBlank(trayNo)) {
                        throw new Exception("请输入托盘编号！");
                    }
                    ybOemData.setTrayNo(trayNo);
                    String productTypeSecond = StringUtils.valueOf(map.get("实际规格")).toUpperCase();
                    if (StringUtils.isBlank(productTypeSecond)) {
                        throw new Exception("请输入实际规格！");
                    }
                    ybOemData.setProductTypeSecond(productTypeSecond);
                    String dopant = StringUtils.valueOf(map.get("掺杂剂")).toUpperCase();
                    if (StringUtils.isBlank(dopant)) {
                        throw new Exception("请输入掺杂剂！");
                    }
                    ybOemData.setDopant(dopant);
                    String conductiveType = StringUtils.valueOf(map.get("导电型号")).toUpperCase();
                    if (StringUtils.isBlank(conductiveType)) {
                        throw new Exception("请输入导电型号！");
                    }
                    ybOemData.setConductiveType(conductiveType);
                    String actualLength = StringUtils.valueOf(map.get("圆棒长度")).toUpperCase();
                    if (StringUtils.isBlank(actualLength)) {
                        throw new Exception("请输入圆棒长度！");
                    }
                    ybOemData.setActualLength(new BigDecimal(actualLength));
                    String sideLengthRange = StringUtils.valueOf(map.get("方棒边长"));
                    if (StringUtils.isBlank(sideLengthRange)) {
                        throw new Exception("请输入方棒边长！");
                    }
                    ybOemData.setSideLengthRange(sideLengthRange);

                    String theoryWeight = StringUtils.valueOf(map.get("理论重量"));
                    if (StringUtils.isBlank(theoryWeight)) {
                        throw new Exception("请输入理论重量！");
                    }
                    ybOemData.setTheoryWeight(new BigDecimal(theoryWeight));

                    String productLevel = StringUtils.valueOf(map.get("产品等级"));
                    if (StringUtils.isBlank(productLevel)) {
                        throw new Exception("请输入产品等级！");
                    }
                    ybOemData.setProductLevel(productLevel);
                    String productType = StringUtils.valueOf(map.get("产品类别"));
                    if (StringUtils.isBlank(productType)) {
                        throw new Exception("请输入产品类别！");
                    }
                    ybOemData.setProductType(productType);
                    String diameterMax = StringUtils.valueOf(map.get("最大直径"));
                    if (StringUtils.isBlank(diameterMax)) {
                        throw new Exception("请输入最大直径！");
                    }
                    ybOemData.setDiameterMax(new BigDecimal(diameterMax));

                    String diameterMin = StringUtils.valueOf(map.get("最小直径"));
                    if (StringUtils.isBlank(diameterMin)) {
                        throw new Exception("请输入最小直径！");
                    }
                    ybOemData.setDiameterMin(new BigDecimal(diameterMin));

                    String headRadiaCentre = StringUtils.valueOf(map.get("电阻率T中心点"));
                    if (StringUtils.isBlank(headRadiaCentre)) {
                        throw new Exception("请输入电阻率T中心点！");
                    }
                    ybOemData.setTRadialCentre(new BigDecimal(headRadiaCentre));
                    //电阻率T边缘1
                    String radiaCentreEdge1 = StringUtils.valueOf(map.get("电阻率T边缘1")).toUpperCase();
                    if (StringUtils.isBlank(radiaCentreEdge1)) {
                        throw new Exception("请输入电阻率T边缘1！");
                    }
                    ybOemData.setTRadialEdge1(new BigDecimal(radiaCentreEdge1));
                    //电阻率T边缘2
                    String radiaCentreEdge2 = StringUtils.valueOf(map.get("电阻率T边缘2")).toUpperCase();
                    if (StringUtils.isBlank(radiaCentreEdge2)) {
                        throw new Exception("请输入电阻率T边缘2！");
                    }
                    ybOemData.setTRadialEdge2(new BigDecimal(radiaCentreEdge2));
                    //电阻率W中心点
                    String tailRadiaCentre = StringUtils.valueOf(map.get("电阻率W中心点")).toUpperCase();
                    if (StringUtils.isBlank(tailRadiaCentre)) {
                        throw new Exception("请输入电阻率W中心点！");
                    }
                    ybOemData.setWRadialCentre(new BigDecimal(tailRadiaCentre));
                    //电阻率W边缘1
                    String tailCentreEdge1 = StringUtils.valueOf(map.get("电阻率W边缘1")).toUpperCase();
                    if (StringUtils.isBlank(tailCentreEdge1)) {
                        throw new Exception("请输入电阻率W边缘1！");
                    }
                    //ybOemData.setWRadialEdge1(new BigDecimal(tailCentreEdge1));
                    //电阻率W边缘2
                    String tailCentreEdge2 = StringUtils.valueOf(map.get("电阻率W边缘2")).toUpperCase();
                    if (StringUtils.isBlank(tailCentreEdge2)) {
                        throw new Exception("请输入电阻率W边缘2！");
                    }
                    ybOemData.setWRadialEdge2(new BigDecimal(tailCentreEdge2));
                    //T氧含量中心
                    String oxygenContentHead = StringUtils.valueOf(map.get("T氧含量中心")).toUpperCase();
                    if (StringUtils.isBlank(oxygenContentHead)) {
                        throw new Exception("请输入T氧含量中心！");
                    }
                    ybOemData.setOxygenContentHead(new BigDecimal(oxygenContentHead));
                    //T碳含量中心
                    String carbonContentHead = StringUtils.valueOf(map.get("T碳含量中心")).toUpperCase();
                    if (StringUtils.isBlank(carbonContentHead)) {
                        throw new Exception("请输入T碳含量中心！");
                    }
                    ybOemData.setCarbonContentHead(new BigDecimal(carbonContentHead));
                    //W氧含量中心
                    String oxygenContentTail = StringUtils.valueOf(map.get("W氧含量中心")).toUpperCase();
                    if (StringUtils.isBlank(oxygenContentTail)) {
                        throw new Exception("请输入W氧含量中心！");
                    }
                    ybOemData.setOxygenContentTail(new BigDecimal(oxygenContentTail));
                    //W碳含量中心
                    String carbonContentTail = StringUtils.valueOf(map.get("W碳含量中心")).toUpperCase();
                    if (StringUtils.isBlank(carbonContentTail)) {
                        throw new Exception("请输入W碳含量中心！");
                    }
                    ybOemData.setCarbonContentTail(new BigDecimal(carbonContentTail));

                    //T中心寿命
                    String tCentreLife = StringUtils.valueOf(map.get("T中心寿命")).toUpperCase();
                    if (StringUtils.isBlank(tCentreLife)) {
                        throw new Exception("请输入T中心寿命！");
                    }
                    ybOemData.setTCentreLife(new BigDecimal(tCentreLife));

                    //T边缘寿命1
                    String tLifeEdge1 = StringUtils.valueOf(map.get("T边缘寿命1")).toUpperCase();
                    if (StringUtils.isBlank(tLifeEdge1)) {
                        throw new Exception("请输入T边缘寿命1！");
                    }
                    ybOemData.setTLifeEdge1(new BigDecimal(tLifeEdge1));

                    //T边缘寿命2
                    String tLifeEdge2 = StringUtils.valueOf(map.get("T边缘寿命2")).toUpperCase();
                    if (StringUtils.isBlank(tLifeEdge2)) {
                        throw new Exception("请输入T边缘寿命2！");
                    }
                    ybOemData.setTLifeEdge2(new BigDecimal(tLifeEdge2));

                    //W中心寿命
                    String wCentreLife = StringUtils.valueOf(map.get("W中心寿命")).toUpperCase();
                    if (StringUtils.isBlank(wCentreLife)) {
                        throw new Exception("请输入W中心寿命！");
                    }
                    ybOemData.setWCentreLife(new BigDecimal(wCentreLife));

                    //W边缘寿命1
                    String wLifeEdge1 = StringUtils.valueOf(map.get("W边缘寿命1")).toUpperCase();
                    if (StringUtils.isBlank(carbonContentTail)) {
                        throw new Exception("请输入W边缘寿命1！");
                    }
                    ybOemData.setWLifeEdge1(new BigDecimal(wLifeEdge1));

                    //W边缘寿命2
                    String wLifeEdge2 = StringUtils.valueOf(map.get("W边缘寿命2")).toUpperCase();
                    if (StringUtils.isBlank(wLifeEdge2)) {
                        throw new Exception("请输入W边缘寿命2！");
                    }
                    ybOemData.setWLifeEdge2(new BigDecimal(wLifeEdge2));


                    ybOemData.setCreateUser(BaseContextHandler.getUsername());
                    ybOemData.setUpdateUser(BaseContextHandler.getUsername());
                    ybOemData.setCreateTime(new Date());
                    ybOemData.setUpdateTime(new Date());
                    ybOemData.setStatus(0);
                    ybOemList.add(ybOemData);
                    List<YbOemData> bySectionNo = dao.findBySectionNo(ybOemData.getSectionNo());
                    if (!CollectionUtils.isEmpty(bySectionNo)) {
                        throw new BusinessException("单晶编号:" + ybOemData.getSectionNo() + "已经存在!");
                    }
                }
            }
            dao.saveAll(ybOemList);
            return ResultGenerator.success();
        }
        return ResultGenerator.fail("数据有误，导入失败");
    }

    @Override
    public void updateStatus(Long id, int status) {
        dao.UpdateState(id, status);
    }

}