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.UserWareService;
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 = "/MaintUserWare")
public class UserWareController {
    @Autowired
    public UserWareService userWareService;
    String result = Type.FAIL;

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

        try {
           // String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = userWareService.queryByUserId(userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
//
//    @RequestMapping(value = "/queryAll.do")//查询方法 前两个参数是分页参数
//    public String queryAll(HttpServletRequest request) {
//
//        try {
//            String userId = (String) request.getAttribute("userId");
//            String belongId = (String) request.getAttribute("belongId");
//            result = userWareService.queryAll(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 selectUserId,
                       String selectWareId,
                       HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = userWareService.save(selectUserId, selectWareId,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 = userWareService.remove(id, userId);
//        } catch (NullPointerException ex) {
//            ex.printStackTrace();
//            result = JSONTools.toString(ResponseCodeMsg.CODE_NULL, ResponseCodeMsg.MSG_NULL);
//        } finally {
//            return result;
//        }
//    }
////

}
