package com.shinsoft.sto.controller;

import com.shinsoft.sto.service.TransRecordService;
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.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 库存变更记录表 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-09-30
 */
@RestController
//@RequestMapping("/trans-record")
@RequestMapping("/TransRecord")
public class TransRecordController {


    @Autowired
    private TransRecordService transRecordService;

    ResultJSON resultJSON;


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

    /**
     * 修改 库存现有量，并添加库存变更记录
     * 根据  不同的业务类型
     *
     * @param json
     * @param request
     * @return
     */
    @RequestMapping(value = "/add")
    public ResultJSON save(String json, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            if (ObjectUtils.isEmpty(json)){
                resultJSON = JSONTools.toResultJSON("500", "参数为空！");
            } else {
                resultJSON = transRecordService.add(userId, json);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

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

    /**
     *  物料收发存
     * @param page
     * @param rows
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryWlsfc")
    public ResultJSON queryWlsfc(int page, int rows, String params, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = transRecordService.queryWlsfc(page, rows, params);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

    @RequestMapping(value = "/queryHistory")
    public ResultJSON queryHistory(int page, int rows, String params, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = transRecordService.queryHistory(page, rows, params);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }


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


}
