1:php接口
可退运费订单号串接口
/**
* [物流管理-可批量退运费订单号列表-供python脚本调用]
* @Author 田金刚
* @DateTime 2021-03-06
* @return [json] [description]
*/
public function refundOrder(){
$res=Db::table('wechat_drug_freight_refund')->where(['can_refund'=>['gt',0.00],'surplus_freight'=>['gt',0.00],'status'=>0])->order('ordernumber desc')->column('ordernumber');
return json(['code' => 200, 'msg' => '请求成功', 'data' =>$res]);
}
进行批量退运费接口
/**
* [物流管理-批量退运费-供python脚本调用]
* @Author 田金刚
* @DateTime 2021-03-06
* @param ordernumber int 订单号
* @return [json] [description]
*/
public function multiRefund(){
$ordernumber=trim(input('ordernumber'));
if(empty($ordernumber)){
return json(['code' => 201, 'msg' => '参数缺失!', 'data' =>[]]);
}
/*同一个订单号一分钟只允许请求一次接口*/
$key = 'ordernumber.online.freight' . $ordernumber;
$redis = new RedisCache();
$key_ordernumber = $redis->get($key);
if ($key_ordernumber) {
return json(['code' => 201, 'msg' => '同一个订单号一分钟只允许请求一次接口', 'data' => []]);
} else {
$redis->set($key, $ordernumber, 60);//設置有效時間為1分鐘
}
/*合并订单运费不可退*/
$logistics_merge_order=Db::table('wechat_logistics')->where(['ordernumber'=>$ordernumber,'Merge_order'=>0])->find();
if(!empty($logistics_merge_order)){
return json(['code' => 202, 'msg' => '未合并订单不可批量操作!', 'data' =>[]]);
}
/*查询药品订单表是否已退过邮费*/
$drug_pay=Db::table('wechat_drug_pay')->where(['ordernumber'=>$ordernumber])->find();
if(!empty($drug_pay)){
if($drug_pay['status']==5 || $drug_pay['status']==7){
return json(['code' => 203, 'msg' => '该笔订单药品订单中已退过邮费!', 'data' =>[]]);
}
if($drug_pay['status']==4){
$drug_order_fright=Db::table('wechat_drug_order_refund')->where(['ordernum'=>$ordernumber,'order_type'=>['in','4,5,7']])->find();
if(!empty($drug_order_fright)){
return json(['code' => 204, 'msg' => '该笔订单药品订单中已退过邮费!', 'data' =>[]]);
}
}
}
// /*查询药品订单退款表记录*/
$drug_order_refund=Db::table('wechat_drug_order_refund')->where(['ordernum'=>$ordernumber,'order_type'=>['in','4,5,7']])->find();
if(!empty($drug_order_refund)){
return json(['code' => 205, 'msg' => '该笔订单药品订单退款记录中已退过邮费!', 'data' =>[]]);
}
// /*查询退款表记录*/
$drug_refundlog=Db::table('wechat_drug_refundlog')->where(['ordernum'=>$ordernumber,'refund_type'=>1,'order_type'=>['in','4,5,7']])->find();
if(!empty($drug_refundlog)){
return json(['code' => 206, 'msg' => '该笔订单退款记录中已退过邮费!', 'data' =>[]]);
}
// /*查询批量退运费表是否可退总运费是0*/
$drug_freight_refund=Db::table('wechat_drug_freight_refund')->where(['ordernumber'=>$ordernumber])->find();
if(!empty($drug_freight_refund) && $drug_freight_refund['can_refund']==0.00 && $drug_freight_refund['status']==1){
return json(['code' => 207, 'msg' => '该笔订单可退总运费是0.00元!', 'data' =>[]]);
}
if($drug_freight_refund['surplus_freight']==0.00 && $drug_freight_refund['status']==0){
return json(['code' => 208, 'msg' => '该笔订单剩余运费是0.00元!', 'data' =>[]]);
}
if($drug_freight_refund['can_refund']==0.00){
return json(['code' => 209, 'msg' => '该笔订单可退运费是0.00元!', 'data' =>[]]);
}
/*可退总运费金额*/
$can_refund=intval($drug_freight_refund['can_refund']);
/*可退剩余运费金额*/
$surplus_freight=intval($drug_freight_refund['surplus_freight']);
if($can_refund>$surplus_freight){
$new_can_refund=sprintf("%.2f",$can_refund-$surplus_freight);
$new_refund_freight=intval($drug_freight_refund['freight']);
$new_surplus_freight=intval('0.00');
}else{
$new_surplus_freight=intval($surplus_freight)-intval($can_refund);
if($new_surplus_freight==0){
$new_surplus_freight=intval('0.00');
}
$new_can_refund=intval('0.00');
$new_refund_freight=$can_refund;
}
$why=$drug_freight_refund['desc'];
$pay_number=$drug_pay['pay_number'];
$money=$new_refund_freight;
$service_id=$drug_freight_refund['service_id'];
$multi_order=$drug_freight_refund['multi_order'];
//可进行退运费
$drugArr = [
'money' => $drug_pay['money'],
'ordernumber' => $pay_number,
'refund_money' => $money
];
$wxResult = self::refund_money($drugArr, 3);
if($wxResult['result_code'] == 'SUCCESS'){
$out_refund_no=$wxResult['out_refund_no'];
//处理老库逻辑
$resoldrefund=self::oldRefund($ordernumber,$money,$wxResult,$why);
}
Db::table('wechat_drug_pay')->where(['ordernumber'=>$ordernumber])->update(['drug_refund_status'=>1]);
// 启动事务
Db::startTrans();
try {
if ($wxResult['result_code'] == 'SUCCESS') {
//处理新数据库逻辑
//step1 查询发送短信需要订单信息
$DrugPay = Db::table('wechat_drug_pay')->where(['ordernumber' => $ordernumber])->field('id,unitid,consultant_unionid,patient_unionid')->find();
$UserMessage=Db::table('wechat_user_message')->where(['order_id'=>$DrugPay['id']])->field('user_name')->find();
$DrugPay['user_name']=$UserMessage['user_name'];
$DrugFinal=Db::table('wechat_drug_final')->where(['ordernumber' => $ordernumber])->field('id,pay_number')->find();
//step2 记录订单退款成功记录表
$insertDOR = ['order_id' => $DrugPay['id'], 'ordernum' => $ordernumber,'money'=>$money,'postage'=>$money,'type'=>2,'order_type'=>7,'user_id'=>$service_id,'remarks'=>$why,'create_time'=>time(),'update_time'=>time()];
Db::table('wechat_drug_order_refund')->insert($insertDOR);
//step3 记录订单退款记录表
$insertDRL=['ordernum'=>$ordernumber,'money'=>$money,'postage'=>$money,'out_refund_no'=>$out_refund_no,'order_type'=>7,'create_time'=>time(),'update_time'=>time(),'wxresult'=>json_encode($wxResult),'refund_type'=>1];
Db::table('wechat_drug_refundlog')->insert($insertDRL);
//step4 记录订单跟踪表
$contents=['refund_money'=>0.00,'postage'=>$money,'refund_total_money'=>$money,'remarks'=>$why];
$insertDORL=['ordernumber'=>$ordernumber,'type'=>3,'contents'=>json_encode($contents),'create_time'=>time(),'update_time'=>time()];
Db::table('wechat_drug_order_recordlog')->insert($insertDORL);
//step5 药品订单表
Db::table('wechat_drug_pay')->where(['ordernumber' => $ordernumber])->update(['status'=>7,'drug_refund_status'=>0,'update_time'=>time()]);
//step6 台账表
$insertST=['order_id'=>$DrugPay['id'],'payment_id'=>$DrugFinal['id'],'pay_number'=>$DrugFinal['pay_number'],'order_number'=>$ordernumber,'refund_number'=>$out_refund_no,'money'=>$money,'freight'=>$money,'drug_bd'=>0,'pay_dpi'=>0,'sweater_embellish'=>0,'type'=>2,'buy_content'=>3,'create_time'=>time(),'update_time'=>time()];
Db::table('wechat_statistics')->insert($insertST);
//step7 批量退运费表
Db::table('wechat_drug_freight_refund')->where(['multi_order'=>$multi_order])->update(['can_refund'=>$new_can_refund,'updated_at'=>time()]);
Db::table('wechat_drug_freight_refund')->where(['ordernumber'=>$ordernumber])->update(['refund_freight'=>$money,'surplus_freight'=>$new_surplus_freight,'refund_freight'=>$new_refund_freight,'status'=>1,'updated_at'=>time()]);
//如果退款成功,并且同一个合并单号可退运费为0.00时,将同一合并单号还未退运费订单退款运费修改为0.00
if($new_can_refund==0.00 || $new_can_refund==0){
Db::table('wechat_drug_freight_refund')->where(['multi_order'=>$multi_order,'status'=>0,'ordernumber'=>['neq',$ordernumber]])->update(['refund_freight'=>0.00,'updated_at'=>time()]);
}
Db::commit();//提交
//发送短信
self::wxSendLog($DrugPay, 3, $money);
return json(['code' => 200, 'msg' => '退款成功','data'=>['wxResult'=>$wxResult]]);
}else{
//记录订单退款记录表
$insertDRL=['ordernum'=>$ordernumber,'money'=>$money,'postage'=>$money,'out_refund_no'=>$out_refund_no,'order_type'=>7,'create_time'=>time(),'wxresult'=>$wxResult,'refund_fail'=>$wxResult,'refund_type'=>2];
Db::table('wechat_drug_refundlog')->insert($insertDRL);
return json(['code' => -1, 'msg' => '退款失败','data'=>['wxResult'=>$wxResult,'drugArr'=>$drugArr]]);
}
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
Log::record('退款失败的订单号:' . $ordernumber);
return json(['code' => -2, 'msg' => '退款失败','data'=>['wxResult'=>$wxResult,'drugArr'=>$drugArr]]);
}
}
python脚本
run.py文件内容
# coding:utf-8
'''
Description: 入口文件
Autor: Smarttang
Date: 2021-02-05 22:46:06
LastEditTime: 2021-02-05 23:02:10
'''
from config.con import *
import gc
from apscheduler.schedulers.blocking import BlockingScheduler
from mode.sfOrders import Jobs as sfJobs
import logging
logging.basicConfig()
def sfTasks():
sfJobs().run()
gc.collect()
if __name__ == '__main__':
scheduler = BlockingScheduler(timezone="Asia/Shanghai")
scheduler.add_job(sfTasks, 'interval', seconds=300, id='job2') # 每隔5秒执行一次
scheduler.start()
sfOrders.py文件内容
#coding:utf-8
'''
Description: 顺丰退运费逻辑
Autor: Smarttang
Date: 2021-02-05 22:49:38
LastEditTime: 2021-02-05 23:29:11
'''
import requests
from config.con import *
class Jobs:
def __init__(self):
pass
def getOrderID(self):
'''
获取所有的可退运费的订单id
'''
try:
req = requests.post(LOGISTICS_ORDER_URL + '/api/Logisticmanage/refundOrder')
response = req.json()
if int(response['code']) == 200:
return response['data']
else:
return []
except:
return []
def pushOrderID(self, orderNumber):
'''
单个订单退运费
'''
try:
req = requests.post(LOGISTICS_ORDER_URL + '/api/Logisticmanage/multiRefund', data={'ordernumber':orderNumber})
response = req.json()
if int(response['code']) == 200:
return True
else:
return False
except:
return False
def run(self):
'''
退运费入口
'''
orderGroup = self.getOrderID()
print("[*]RefundOrder Count: %s" % len(orderGroup))
for orderId in orderGroup:
self.pushOrderID(orderId)
print("[*]RefundOrder Finish!")