<?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;

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

    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']);
        $rebate_pintuan = $plugin['rebate_pintuan'];

        if($rebate_pintuan['status'] != 1){
            return __json(0,"返利未开启");
        }
        $pintuan_success_num = $rebate_pintuan['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] = true;
            }
            else{
                $arr[$v] = false;
            }
        }
        
        foreach ($order_list as $key => $order){
            if($arr[$key]){
                $rebate_pintuan_status = 1;
                Order::execute($order);
            }
            else{
                $rebate_pintuan_status = 0;
                
                $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);
                    }
                }
                
            }

            Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['rebate_pintuan_status'=>$rebate_pintuan_status]);
            if($rebate_pintuan_status === 0){
                RebatePintuan::setReward($order['id']);
            }
        }

        return __json(1,"操作成功");
    }
    
    public static function setReward($order_id){
        $config = self::getSet();
        if($config['status'] != 1){
            return __json(0,"未开启");
        }
        
        $order = Db::name("bbfx_order")->where(['id'=>$order_id,'order_source'=>3,'pintuan_order_id'=>['>',0],'pintuan_status'=>1,'rebate_pintuan_status'=>0,'status'=>['>',0],'refund_status'=>1])->find();
        if(empty($order)){
            return __json(0,"订单不存在");
        }
        
        /*
        if($config['daozhang_type'] == 1){
            if($order['status'] < 2){
                return __json(0,"订单状态错误");
            }
        }
        else if($config['daozhang_type'] == 3){
            if($order['status'] != 3){
                return __json(0,"订单状态错误");
            }
        }
        */
        
        $pintuan_goods = Db::name("bbfx_pintuan_goods")->alias("a")->join("bbfx_pintuan_order b","a.id=b.pintuan_goods_id","LEFT")->where(['b.id'=>$order['pintuan_order_id']])->field("a.*")->find();
        if(empty($pintuan_goods)){
            return __json(0,"拼团商品不存在");
        }
        
        $rebate_pintuan = iunserializer($pintuan_goods['plugin'])['rebate_pintuan'];
        if($rebate_pintuan['status'] != 1){
            return __json(0,"返利未开启");
        }
        
        $total = Db::name("bbfx_order_goods")->where(['order_id'=>$order['id'],'goods_id'=>$pintuan_goods['goods_id']])->value("total");
        $total = max(1,intval($total));
        
        $money = round($rebate_pintuan['pintuan_fail_reward'],2) * $total;
        if($money <= 0){
            return __json(0,"未设置未拼中奖励");
        }
        
        $log = Db::name("bbfx_money_log")->where(['user_id'=>$order['user_id'],'type'=>1,'item_type'=>36,'item_id'=>$order['id']])->find();
        if(!empty($log)){
            return __json(0,"已发放奖励");
        }
        
        User::setUserMoney($order['user_id'],$money,1,36,"拼团未拼中奖励",$order);
        
        return __json(1,"操作成功");
    }
}

