package com.foreverwin.mesnac.dispatch.controller;

import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.dispatch.service.SurplusReturnService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 *
 * @author Leon.L
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/Z-SURPLUS-RETURN")
public class SurplusReturnController {

    @Autowired
    public SurplusReturnService surplusReturnService;

    @ResponseBody
    @GetMapping("/{inventory:.+}")
    public R getInventoryData(@PathVariable String inventory) {
        Map<String, Object> result;

        try {
            if (StringUtil.isBlank(inventory)) {
                throw BusinessException.build("物料条码不能为空！");
            }

            String site = CommonMethods.getSite();
            result = surplusReturnService.getInventoryData(site, inventory);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    @ResponseBody
    @GetMapping("/surplusReturn")
    public R surplusReturn(String inventory, String length, String width, String numbers, String qty) {
        try {
            if (StringUtil.isBlank(inventory)) {
                throw BusinessException.build("物料条码不能为空！");
            }
            if (StringUtil.isBlank(length) && StringUtil.isBlank(width)) {
                throw BusinessException.build("长度和宽度不能同时为空！");
            }
            if (StringUtil.isBlank(qty)) {
                throw BusinessException.build("数量不能为空！");
            }

            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            surplusReturnService.surplusReturn(site, user, inventory, length, width, new BigDecimal(numbers), new BigDecimal(qty));
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(0,"物料条码【" +inventory+ "】余料退回操作成功！");
    }
}