如果给你一个批量数据,比如订单数据,订单的状态可能更加很多个字段决定的,这些订单类型还可能很多种类型的情况下,去和第三方对接或者ERP对接数据的时候;怎么做到精确输出所有信息?一条不漏,一条不错呢?
①系统中定义一套规则类似,支付宝等之类的需要验签的规则。
②进行逻辑代码的处理。
本文主要讲逻辑代码:
<?php
namespace app\modules\oms\models;
use app\modules\api\models\integralmall\OrderSubmitPreviewForm;
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020年11月30日
* Time: 上午 10:40
*/
class OrderConfirmForm extends ApiModel{
public $store_id;
public $type;
public $all;
public $limit;
/**
* 获取列表
*/
public function confirmOrders(){
$list = array_unique(array_column($this->all, 'order_no'));
$field = 'id,order_no,pay_type,pay_time,is_send,send_time,apply_delete,is_delete,shop_id,update_time,is_cancel,is_pay,user_id,is_confirm';
$orderArr = implode(',', $list);
$inStr = "'". str_replace(",", "','",$orderArr). "'";
//普通订单and砍价订单
$sql1 = "(SELECT $field,1 order_type,is_offline,send_type FROM {{%order}} WHERE store_id=:store_id and order_no in($inStr))";
//拼团订单 2自提
$sql2 = "(SELECT $field,2 order_type,IF(offline=2,1,0) as is_offline,send_type FROM {{%pt_order}} WHERE store_id=:store_id and order_no in($inStr))";
//秒杀订单
$sql3 = "(SELECT $field,3 order_type,is_offline,send_type FROM {{%ms_order}} WHERE store_id=:store_id and order_no in($inStr))";
//积分商城订单 2自提
$sql4 = "(SELECT $field,4 order_type,IF(is_offline=2,1,0) as is_offline,0 send_type FROM {{%integral_order}} WHERE store_id=:store_id and order_no in($inStr))";
$value[':store_id'] = $this->store_id;
$value[':order_no'] = $inStr;
$sql = "select * from ($sql1 UNION $sql2 UNION $sql3 UNION $sql4 ) b ORDER BY b.update_time DESC limit {$this->limit}";
$orderData = \Yii::$app->db->createCommand($sql)->bindValues($value)->queryAll();
$orderNoArr = array_column($orderData, 'order_no');
//记录成功
$success = 0;
//记录失败
$error = 0;
$data = [];
$error_msg = '';
foreach ($this->all as $k => $v) {
//验证订单是否存在
if(!in_array($v['order_no'], $orderNoArr)){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. '订单不存在;';
$data[] = $error_data;
continue;
}
//可拓展类型
if(!in_array($v['btc_type'], [1]) && $this->type==0){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. '==>B2C订单中btc_type字段值应为1;';
$data[] = $error_data;
continue;
}
//配送公司
if(!$v['delivery_company'] && $this->type==1){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. '==>O2O中delivery_company值不能为空';
$data[] = $error_data;
continue;
}
//配送状态:0待调度;1已接单;2已取货;3已送达;4已取消(为4时,请调用取消订单接口)
if(!in_array($v['delivery_status'], [0,1,2,3]) && $this->type==1){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. '==>O2O中delivery_status值应为:0待调度;1已接单;2已取货;3已送达;4已取消(为4时,请调用取消订单接口)';
$data[] = $error_data;
continue;
}
$order = $orderData[array_search($v['order_no'], $orderNoArr)];
//验证订单是否已取消
if($order['is_delete'] != 0 || $order['is_cancel'] != 0){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. '订单已取消;';
$data[] = $error_data;
continue;
}
//验证订单是否已收货
if($order['is_confirm'] != 0){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. '订单是已收货状态;';
$data[] = $error_data;
continue;
}
switch ($order['order_type']) {
case 1://普通与砍价
$form = new \app\modules\api\models\OrderConfirmForm();
$form1= new \app\modules\mch\models\OrderSendForm();
break;
case 2://拼团
$form = new \app\modules\api\models\group\OrderConfirmForm();
$form1= new \app\modules\mch\models\group\OrderSendForm();
break;
case 3://秒杀
$form = new \app\modules\api\models\miaosha\OrderConfirmForm();
$form1= new \app\modules\mch\models\miaosha\OrderSendForm();
break;
case 4://积分
$form = new OrderSubmitPreviewForm();
$form1= new \app\modules\mch\models\integralmall\IntegralOrderForm();
break;
}
$form->order_id = $order['id'];
$form->user_id = $order['user_id'];
$form->store_id = $this->store_id;
//O2O订单处理
if($this->type==1){
if ($order['order_type'] != 4) {
$order_status=self::getOrderStatus($order);
//订单状态:0:未付款 1:待发货 2:待收货 3:已完成 4:已取消
//配送状态:0待调度;1已接单;2已取货;3已送达;4已取消(为4时,请调用取消订单接口)
if($order_status==3 && in_array($v['delivery_status'],[0,1,2])){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单已完成,delivery_status值'.$v['delivery_status'].'非法';
$data[] = $error_data;
continue;
}
if($order_status==0 && in_array($v['delivery_status'],[0,1,2,3])){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单未付款,delivery_status值'.$v['delivery_status'].'非法';
$data[] = $error_data;
continue;
}
if ($order_status==1 && in_array($v['delivery_status'],[3])){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单未发货,delivery_status值'.$v['delivery_status'].'非法';
$data[] = $error_data;
continue;
}
//订单待收货,修改成功条数
if($order_status==2 && $order['is_offline']==0 &&in_array($v['delivery_status'],[0,1,2])){
$success++;
continue;
}
//兼容:自提订单待收货,修改成功条数 自提订单配送状态:配送状态:0待调度;1已接单;2已取货;
if($order_status==2 && $order['is_offline']==1 && in_array($v['delivery_status'],[0,1,2])){
$form1->attributes = ['is_express'=>0,'order_id'=>$order['id'],'express'=>$v['delivery_company']];
$form1->store_id = $this->store_id;
$res=$form1->save();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
//o2o订单待发货处理;根据O2O状态。
if($order_status == 1 && in_array($v['delivery_status'],[0,1,2])){
$form1->attributes = ['is_express'=>0,'order_id'=>$order['id'],'express'=>$v['delivery_company']];
$form1->store_id = $this->store_id;
$res=$form1->save();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
//待收货,确认收货
if($order_status==2 && in_array($v['delivery_status'],[3])){
if($order['is_offline']){
$form1->attributes = ['is_express'=>0,'order_id'=>$order['id'],'express'=>$v['delivery_company']];
$form1->store_id = $this->store_id;
$form1->save();
}
$form->is_confirm=true;
$form->express=$v['delivery_company'];
$res = $form->save();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
}else{
$order_status=self::getOrderStatus($order);
//订单状态:0:未付款 1:待发货 2:待收货 3:已完成 4:已取消
//配送状态:0待调度;1已接单;2已取货;3已送达;4已取消(为4时,请调用取消订单接口)
if($order_status==3 && in_array($v['delivery_status'],[0,1,2])){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单已完成,delivery_status值'.$v['delivery_status'].'非法';
$data[] = $error_data;
continue;
}
if($order_status==0 && in_array($v['delivery_status'],[0,1,2,3])){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单未付款,delivery_status值'.$v['delivery_status'].'非法';
$data[] = $error_data;
continue;
}
if($order_status == 1 && in_array($v['delivery_status'],[3])){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单未发货,delivery_status值'.$v['delivery_status'].'非法';
$data[] = $error_data;
continue;
}
//订单待收货,修改成功条数
if($order_status==2 && in_array($v['delivery_status'],[0,1,2])){
$success++;
continue;
}
//兼容:自提订单待收货,修改成功条数 自提订单配送状态:配送状态:0待调度;1已接单;2已取货;
if($order_status==2 && $order['is_offline']==1 && in_array($v['delivery_status'],[0,1,2])){
$form1->attributes = ['is_express'=>0,'order_id'=>$order['id'],'express'=>$v['delivery_company']];
$form1->store_id = $this->store_id;
$res=$form1->save();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
//o2o订单待发货处理;根据O2O状态。
if($order_status == 1 && in_array($v['delivery_status'],[0,1,2])){
$form1->attributes = ['is_express'=>0,'order_id'=>$order['id'],'express'=>$v['delivery_company']];
$form1->store_id = $this->store_id;
$res=$form1->save();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
if($order_status==2 && in_array($v['delivery_status'],[3])){
if($order['is_offline']){
$form1->attributes = ['is_express'=>0,'order_id'=>$order['id'],'express'=>$v['delivery_company']];
$form1->store_id = $this->store_id;
$form1->save();
}
$form->is_confirm=true;
$form->express=$v['delivery_company'];
$res = $form->confirm();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
}
}
//B2C订单处理
if($this->type==0){
if ($order['order_type'] != 4) {
$order_status=self::getOrderStatus($order);
//订单状态:0:未付款 1:待发货 2:待收货 3:已完成 4:已取消
//配送状态:0待调度;1已接单;2已取货;3已送达;4已取消(为4时,请调用取消订单接口)
if($order_status==3){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单已完成;请勿重复操作';
$data[] = $error_data;
continue;
}
if($order_status==0){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单未付款;无法确认收货';
$data[] = $error_data;
continue;
}
if($order_status==1){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单未发货,无法确认收货';
$data[] = $error_data;
continue;
}
if($order_status==2 && $order['is_offline']==0 ){
$res = $form->save();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
if($order_status==2 && $order['is_offline']==1){
$form1->attributes = ['is_express'=>0,'order_id'=>$order['id'],'express'=>$v['delivery_company']];
$form1->store_id = $this->store_id;
$form1->save();
$res = $form->save();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
}else{
$order_status=self::getOrderStatus($order);
//订单状态:0:未付款 1:待发货 2:待收货 3:已完成 4:已取消
//配送状态:0待调度;1已接单;2已取货;3已送达;4已取消(为4时,请调用取消订单接口)
if($order_status==3){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单已完成;请勿重复操作';
$data[] = $error_data;
continue;
}
if($order_status==0){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单未付款;无法确认收货';
$data[] = $error_data;
continue;
}
if($order_status==1){
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no'].'订单未发货,无法确认收货';
$data[] = $error_data;
continue;
}
if($order_status==2 && $order['is_offline']==0 ){
$res = $form->confirm();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
if($order_status==2 && $order['is_offline']==1){
$form1->attributes = ['is_express'=>0,'order_id'=>$order['id'],'express'=>$v['delivery_company']];
$form1->store_id = $this->store_id;
$form1->save();
$res = $form->confirm();
if ($res['code'] == 0) {
$success++;
}else{
$error++;
$error_data['order_no'] = $v['order_no'];
$error_msg .= $error_data['msg'] = 'order_no:'.$v['order_no']. $res['msg'];
$data[] = $error_data;
continue;
}
}
}
}
}
return [
'code'=> 0,
'msg'=>'更新成功:成功'.$success.'条,失败'.$error."条",
'data' => $data,
];
}
/**
* 获取订单状态
* @param $order array 订单
* @return int 0:未付款 1:待发货 2:待收货 3:已完成 4:已取消
* name:caiwh
*/
public function getOrderStatus($order){
$status = 0;
if ($order['is_delete'] == 1 || ($order['order_type'] == 2 && $order['is_cancel'] == 1)) {
$status = 4;
}elseif ($order['is_pay'] == 0) {
$status = 0;
}elseif ($order['is_pay'] == 1 && $order['is_send'] == 0) {
$status = 1;
}elseif ($order['is_pay'] == 1 && $order['is_send'] == 1 && $order['is_confirm'] == 0) {
$status = 2;
}elseif ($order['is_pay'] == 1 && $order['is_send'] && $order['is_confirm'] == 1) {
$status = 3;
}
//兼容自提订单状态
//①兼容自提;只要支付;即为待收货
//②兼容自提;无论是否发货;只要确认收货;即为已完成
if ($order['is_pay'] == 1 && $order['is_offline'] == 1 && $order['is_confirm'] == 0){
unset($status);
$status = 2;
}elseif ($order['is_pay'] == 1 && $order['is_offline'] == 1 && $order['is_confirm'] == 1){
unset($status);
$status = 3;
}
return $status;
}
}
效果: