package com.th.demo.controller.ware;

import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.Type;
import com.th.demo.service.ware.CombineService;
import com.th.demo.service.ware.DeficitService;
import com.th.demo.tools.JSONTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/WareDeficit")
public class DeficitController {

    @Autowired
    public DeficitService deficitService;
    String result = Type.FAIL;

    @RequestMapping(value = "/deficit.do", method = RequestMethod.POST)
    public String deficit(String json,
                     String reason,
                     HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = deficitService.deficit(json,reason,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }


    @RequestMapping(value = "/cancel.do", method = RequestMethod.POST)
    public String cancel(String json,
                          String reason,
                          String toStack,
                          int layer,
                          HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = deficitService.cancel(json,reason,toStack,layer,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

}
