package com.shinsoft.sto.controller.main;

import com.shinsoft.sto.model.system.ResponseCodeMsg;
import com.shinsoft.sto.model.system.ResultJSON;
import com.shinsoft.sto.service.main.StackService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/MaintStack")
public class StackController {

    @Autowired
    private StackService stackService;

    @RequestMapping(value = {"/query", "/query.do"})
    public ResultJSON query( int page,
                            int rows,
                            @RequestParam(required = false) String query,
                            @RequestParam(required = false) String queryWare,
                            HttpServletRequest request) {
        try {
            String userId = resolveUserId(request);
            String belongId = resolveBelongId(request);
            return stackService.query(page, rows, query, queryWare, userId, belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }

    @RequestMapping(value = {"/queryByWareId", "/queryByWareId.do"})
    public ResultJSON queryByWareId(@RequestParam String wareId,
                                    @RequestParam(required = false) String areaId,
                                    HttpServletRequest request) {
        try {
            String userId = resolveUserId(request);
            String belongId = resolveBelongId(request);
            return stackService.queryByWareId(wareId, areaId, userId, belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }

    @RequestMapping(value = {"/save", "/save.do"}, method = RequestMethod.POST)
    public ResultJSON save(@RequestParam String json, HttpServletRequest request) {
        try {
            String userId = resolveUserId(request);
            String belongId = resolveBelongId(request);
            return stackService.save(json, userId, belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }

    @RequestMapping(value = {"/remove", "/remove.do"}, method = RequestMethod.POST)
    public ResultJSON remove(@RequestParam String id, HttpServletRequest request) {
        try {
            String userId = resolveUserId(request);
            return stackService.remove(id, userId);
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_NULL, ResponseCodeMsg.MSG_NULL);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }

    private String resolveUserId(HttpServletRequest request) {
        String userId = (String) request.getHeader("userId");
        if (userId == null) {
            userId = (String) request.getAttribute("userId");
        }
        return userId;
    }

    private String resolveBelongId(HttpServletRequest request) {
        String belongId = (String) request.getHeader("belongId");
        if (belongId == null) {
            belongId = (String) request.getAttribute("belongId");
        }
        return belongId;
    }
}

