<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\common\model\bbfxshop;

use think\Model;
use think\Db;
use app\common\model\bbfxshop\User;
use app\common\model\bbfxshop\Order;
use app\common\model\bbfxshop\Printer;
use app\common\model\bbfxshop\RebatePintuan;

class PintuanMiandan extends Model
{
    public static function getSet(){
        global $_W;
        $base = $_W['base'];
        if(empty($base)){
            $base = \app\common\model\bbfxshop\Common::getConfig(true);
        }

        return $base['pintuan_miandan'];
    }

    public static function jiesuan($pintuan_order,$order_list,$pintuan_goods){
        $config = self::getSet();
        if($config['status'] != 1){
            return __json(0,"未开启");
        }
        $uniacid = $pintuan_order['uniacid'];
        $plugin = iunserializer($pintuan_goods['plugin']);
        $pintuan_miandan = $plugin['pintuan_miandan'];

        if($pintuan_miandan['status'] != 1){
            return __json(0,"拼团免单未开启");
        }
        $pintuan_success_num = $pintuan_miandan['pintuan_success_num'];
        $pintuan_fail_num = $pintuan_order['total'] - $pintuan_success_num;
        $arr1 = [];
        $arr = [];
        foreach ($order_list as $k => $v){
            $arr1[] = $k;
        }
        
        shuffle($arr1);
        
        foreach ($arr1 as $k => $v){
            if($pintuan_success_num > $k){
                $arr[$v] = false;
            }
            else{
                $arr[$v] = true;
            }
        }
        
        foreach ($order_list as $key => $order){
            
            if($arr[$key]){
                // 未拼中：正常发货核销
                Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['pintuan_miandan_status'=>0]);
                Order::execute($order);
            }
            else{
                // 拼中：退款后，正常发货核销
                Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['pintuan_miandan_status'=>1]);
                
                $order_id = $order['id'];
                $refund = Db::name("bbfx_order_refund")->where(['order_id'=>$order['id'],'uniacid'=>$uniacid])->find();
                if(empty($refund)){
                    $order_no = date('YmdHis').random(8,true);
                    $data = array(
                        "uniacid"=>$uniacid,
                        "user_id"=>$order['user_id'],
                        "order_no"=>$order_no,
                        "money"=>$order['price'],
                        "order_id"=>$order_id,
                        "content"=>"拼团免单退款",
                        "createtime"=>time()
                    );
                    
                    $refund_id = Db::name("bbfx_order_refund")->insertGetId($data);
            
                    if(!empty($refund_id)){
                        // Db::name("bbfx_order")->where(['id'=>$order_id])->update(['refund_status'=>0]);
                        Order::refund($order_id,1);
                    }
                }
                else{
                    if($refund['status'] == 1){
                        Db::name("bbfx_order")->where(['id'=>$order_id])->update(['refund_status'=>$refund['status']]);
                    }
                    else{
                        Db::name("bbfx_order_refund")->where(['id'=>$refund['id']])->update(['content'=>"拼团免单退款"]);
                        Order::refund($order_id,1);
                    }
                }
                
            }

        }

        return __json(1,"操作成功");
    }
}