package com.project.controller.GetImg;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.project.util.StringUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/superadmin/getInfo")
public class GetInfo {
@RequestMapping("/updateJdOrder")
@ResponseBody
public void updateJdOrder(HttpServletRequest request){
GetInfo gif = new GetInfo();
String theFilePath ="D:\\updateinfo.txt";
String fileName = "D:\\idinfo.txt";
String allinfo = gif.readToString(fileName);
String[] array_info = allinfo.split(",");
String supplieridarray;
System.out.println("length:"+array_info.length);
for(int i=0;i<array_info.length;i++){
supplieridarray=array_info[i];
String msg = gif.select(supplieridarray);
String msg3 = msg.replaceAll("\\\\\"", "'");
String msg4 = gif.trimFirstAndLastChar(msg3, "\"");
System.out.println("msg4:"+msg4);
if (!StringUtils.isNull(msg4)) {
// 解析第一层
msg4 = "[" + msg4 + "]";
JSONArray modelJsonArray = JSONArray.fromObject(msg4);
for (int n = 0; n < modelJsonArray.size(); n++) {
JSONObject jsonObject1 = modelJsonArray.getJSONObject(n);
String type = jsonObject1.getString("type"); // 订单类型 1是父订单 2是子订单
// (1)如果该订单编号为父订单,则返回拆分订单信息
if ("1".equals(type)) {
String jsonStringcOrder = jsonObject1.getString("cOrder");
if (!StringUtils.isNull(jsonStringcOrder)) {
JSONArray cOrderDetail = JSONArray.fromObject(jsonStringcOrder);
// 解析第二层子订单的信息
for (int ii = 0; ii < cOrderDetail.size(); ii++) {
JSONObject oneChildrenOrder = cOrderDetail.getJSONObject(ii);
// 将查出的信息添加到emallordergoods表中
String jsonStringpOrder = oneChildrenOrder.getString("pOrder");
String jsonStringjdOrder = oneChildrenOrder.getString("jdOrderId");
String jsonStringcOrderSku = oneChildrenOrder.getString("sku");
if (!StringUtils.isNull(jsonStringcOrderSku)) {
JSONArray jsonForGoods = JSONArray.fromObject(jsonStringcOrderSku);
for (int j = 0; j < jsonForGoods.size(); j++) {
JSONObject jsonObject3 = jsonForGoods.getJSONObject(j);
String skuId = jsonObject3.getString("skuId");
String ss="HaveSplit-->"+"P_ORDER:"+jsonStringpOrder+"--C_ORDER:"+jsonStringjdOrder+"--SKUID:"+skuId;
System.out.println(ss);
gif.WriteStringToFile(theFilePath, ss);
}
}
}
}
}else if("2".equals(type)){
String pOrder = jsonObject1.getString("pOrder"); // 父订单
String supplierOrderIdChild = jsonObject1.getString("jdOrderId");
System.out.println("NOSplit-->"+supplierOrderIdChild);
int orderid=gif.select2(supplierOrderIdChild);
System.out.println("ORDERID:"+orderid);
int result = gif.updateeo(orderid, supplierOrderIdChild);
System.out.println("RESULT:"+result);
String tips;
if(result > 0){
tips = "UPDATE_SUCESS "+result+" OF DATA";
}else{
tips = "UPFAILURE "+result+" OF DATA";
}
String printinfo="NOSplit-->C_ORDER:"+supplierOrderIdChild+",ORDERID:"+orderid+"###"+tips;
gif.WriteStringToFile(theFilePath, printinfo);
}
}
}
}
}
public Connection getConn(){
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Emall";
String username = "sa";
String password = "tfsadmin";
Connection conn = null;
try {
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public String select(String id){
String sql = "select * from EMALL_INTERFACE_JD_LOG_BACKUP where RESPONE_MESSAGE like '%"+id+"%' and PORT_METHOD_NAME = 'biz.order.jdOrder.query'";
PreparedStatement pst = null;
ResultSet rs = null;
Connection conn = this.getConn();
String msg = null;
try {
pst = conn.prepareStatement(sql);
// pst.setInt(1, id);
rs = pst.executeQuery();
if(rs.next()){
msg=rs.getString("RESPONE_MESSAGE");
//System.out.println(rs.getString("RESPONE_MESSAGE"));
// System.out.println(rs.getString("content"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return msg;
}
public String trimFirstAndLastChar(String str, String element){
boolean beginIndexFlag = true;
boolean endIndexFlag = true;
do{
int beginIndex = str.indexOf(element) == 0 ? 1 : 0;
int endIndex = str.lastIndexOf(element) + 1 == str.length() ? str.lastIndexOf(element) : str.length();
str = str.substring(beginIndex, endIndex);
beginIndexFlag = (str.indexOf(element) == 0);
endIndexFlag = (str.lastIndexOf(element) + 1 == str.length());
} while (beginIndexFlag || endIndexFlag);
return str;
}
public Integer select2(String supplierorderid){
String sql = "select * from EMALL_ORDER where SUPPLIER_ORDERID =?";
PreparedStatement pst = null;
ResultSet rs = null;
Connection conn = this.getConn();
int msg = 0;
try {
pst = conn.prepareStatement(sql);
// pst.setInt(1, id);
pst.setString(1, supplierorderid);
rs = pst.executeQuery();
if(rs.next()){
msg=rs.getInt("ORDERID");
//System.out.println(rs.getString("RESPONE_MESSAGE"));
// System.out.println(rs.getString("content"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return msg;
}
public Integer updateeo(int order_id,String supplier_orderid){
String sql = "update EMALL_ORDER_GOODS set SUPPLIER_ORDERID = ? where ORDER_ID =?";
PreparedStatement pst = null;
int rs = 0;
Connection conn = this.getConn();
try {
pst = conn.prepareStatement(sql);
// pst.setInt(1, id);
pst.setString(1, supplier_orderid);
pst.setInt(2, order_id);
rs = pst.executeUpdate();
System.out.println("rs:"+rs);
if(rs>0){
System.out.println("成功更新"+rs+"条数据");
}else{
System.out.println("更新失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public void WriteStringToFile(String filePath,String contents) {
try {
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.append(contents);
bw.write("\r\n");// 往已有的文件上添加字符串
bw.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String readToString(String fileName) {
String encoding = "UTF-8";
File file = new File(fileName);
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
return new String(filecontent, encoding);
} catch (UnsupportedEncodingException e) {
System.err.println("The OS does not support " + encoding);
e.printStackTrace();
return null;
}
}
}