package com.slcf.controller;
|
|
import java.io.*;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
|
import com.alibaba.excel.EasyExcel;
|
import com.slcf.controller.param.SyncStockParam;
|
import org.apache.commons.io.IOUtils;
|
import org.apache.log4j.Logger;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.bind.annotation.*;
|
|
import com.slcf.pojo.UserBean;
|
import com.slcf.pojo.WaitPakInBean;
|
import com.slcf.bean.LocDetailCondition;
|
import com.slcf.bean.LocationCondition;
|
import com.slcf.pojo.LocDetailBean;
|
import com.slcf.pojo.LocationBean;
|
import com.slcf.pojo.SysLogBean;
|
import com.slcf.service.LocDetailService;
|
import com.slcf.service.LocationService;
|
import com.slcf.service.RoleService;
|
import com.slcf.service.SysLogService;
|
import com.slcf.util.AuthorityCode;
|
import com.slcf.util.Constants;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import org.springframework.web.multipart.MultipartFile;
|
import sun.misc.BASE64Encoder;
|
|
/**
|
* 库位管理控制器层
|
* @author admin
|
* @date 2018年11月13日
|
*/
|
@Controller
|
@RequestMapping("/stock")
|
public class StockController {
|
|
@Resource
|
LocationService locationService;
|
@Autowired
|
LocDetailService locDetailService;
|
@Autowired
|
SysLogService sysLogService;
|
@Autowired
|
RoleService roleService;
|
|
Logger logger=Logger.getLogger(StockController.class);
|
|
@RequestMapping("/goStock.action")
|
public String goLocationPage(HttpServletRequest request){
|
try {
|
String rid = request.getSession().getAttribute("ROLEID").toString();
|
String authCode = roleService.getAuthListByRoleMenu(Integer.parseInt(rid),
|
AuthorityCode.StockCode);
|
request.getSession().setAttribute("AUTHCODE",authCode);
|
// 插入日志
|
UserBean user = (UserBean) request.getSession().getAttribute("USER");
|
SysLogBean sysLog = new SysLogBean();
|
sysLog.setLogin_no(user.getUser_account());
|
sysLog.setMachine_ip(request.getRemoteAddr());
|
sysLog.setForm_no("goStock.action");
|
sysLog.setTts_keyname("访问:库存查询");
|
sysLog.setModi_user(user.getUser_account());
|
sysLogService.insertSysLog(sysLog);
|
}catch (Exception e) {
|
System.out.println(e.getMessage());
|
}
|
return "stock";
|
}
|
|
/**
|
* 分页查询所有
|
* @return
|
*/
|
@ResponseBody
|
@RequestMapping("/locationList.action")
|
public Map<String,Object> queryLocationListByPages(LocationCondition locationCon){
|
Map<String,Object>map=locationService.queryLocationList(locationCon);
|
return map;
|
}
|
|
/**
|
* 分页查询所有
|
* @return
|
*/
|
@ResponseBody
|
@RequestMapping("/stkDetailList.action")
|
public Map<String,Object> queryLocDetailListByPages(LocDetailCondition locDetailCon){
|
if(locDetailCon.getLoc_no()==null || locDetailCon.getLoc_no().equals("")) {
|
return null;
|
// locationCon.setLoc_no("1");
|
}
|
Map<String,Object>map=locDetailService.queryLocDetailList(locDetailCon);
|
return map;
|
}
|
|
/**
|
* 根据id查询信息
|
* @param id
|
* @return
|
*/
|
@ResponseBody
|
@RequestMapping("/queryLocationById.action")
|
public LocationBean queryLocationById(@RequestParam("did")String id){
|
try {
|
return locationService.queryLocationById(id);
|
}catch(Exception e) {
|
System.out.println(e.getMessage());
|
return null;
|
}
|
}
|
|
/**
|
* 查询所有
|
* @return
|
*/
|
@ResponseBody
|
@RequestMapping("/getLocation.action")
|
public List<LocationBean> getLocation(){
|
try {
|
return locationService.getLocationList();
|
}catch(Exception e) {
|
System.out.println(e.getMessage());
|
return null;
|
}
|
}
|
|
/**
|
* 调用SAP库存同步接口,同步SAP库存到ASRS
|
* @param loc_no
|
* @return
|
*/
|
@ResponseBody
|
@RequestMapping("/syncStockFromSap.action")
|
public Integer syncStockFromSap(@RequestParam("loc_no")String loc_no,
|
@RequestParam("barcode")String barcode,HttpServletRequest request){
|
int result = 0;
|
try {
|
// String sb = "{\"d\":{\"results\":[{\"__metadata\":{\"id\":\"httpTO_CDS(lgnum='385',lqnum='0000126742')\",\"uri\":\"http://d01aas25.sap.kraussmaffeigroup.net:8025/sap/opu/odata/SAP/Z_WM_H5_SRV/ZLQUA_AUTO_CDS(lgnum='385',lqnum='0000126742')\",\"type\":\"Z_WM_H5_SRV.ZLQUA_AUTO_CDSType\"},\"lgnum\":\"385\",\"lqnum\":\"0000126742\",\"lgtyp\":\"A01\",\"lgpla\":\"0400201\",\"matnr\":\"0028009\",\"maktx\":\"RECHTECK\",\"werks\":\"3885\",\"gesme\":\"5.000\",\"altme\":\"KGM\"}]}}";
|
UserBean user=(UserBean)request.getSession().getAttribute("USER");
|
// String httpUrl = Constants.SAP_URL + "/sap/opu/odata/SAP/Z_WM_H5_SRV/autoStorageSet";
|
String httpUrl = Constants.SAP_URL + "/sap/opu/odata/SAP/Z_WM_H5_SRV/ZLQUA_AUTO_CDS";
|
result = sendSyncStockCmd(httpUrl,loc_no,barcode);
|
// result=waitPakInService.upWaitPakIn(waitPakIn);
|
if(result>0) {
|
// 插入日志
|
// UserBean user = (UserBean) request.getSession().getAttribute("USER");
|
SysLogBean sysLog = new SysLogBean();
|
sysLog.setLogin_no(user.getUser_account());
|
sysLog.setMachine_ip(request.getRemoteAddr());
|
sysLog.setForm_no("upWaitPakIn.action");
|
sysLog.setTts_keyname("同步SAP库存到ASRS:" + loc_no);
|
sysLog.setModi_user(user.getUser_account());
|
sysLogService.insertSysLog(sysLog);
|
}
|
}catch(Exception e) {
|
System.out.println(e.getMessage());
|
}
|
return result;
|
}
|
|
/**
|
* 调用SAP库存同步接口,同步SAP库存到ASRS
|
* @param loc_no
|
* @return
|
*/
|
@ResponseBody
|
@RequestMapping(value = "/syncStocksFromSap.action", method = RequestMethod.POST)
|
public String syncStocksFromSap(@RequestBody List<SyncStockParam> params, HttpServletRequest request){
|
StringBuilder res = new StringBuilder();
|
int result = 0;
|
int success = 0;
|
int total = 0;
|
try {
|
if (params != null && params.size()>0) {
|
for (SyncStockParam param : params) {
|
total ++;
|
String sb = "{\"d\":{\"results\":[{\"__metadata\":{\"id\":\"httpTO_CDS(lgnum='385',lqnum='0000126742')\",\"uri\":\"http://d01aas25.sap.kraussmaffeigroup.net:8025/sap/opu/odata/SAP/Z_WM_H5_SRV/ZLQUA_AUTO_CDS(lgnum='385',lqnum='0000126742')\",\"type\":\"Z_WM_H5_SRV.ZLQUA_AUTO_CDSType\"},\"lgnum\":\"385\",\"lqnum\":\"0000126742\",\"lgtyp\":\"A01\",\"lgpla\":\"0400201\",\"matnr\":\"0028009\",\"maktx\":\"RECHTECK\",\"werks\":\"3885\",\"gesme\":\"5.000\",\"altme\":\"KGM\"}]}}";
|
UserBean user=(UserBean)request.getSession().getAttribute("USER");
|
String httpUrl = Constants.SAP_URL + "/sap/opu/odata/SAP/Z_WM_H5_SRV/ZLQUA_AUTO_CDS";
|
result = sendSyncStockCmd(httpUrl,param.getLocNo(),param.getBarcode());
|
// result=waitPakInService.upWaitPakIn(waitPakIn);
|
if(result>0) {
|
// 插入日志
|
// UserBean user = (UserBean) request.getSession().getAttribute("USER");
|
SysLogBean sysLog = new SysLogBean();
|
sysLog.setLogin_no(user.getUser_account());
|
sysLog.setMachine_ip(request.getRemoteAddr());
|
sysLog.setForm_no("upWaitPakIn.action");
|
sysLog.setTts_keyname("同步SAP库存到ASRS:" + param.getBarcode());
|
sysLog.setModi_user(user.getUser_account());
|
sysLogService.insertSysLog(sysLog);
|
success ++ ;
|
} else {
|
res.append(param.getLocNo()).append(",");
|
}
|
}
|
}
|
//
|
}catch(Exception e) {
|
System.out.println(e.getMessage());
|
}
|
res.insert(0, "Number Of FAIL: " + (total - success) + "\n\n");
|
res.insert(0, "Number Of SUCCESS: " + (success) + "\n\n");
|
res.insert(0, "Processing is complete!\n\n");
|
if (res.length() > 0) {
|
res.append("location synchronization failure!");
|
} else {
|
res.append("synchronization success");
|
}
|
return res.toString();
|
}
|
|
// @RequestMapping(value = "/sync/excel/file/import.action", method = RequestMethod.POST)
|
// @ResponseBody
|
// public String matCodeImport(MultipartFile file) throws IOException, InterruptedException {
|
// MatCodeExcelListener listener = new MatCodeExcelListener(getUserId());
|
// EasyExcel.read(file.getInputStream(), MatCodeExcel.class, listener).sheet().doRead();
|
// System.out.println(file.getOriginalFilename());
|
// return "ok";
|
// }
|
|
|
/**
|
* 调用sap接口完成ASRS库存更新
|
*/
|
private int sendSyncStockCmd(String httpUrl,String loc_no,String barcode) {
|
int result = 0;
|
String input = Constants.SAP_USER + ":" + Constants.SAP_PASSWORD;//"ZHANGX:654321";
|
BASE64Encoder base = new BASE64Encoder();
|
String encodedPassword;
|
try {
|
encodedPassword = base.encode(input.getBytes("UTF-8"));
|
// httpUrl += "?$filter=lgnum eq '385' and lgtyp eq 'A01' and lgpla eq '" + loc_no + "'";
|
//"?$filter=lgnum%20eq%20%27385%27"
|
// String filter = java.net.URLEncoder.encode("?$filter=lgnum eq '385'", "utf-8");
|
// httpUrl += "?$filter=lgnum%20eq%20%27385%27%20";
|
//空格20%,单引号27%
|
// String filter1 = "?$filter=lgnum%20eq%20%27385%27%20and20%lgtyp%20eq%20%27A01%27";//20%and20%lgpla20%eq%20%27" + loc_no + "27%";
|
String filter = "?$filter=lgnum%20eq%20%27385%27%20and%20lgtyp%20eq%20%27A01%27%20and%20lgpla%20eq%20%27" + loc_no + "%27%20";
|
httpUrl += filter;
|
URL url1 = new URL(httpUrl);
|
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setRequestMethod("GET");
|
connection.setRequestProperty("Authorization", "Basic " + encodedPassword);
|
connection.setRequestProperty("Content-Type","application/json");
|
connection.setRequestProperty("Accept","application/json");
|
connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9");
|
// connection.setRequestProperty("Accept-Encoding","gzip, deflate");
|
connection.setRequestProperty("x-csrf-token","Fetch");
|
|
//读取响应
|
InputStream instream = connection.getInputStream();
|
String sb = IOUtils.toString(instream,"utf-8");
|
// BufferedReader reader = new BufferedReader(new InputStreamReader(
|
// connection.getInputStream(),"utf-8"));
|
// String lines;
|
// StringBuffer sb = new StringBuffer("");
|
// while ((lines = reader.readLine()) != null) {
|
// lines = new String(lines.getBytes(), "utf-8");
|
// sb.append(lines);
|
// }
|
logger.error("syncStocksFromSap---同步库存返回数据" + sb.toString());
|
//解析json,更新库存
|
JSONObject json = JSONObject.fromObject(sb.toString());
|
String d = json.get("d").toString();
|
JSONObject jsonItem = JSONObject.fromObject(d);
|
String results = jsonItem.get("results").toString();
|
JSONArray array = JSONArray.fromObject(results);
|
|
result = locDetailService.delLocDetailByLocNo(loc_no);
|
if(array.size()>0) {
|
for(int i=0;i<array.size();i++){
|
JSONObject job = array.getJSONObject(i);
|
String matnr = job.get("matnr").toString();
|
String maktx = job.get("maktx").toString();
|
String werks = job.get("werks").toString();
|
double gesme = job.get("gesme").toString().equals("")?0:Double.parseDouble(job.get("gesme").toString());
|
String altme = job.get("altme").toString();
|
|
LocDetailBean locDetl = new LocDetailBean();
|
// logger.error("syncStocksFromSap---插入数据" + matnr+"-"+maktx+"-"+werks+"-"+gesme+"-"+altme);
|
locDetl.setLoc_no(loc_no);
|
locDetl.setMatnr(matnr);
|
locDetl.setLgnum("385");
|
locDetl.setTbnum(1);
|
locDetl.setTbpos(1);
|
locDetl.setZmatid("1");
|
locDetl.setMaktx(maktx);
|
locDetl.setWerks(werks);
|
locDetl.setAnfme(gesme);
|
locDetl.setAltme(altme);
|
if(barcode == null || "".equals(barcode)) {
|
LocationBean locationBean = locationService.queryLocationById(loc_no);
|
barcode = locationBean.getBarcode();
|
}
|
locDetl.setZpallet(barcode);
|
locDetl.setBname("");
|
result = locDetailService.insertLocDetail(locDetl);
|
// logger.error("syncStocksFromSap---插入结果" + result);
|
|
if(result<=0) {
|
break;
|
}
|
}
|
}
|
} catch (Exception e) {
|
logger.error("sendSyncStockCmd Error----" + e.getMessage());
|
System.out.println(e.getMessage());
|
}
|
return result;
|
}
|
|
}
|