package com.iotechn.unimall.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dangdang.openplatform.openapi.sdk.ApiException;
import com.dangdang.openplatform.openapi.sdk.SdkClient;
import com.dangdang.openplatform.openapi.sdk.internal.util.FileItem;
import com.dangdang.openplatform.openapi.sdk.request.order.OrderDetailsGetRequest;
import com.dangdang.openplatform.openapi.sdk.request.order.OrderGoodsSendRequest;
import com.dangdang.openplatform.openapi.sdk.request.order.OrdersContentDecryptRequest;
import com.dangdang.openplatform.openapi.sdk.requestmodel.order.EncryptDTO;
import com.dangdang.openplatform.openapi.sdk.requestmodel.order.OrderDetailsGet;
import com.dangdang.openplatform.openapi.sdk.response.order.OrderDetailsGetResponse;
import com.dangdang.openplatform.openapi.sdk.response.order.OrderGoodsSendResponse;
import com.dangdang.openplatform.openapi.sdk.response.order.OrdersContentDecryptResponse;
import com.dangdang.openplatform.openapi.sdk.responsemodel.order.CryptUserInfo;
import com.dangdang.openplatform.openapi.sdk.responsemodel.order.OrdersContentDecryptDTO;
import com.iotechn.unimall.admin.api.user.AdminUserServiceImpl;
import com.iotechn.unimall.biz.service.order.OrderBizService;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.data.component.LockComponent;
import com.iotechn.unimall.data.domain.GroupShopDO;
import com.iotechn.unimall.data.domain.OrderDO;
import com.iotechn.unimall.data.domain.SpuDO;
import com.iotechn.unimall.data.domain.shop.ShopAccount;
import com.iotechn.unimall.data.domain.shop.ShopLog;
import com.iotechn.unimall.data.domain.shop.ShopOrder;
import com.iotechn.unimall.data.domain.shop.ShopTran;
import com.iotechn.unimall.data.dto.shop.XmlObject.OrderDetail;
import com.iotechn.unimall.data.dto.shop.XmlObject.SendGood;
import com.iotechn.unimall.data.dto.shop.XmlObject.SendGoodsInfo;
import com.iotechn.unimall.data.enums.GroupShopAutomaticRefundType;
import com.iotechn.unimall.data.enums.OrderStatusType;
import com.iotechn.unimall.data.enums.StatusType;
import com.iotechn.unimall.data.mapper.GroupShopMapper;
import com.iotechn.unimall.data.mapper.OrderMapper;
import com.iotechn.unimall.data.mapper.SpuMapper;
import com.iotechn.unimall.data.mapper.shop.ShopAccountMapper;
import com.iotechn.unimall.data.mapper.shop.ShopLogMapper;
import com.iotechn.unimall.data.mapper.shop.ShopOrderMapper;
import com.iotechn.unimall.data.mapper.shop.ShopTranMapper;
import com.iotechn.unimall.data.util.XMLUtil;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Created by rize on 2019/7/21.
 */
@Component
@EnableScheduling
public class AdminCheckQuartz {

    private static final Logger logger = LoggerFactory.getLogger(AdminCheckQuartz.class);
    private static final String ORDER_STATUS_LOCK = "ORDER_STATUS_QUARTZ_LOCK";
    private static final String GROUP_SHOP_START_LOCK = "GROUP_SHOP_START_LOCK";
    private static final String GROUP_SHOP_END_LOCK = "GROUP_SHOP_END_LOCK";
    private static final String GROUP_SHOP_LOCK_LOCK = "GROUP_SHOP_LOCK_LOCK";
    @Autowired
    private ShopOrderMapper shopOrderMapper;
    @Autowired
    private ShopAccountMapper shopAccountMapper;
    @Autowired
    private ShopTranMapper shopTranMapper;
    @Autowired
    private ShopLogMapper shopLogMapper;
    ExecutorService pool = Executors.newFixedThreadPool(2);

    /**
     * 订单数据定时轮训处理 每小时执行一次
     */
    @Scheduled(cron = "0 0 7-22 * * ? ")
    public void task1(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                doSomething1();
            }
        }).start();
    }
    /**
     * 订单数据定时轮训处理 每3小时执行一次
     */
