package com.shinsoft.sto.controller;

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.ConfigService;

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

/**
 * <p>
 * 库房配置表 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-10-15
 */
@RestController
//@RequestMapping("/config")
@RequestMapping("/Config")
public class ConfigController {


    @Autowired
    private ConfigService configService;

    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 storeType,
                            String params, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            Map<String,Object> map = new HashMap<>();
            map.put("storeNm" ,storeNm );
            map.put("areaCd" ,areaCd );
            map.put("stackCd" ,stackCd );
            map.put("storeType" ,storeType );
            resultJSON = configService.query(page, rows, params , map);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }
    @RequestMapping(value = "/queryAll")
    public ResultJSON query(String params,
                            HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            Map<String,Object> map = new HashMap<String,Object>();

            resultJSON = configService.queryAll( params ,new HashMap<>());
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }
    @RequestMapping(value = "/queryAllKF")
    public ResultJSON queryAllKF(String params,
                            HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            Map<String,Object> map = new HashMap<String,Object>();

            resultJSON = configService.queryAllKF( params ,new HashMap<>());
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }
    @RequestMapping(value = "/queryAllQH")
    public ResultJSON queryAllQH(String params,
                            HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            Map<String,Object> map = new HashMap<String,Object>();

            resultJSON = configService.queryAllQH( params ,new HashMap<>());
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }
    @RequestMapping(value = "/queryAllDWH")
    public ResultJSON queryAllDWH(String params,
                            HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            Map<String,Object> map = new HashMap<String,Object>();

            resultJSON = configService.queryAllDWH( params ,new HashMap<>());
        } 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 = configService.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 = configService.save(userId, json);
        } catch (Exception ex) {
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ExceptionTools.getExceptionAllinformation(ex));
            ex.printStackTrace();
        } finally {
            return resultJSON;
        }
    }
    @RequestMapping(value = "/insert")
    public ResultJSON insert(String json, HttpServletRequest request) {
        try {
            String userId = (String) request.getHeader("userId");
            resultJSON = configService.insert(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 = configService.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 = configService.removeBatch(userId, ids);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        } finally {
            return resultJSON;
        }
    }

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

}
