package com.foreverwin.mesnac.dispatch.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.dispatch.dto.IssueItemDto;
import com.foreverwin.mesnac.dispatch.model.CallItem;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.CommonMethods;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mesnac.dispatch.service.IssueItemService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Leon.L
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/Z-ISSUE-ITEM")
public class IssueItemController {

    @Autowired
    public IssueItemService issueItemService;

    @ResponseBody
    @GetMapping("/scanIssueInventory")
    public R scanIssueInventory(String item, String inventory, Boolean oddments){
        Map<String, Object> result;
        try {
            String site = CommonMethods.getSite();
            result = issueItemService.scanIssueInventory(site, item, inventory, oddments);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }


    @ResponseBody
    @PostMapping("/issueItem")
    public R issueItem(@RequestBody JSONObject jsonObject){
        try {
            if (jsonObject == null) {
                throw BusinessException.build("请添加发料清单！");
            }
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();

            issueItemService.issueItem(site, user, jsonObject);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }
}