package com.foreverwin.mesnac.production.service.impl;


import com.foreverwin.mesnac.common.constant.CustomFieldConstant;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.meapi.dto.SfcDto;
import com.foreverwin.mesnac.meapi.service.CustomFieldsService;
import com.foreverwin.mesnac.production.mapper.SfcCrossMapper;
import com.foreverwin.mesnac.production.service.SfcCrossService;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.meext.MEServices;
import com.foreverwin.modular.core.util.CommonMethods;
import com.sap.me.plant.ResourceBOHandle;
import com.sap.me.production.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 描述: 工序开始
 * 作者: philip
 * 时间: 2021-06-30
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SfcCrossServiceImpl implements SfcCrossService {
    @Autowired
    private SfcCrossMapper sfcCrossMapper;
    @Autowired
    private CustomFieldsService customFieldsService;
    @Override
    public void startAction(String site, String operationRef,
                            String resource, String sfcRef, BigDecimal qty) throws Exception {
        SfcStartServiceInterface sfcStartService = MEServices.create("com.sap.me.production", "SfcStartService", site);
        StartSfcRequest paramStartSfcRequest = new StartSfcRequest();
        paramStartSfcRequest.setQty(qty);
        paramStartSfcRequest.setOperationRef(operationRef);
        paramStartSfcRequest.setResourceRef(new ResourceBOHandle(site, resource).getValue());
        paramStartSfcRequest.setSfcRef(sfcRef);
        paramStartSfcRequest.setUserRef(CommonMethods.getUserBo());
        sfcStartService.startSfc(paramStartSfcRequest);
    }

    @Override
    public void completeAction(String site, String operationRef,
                               String resource, String sfcRef, BigDecimal qty) throws Exception {
        SfcCompleteServiceInterface sfcCompleteService = MEServices.create("com.sap.me.production", "SfcCompleteService", site);
        CompleteSfcRequest paramCompleteSfcRequest = new CompleteSfcRequest();
        paramCompleteSfcRequest.setOperationRef(operationRef);
        paramCompleteSfcRequest.setResourceRef(new ResourceBOHandle(site, resource).getValue());
        paramCompleteSfcRequest.setSfcRef(sfcRef);
        if(qty != null) {
            paramCompleteSfcRequest.setQuantity(qty);
        }
        paramCompleteSfcRequest.setLocation("");
        sfcCompleteService.completeSfc(paramCompleteSfcRequest);
    }

    @Override
    public void passAction(String site, String operationRef,
                           String resource, String sfcRef, BigDecimal qty) throws Exception {
        SfcCompleteServiceInterface sfcCompleteService = MEServices.create("com.sap.me.production", "SfcCompleteService", site);
        CompleteSfcQuickRequest paramCompleteSfcRequest = new CompleteSfcQuickRequest();
        paramCompleteSfcRequest.setOperationRef(operationRef);
        paramCompleteSfcRequest.setResourceRef(new ResourceBOHandle(site, resource).getValue());
        paramCompleteSfcRequest.setSfcRef(sfcRef);
        if(qty != null) {
            paramCompleteSfcRequest.setQuantity(qty);
        }
        sfcCompleteService.completeSfcQuick(paramCompleteSfcRequest);
    }

    @Override
    public List<SfcDto> getSfcListByResrce(String site, String resrce) {
        String locale = LocaleContextHolder.getLocale().getLanguage();
        return sfcCrossMapper.getSfcListByResrce(site,resrce,locale);
    }

    @Override
    public void resourceCheck(String resrce) {
        LocalDateTime now = LocalDateTime.now();
        String site = CommonMethods.getSite();
        String handle = HandleEnum.RESOURCE.getHandle(site, resrce);
        String isSpot = customFieldsService.getCustomFieldsValue(handle, CustomFieldConstant.RESRCE_CUSTOM_FIELD_IS_SPOT);
        if (StringUtil.isBlank(isSpot)||!isSpot.equals("N")){
            LocalDateTime dateTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 0, 0, 0);
            Integer integer = sfcCrossMapper.resourceCheck(site,resrce, dateTime);
            if (integer<1){
                throw new BaseException("当前设备点检未完成");
            }
        }

    }
}
