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.PriceService;
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("/Price")
public class PriceController {

    @Autowired
    private PriceService priceService;

    ResultJSON resultJSON;

    @RequestMapping(value = "/query")
    public ResultJSON query(@RequestParam int page,
                            @RequestParam int rows,
                            @RequestParam(required = false) String customerName,
                            @RequestParam(required = false) String materialName,
                            HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            Map<String, Object> params = new HashMap<>();
            if (customerName != null) {
                params.put("customerName", customerName);
            }
            if (materialName != null) {
                params.put("materialName", materialName);
            }
            resultJSON = priceService.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(@RequestParam String id, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = priceService.queryByPK(id);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

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

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

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