package com.th.demo.controller.maint;

import com.alibaba.fastjson.JSON;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.ResultJSON;
import com.th.demo.model.system.Type;
import com.th.demo.service.maint.StackService;
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 = "/MaintStack")
public class StackController {
    @Autowired
    public StackService stackService;
    String result = Type.FAIL;

    @RequestMapping(value = "/query.do")//查询方法 前两个参数是分页参数
    public String query(int page,
                        int rows,
                        String query,
                        String queryWare,
                        HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = stackService.query(page, rows, query,   queryWare,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }




    @RequestMapping(value = "/queryByWareId.do")//查询方法 前两个参数是分页参数
    public String queryByWareId(
         String   wareId, String areaId ,HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = stackService.queryByWareId(wareId,areaId,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }


    @RequestMapping(value = "/save.do", method = RequestMethod.POST)//Post方法
    public String save(String json,
                       HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = stackService.save(json, userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/remove.do", method = RequestMethod.POST)
    public String remove(String id,
                         HttpServletRequest request)  {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = stackService.remove(id, userId);
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_NULL, ResponseCodeMsg.MSG_NULL);
        } finally {
            return result;
        }
    }
//

}
