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

import com.sw.mes.base.factory.utils.PageUtils;
import com.sw.mes.core.api.system.FbFeignClients;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.entity.FbCheckOutData;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.inspect.entity.YbInspectData;
import com.sw.mes.inspect.service.YbInspectDataService;
import com.sw.mes.yb.pad.dao.*;
import com.sw.mes.yb.pad.entity.*;
import com.sw.mes.yb.pad.entity.dto.EapCheckSheetDto;
import com.sw.mes.yb.pad.service.CheckSheetService;
import com.sw.mes.yb.production.entity.LongCrystal;
import com.sw.mes.yb.production.service.LongCrystalService;
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.util.CollectionUtils;

import java.util.*;

@Service
@Transactional
public class CheckSheetServiceImpl extends BaseServiceImpl<CheckSheet> implements CheckSheetService {

    @Autowired
    private FbFeignClients fbFeignClients;

    @Autowired
    private YbInspectDataService ybInspectDataService;

    @Autowired
    private LongCrystalService longCrystalService;

    CheckSheetDao checkSheetDao;

    @Autowired
    public void setDao(CheckSheetDao checkSheetDao) {
        this.checkSheetDao = checkSheetDao;
        super.setBaseDao(checkSheetDao);
    }

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

    @Override
    public Result processData(EapCheckSheetDto eapCheckSheetDto) {
        if (null == eapCheckSheetDto) {
            return ResultGenerator.fail("数据为空");
        }
        Map<String, Object> startCollectionData = eapCheckSheetDto.getStartCollectionData();

        for(int i = 64; i <= 70; i = i+2){
            if(!startCollectionData.containsKey(i + "")){
                continue;
            }
            Integer status = Integer.valueOf(String.valueOf(startCollectionData.get(i + 1 + "")));
            String sectionNo = startCollectionData.get(i + "").toString();
            CheckSheet checkSheet = new CheckSheet();
            String deviceCode = eapCheckSheetDto.getDeviceCode();
            checkSheet.setLocation(deviceCode);
            checkSheet.setSectionNo(sectionNo);
            checkSheet.setCreateDate(new Date());
            checkSheet.setCreateUser("自动采集");
            switch (status) {
                case 1:
                    checkSheet.setMaterialType("圆棒");
                    break;
                case 3:
                    checkSheet.setMaterialType("半方棒");
                    break;
                default:
                    checkSheet.setMaterialType("半成品方棒");
                    break;
            }
            checkSheet.setMaterialStatus("半成品");
            getData(checkSheet);
            CheckSheet bySectionNo = findBySectionNo(checkSheet.getSectionNo());
            if(null != bySectionNo){
                checkSheet.setId(bySectionNo.getId());
            }
            checkSheetDao.save(checkSheet);
        }
        return ResultGenerator.success();
    }

    @Override
    public CheckSheet findBySectionNo(String sn) {
        return checkSheetDao.findBySectionNo(sn);
    }


    public void getData(CheckSheet checkSheet) {
        String sectionNo = checkSheet.getSectionNo();
        Result<FbCheckOutData> fbCheckOutDataByFbSectionNo = fbFeignClients.findFbCheckOutDataByFbSectionNo(sectionNo);
        if (null != fbCheckOutDataByFbSectionNo.getData()) {
            FbCheckOutData data = fbCheckOutDataByFbSectionNo.getData();
            checkSheet.setLength(data.getLength());
            checkSheet.setDopant(data.getDopant());
            checkSheet.setSpec(data.getSpecification());
        }

        YbInspectData bySectionNo = ybInspectDataService.findBySectionNo(sectionNo);
        if (null != bySectionNo) {
            checkSheet.setLength(bySectionNo.getActualLength());
            checkSheet.setDopant(bySectionNo.getDopant());
            checkSheet.setLevel(bySectionNo.getProductLevel());
            checkSheet.setSpec(bySectionNo.getProductTypeSecond());
        }

        LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(sectionNo);
        if (null != byCrystalNo) {
            checkSheet.setLength(byCrystalNo.getLength());
            checkSheet.setDopant(byCrystalNo.getDopant());
            checkSheet.setSpec(byCrystalNo.getSpecs());
        }
    }
}
