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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.service.CommonService;
import com.foreverwin.mesnac.common.util.ExceptionUtil;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.meapi.mapper.OperationMapper;
import com.foreverwin.mesnac.meapi.mapper.ResrceMapper;
import com.foreverwin.mesnac.meapi.model.*;
import com.foreverwin.mesnac.meapi.service.ResourceTypeService;
import com.foreverwin.mesnac.meapi.service.WorkCenterMemberService;
import com.foreverwin.mesnac.meapi.service.WorkCenterService;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.meext.MEServices;
import com.foreverwin.modular.core.util.CommonMethods;
import com.sap.me.inventory.InventoryIdentifier;
import com.sap.me.inventory.InventoryServiceInterface;
import com.sap.me.inventory.InventoryStatusEnum;
import com.sap.me.inventory.InventoryValidateAndUpdateRequest;
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.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class CommonServiceImpl implements CommonService {
    @Autowired
    private WorkCenterMemberService workCenterMemberService;
    @Autowired
    private WorkCenterService workCenterService;
    @Autowired
    private OperationMapper operationMapper;
    @Autowired
    private ResourceTypeService resourceTypeService;
    @Autowired
    private ResrceMapper resrceMapper;
    @Override
    public String getWorkShopBo(String workCenterBo) {
        if (StrUtil.isBlank(workCenterBo)) {
            return null;
        }
        WorkCenterMember workCenterMember = new WorkCenterMember();
        workCenterMember.setWorkCenterOrResourceGbo(workCenterBo);
        QueryWrapper<WorkCenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(workCenterMember);
        List<WorkCenterMember> workCenterMembers = workCenterMemberService.list(queryWrapper);
        if (workCenterMembers.size() == 0) {
            return null;
        }
        if (workCenterMembers.size() > 1) {
            throw BusinessException.build("资源或工作中心"+ StringUtil.trimHandle(workCenterBo) +"不能属于多个产线");
        }
        workCenterMember = workCenterMembers.get(0);
        if (workCenterMember != null) {
            WorkCenter workCenter = workCenterService.getById(workCenterMember.getWorkCenterBo());
            if ("LEVEL4".equals(workCenter.getWcCategory())) {
                return workCenter.getHandle();
            } else {
                return getWorkShopBo(workCenter.getHandle());
            }
        }
        return null;
    }
    @Override
    public Operation getOperationBySfcBo(String sfcBo) {
        Operation operation = operationMapper.selectOperationBySfcBo(sfcBo, LocaleContextHolder.getLocale().getLanguage());
        return operation;
    }
    @Override
    public void updateInventory(String site, String inventoryId, BigDecimal measuredQty) {
        try {
            InventoryServiceInterface inventoryService = MEServices.create("com.sap.me.inventory", "InventoryService", site);
            com.sap.me.inventory.Inventory inventory = inventoryService.findInventory(new InventoryIdentifier(inventoryId));
            if (inventory != null) {
                InventoryValidateAndUpdateRequest updateRequest = new InventoryValidateAndUpdateRequest();
                updateRequest.setInventoryId(inventory.getInventoryId());
                updateRequest.setItemRef(inventory.getItemRef());
                updateRequest.setQuantityOnHand(measuredQty);
                updateRequest.setStatus(InventoryStatusEnum.AVAILABLE);
                updateRequest.setAssyDataTypeRef(inventory.getAssyDataTypeRef());
                updateRequest.setOperationLocationRef(inventory.getOperationLocRef());
                updateRequest.setOperationLocationReserve(inventory.isOperationLocRes());
                updateRequest.setShopOrderLocationRef(inventory.getShopOrderLocRef());
                updateRequest.setShopOrderLocationReserve(inventory.isShopOrderLocRes());
                updateRequest.setWorkCenterLocationRef(inventory.getWorkCenterLocRef());
                updateRequest.setWorkCenterLocationReserve(inventory.isWorkCenterLocRes());
                updateRequest.setResourceLocationRef(inventory.getResourceLocRef());
                updateRequest.setResourceLocationReserve(inventory.isResourceLocRes());
                updateRequest.setStorageLocationRef(inventory.getStorageLocationRef());
                updateRequest.setAssemblyDataList(inventory.getAssemblyDataList());
                updateRequest.setModifiedDateTime(inventory.getModifiedDateTime());
                inventoryService.validateAndUpdate(updateRequest);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtil.throwException(e);
        }
    }

    @Override
    public String getResrceByOperation(String operation) {
        String site = CommonMethods.getSite();
        String resourceTypeHandle = HandleEnum.RESOURCE_TYPE.getHandle(CommonMethods.getSite(), operation);
        ResourceType resourceTypeServiceById = resourceTypeService.getById(resourceTypeHandle);
        if (resourceTypeServiceById==null){
            throw  new BaseException("设备类型"+operation+"不存在");
        }
        List<Resrce> resrces = resrceMapper.findResourceListByResourceType(site, resourceTypeServiceById.getHandle());
        if (resrces.isEmpty()){
            throw  new BaseException("设备类型"+operation+"下没有资源");
        }
        return resrces.get(0).getHandle();
    }
}
