<?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\allwin\model;
use think\Model;
use app\allwin\model\CouponUser;     //用户优惠券
use think\helper\Time;

class Order extends Model{

    protected $pk = 'id';
    protected $table = 'ai_allwin_order';

    public function store(){
        return $this->hasOne('AllwinStore','id','store_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['uid']               = $param['uid'];
        $data['coupon_user_id']    = $param['user_couponr_id'];
        $data['coupon_ids']        = json_encode($param['coupon_ids']);
        $data['platform_amount']   = $param['platform_amount'];
        $data['user_amount']       = $param['user_amount'];
        $data['coupon_price']      = $param['coupon_price'];
        $data['store_amount']      = $param['store_amount'];
        $data['order_amount']      = $param['order_amount'];
        $data['order_no']          = $param['order_no'];
        $data['state']             = 0;
        $data['update_time']       = time();
        return model('order')->insertGetId($data);
    }

  /**
     * 计算器付款价格
     * @param array $param  [计算参数]
     * @return type
     */
    public static function countPrice(array $param){
        $user_couponr_id = intval($param['user_couponr_id']);
        $user_id         = intval($param['uid']);
        $money           = floatval($param['money']);  //用户默认付款价
        $amount          = floatval($param['amount']); //前端计算付款价
        //我的优惠券
        $coupon_user = [];
        $coupon_user['member_miniapp_id'] = intval($param['member_miniapp_id']);
        $coupon_user['store_id']          = intval($param['store_id']);
        $coupon_user['id']                = $user_couponr_id;        
        $coupon_user['uid']               = $user_id;
        $coupon_user['is_end']            = 0;
        $coupon_user_rel = CouponUser::field('longtime,user_size,price,types,weekday,howmuch,discount,starttime,endtime')->where($coupon_user)->find();
        //需要优惠的价格(分)
        $coupon_user_price = 0;
        if(!empty($coupon_user_rel)){
            if(time() < $coupon_user_rel->starttime || time() > $coupon_user_rel->endtime){
                return ['code'=>403,'msg'=>'优惠券未在有效期'];
            }
            list($start,$end) = Time::today();
            $count = model('CouponLog')->where(['coupon_id' => $user_couponr_id,'uid' => $user_id])->where('times','>=',$start)->count();
            if($count >= $coupon_user_rel->longtime){
                return ['code'=>403,'msg'=>'每天仅允许使用'.$count.'次'];
            }
            if($coupon_user_rel->types){//折扣
                $coupon_user_price = $money - $money * ($coupon_user_rel->discount/10); 
                if ($coupon_user_price >= $coupon_user_rel->user_size) {
                    $coupon_user_price = $coupon_user_rel->user_size;
                }
            }else{//抵扣
                if($money >= $coupon_user_rel->howmuch){
                    if($coupon_user_rel->user_size >= $coupon_user_rel->price){
                        $coupon_user_price = $coupon_user_rel->price;
                    }else{
                        $coupon_user_price = $coupon_user_rel->user_size;
                    }
                }
            }
        }
        //领取的优惠券价格
        $coupon_price = 0;
        if(!empty($param['coupon_ids'])){
            $coupon['id'] = ids($param['coupon_ids'],true);
            $coupon_price = model('Coupon')->where($coupon)->where('num','>','0')->sum('pay_price'); 
            $coupon_price = $coupon_price;
        }
        $amount = $money - $coupon_user_price;
        $store_amount = $amount < 0 ? 0 : $amount;
        $order_amount = abs($store_amount + $coupon_price);
        $data = [
            'coupon_user_price' => abs($coupon_user_price),
            'store_amount'      => abs($store_amount),
            'platform_amount'   => abs($coupon_price),
            'user_amount'       => abs($money),
            'order_amount'      => $order_amount <= 0 ? 0.01: $order_amount,
        ];
        return ['code'=>200,'msg'=>'成功','data' => $data];
    }
}