package com.zt.plat.module.erp.api;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.erp.api.dto.*;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpWarehouseDO;
import com.zt.plat.module.erp.service.erp.ErpMaterialService;
import com.zt.plat.module.erp.service.erp.ErpProductiveVersionService;
import com.zt.plat.module.erp.service.erp.ErpWarehouseService;
import com.zt.plat.module.erp.utils.ErpConfig;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

/**
 * ERP  Api 实现类
 *
 * @author ZT
 * @author jason
 */
@RestController
@Validated
@Component
public class ErpExternalApiImpl implements ErpExternalApi {

    @Resource
    private ErpConfig erpConfig;
    @Resource
    private ErpProductiveVersionService erpProductiveVersionService;
    @Resource
    private ErpMaterialService erpMaterialService;
    @Resource
    private ErpWarehouseService erpWarehouseService;


    @Override
    @ApiAccessLog(responseEnable = true)
    public Map<String, String> submitDataToErp(ErpSubmitReqDTO reqDTO) {
        return erpConfig.pushDataToErp(reqDTO);
    }

    @Override
    public Map<String, Object> queryDataToErp(ErpQueryReqDTO reqDTO) {
        String funcnr = reqDTO.getFuncnr();
        Map<String, Object> req = reqDTO.getReq();
        return erpConfig.fetchDataFromERP(funcnr, req);
    }

    @Override
    public CommonResult<String> getErpProductiveVersionByFM(ErpProductiveVersionReqDTO reqDTO) {
        String productiveVersionNumber = erpProductiveVersionService.getErpProductiveVersionByFM(reqDTO);
        return success(productiveVersionNumber);
    }

    @Override
    public CommonResult<String> getMaterialUnit(String materialNumber) {
        String materialUnit = erpMaterialService.getMaterialUnit(materialNumber);
        return success(materialUnit);
    }

    @Override
    public CommonResult<ErpWarehouseDTO> getErpWarehouse(Long id) {
        ErpWarehouseDO erpWarehouse = erpWarehouseService.getErpWarehouse(id.toString());
        return success(BeanUtils.toBean(erpWarehouse, ErpWarehouseDTO.class));
    }
}
