| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package com.uas.eis.serviceImpl;
- import com.taobao.api.DefaultTaobaoClient;
- import com.taobao.api.TaobaoClient;
- import com.taobao.api.domain.Order;
- import com.taobao.api.domain.Trade;
- import com.uas.eis.dao.BaseDao;
- import com.uas.eis.service.InsertService;
- import net.sf.json.JSON;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import oracle.sql.DATE;
- import org.apache.commons.lang.StringUtils;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import java.text.SimpleDateFormat;
- import com.uas.eis.utils.DateUtil;
- import java.util.*;
- import com.taobao.api.request.TradesSoldGetRequest;
- import com.taobao.api.response.TradesSoldGetResponse;
- import com.taobao.api.request.TradesSoldIncrementGetRequest;
- import com.taobao.api.response.TradesSoldIncrementGetResponse;
- import com.taobao.api.request.TopOaidDecryptRequest;
- import com.taobao.api.response.TopOaidDecryptResponse;
- import com.uas.eis.utils.HttpUtil;
- import com.uas.eis.utils.HttpUtil.Response;
- @Service
- public class InsertServiceImpl implements InsertService {
- // TOP服务地址,正式环境需要设置为http://gw.api.taobao.com/router/rest
- public static final String serverUrl = "gw.api.taobao.com/router/rest";
- // private static final String serverUrl = "http://gw.api.tbsandbox.com/router/rest";
- public static final String appKey = "31921866"; // 可替换为您的沙箱环境应用的appKey /* 待修改 */
- public static final String appSecret = "7121c52d4c9970ba08bf09e2f525acd0"; // 可替换为您的沙箱环境应用的appSecret /* 待修改 */
- // public static final String sessionKey = "61024291979328cd09f0ba138f79236d66bd313f3364cf148715824"; // 必须替换为沙箱账号授权得到的真实有效sessionKey /* 待修改 */
- public static Logger logger = Logger.getLogger(InsertServiceImpl.class);
- @Autowired
- private BaseDao baseDao;
- @Override
- public String Insert(String code, String param) {
- try {
- Timer timer = new Timer();//实例化Timer类
- timer.schedule(new TimerTask() {
- public void run() {
- getAllDate();
- }
- }, 600000);//10分钟
- } catch (Exception e) {
- logger.info(this.getClass() + "服务挂掉");
- return e.getMessage();
- }
- return null;
- }
- public String trimNull(String str){
- if("".equals(str)||str==null||"null".equals(str)){
- return "";
- }else{
- return str.trim();
- }
- }
- @Autowired
- @Scheduled(cron = "0 0/8 * * * ?")
- public void getAllDate() {
- // 1.无淘宝自动抓取的 订单 ,则正常备份历史订单
- // 有淘宝自动抓取的 订单 ,则调用增量备份
- List<Trade> trades = new ArrayList<Trade>();
- String sa_remark = baseDao.queryForObject("select distinct sa_remark from sale where sa_remark ='淘宝对接接口抓所有' ", String.class);
- //String session_Key = baseDao.queryForObject(" select tb_code from ( select row_number() over(partition by TB_OPENUID order by tb_date desc ) xuhao , tb_code from tbauthorization ) where xuhao=1 ", String.class);
- List<Map<String,Object>> list = baseDao.queryForList(" select tb_code from ( select row_number() over(partition by TB_OPENUID order by tb_date desc ) xuhao , tb_code from tbauthorization ) where xuhao=1");
- for (int z = 0; z < list.size(); z++) {
- String session_Key = list.get(z).get("tb_code").toString();
- if (sa_remark == null || sa_remark == "") {
- TaobaoClient client = new DefaultTaobaoClient("http://39.98.201.114:30001/router/rest", "31921866", "7121c52d4c9970ba08bf09e2f525acd0");
- TradesSoldGetRequest req = new TradesSoldGetRequest(); //正常备份接口
- req.setFields("total_results,has_next,trades,seller_nick,pic_path,payment,seller_rate,post_fee,receiver_name,receiver_state,receiver_address,receiver_zip,receiver_mobile"+
- ",receiver_phone,consign_time,received_payment,receiver_country,receiver_town,order_tax_fee,shop_pick,tid,num,num_iid,status,title,type,price,discount_fee"+
- ",total_fee,created,pay_time,modified,end_time,seller_flag,buyer_nick,has_buyer_message,credit_card_fee,step_trade_status,step_paid_fee,mark_desc,shipping_type"+
- ",adjust_fee,trade_from"+
- ",service_orders,oid,item_oid,service_id,service_detail_url,num,price,payment,title,total_fee,buyer_nick,refund_id,seller_nick,pic_path"+
- ",tmser_spu_code,oid_str,buyer_rate,receiver_city,receiver_district,o2o,o2o_guide_id,o2o_delivery,"+
- ",orders,item_meal_name,pic_path,seller_nick,buyer_nick,refund_status,outer_iid,snapshot_url,snapshot,timeout_action_time,buyer_rate,seller_rate"+
- ",seller_type,cid,sub_order_tax_fee,sub_order_tax_rate,oid,status,title,item_oid,type,sku_properties_name,order_attr,shipping_type"+
- ",md_qualification,customization,inv_type,shipper,f_type,f_status,f_term,assembly_rela,cl_down_payment,cl_month_payment,cl_tail_payment"+
- ",cl_installment_num,cal_penalty,cl_service_fee,cl_car_taker,cl_car_taker_phone,cl_car_taker_i_d_num,cl_car_taker_id_num,down_payment"+
- ",down_payment_ratio,month_payment,tail_payment,installment_num,penalty,service_fee,oid_str,nr_reduce_inv_fail,sort_info"+
- ",rt_omni_outer_sc_id,rt_omni_sc_id,modify_address,ti_modify_address_time,omni_jxs_outerid,propoint,brand_light_shop_source,special_refund_type"+
- ",extend_info,service_type,threepl_timing,is_o2o_passport,delivery_time,sign_time");
- Calendar calendar3 = Calendar.getInstance();
- calendar3.setTime(new Date());
- calendar3.set(Calendar.HOUR_OF_DAY, 0);
- calendar3.set(Calendar.MINUTE, 0);
- calendar3.set(Calendar.SECOND, 0);
- calendar3.add(calendar3.DATE,-2);
- Date zero3 = calendar3.getTime();
- // req.setStartCreated(StringUtils.parseDateTime("2020-01-05 00:00:00"));
- Date date = new Date();
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(new Date());
- calendar.set(Calendar.HOUR_OF_DAY, 23);
- calendar.set(Calendar.MINUTE, 59);
- calendar.set(Calendar.SECOND, 59);
- Date endtime = calendar.getTime();
- req.setStartCreated(zero3);
- req.setEndCreated(endtime);
- try {
- TradesSoldGetResponse rsp = client.execute(req, session_Key);
- trades = rsp.getTrades();
- } catch (Exception e) {
- //logger.info( new SimpleDateFormat("YYYY-MM-DD hh24:mi:ss").format(new Date())+ this.getClass() + "淘宝未响应并返回订单数据");
- logger.info("淘宝未响应并返回订单数据");
- }
- } else {
- TaobaoClient client2 = new DefaultTaobaoClient("http://39.98.201.114:30001/router/rest", "31921866", "7121c52d4c9970ba08bf09e2f525acd0");
- TradesSoldIncrementGetRequest req2 = new TradesSoldIncrementGetRequest(); //增量备份接口
- req2.setFields("total_results,has_next,trades,seller_nick,pic_path,payment,seller_rate,post_fee,receiver_name,receiver_state,receiver_address,receiver_zip,receiver_mobile"+
- ",receiver_phone,consign_time,received_payment,receiver_country,receiver_town,order_tax_fee,shop_pick,tid,num,num_iid,status,title,type,price,discount_fee"+
- ",total_fee,created,pay_time,modified,end_time,seller_flag,buyer_nick,has_buyer_message,credit_card_fee,step_trade_status,step_paid_fee,mark_desc,shipping_type"+
- ",adjust_fee,trade_from"+
- ",service_orders,oid,item_oid,service_id,service_detail_url,num,price,payment,title,total_fee,buyer_nick,refund_id,seller_nick,pic_path"+
- ",tmser_spu_code,oid_str,buyer_rate,receiver_city,receiver_district,o2o,o2o_guide_id,o2o_delivery,"+
- ",orders,item_meal_name,pic_path,seller_nick,buyer_nick,refund_status,outer_iid,snapshot_url,snapshot,timeout_action_time,buyer_rate,seller_rate"+
- ",seller_type,cid,sub_order_tax_fee,sub_order_tax_rate,oid,status,title,item_oid,type,sku_properties_name,order_attr,shipping_type"+
- ",md_qualification,customization,inv_type,shipper,f_type,f_status,f_term,assembly_rela,cl_down_payment,cl_month_payment,cl_tail_payment"+
- ",cl_installment_num,cal_penalty,cl_service_fee,cl_car_taker,cl_car_taker_phone,cl_car_taker_i_d_num,cl_car_taker_id_num,down_payment"+
- ",down_payment_ratio,month_payment,tail_payment,installment_num,penalty,service_fee,oid_str,nr_reduce_inv_fail,sort_info"+
- ",rt_omni_outer_sc_id,rt_omni_sc_id,modify_address,ti_modify_address_time,omni_jxs_outerid,propoint,brand_light_shop_source,special_refund_type"+
- ",extend_info,service_type,threepl_timing,is_o2o_passport,delivery_time,sign_time");
- Date date2 = new Date();
- Calendar calendar2 = Calendar.getInstance();
- calendar2.setTime(new Date());
- calendar2.set(Calendar.HOUR_OF_DAY, 0);
- calendar2.set(Calendar.MINUTE, 0);
- calendar2.set(Calendar.SECOND, 0);
- Date zero = calendar2.getTime();
- req2.setStartModified(zero);
- // req2.setStartModified(StringUtils.parseDateTime("2020-01-05 00:00:00"));
- calendar2.set(Calendar.HOUR_OF_DAY, 23);
- calendar2.set(Calendar.MINUTE, 59);
- calendar2.set(Calendar.SECOND, 59);
- Date endtime = calendar2.getTime();
- req2.setEndModified(endtime);
- // req2.setEndModified(StringUtils.parseDateTime("2020-01-05 23:59:59"));
- try {
- TradesSoldIncrementGetResponse rsp2 = client2.execute(req2, session_Key);
- trades = rsp2.getTrades();
- } catch (Exception e) {
- logger.info(this.getClass() + "淘宝未响应并返回订单数据");
- }
- }
- List<String> insertSqlsList = new ArrayList<String>();
- if (trades ==null || trades.size() == 0) {
- logger.info(this.getClass() + " 未录入淘宝订单 ");
- } else {
- int L=0;
- List<Long> saleIDS =new ArrayList<>();
- for (int i = 0; i < trades.size(); i++) {
- /* 插入淘宝订单ID sa_tradeid 字段 alter table sale add sa_tradeid number; */
- Long said = baseDao.queryForObject("select sale_seq.nextval from dual", Long.class);
- saleIDS.add(said);
- String sa_code = baseDao.sGetMaxNumber("Sale", 2);
- SimpleDateFormat formatdx = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
- String createdtime="2020-11-25";
- if(trades.get(i).getCreated() == null ) {
- createdtime = formatdx.format(new DATE());
- } else {
- createdtime = formatdx.format(trades.get(i).getCreated());
- }
- String modified="2020-11-25";
- if(trades.get(i).getModified() == null ) {
- modified = formatdx.format(trades.get(i).getCreated());
- } else {
- modified = formatdx.format(trades.get(i).getModified());
- }
- String end_time="2020-11-25";
- if(trades.get(i).getEndTime() == null ) {
- end_time = formatdx.format(trades.get(i).getCreated());
- } else {
- end_time = formatdx.format( trades.get(i).getEndTime() );
- }
- String consigntime="2020-11-25";
- if(trades.get(i).getConsignTime() == null ) {
- consigntime = formatdx.format(trades.get(i).getCreated());
- } else {
- consigntime = formatdx.format( trades.get(i).getConsignTime() );
- }
- if(trades.get(i).getConsignTime() == null ) {
- consigntime = formatdx.format(trades.get(i).getCreated());
- } else {
- consigntime = formatdx.format( trades.get(i).getConsignTime() );
- }
- //0819
- String oaid=trades.get(i).getOaid();
- String tid6=trades.get(i).getTid().toString();
- TopOaidDecryptRequest req6 = new TopOaidDecryptRequest();
- List<TopOaidDecryptRequest.ReceiverQuery> list2 = new ArrayList<TopOaidDecryptRequest.ReceiverQuery>();
- TopOaidDecryptRequest.ReceiverQuery obj3 = new TopOaidDecryptRequest.ReceiverQuery();
- list2.add(obj3);
- obj3.setOaid(oaid);
- obj3.setTid(tid6);
- obj3.setScene("1001");
- req6.setQueryList(list2);
- TaobaoClient client6 = new DefaultTaobaoClient("http://39.98.201.114:30001/router/rest", "31921866", "7121c52d4c9970ba08bf09e2f525acd0");
- String name_oaid=trimNull(trades.get(i).getReceiverName() );
- String receiver_mobile=trimNull(trades.get(i).getReceiverMobile() );
- String address=trimNull(trades.get(i).getReceiverState() ) + trimNull(trades.get(i).getReceiverCity() ) + trimNull(trades.get(i).getReceiverDistrict() ) + trimNull( trades.get(i).getReceiverAddress() );
- if(oaid!=null) {
- try {
- TopOaidDecryptResponse rsp6 = client6.execute(req6, session_Key);
- name_oaid=rsp6.getReceiverList().get(0).getName();
- receiver_mobile=rsp6.getReceiverList().get(0).getMobile();
- address=trimNull(rsp6.getReceiverList().get(0).getState() ) + trimNull(rsp6.getReceiverList().get(0).getCity() ) + trimNull(rsp6.getReceiverList().get(0).getDistrict() ) + trimNull( rsp6.getReceiverList().get(0).getAddressDetail() );
- } catch (Exception e) {
- logger.info(new SimpleDateFormat("YYYY-MM-DD hh24:mm:ss").format(new Date()) + this.getClass() + "淘宝未返回敏感数据");
- }
- }
- //
- String INSERTSALESql = "insert into sale( SA_ISUPDATE,SA_DISCOUNTFEE,SA_TRADESTATUS, sa_sellernick,sa_custcode,sa_paymentscode,SA_TAOBAOID,sa_payments,sa_custname,sa_kind,sa_wwh_user,sa_need15,sa_id,sa_tradeid,sa_code,sa_date,sa_recorder,sa_recorddate,sa_currency,sa_rate" +
- ",sa_apcustname,sa_need9,sa_toplace" +
- ",sa_transport,sa_seller,sa_updatedate" +
- ",sa_enddate,sa_statuscode,sa_status,sa_fare,sa_total" +
- ",sa_recamount,sa_plandelivery,sa_printstatuscode,sa_remark)" +
- " SELECT 1,"+trades.get(i).getDiscountFee() +",'"+trimNull(trades.get(i).getStatus() )+"','" + trimNull(trades.get(i).getSellerNick() ) + " ', 'GN0008358','P001', " + trades.get(i).getTid() + " , '现结','HLK淘宝','国内标准销售订单', ' " +trimNull( name_oaid ) + " ' , ' " + trimNull( receiver_mobile ) + " ' , " + said + "," + trades.get(i).getTid() + "," + trades.get(i).getTid()
- + " , to_date ( '" + end_time + "' ,'YYYY-MM-DD hh24:mi:ss') , '管理员',sysdate,'RMB','1','"
- + trimNull(name_oaid )+ "','" + trimNull( trades.get(i).getBuyerNick() )+ "','" + address + " ', ' "
- + trimNull( trades.get(i).getShippingType() ) + " ', ' " + trimNull (trades.get(i).getO2oGuideName() ) + " ', to_date ( '" + modified
- + "','YYYY-MM-DD hh24:mi:ss') , to_date ( '" +end_time + "','YYYY-MM-DD hh24:mi:ss') ,'ENTERING','在录入'," + trimNull( trades.get(i).getPostFee() )+ "," +trimNull( trades.get(i).getPayment() ) + ","
- + trimNull(trades.get(i).getPayment() ) + " , to_date ( '" +consigntime + "','YYYY-MM-DD hh24:mi:ss') ,'unprint', '淘宝自动抓取' FROM DUAL " +
- " WHERE '" + trimNull(trades.get(i).getStatus() ) + "' NOT IN ( 'TRADE_NO_CREATE_PAY','WAIT_BUYER_PAY','PAY_PENDING' ) AND NOT EXISTS ( select sa_id from sale WHERE SA_iD=" + said + " ) AND NOT EXISTS ( select SA_CODE from sale WHERE SA_CODE= '" + trades.get(i).getTid() + "' ) AND NOT EXISTS ( select SA_TAOBAOID from sale WHERE SA_TAOBAOID=" + trades.get(i).getTid() + " ) ";
- insertSqlsList.add(INSERTSALESql);
- List<Order> orders = trades.get(i).getOrders();
- for (int j = 0; j < orders.size(); j++) {
- int k = j + 1;
- L++;
- Long sdid = baseDao.queryForObject("select saledetail_seq.nextval from dual", Long.class);
- String INSERTSALEDETAILSql = "insert into saledetail(SD_PROPERTIES,sd_remark,sd_id,sd_said,sd_code,sd_detno,sd_prodcode,sd_qty,sd_price,sd_total" +
- ",sd_statuscode,sd_status,sd_discount,SD_DIVIDE_ORDER_FEE,SD_PART_MJZ_DISCOUNT )" +
- " SELECT ' " + trimNull ( orders.get(j).getSkuPropertiesName() ) + " ' , ' " + orders.get(j).getTitle() + " ' , " + sdid + " ," + said + " ,' " + sdid + " '," + k + ",'" + trimNull(orders.get(j).getOuterIid()) + "'," + orders.get(j).getNum() + "," + orders.get(j).getPrice() + " ," + orders.get(j).getPayment() +
- ",'ENTERING','在录入', " + orders.get(j).getDiscountFee() +","+orders.get(j).getDivideOrderFee() +","+orders.get(j).getPartMjzDiscount() + " FROM DUAL WHERE NOT EXISTS ( select sd_id from saledetail " +
- "WHERE '" + trimNull(trades.get(i).getStatus() ) + "' NOT IN ( 'TRADE_NO_CREATE_PAY','WAIT_BUYER_PAY','PAY_PENDING' ) AND sd_id=" + sdid + " ) AND NOT EXISTS ( select sd_id from sale left join saledetail on sa_id=sd_said " +
- "WHERE SA_TAOBAOID = " + trades.get(i).getTid() + " and sd_detno = " + k +" ) ";
- insertSqlsList.add(INSERTSALEDETAILSql);
- }
- }
- try{
- baseDao.execute(insertSqlsList);
- if(saleIDS.size()>0) {
- baseDao.callProcedure("SP_UPDATE_SALE", StringUtils.join(saleIDS, ','));
- }
- }catch (Exception e) {
- logger.info( e.getMessage() );
- }
- }
- logger.info(this.getClass() + "插入销售明细成功");
- }
- }
- }
|