package com.shinsoft.sto.controller.ware;

import com.alibaba.fastjson.JSON;
import com.shinsoft.sto.model.system.ResultJSON;
import com.shinsoft.sto.service.ware.ChangeRecordService;
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("/ChangeRecord")
public class ChangeRecordController {

    @Autowired
    private ChangeRecordService changeRecordService;

    @RequestMapping(value = "/query")
    public ResultJSON query(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "50") int rows,
                            String params,
                            HttpServletRequest request) {
        try {
            Map<String, Object> map = buildQueryParams(params, request);
            return changeRecordService.query(page, rows, map);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error("查询失败：" + ex.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    private Map<String, Object> buildQueryParams(String params, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        if (params != null && !params.trim().isEmpty()) {
            map.putAll(JSON.parseObject(params, Map.class));
        }
        return map;
    }
}

