package com.shinsoft.sto.controller.main;

import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.model.common.ResponseCodeMsg;
import com.shinsoft.tools.model.common.ResultJSON;
import com.shinsoft.sto.service.main.WareService;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/Ware")
public class WareController {

    @Autowired
    private WareService wareService;

    ResultJSON resultJSON;

    @RequestMapping(value = "/query")
    public ResultJSON query(int page, int rows,
                            @RequestParam(required = false) String wareNm,
                            HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            Map<String, Object> map = new HashMap<>();
            map.put("wareNm",wareNm);
            resultJSON = wareService.query(page, rows, map);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

    @RequestMapping(value = "/queryByPK")
    public ResultJSON queryByPK(String id, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = wareService.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 = wareService.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 = wareService.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 = wareService.removeBatch(userId, ids);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }
}

