<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 订单管理
 */
namespace app\smartbc\model;
use think\Model;
use app\smartbc\model\SmartbcCouponUser;     //用户优惠券

class SmartbcOrder extends Model{


    public function store(){
        return $this->hasOne('SmartbcStore','id','store_id');
    }


    public function conponUser(){
        return $this->hasOne('SmartbcCouponUser','id','coupon_user_id');
    }
    /**
     * 创建订单
     * @param array $param
     * @return void
     */
    public static function addOrder(array $param){
        $data                      = [];
        $data['member_miniapp_id'] = $param['member_miniapp_id'];
        $data['store_id']          = $param['store_id'];
        $data['store_chain_id']    = $param['store_chain_id'];
        $data['parent_store_id']   = $param['parent_store_id'] ?? 0;
        $data['uid']               = $param['uid'];
        $data['coupon_user_id']    = $param['user_couponr_id'];
        $data['order_no']          = $param['order_no'];
        $data['amount']            = $param['money'];   //输入金额
        $data['price']             = $param['price'];   //实际金额
        $data['coupon_cache']      = empty($param['coupon_cache'])?'':json_encode($param['coupon_cache']);  //留存优惠券
        $data['state']             = 0;
        $data['update_time']       = time();
        return self::insertGetId($data);
    }

  /**
     * 计算器付款价格
     * @param array $param  [计算参数]
     * @return type
     */
    public static function countPrice(array $param){
        $money = floatval($param['money']);  //用户默认付款价
        //我的优惠券
        $coupon_user = [];
        $coupon_user['member_miniapp_id'] = $param['member_miniapp_id'];
        $coupon_user['store_id']          = $param['store_id'];
        $coupon_user['id']                = $param['user_couponr_id'];
        $coupon_user['uid']               = $param['uid'];
        $coupon_user['is_end']            = 0;
        $coupon_user_rel = SmartbcCouponUser::where($coupon_user)->find();
        //需要优惠的价格(分)
        $coupon_user_price = 0;
        if(!empty($coupon_user_rel)){
            if($money < $coupon_user_rel->howmuch){
                return false;
            }
            $config = SmartbcConfig::getConfig($coupon_user['member_miniapp_id']);
            if(!empty($config->end_time) && ($coupon_user_rel->create_time + $config->end_time * 60 * 60) <= time()){
                return false;
            }
            if($coupon_user_rel->types){//折扣
                $coupon_user_price = $money - $money * ($coupon_user_rel->discount/10); 
            }else{//抵扣
                $coupon_user_price = $coupon_user_rel->price;
            }
        }
        $amount         = $money - $coupon_user_price;
        $data['price']  = $amount <= 0 ? 0.01 : $amount;
        $data['coupon'] = $coupon_user_rel;
        return $data;
    }


    /**
     * 微信买单收益分账
     * @param array $order  订单信息
     * @return void
     */
    public static function income($order,$allMoney){
        $store = SmartbcStore::where(['id' => $order->store_id])->find();
        if(empty($store)){
            $order->parent_store_price = 0;
            $order->real = $order->price;
            return $order;
        }
        $setting = SmartbcConfig::getConfig($store->member_miniapp_id);
        if($order->parent_store_id > 0 && !empty($setting) && !empty($setting->profit)){
            $storeProfit = SmartbcStore::where(['id' => $order->parent_store_id])->find();
            if(empty($storeProfit)){
                return;
            }
            $queen       = []; //分账队列参数
            $storeMoney  = ($allMoney - money($allMoney * $setting->profit)) * 100;
            $profitMoney = money($allMoney * $setting->profit) * 100;
            //分给商户
            $queen[] = [
                'member_miniapp_id' => $store->member_miniapp_id,
                'store_id'          => $store->id,
                'uid'               => 0,
                'mch_id'            => $store->mch_id,
                'amount'            => $storeMoney,
                'transaction_id'    => $order->paid_no,
                'out_order_no'      => $order->order_no,
                'is_finish'         => 0,
                'types'             => 1,
                'msg'               => '结算到商户号',
            ];
            $queen[] = [
                'member_miniapp_id' => $storeProfit->member_miniapp_id,
                'store_id'          => $storeProfit->id,
                'uid'               => 0,
                'mch_id'            => $storeProfit->mch_id,
                'amount'            => $profitMoney,
                'transaction_id'    => $order->paid_no,
                'out_order_no'      => $order->order_no,
                'is_finish'         => 0,
                'types'             => 1,
                'msg'               =>'结算到商户号',
            ];
            $order->parent_store_price = $profitMoney;
            $order->real = $storeMoney;
            SmartbcQueen::createQueen($queen);
            SmartbcStoreBill::add(['miniapp_id' => $storeProfit->member_miniapp_id,'uid' => $storeProfit->manage_uid,'store_id' => $storeProfit->id,'pay_uid' => $order['user_id'],'money' => $profitMoney],'商圈引荐买单奖励'); //引荐商家账单
        }
        return $order;
    }

}