package com.shinsoft.sto.controller.main;

import com.shinsoft.sto.service.main.AreaService;
import com.shinsoft.sto.service.main.WareService;
import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.model.common.ResponseCodeMsg;
import com.shinsoft.tools.model.common.ResultJSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/Area")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @Autowired
    private WareService wareService;

    ResultJSON resultJSON;

    @GetMapping("/query")
    public ResultJSON query(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "50") int rows,
                            @RequestParam(required = false) String wareId,
                            HttpServletRequest request) {
        try {
            return areaService.query(page, rows, wareId);
        } catch (Exception ex) {
            ex.printStackTrace();
            return JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        }
    }

    @RequestMapping(value = "/queryByPK")
    public ResultJSON queryByPK(String id, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = areaService.queryByPK(id);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

    @RequestMapping(value = "/save")
    public ResultJSON save(String json, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = areaService.save(userId, json);
        } catch (Exception ex) {
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
            ex.printStackTrace();
        } finally {
            return resultJSON;
        }
    }

    @RequestMapping(value = "/remove")
    public ResultJSON remove(String id, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = areaService.remove(userId, id);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

    @RequestMapping(value = "/removeBatch")
    public ResultJSON removeBatch(String ids, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = areaService.removeBatch(userId, ids);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

    @RequestMapping(value = "/wareOptions")
    public ResultJSON wareOptions(HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = wareService.getWareOptions();
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }
}

