package com.shinsoft.sto.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.ExceptionTools;
import com.shinsoft.tools.model.common.ResultJSON;
import com.shinsoft.tools.model.common.ResponseCodeMsg;

import com.shinsoft.sto.service.OnhandService;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 库存现有量 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-10-08
 */
@RestController
//@RequestMapping("/onhand")
@RequestMapping("/Onhand")
public class OnhandController {


    @Autowired
    private OnhandService onhandService;

    ResultJSON resultJSON;


    @RequestMapping(value = "/query")
    public ResultJSON query(int page, int rows,
                            @RequestParam(required = false) String storeNm,
                            @RequestParam(required = false) String areaCd,
                            @RequestParam(required = false) String stackCd,
                            @RequestParam(required = false) String itemNm,
                            @RequestParam(required = false) String itemType,
                            String params, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            JSONObject jsonObject = JSON.parseObject(params);
            if (ObjectUtils.isEmpty(jsonObject)) {
                jsonObject = new JSONObject();
            }
//               jsonObject.put("storeNm" , storeNm);
//               jsonObject.put("areaCd" , areaCd);
//               jsonObject.put("stackCd" , stackCd);
//               jsonObject.put("itemNm" , itemNm);
//               jsonObject.put("itemType" , itemType);

            resultJSON = onhandService.query(page, rows, jsonObject);
        } 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 = onhandService.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 = onhandService.save(userId, json);
        } catch (Exception ex) {
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ExceptionTools.getExceptionAllinformation(ex));
            ex.printStackTrace();
        } finally {
            return resultJSON;
        }
    }

    @RequestMapping(value = "/remove")
    public ResultJSON remove(String id, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = onhandService.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 = onhandService.removeBatch(userId, ids);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }
    /**
     * 根据物料，查询库存现有量。
     * @param params
     * @return
     */
    @RequestMapping(value = "/queryOnhand")
    public ResultJSON queryOnhand(String params, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = onhandService.queryOnhand(userId, params);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

//销售出库查询物料库房信息
    @RequestMapping(value = "/queryDelvOut")
    public ResultJSON queryDelvOut(String params, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = onhandService.queryDelvOut(userId, params);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

    /**
     * 盘库
     * @param json
     * @param request
     * @return
     */
    @RequestMapping(value = "/savePk")
    public ResultJSON savePk(String json, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = onhandService.savePk(userId, json);
        } catch (Exception ex) {
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ExceptionTools.getExceptionAllinformation(ex));
            ex.printStackTrace();
        } finally {
            return resultJSON;
        }
    }

    /**
     * 倒库
     * @param json
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveDk")
    public ResultJSON saveDk(String json, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = onhandService.saveDk(userId, json);
        } catch (Exception ex) {
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ExceptionTools.getExceptionAllinformation(ex));
            ex.printStackTrace();
        } finally {
            return resultJSON;
        }
    }
    /**
     *  库房领料
     * @param json
     * @param request
     * @return
     */
    @RequestMapping(value = "/savePick")
    public ResultJSON savePick(String json, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = onhandService.savePick(userId, json);
        } catch (Exception ex) {
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ExceptionTools.getExceptionAllinformation(ex));
            ex.printStackTrace();
        } finally {
            return resultJSON;
        }
    }
}
