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


import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.inspect.service.YbInspectDataService;
import com.sw.mes.yb.pad.service.ProductionPaintingLineDetailService;
import com.sw.mes.yb.pc.dao.SideSkinCodingRuleDao;
import com.sw.mes.yb.pc.entity.SideSkinCodingRule;
import com.sw.mes.yb.pc.service.SideSkinCodingRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class SideSkinCodingRuleServiceImpl extends BaseServiceImpl<SideSkinCodingRule> implements SideSkinCodingRuleService {
    SideSkinCodingRuleDao dao;

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

    @Autowired
    DictionaryItemService dictionaryItemService;

    @Autowired
    private YbInspectDataService inspectDataBaseService;

    @Autowired
    private ProductionPaintingLineDetailService productionPaintingLineDetailService;

    @Override
    public Integer findCountBySkinName(String skinName) {
        return dao.findCountBySkinName(skinName);
    }

    @Override
    public SideSkinCodingRule findBySegmentInfo(String area, String series, String thermal, String lotNo, String specs, Integer againNum, Integer againCount, Integer lotCount, String piecewiseNum) {
        List<SideSkinCodingRule> sideSkinCodingRuleList = dao.findBySegmentInfo(area, series, thermal, lotNo, specs, againNum, againCount, lotCount, piecewiseNum);
        if (sideSkinCodingRuleList.size() > 0) {
            return sideSkinCodingRuleList.get(0);
        }
        return null;
    }

    @Override
    public SideSkinCodingRule findByYbSegmentNo(String ybSegmentNo) {

        //模块
        String area = null;
        //系列
        String series;
        // 热场
        String thermal = null;
        //炉次号
        String lotNo;
        //产品规格
        String specs;
        //复投次数
        Integer againNum;
        //复投第几颗
        Integer againCount;
        //炉次第几根
        Integer lotCount;
        //画线长度min
        BigDecimal lineMin = null;
        //画线长度max
        BigDecimal lineMax = null;
        //段号
        String piecewiseNum;

        //搭配方案
        String techBom = null;
        try {
            //解析晶段编码 CIGC2952201A0211D02Y04
            String[] strings = ybSegmentNo.split("");
            String areaCode = strings[1];
            List<DictionaryItem> dictionaryItemList = dictionaryItemService.findItemByThemeValue("module", true);
            for (DictionaryItem dictionaryItem : dictionaryItemList) {
                if (dictionaryItem.getExtendValue().equals(areaCode)) {
                    area = dictionaryItem.getValue();
                    break;
                }
            }
            series = strings[11];
            lotNo = strings[7] + strings[8] + strings[9] + strings[10] + strings[11] + strings[12] + strings[13] + strings[14] + strings[15];
            specs = strings[4] + strings[5] + strings[6];
            againNum = Integer.valueOf(strings[17]);
            againCount = Integer.valueOf(strings[18]);
            piecewiseNum = strings[20] + strings[21];

        } catch (Exception e) {
            throw new RuntimeException("解析晶段编号异常");
        }

        List<SideSkinCodingRule> sideSkinCodingRuleList = dao.findBySegmentInfo(area, series, null, lotNo, specs, againNum, againCount, null, piecewiseNum);
        if (sideSkinCodingRuleList.size() > 0) {
            return sideSkinCodingRuleList.get(0);
        }
        return null;
    }


}
