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


import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.annotation.ServiceLogs;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.dto.ProductionPaintingLineDetailDto1;
import com.sw.mes.core.utils.NumberUtil;
import com.sw.mes.yb.pad.dto.PdaLineInfoDto;
import com.sw.mes.yb.pad.dto.ProductionPaintingLineDetailDto;
import com.sw.mes.yb.pad.entity.ProductionPaintingLineDetail;
import com.sw.mes.yb.pad.service.ProductionPaintingLineDetailService;
import com.sw.mes.yb.pad.dao.ProductionPaintingLineDetailDao;
import com.sw.mes.yb.production.dao.LongCrystalDao;
import com.sw.mes.yb.production.entity.LongCrystal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class ProductionPaintingLineDetailServiceImpl extends BaseServiceImpl<ProductionPaintingLineDetail> implements ProductionPaintingLineDetailService {
    ProductionPaintingLineDetailDao dao;

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

    @Autowired
    LongCrystalDao longCrystalDao;

    @Autowired
    private DictionaryItemService dictionaryItemService;

    @Autowired
    private ProductionPaintingLineDetailService productionPaintingLineDetailService;

    @Override
    public ProductionPaintingLineDetail findBySegmentNo(String segmentNo) {
        List<ProductionPaintingLineDetail> bySegmentNo = this.dao.findBySegmentNo(segmentNo);
        if (bySegmentNo != null && bySegmentNo.size() > 0) {
            return bySegmentNo.get(0);
        }
        return null;
    }

    @Override
    public List<ProductionPaintingLineDetail> findBySegmentNos(List<String> segmentNos) {
        List<ProductionPaintingLineDetail> bySegmentNo = this.dao.findBySegmentNos(segmentNos);
        return bySegmentNo;
    }

    @Override
    public List<ProductionPaintingLineDetail> findByOperateDrawId(Long id) {
        return dao.findProductionPaintingLineDetailByOperateDrawId(id);

    }

    @Override
    public List<ProductionPaintingLineDetail> findByOperateReversecutDrawId(Long id) {
        return dao.findByOperateReversecutDrawId(id);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByOperateRecutDrawIdFb(Long id) {
        return dao.findByOperateRecutDrawIdFb(id);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByCrystalId(Long crystalId) {
        return dao.findByCrystalId(crystalId);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByTypeAndCrystalTypeAndCrystalId(Integer type, Integer crystalType, Long crystalId) {
        return dao.findByTypeAndCrystalTypeAndCrystalId(type, crystalType, crystalId);
    }

    @Override
    public void deleteByOperateReversecutDrawIdAndCrystalType(Long id, Integer crystalType) {
        dao.deleteByOperateReversecutDrawIdAndCrystalType(id,crystalType);
    }

    @Override
    public ProductionPaintingLineDetail findByOperateDrawIdAndAndNo(Long operateDrawId, String crystalNo) {
        return dao.findTopByOperateDrawIdAndAndNoOrderByIdDesc(operateDrawId,crystalNo);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByOperateDrawIdAndAndType(Long operateDrawId, int type) {
        return dao.findByCrystalIdAndType(operateDrawId, type);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByOperateReversecutDrawIdAndType(Long operateReversecutDrawId, int type) {
        return null;
    }

    @Override
    public List<ProductionPaintingLineDetail> findByRecycledSectionNo(String recycledSectionNo) {
        return dao.findByRecycledSectionNo(recycledSectionNo);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByRecycledSectionNoFQ(String recycledSectionNo) {
        return dao.findByRecycledSectionNoFQ(recycledSectionNo);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByRecycledCrystalNo(String crystalNo) {
        return dao.findByRecycledCrystalNo(crystalNo);
    }

    @Override
    public List<String> findDetailByCrystalNo(String crystalNo) {
        return dao.findDetailByCrystalNo(crystalNo);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByOperateReversecutDrawId(Long operateReversecutDrawId, Integer crystalType, Integer isCut) {
        return dao.findByOperateReversecutDrawId(operateReversecutDrawId, crystalType, isCut);
    }

    @Override
    public List<ProductionPaintingLineDetail> findByOperateReversecutDrawIdAndCrystalIdAndType(Long operateReversecutDrawId, Long crystalId, Integer type) {
        return dao.findByOperateReversecutDrawIdAndCrystalIdAndType(operateReversecutDrawId, crystalId, type);
    }


    @Override
    public List<String> findByCrystalIdAndType(Integer crystalId, Integer type, Integer crystalType) {
        return dao.findByCrystalIdAndType(crystalId, type, crystalType);
    }

    @Override
    public List<ProductionPaintingLineDetail> findType(Integer crystalId) {
        return dao.findType(crystalId);
    }

    @Override
    @ServiceLogs(description = "晶段免检")
    public Boolean flowDirectionV2(String crystalNo, String sectionNo, String source) {
        try {
            //完结尾、或者完结棒后三段 给完结棒
            List<DictionaryItem> dictionaryItem1s = dictionaryItemService.findByThemeValueAndItemName("centre_exemption", "开关", true);
            if (!dictionaryItem1s.isEmpty()) {
                DictionaryItem dictionaryItem = dictionaryItem1s.get(0);
                String extendValue2 = dictionaryItem.getExtendValue2();
                // 0：关，1：开
                if("0".equals(dictionaryItem.getValue())){
                    return false;
                }
                Integer integer = NumberUtil.Object2Integer(extendValue2, 0);
                if(integer > 0) {
                    LongCrystal byCrystalNo = longCrystalDao.findByCrystalNo(crystalNo);
                    if (null != byCrystalNo) {
                        if ("W".equals(byCrystalNo.getClosureIdentification()) || "V".equals(byCrystalNo.getClosureIdentification())) {
                            List<ProductionPaintingLineDetail> byCrystalIdAndType = findType(byCrystalNo.getId().intValue());
                            if (!byCrystalIdAndType.isEmpty()) {
                                Map<String, List<ProductionPaintingLineDetail>> collect =
                                        byCrystalIdAndType.stream().collect(Collectors.groupingBy(ProductionPaintingLineDetail::getNo));
                                if (collect.containsKey(sectionNo)) {
                                    if (byCrystalIdAndType.size() <= integer) {
                                        return true;
                                    } else {
                                        int size = byCrystalIdAndType.size();
                                        List<ProductionPaintingLineDetail> productionPaintingLineDetails = collect.get(sectionNo);
                                        ProductionPaintingLineDetail productionPaintingLineDetail = productionPaintingLineDetails.get(0);
                                        Integer segmentNum = productionPaintingLineDetail.getSegmentNum();
                                        int value = size - segmentNum;
                                        if (value <= integer -1) {
                                            // 后三段给 完结尾
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return false;
    }

    @Override
//    @ServiceLogs(description = "中间段免检")
    public Map<String, String> flowDirection(String sectionNo, String source) {
        Map<String,String> map = new HashMap<>();
//        Boolean aBoolean = productionPaintingLineDetailService.flowDirectionV2(sectionNo.substring(0, 12),sectionNo,source);
//        if(aBoolean){
//            map.put("isAvoidCheck","1");
//        }
//        List<DictionaryItem> dictionaryItem1s = dictionaryItemService.findByThemeValueAndItemName("centre_exemption", "开关", true);
//        if(dictionaryItem1s.isEmpty()){
//            map.put("index","0");
//            return map;
//        }
//        DictionaryItem dictionaryItem = dictionaryItem1s.get(0);
//        // 0：关，1：开
//        if("0".equals(dictionaryItem.getValue())){
//            map.put("index","-1");
//            return map;
//        }
//        LongCrystal byCrystalNo = longCrystalDao.findByCrystalNo(sectionNo.substring(0,12));
//        if(null != byCrystalNo){
//            if("W".equals(byCrystalNo.getClosureIdentification())){
//                // 完结棒
//                List<String> byCrystalIdAndType = findByCrystalIdAndType(byCrystalNo.getId().intValue(), 0, 0);
//                if(!byCrystalIdAndType.isEmpty()){
//                    int size = byCrystalIdAndType.size();
//                    if(size >= 6){
//
//                        int index = byCrystalIdAndType.indexOf(sectionNo);
//                        // 头段与后4段
//                        if(index <= 0 || index >= (size - 4)){
//                            map.put("isAvoidCheck","0");
//                            map.put("index","3");
//                        } else {
//                            // 中间段
//                            map.put("isAvoidCheck","1");
//                            map.put("index","4");
//                        }
//                    } else {
//                        // 小于6段
//                        map.put("isAvoidCheck","0");
//                        map.put("index","5");
//                    }
//                }
//            } else {
//                // 循环段
//                if(sectionNo.endsWith("1Y") || sectionNo.endsWith("WY")){
//                    // 头尾
//                    map.put("isAvoidCheck","0");
//                    map.put("index","1");
//                } else {
//                    // 中间段
//                    map.put("isAvoidCheck","1");
//                    map.put("index","2");
//                }
//            }
//        }
        return map;
    }

    @Override
    public PdaLineInfoDto findDetailByNo(String no) {
        ProductionPaintingLineDetail detail = dao.findDetailByNo(no);
        PdaLineInfoDto pdaLineInfoDto = new PdaLineInfoDto();
        if (null != detail) {
            pdaLineInfoDto.setSectionNo(no);
            if (no.endsWith("P")) {
                pdaLineInfoDto.setMaterialType("样片");
            } else {
                pdaLineInfoDto.setMaterialType("回收料");
            }
        } else {
            if (no.endsWith("T") || no.endsWith("W")) {
                String substring = no.substring(0, no.length() - 1);
                LongCrystal longCrystal = longCrystalDao.likeByCrystalNo("%" + substring + "%");
                if (null != longCrystal) {
                    pdaLineInfoDto.setSectionNo(no);
                    pdaLineInfoDto.setMaterialType("回收料");
                }
            } else {
                pdaLineInfoDto = null;
            }
        }
        return pdaLineInfoDto;
    }

	@Override
	public ProductionPaintingLineDetail getDetailByNo(String no) {
		return dao.findDetailByNo(no);
	}

}
