zhaojs
2023-07-12 87940848162256b5adaa125408b297dba3e8691b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace app\api\logic\order;
 
use think\Db;
use app\common\logic\Basic as BasicLogic;
use app\common\model\order\PddOrder as ModelPddOrder;
use app\api\logic\commission\Pdd as LogicPddCommission;
 
class PddOrder extends BasicLogic
{
    protected $model;
 
    protected static $userRate = 0; //用户反佣比例
 
 
    public function __construct(){
        $this->model = new ModelPddOrder();
    }
 
 
 
    /**
     * 订单入库和更新
     *
     * @return void
     */
    public function fanliOrder($order)
    {
        $order_sn = isset($order['order_sn']) ? $order['order_sn'] : '';
        if(empty($order_sn)) return ['status'=>false,'info'=>'订单号为空'];
        Db::startTrans();
        $find_order = $this->model->where(['order_sn'=>$order_sn])->find();
        $custom_parameters = isset($order['custom_parameters']) ? $order['custom_parameters'] : "";
        $uid_arr = json_decode($custom_parameters,true);
        $uid = !$uid_arr ? "" : $uid_arr['uid'];
        $save_order = [
            'auth_duo_id' => isset($order['auth_duo_id']) ? $order['auth_duo_id'] : '',
            'order_id' => isset($order['order_id']) ? $order['order_id'] : (!empty($find_order) ? $find_order['order_id'] : ''),
            'batch_no' => isset($order['batch_no']) ? $order['batch_no'] : '',
            'cpa_new' => isset($order['cpa_new']) ? $order['cpa_new'] : '',
            'custom_parameters' => isset($order['custom_parameters']) ? $order['custom_parameters'] : '',
            'fail_reason' => isset($order['fail_reason']) ? $order['fail_reason'] : '',
            'goods_id' => isset($order['goods_id']) ? $order['goods_id'] : '',
            'goods_name' => isset($order['goods_name']) ? $order['goods_name'] : '',
            'goods_price' => isset($order['goods_price']) ? $order['goods_price'] : '',
            'goods_quantity' => isset($order['goods_quantity']) ? $order['goods_quantity'] : '',
            'goods_thumbnail_url' => isset($order['goods_thumbnail_url']) ? $order['goods_thumbnail_url'] : '',
            'group_id' => isset($order['group_id']) ? $order['group_id'] : '',
            'order_amount' => isset($order['order_amount']) ? $order['order_amount'] : '',
            'order_create_time' => isset($order['order_create_time']) ? $order['order_create_time'] : '',
            'order_group_success_time' => isset($order['order_group_success_time']) ? $order['order_group_success_time'] : '',
            'order_modify_at' => isset($order['order_modify_at']) ? $order['order_modify_at'] : '',
            'order_pay_time' => isset($order['order_pay_time']) ? $order['order_pay_time'] : '',
            'order_receive_time' => isset($order['order_receive_time']) ? $order['order_receive_time'] : '',
            'order_settle_time' => isset($order['order_settle_time']) ? $order['order_settle_time'] : '',
            'order_sn' => isset($order['order_sn']) ? $order['order_sn'] : '',
            'order_status' => isset($order['order_status']) ? $order['order_status'] : '',
            'order_status_desc' => isset($order['order_status_desc']) ? $order['order_status_desc'] : '',
            'order_verify_time' => isset($order['order_verify_time']) ? $order['order_verify_time'] : (!empty($find_order) ? $find_order['order_verify_time'] : ''),
            'promotion_amount' => isset($order['promotion_amount']) ? $order['promotion_amount'] : '',
            'promotion_rate' => isset($order['promotion_rate']) ? $order['promotion_rate'] : '',
            'p_id' => isset($order['p_id']) ? $order['p_id'] : '',
            'type' => isset($order['type']) ? $order['type'] : '',
            'zs_duo_id' => isset($order['zs_duo_id']) ? $order['zs_duo_id'] : '',
            'cat_ids' => isset($order['cat_ids']) ? (is_array($order['cat_ids']) ? implode(',', $order['cat_ids']) : $order['cat_ids']) : '',
            'is_direct' => isset($order['is_direct']) ? $order['is_direct'] : '',
            'sep_duo_id' => isset($order['sep_duo_id']) ? $order['sep_duo_id'] : '',
            'sep_market_fee' => isset($order['sep_market_fee']) ? $order['sep_market_fee'] : '',
            'sep_pid' => isset($order['sep_pid']) ? $order['sep_pid'] : '',
            'sep_rate' => isset($order['sep_rate']) ? $order['sep_rate'] : '',
            'sep_parameters' => isset($order['sep_parameters']) ? $order['sep_parameters'] : '',   
            'price_compare_status' => isset($order['price_compare_status']) ? $order['price_compare_status'] : '',
            'uid' => $uid,
            'pid' => isset($order['pid']) ? $order['pid'] : ''
        ];
        if(empty($find_order)){
            $save_order['tk_z_status'] = 1;
            $save_order['is_distribution'] = (empty($save_order['uid']) || $save_order['uid'] == 0) ? 2 : 0; //如果没有渠道关系ID则该订单无须反佣
            $save_order['create_time'] = time();
            $res = $this->model->insert($save_order);
            if(!$res){
                Db::rollback();
                return ['status'=>false,'info'=>'订单添加失败,订单order_sn:'.$order_sn];
            }
        }else{
            if($find_order['order_status'] != $save_order['order_status']){ //订单发生改变,更新订单
                $save_order['update_time'] = time();
                $save_order['tk_z_status'] = 2;
                $res_update = $this->model->where(['order_sn'=> $order_sn])->update($save_order);
                if(!$res_update){
                    Db::rollback();
                    return ['status'=>false,'info'=>'订单更新失败,订单order_sn:'.$order_sn];
                }
            }   
        }
        Db::commit();
        return ['status'=>true,'info'=>'订单逻辑处理完成'];
    }
 
 
    /**
     * 拆分订单
     *
     * @return void
     */
    public function chaifenOrder()
    {
        $list = $this->model->where(['is_distribution'=>0,'uid'=>['neq',''],'uid'=>['neq',0]])->whereOr("is_distribution = 1 And tk_z_status = 2 And uid !=''")->limit(20)->order('id desc')->select();
        if(!empty($list)){
            $LogicPddCommission = new LogicPddCommission();
            foreach($list as $order){
                $result = $LogicPddCommission->fanliCommission($order);
                if(!$result){
                    mylog('订单拆分错误',$result['info'],"errorInfo");
                }
            }
        }
        return ['status'=>true,'info'=>'拆分逻辑执行完成'];
    }