//    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void task2(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                doSomething2();
            }
        }).start();
    }
    public void dealOrder() throws Exception {
        try{
            List<ShopOrder> shopOrders = shopOrderMapper.selectList(new EntityWrapper<ShopOrder>()
                    .eq("dang_order_status","未发货")
                    .eq("order_status_name","卖家已发货"));
            for (ShopOrder qqq:shopOrders
            ) {
                ShopAccount shopAccount = shopAccountMapper.selectById(qqq.getAccountId());
                flushCookie(shopAccount);
                CloseableHttpClient httpClient = HttpClientBuilder.create().build();
                HttpGet get = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=9999&orderId="+qqq.getOrderId());
                //这里可以设置请求参数，token等
                get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
                get.addHeader("cookie",shopAccount.getFuziCookie());
                get.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                CloseableHttpResponse response = httpClient.execute(get);//执行获取响应
                try{

                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//根据状态码处理
                        String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
                        //返回字符串
                        String res = unicodeToString(strTmp);
                        JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
                        Boolean status = (Boolean) datas.get("status");//获取返回数据状态，get获取的字段需要根据提供的返回值去获取
                        List<ShopOrder> data = JSONArray.parseArray(datas.get("data").toString(),ShopOrder.class);//"data"是根据返回值设定
                        for (ShopOrder tmp: data
                        ) {
                            //正则表达式，用于匹配非数字串，+号用于匹配出多个非数字串
                            String regEx="[^0-9]+";
                            Pattern pattern = Pattern.compile(regEx);
                            //用定义好的正则表达式拆分字符串，把字符串中的数字留出来
                            String[] cs = pattern.split(tmp.getRemarkText());
                            String dangOrder = "";
                            for (String str:cs
                            ) {
                                if(dangOrder.length()>= 11 || str.equals("*")){
                                    break;
                                }
                                dangOrder += str;
                            }
                            tmp.setDangOrder(dangOrder);
                            tmp.setAccountId(shopAccount.getId());
                            Long time  = Long.parseLong(tmp.getShippingTime())+8*60*60;
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
                            String time1 = sdf.format(new Date(time * 1000));
                            tmp.setShippingTime(time1);
                            tmp.setDeleteFlag(0);
                            tmp.setGmtUpdate(new Date());
                            tmp.setGmtCreate(new Date());
                            if(tmp.getShippingComName().equals("快递包裹")||tmp.getShippingComName().equals("挂号印刷品")){
                                tmp.setShippingComName("中国邮政");
                            }
                            ShopOrder tt = new ShopOrder();
                            tt.setOrderId(tmp.getOrderId());
                            tt =  shopOrderMapper.selectOne(tt);
                            if(tt == null){
                                shopOrderMapper.insert(tmp);
                            }
                            else{
                                if(!tt.getDangOrder().equals(tmp.getDangOrder())||!tt.getShippingComName().equals(tmp.getShippingComName())){
                                    shopOrderMapper.update(tmp,new EntityWrapper<ShopOrder>().eq("order_id",tt.getOrderId()));
                                }
                                dangOrderDeal(shopAccount,qqq);
                            }
                        }

                    }


                } catch (IOException e) {
                    throw e;
                }
                finally {
                    response.close();
                    httpClient.close();
                }
            }
        }
        catch (Exception e) {
            ShopLog shopLog = new ShopLog();
            shopLog.setGmtCreate(new Date());
            shopLog.setTitle("同步定时任务异常");
            shopLog.setContent(e.getMessage());
            shopLogMapper.insert(shopLog);
            e.printStackTrace();
        }
    }

    public void pullOrderList() throws Exception {
        try{
            List<ShopAccount> listAccount = shopAccountMapper.selectList(new EntityWrapper<ShopAccount>().eq("delete_flag",0));
            for (ShopAccount shopAccount:listAccount
            ) {
                flushCookie(shopAccount);
                for(int i=1;i<=5;i++){
                    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
                    HttpGet get = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr="+i+"&pageShow=300&orderStatus=ShippedToReceipt");
                    //这里可以设置请求参数，token等
                    get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
                    get.addHeader("cookie",shopAccount.getFuziCookie());

                    CloseableHttpResponse response = httpClient.execute(get);//执行获取响应
                    try{

                        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//根据状态码处理
                            String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
                            //返回字符串
                            String res = unicodeToString(strTmp);
                            JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
                            Boolean status = (Boolean) datas.get("status");//获取返回数据状态，get获取的字段需要根据提供的返回值去获取
                            List<ShopOrder> data = JSONArray.parseArray(datas.get("data").toString(),ShopOrder.class);//"data"是根据返回值设定
                            if(data.size() == 0){
                                break;
                            }
                            for (ShopOrder tmp: data
                            ) {
                                //正则表达式，用于匹配非数字串，+号用于匹配出多个非数字串
                                String regEx="[^0-9]+";
                                Pattern pattern = Pattern.compile(regEx);
                                //用定义好的正则表达式拆分字符串，把字符串中的数字留出来
                                String[] cs = pattern.split(tmp.getRemarkText());
                                String dangOrder = "";
                                for (String str:cs
                                ) {
                                    if(dangOrder.length()>= 11 || str.equals("*")){
                                        break;
                                    }
                                    dangOrder += str;
                                }
                                tmp.setDangOrder(dangOrder);
                                tmp.setAccountId(shopAccount.getId());
                                Long time  = Long.parseLong(tmp.getShippingTime())+8*60*60;
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
                                String time1 = sdf.format(new Date(time * 1000));
                                tmp.setShippingTime(time1);
                                tmp.setDeleteFlag(0);
                                tmp.setGmtUpdate(new Date());
                                tmp.setGmtCreate(new Date());
                                if(tmp.getShippingComName().equals("快递包裹")||tmp.getShippingComName().equals("挂号印刷品")){
                                    tmp.setShippingComName("中国邮政");
                                }
                                ShopOrder tt = new ShopOrder();
                                tt.setOrderId(tmp.getOrderId());
                                tt =  shopOrderMapper.selectOne(tt);
                                if(tt == null){
                                    shopOrderMapper.insert(tmp);
                                }
                                else{
                                    if(tt.getDangOrderStatus().equals("未发货")&&!tt.getDangOrder().equals(tmp.getDangOrder())){
                                        shopOrderMapper.update(tmp,new EntityWrapper<ShopOrder>().eq("order_id",tt.getOrderId()));
                                    }
                                }
                            }

                        }


                    } catch (IOException e) {
                        throw e;
                    }
                    finally {
                        response.close();
                        httpClient.close();
                    }
                }

            }
        }
        catch (Exception e){
            ShopLog shopLog = new ShopLog();
            shopLog.setGmtCreate(new Date());
            shopLog.setTitle("爬取定时任务异常");
            shopLog.setContent(e.getMessage());
            shopLogMapper.insert(shopLog);
            e.printStackTrace();
        }

    }
    public void flushCookie(ShopAccount shopAccount){
        try{
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            HttpGet getMethod = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=30");
            getMethod.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
            getMethod.addHeader("cookie",shopAccount.getFuziCookie());

            httpClient.execute(getMethod);
            CloseableHttpResponse response = httpClient.execute(getMethod);//执行获取响应
            try{
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//根据状态码处理
                    String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
                    JSONObject datas = JSONObject.parseObject(strTmp);//转换成JSON格式
                    Boolean status = (Boolean) datas.get("status");//获取返回数据状态，get获取的字段需要根据提供的返回值去获取
                    if(status){
                        return;
                    }
                }
            }
            catch (Exception e){
                throw e;
            }
            finally {
                response.close();
                httpClient.close();
            }

            System.out.println("原cookies = "+shopAccount.getFuziCookie());

            // 登陆 Url
            String loginUrl = "https://login.kongfz.com/Pc/Login/account";
            BasicCookieStore store= new BasicCookieStore();
            httpClient =HttpClients.custom().setDefaultCookieStore(store).build();
            // 模拟登陆，按实际服务器端要求选用 Post 或 Get 请求方式
            HttpPost httpPost = new HttpPost(loginUrl);

            // 装填参数
            List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
            nvps.add(new BasicNameValuePair("loginName", shopAccount.getFuziAccount()));
            nvps.add(new BasicNameValuePair("loginPass", shopAccount.getFuziPassword()));
            // 设置参数到请求对象中
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

            // 设置header信息
            // 指定报文头【Content-type】、【User-Agent】
            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");

            // 执行请求操作，并拿到结果（同步阻塞）
            response = httpClient.execute(httpPost);
            if(response.getStatusLine().getStatusCode() == 302){
                List<org.apache.http.cookie.Cookie> cookielist = store.getCookies();
                String nCookie = "";
                for(org.apache.http.cookie.Cookie cookie: cookielist){
                    String name=cookie.getName();
                    String value=cookie.getValue();
                    nCookie += name + "="+value+";";
                }
                System.out.println("nCookie:" + nCookie);
                shopAccount.setFuziCookie(nCookie);
                shopAccountMapper.updateById(shopAccount);
                ShopLog shopLog = new ShopLog();
                shopLog.setGmtCreate(new Date());
                shopLog.setTitle("更新cookie成功");
                shopLog.setContent("账号："+shopAccount.getFuziAccount()+",cookie："+nCookie);
                shopLogMapper.insert(shopLog);
            }
            else{
                ShopLog shopLog = new ShopLog();
                shopLog.setGmtCreate(new Date());
                shopLog.setTitle("更新cookie失败");
                shopLog.setContent(response.toString());
                shopLogMapper.insert(shopLog);
            }
            // 释放链接
            response.close();

        }catch (Exception e) {
            e.printStackTrace();
            ShopLog shopLog = new ShopLog();
            shopLog.setGmtCreate(new Date());
            shopLog.setTitle("更新cookie异常");
            shopLog.setContent(e.getMessage());
            shopLogMapper.insert(shopLog);
        }

    }

    /**
     * Unicode转 汉字字符串
     *
     * @param str
     *            \u6728
     * @return '木' 26408
     */
    public static String unicodeToString(String str) {

        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            // group 6728
            String group = matcher.group(2);
            // ch:'木' 26408
            ch = (char) Integer.parseInt(group, 16);
            // group1 \u6728
            String group1 = matcher.group(1);
            str = str.replace(group1, ch + "");
        }
        return str;
    }
    public void dangOrderDeal(ShopAccount shopAccount, ShopOrder shopOrder) throws Exception {
        try{
            /**
             * 获取一下店铺的所有静态参数
             */
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            SdkClient sdkClient = new SdkClient(shopAccount.getDangAppKey(),shopAccount.getDangAppSecret(),shopAccount.getDangSession(),shopAccount.getDangVersion());

            OrderDetailsGetRequest ss = new OrderDetailsGetRequest();
            OrderDetailsGet orderDetailsGet = new OrderDetailsGet();
            orderDetailsGet.setO(shopOrder.getDangOrder());
            ss.setOrderDetailsGet(orderDetailsGet);
            OrderDetailsGetResponse resultt = null;
            try {
                resultt = sdkClient.excute(ss);
                if(resultt.getOrderID() == null){
                    shopOrder.setDangOrderStatus("订单错误");
                    shopOrderMapper.updateById(shopOrder);
                    ShopLog shopLog = new ShopLog();
                    shopLog.setGmtCreate(new Date());
                    shopLog.setTitle("当当订单编号错误");
                    shopLog.setContent("店铺："+shopAccount.getDangName()+",孔网订单："+shopOrder.getOrderId()+",当当订单："+shopOrder.getDangOrder());
                    shopLogMapper.insert(shopLog);
                    return;
                }
            } catch (ApiException e) {
                throw e;
            }

            /**
             * 从当当获取订单内应当有的所有图书
             */
            OrderDetailsGetRequest odgr = new OrderDetailsGetRequest();
            OrderDetailsGet ogg = new OrderDetailsGet();
            ogg.setO(shopOrder.getDangOrder());
            odgr.setOrderDetailsGet(ogg);
            OrderDetailsGetResponse odgrp = null;
            try {
                odgrp = sdkClient.excute(odgr);
            } catch (ApiException e) {
                throw e;
            }
            SdkClient jiemi = new SdkClient(shopAccount.getDangAppKey(),shopAccount.getDangAppSecret(),shopAccount.getDangSession(),shopAccount.getDangVersion());
            OrdersContentDecryptRequest ordersContentDecryptRequest=new OrdersContentDecryptRequest();

            List<EncryptDTO> encryptVOS = new ArrayList<EncryptDTO>();

            EncryptDTO encryptVO1 = new EncryptDTO();
            encryptVO1.setO(resultt.getOrderID().toString());
            encryptVO1.setEncrypt_content(resultt.getEncrypt_content());
            encryptVOS.add(encryptVO1);

            ordersContentDecryptRequest.setO_cryptStr(JSON.toJSONString(encryptVOS));
            OrdersContentDecryptResponse ordersContentDecryptResponse = null;
            try {
                ordersContentDecryptResponse=jiemi.excute(ordersContentDecryptRequest);
            } catch (ApiException e) {
                throw e;
            }
            List<OrdersContentDecryptDTO> data = ordersContentDecryptResponse.getData();
            CryptUserInfo cryptUserInfo = data.get(0).getCryptUserInfo();



            OrderDetail o = (OrderDetail) XMLUtil.convertXmlStrToObject(OrderDetail.class, odgrp.getBody());
            SendGoodsInfo sgi = new SendGoodsInfo();
            sgi.setConsigneeAddr(cryptUserInfo.getConsigneeAddr());
            sgi.setConsigneeMobileTel(cryptUserInfo.getConsigneeMobileTel());
            sgi.setConsigneeTel(cryptUserInfo.getConsigneeTel());
            sgi.setConsigneeName(cryptUserInfo.getConsigneeName());
            o.setSendGoodsInfo(sgi);
            List<com.iotechn.unimall.data.dto.shop.XmlObject.ItemInfo> itemInfo = o.getItemsList().getItemInfo();
            /**
             * 将多个图书拼接XML
             */
            String itemsStr = "";
            for (com.iotechn.unimall.data.dto.shop.XmlObject.ItemInfo ii : itemInfo) {
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("DANG_DANG_ITEM_ID", ii.getItemID());
                hashMap.put("SHU_LIANG", ii.getOrderCount());
                hashMap.put("PRODUCT_ID", ii.getProductItemId());
                itemsStr += SendGood.processTemplate(SendGood.ONE_ITEM, hashMap);
            }


            /**
             * 组成OrderInfo
             */
            Wrapper wrapper = new EntityWrapper();
            wrapper.like("fuzi_name",shopOrder.getShippingComName());
            wrapper.eq("delete_flag",0);
            List<ShopTran> list = shopTranMapper.selectList(wrapper);
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("DANG_DANG_ORDER", shopOrder.getDangOrder());
            if(list.size() == 0 ){
//                throw new Exception("物流公司不存在");
                System.out.println("物流公司不存在");
                stringObjectHashMap.put("KUAI_DI_GONG_SI", "99999");
                stringObjectHashMap.put("KUAI_DI_BIANMA", "99999");
                stringObjectHashMap.put("KUAI_DI_GONG_SI_DIAN_HUA", "99999");
            }
            else{
                stringObjectHashMap.put("KUAI_DI_GONG_SI", list.get(0).getName());
                stringObjectHashMap.put("KUAI_DI_BIANMA", list.get(0).getCode1());
                stringObjectHashMap.put("KUAI_DI_GONG_SI_DIAN_HUA", list.get(0).getPhone());
            }
            stringObjectHashMap.put("KUAI_DI_DAN_HAO", shopOrder.getShipmentNum());
            stringObjectHashMap.put("SEND_ITEMS", itemsStr);

            String orderListStr = SendGood.processTemplate(SendGood.ONE_ORDER, stringObjectHashMap);
            HashMap<String, Object> tmplHash = new HashMap<>();
            tmplHash.put("SEND_TIME", dateFormat.format(new Date()));
            tmplHash.put("ORDER_LIST", orderListStr);

            String xmlFileStr = SendGood.processTemplate(SendGood.TMPL, tmplHash);
            System.out.println(xmlFileStr);
            PrintWriter out = null;
            try {
                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("sendGoodNotPlat.xml"), "GBK")));
            }catch (Exception e) {
                throw e;
            }
            out.write(xmlFileStr);
            out.close();
            System.out.println("XML文件创建成功！");


            /**
             * 发货！！
             */
            OrderGoodsSendRequest sendGoodsRequest = new OrderGoodsSendRequest();
            sendGoodsRequest.setSendGoodsFile(new FileItem(new File("sendGoodNotPlat.xml")));
            OrderGoodsSendResponse result = null;
            try {
                result = sdkClient.excute(sendGoodsRequest);
            } catch (ApiException e) {
                throw e;
            }
            System.out.println(result.toString());
            if(result.getResult().getOrdersList().get(0).getOrderOperCode() == 0 || result.getResult().getOrdersList().get(0).getOrderOperCode() == 605){
                ShopLog shopLog = new ShopLog();
                shopLog.setGmtCreate(new Date());
                shopLog.setTitle("当当订单发货成功");
                shopLog.setContent("店铺："+shopAccount.getDangName()+",孔网订单："+shopOrder.getOrderId()+",当当订单："+shopOrder.getDangOrder());
                shopLogMapper.insert(shopLog);
                shopOrder.setDangOrderStatus("已发货");
                shopOrderMapper.update(shopOrder,new EntityWrapper<ShopOrder>().eq("order_id",shopOrder.getOrderId()));
            }
            else{
                ShopLog shopLog = new ShopLog();
                shopLog.setGmtCreate(new Date());
                shopLog.setTitle("当当订单发货失败");
                shopLog.setContent("店铺："+shopAccount.getDangName()+",孔网订单："+shopOrder.getOrderId()+",当当订单："+shopOrder.getDangOrder()+",快递："+shopOrder.getShippingComName());
                shopLogMapper.insert(shopLog);
            }
        }
        catch (Exception e){
            e.printStackTrace();
            ShopLog shopLog = new ShopLog();
            shopLog.setGmtCreate(new Date());
            shopLog.setTitle("当当发货异常");
            shopLog.setContent(e.getMessage());
            shopLogMapper.insert(shopLog);
        }

    }
    /**
     * 需要处理的任务
     * */
    private void doSomething1() {
        try{
            List<ShopAccount> list = shopAccountMapper.selectList(new EntityWrapper<>());
            for (ShopAccount shopAccount:list
            ) {
                flushCookie(shopAccount);
                List<ShopOrder> shopOrders = shopOrderMapper.selectList(new EntityWrapper<ShopOrder>()
                        .eq("dang_order_status","未发货")
                        .eq("account_id",shopAccount.getId())
                        .eq("order_status_name","卖家已发货"));
                for (ShopOrder qqq:shopOrders
                ) {
                    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
                    HttpGet get = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=9999&orderId="+qqq.getOrderId());
                    //这里可以设置请求参数，token等
                    get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
                    get.addHeader("cookie",shopAccount.getFuziCookie());
                    get.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    CloseableHttpResponse response = httpClient.execute(get);//执行获取响应
                    try{

                        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//根据状态码处理
                            String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
                            //返回字符串
                            String res = unicodeToString(strTmp);
                            JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
                            Boolean status = (Boolean) datas.get("status");//获取返回数据状态，get获取的字段需要根据提供的返回值去获取
                            List<ShopOrder> data = JSONArray.parseArray(datas.get("data").toString(),ShopOrder.class);//"data"是根据返回值设定
                            for (ShopOrder tmp: data
                            ) {
                                //正则表达式，用于匹配非数字串，+号用于匹配出多个非数字串
                                String regEx="[^0-9]+";
                                Pattern pattern = Pattern.compile(regEx);
                                //用定义好的正则表达式拆分字符串，把字符串中的数字留出来
                                String[] cs = pattern.split(tmp.getRemarkText());
                                String dangOrder = "";
                                for (String str:cs
                                ) {
                                    if(dangOrder.length()>= 11 || str.equals("*")){
                                        break;
                                    }
                                    dangOrder += str;
                                }
                                tmp.setDangOrder(dangOrder);
                                tmp.setAccountId(shopAccount.getId());
                                Long time  = Long.parseLong(tmp.getShippingTime())+8*60*60;
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
                                String time1 = sdf.format(new Date(time * 1000));
                                tmp.setShippingTime(time1);
                                tmp.setDeleteFlag(0);
                                tmp.setGmtUpdate(new Date());
                                tmp.setGmtCreate(new Date());
                                if(tmp.getShippingComName().equals("快递包裹")||tmp.getShippingComName().equals("挂号印刷品")){
                                    tmp.setShippingComName("中国邮政");
                                }
                                ShopOrder tt = new ShopOrder();
                                tt.setOrderId(tmp.getOrderId());
                                tt =  shopOrderMapper.selectOne(tt);
                                if(tt == null){
                                    shopOrderMapper.insert(tmp);
                                }
                                else{
                                    if(!tt.getDangOrder().equals(tmp.getDangOrder())||!tt.getShippingComName().equals(tmp.getShippingComName())){
                                        shopOrderMapper.update(tmp,new EntityWrapper<ShopOrder>().eq("order_id",tt.getOrderId()));
                                    }
                                    dangOrderDeal(shopAccount,qqq);
                                }
                            }

                        }


                    } catch (IOException e) {
                        throw e;
                    }
                    finally {
                        response.close();
                        httpClient.close();
                    }
                }
            }

        }
        catch (Exception e) {
            ShopLog shopLog = new ShopLog();
            shopLog.setGmtCreate(new Date());
            shopLog.setTitle("同步定时任务异常");
            shopLog.setContent(e.getMessage());
            shopLogMapper.insert(shopLog);
            e.printStackTrace();
        }

    }
    private void doSomething2() {
        try{
            List<ShopAccount> list = shopAccountMapper.selectList(new EntityWrapper<>());
            for (ShopAccount shopAccount:list
            ) {
                flushCookie(shopAccount);
                for (int i = 1; i <= 5; i++) {
                    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
                    HttpGet get = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=" + i + "&pageShow=300&orderStatus=ShippedToReceipt");
                    //这里可以设置请求参数，token等
                    get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
                    get.addHeader("cookie", shopAccount.getFuziCookie());

                    CloseableHttpResponse response = httpClient.execute(get);//执行获取响应
                    try {
                        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//根据状态码处理
                            String strTmp = EntityUtils.toString(response.getEntity(), "UTF-8");
                            //返回字符串
                            String res = unicodeToString(strTmp);
                            JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
                            Boolean status = (Boolean) datas.get("status");//获取返回数据状态，get获取的字段需要根据提供的返回值去获取
                            List<ShopOrder> data = JSONArray.parseArray(datas.get("data").toString(), ShopOrder.class);//"data"是根据返回值设定
                            if (data.size() == 0) {
                                break;
                            }
                            for (ShopOrder tmp : data
                            ) {
                                //正则表达式，用于匹配非数字串，+号用于匹配出多个非数字串
                                String regEx = "[^0-9]+";
                                Pattern pattern = Pattern.compile(regEx);
                                //用定义好的正则表达式拆分字符串，把字符串中的数字留出来
                                String[] cs = pattern.split(tmp.getRemarkText());
                                String dangOrder = "";
                                for (String str : cs
                                ) {
                                    if (dangOrder.length() >= 11 || str.equals("*")) {
                                        break;
                                    }
                                    dangOrder += str;
                                }
                                tmp.setDangOrder(dangOrder);
                                tmp.setAccountId(shopAccount.getId());
                                Long time  = Long.parseLong(tmp.getShippingTime())+8*60*60;
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
                                String time1 = sdf.format(new Date(time * 1000));
                                tmp.setShippingTime(time1);
                                tmp.setDeleteFlag(0);
                                tmp.setGmtUpdate(new Date());
                                tmp.setGmtCreate(new Date());
                                if (tmp.getShippingComName().equals("快递包裹") || tmp.getShippingComName().equals("挂号印刷品")) {
                                    tmp.setShippingComName("中国邮政");
                                }
                                ShopOrder tt = new ShopOrder();
                                tt.setOrderId(tmp.getOrderId());
                                tt = shopOrderMapper.selectOne(tt);
                                if (tt == null) {
                                    shopOrderMapper.insert(tmp);
                                } else {
                                    if (tt.getDangOrderStatus().equals("未发货") && !tt.getDangOrder().equals(tmp.getDangOrder())) {
                                        shopOrderMapper.update(tmp, new EntityWrapper<ShopOrder>().eq("order_id", tt.getOrderId()));
                                    }
                                }
                            }
                        }
                    } catch (IOException e) {
                        throw e;
                    } finally {
                        response.close();
                        httpClient.close();
                    }
                }
            }
            doSomething2();
        }
        catch (Exception e){
            ShopLog shopLog = new ShopLog();
            shopLog.setGmtCreate(new Date());
            shopLog.setTitle("爬取定时任务异常");
            shopLog.setContent(e.getMessage());
            shopLogMapper.insert(shopLog);
            e.printStackTrace();
        }

    }

}
