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.CargoChangeRecordService;
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("/CargoChangeRecord")
public class CargoChangeRecordController {

    @Autowired
    private CargoChangeRecordService cargoChangeRecordService;

    ResultJSON resultJSON;

    @RequestMapping(value = "/query")
    public ResultJSON query(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer rows,
                            @RequestParam(required = false) String uperCustomerId,
                            @RequestParam(required = false) String customerId,
                            @RequestParam(required = false) String startDate,
                            @RequestParam(required = false) String endDate,
                            @RequestParam(required = false) String subStr,
                            @RequestParam(required = false) String changeFrom,
                            HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            Map<String, Object> params = new HashMap<>();
            if (uperCustomerId != null) {
                params.put("uperCustomerId", uperCustomerId);
            }
            if (customerId != null) {
                params.put("customerId", customerId);
            }
            if (startDate != null) {
                params.put("startDate", startDate);
            }
            if (endDate != null) {
                params.put("endDate", endDate);
            }
            if (subStr != null) {
                params.put("subStr", subStr);
            }
            if (changeFrom != null) {
                params.put("changeFrom", changeFrom);
            }
            resultJSON = cargoChangeRecordService.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 = cargoChangeRecordService.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 = cargoChangeRecordService.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 = cargoChangeRecordService.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 = cargoChangeRecordService.removeBatch(userId, ids);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }
}