'array', 'odds' => 'array', 'odds_raw' => 'array', ]; protected $appends = [ 'play_type_name', 'buy_type_name', 'pass_mode_name', 'lottery_estimate_send_prize', 'gendan_brokerage', 'order_closed', 'lottery_is_wined', 'money_show', 'union_state_show', 'close_time_str', 'enable_add', ]; public static function boot() { parent::boot(); static::updated(function($model) { if($model->wasChanged('lottery_state')) { dispatch(new ReportOrderDataJob($model->id)); } }); } public function pOrder() { return $this->belongsTo(Order::class, 'pid', 'id'); } public function unionOrder() { return $this->belongsTo(Order::class, 'pid', 'id'); } public function customer() { return $this->belongsTo(Customer::class, 'customer_id', 'id'); } public function receiver() { return $this->belongsTo(Seller::class, 'receive_user_id', 'id'); } public function cooperate() { return $this->hasOne(Shop::class, 'id', 'cooperate_id'); } public function shop() { return $this->hasOne(Shop::class, 'id', 'shop_id'); } public function lottery() { return $this->hasOne(Lottery::class, 'id', 'lottery_id'); } public function lotteryType() { return $this->hasOne(LotteryType::class, 'id', 'lottery_type_id'); } public function materials() { return $this->hasMany(Material::class, 'order_id', 'id'); } public function dlt() { return $this->hasOne(Dlt::class, 'issue_num', 'issue_num') ->where('state', BoolEnum::YES); } public function qxc() { return $this->hasOne(Qxc::class, 'issue_num', 'issue_num') ->where('state', BoolEnum::YES); } public function pls() { return $this->hasOne(Pls::class, 'issue_num', 'issue_num') ->where('state', BoolEnum::YES); } public function plw() { return $this->hasOne(Plw::class, 'issue_num', 'issue_num') ->where('state', BoolEnum::YES); } public function withShuziCai() { switch ($this->lotteryType->type) { case LottType::QXC: $this->qxc; return; case LottType::DLT: $this->dlt; return; case LottType::PLS: $this->pls; return; case LottType::PLW: $this->plw; return; } } public function getEnableAddAttribute() { if ($this->odds && Arr::get($this->odds, '0.enable_add')) { return true; } return false; } public function getUnionStateShowAttribute() { if (!$this->type || $this->type != OrderType::UNION) { return ''; } if ($this->type == OrderType::UNION && $this->id != $this->pid) { return ''; } if ($this->pay_state != PayState::SUCCESS) { return ''; } if ($this->lottery_state == LottState::CANCEL) { return '已流单'; } if ($this->lottery_state == LottState::REVOKE) { return '已流单'; } if ($this->lottery_state == LottState::LOSS) { return '未中奖'; } if ($this->lottery_state == LottState::WAIT) { return '待开奖'; } if ($this->lottery_state == LottState::DRAFT) { return '待出票'; } if (in_array($this->lottery_state, self::zhongJiangStates())) { return '已中奖'; } if ($this->lottery_state == LottState::PENDING) { if ($this->union_piece_total == $this->union_piece_buy) { return '合买成功'; } if ($this->odds_early_close_time >= date('Y-m-d H:i:s')) { return '合买失败'; } return '进行中'; } return '其他'; } public function getCloseTimeStrAttribute() { if (!$this->odds_early_close_time) { return ''; } return date('m-d H:i', strtotime($this->odds_early_close_time)); } public function getMoneyShowAttribute() { if ($this->type == OrderType::UNION) { return Helps::floatFormat($this->union_money); } return Helps::floatFormat($this->money); } public function getLotteryIsWinedAttribute() { if (!$this->lottery_state && !$this->lottery_prize) { return false; } if (in_array($this->lottery_state, [LottState::WIN, LottState::SEND])) { return true; } if ($this->lottery_prize > 0) { return true; } return false; } public function getOrderClosedAttribute() { if (!$this->odds_early_close_time) { return false; } return date('Y-m-d H:i:s') > $this->odds_early_close_time; } public function getLotteryEstimateSendPrizeAttribute() { if ($this->type == OrderType::GENDAN) { return $this->lottery_tax_prize - $this->lottery_tax_prize * Config::fadanAllFeeLv(); } return $this->lottery_tax_prize; } public function getGendanBrokerageAttribute() { return $this->lottery_gendan_brokerage; } public function getPlayTypeNameAttribute() { if (!$this->play_type) { return ''; } return PlayType::getDescription($this->play_type); } public function getBuyTypeNameAttribute() { if (!$this->type) { return ''; } if ($this->type == OrderType::UNION) { return '合买'; } if ($this->type == OrderType::GENDAN) { return '跟单'; } if ($this->type == OrderType::FADAN) { return '发单'; } return '自购'; } public function getPassModeNameAttribute() { if (!$this->pass_mode) { return []; } return $this->getPassModelNameByKeys($this->pass_mode); } public function getPassModelNameByKeys($keys) { $result = []; foreach ($keys as $val) { if ($val == '1.1') { $result[] = '固定单关'; continue; } $result[] = str_replace('.', '串', $val); } return $result; } public function lotteryName() { $lottery = $this->lottery->lotteryType; $name = $lottery->name; $play = PlayType::getDescription($this->play_type); if ($play) { $name .= $play; } return $name; } public function scopeSn($query, $orderSn) { return $query->where('order_sn', $orderSn); } /** * 可用的订单 * @param $query * @return mixed */ public function scopeUsable($query) { return $query->whereIn('lottery_state', [ LottState::WIN, LottState::LOSS, LottState::WAIT, LottState::DRAFT, LottState::PENDING, LottState::SEND, ]); } public function scopeAgentUsable($query) { return $query->whereIn('lottery_state', [ LottState::WIN, LottState::LOSS, LottState::WAIT, LottState::SEND, ]); } public static function makeOrderSn() { $cdate = date('Ymd'); $orderSn = new OrderSn(); $orderSn->cdate = $cdate; $orderSn->save(); $yesterdayOrderSn = OrderSn::where('cdate', '<', $cdate)->orderBy('id', 'desc')->first(); if (!$yesterdayOrderSn) { return sprintf('P%s%08d', date('Ymd'), $orderSn->id); } return sprintf('P%s%08d', date('Ymd'), $orderSn->id - $yesterdayOrderSn->id); } public function isTakeTicket() { return $this->lottery_take_ticket == BoolEnum::YES; } public function isSuccess() { return $this->pay_state = PayState::SUCCESS; } public function setSuccess() { // 如果是合买 if ($this->isUnionLeader() && $this->getPayMoney() == $this->money) { $this->union_pay_success = BoolEnum::YES; } $this->lottery_state = LottState::PENDING; $this->pay_state = PayState::SUCCESS; $this->pay_at = date('Y-m-d H:i:s'); $this->pay_day = date('Ymd'); $this->save(); // 支付成功事件 event(new OrderPaySuccessEvent($this->id)); // 用户最后一次下单时间 Customer::where('id', $this->customer_id) ->update([ 'pay_time' => time() ]); } public function isUnionLeader() { return $this->type == OrderType::UNION && $this->id == $this->pid; } /** * 是否可以被跟单 * @return bool */ public function canCopy() { return $this->pay_state == PayState::SUCCESS && in_array($this->lottery_state, [LottState::WAIT]) && $this->odds_early_close_time > date('Y-m-d H:i:s'); } public function getPayMoney() { $money = $this->money; if ($this->type == OrderType::UNION) { $money = $this->union_money; } return Helps::floatFormat($money); } public function getUnionUnitPrice() { return $this->money / $this->union_piece_total; } public function gendanLotteryState() { return [LottState::WAIT, LottState::WIN, LottState::LOSS, LottState::DRAFT, LottState::SEND]; } public function getGendanInfo() { $info = []; if ($this->type != OrderType::FADAN) { return $info; } $gendanCount = Order::where('pid', $this->id) ->where('pay_state', PayState::SUCCESS) ->whereIn('lottery_state', $this->gendanLotteryState()) ->count(); $info['count_gendan'] = $gendanCount; $info['count_default_show'] = 10; $query = Order::with([ 'customer:id,name,nickname', 'pOrder:id,order_sn,customer_id', 'pOrder.customer:id,nickname,name' ]) ->select(['id', 'pid', 'customer_id', 'money', 'lottery_state', 'lottery_tax_prize', DB::raw('lottery_gendan_brokerage_all as lottery_gendan_brokerage') ]) ->where('pid', $this->id) ->where('pay_state', PayState::SUCCESS) ->whereIn('lottery_state', $this->gendanLotteryState()); $total = $query->sum('lottery_gendan_brokerage_all'); $gendanList = $query->orderBy('money', 'desc') ->limit(10) ->get(); $info['list'] = $gendanList; $info['brokerage'] = $total * Config::fadanUserFeeLv(); return $info; } public function getUnionInfo() { $unionUses = Order::with('customer:id,avatar,nickname,name,level_score') ->select(['id', 'pid', 'customer_id', 'union_piece_total', 'union_piece_buy','union_piece_money', 'union_money', 'union_piece_self']) ->where('pid', $this->id) ->where('type', OrderType::UNION) ->where('pay_state', PayState::SUCCESS) ->orderBy('union_piece_buy', 'desc') ->get(); foreach ($unionUses as $item) { $item->union_show_piece_money = Helps::floatFormat($item->union_piece_self * $this->union_piece_money); } return [ 'list' => $unionUses ]; } public static function kaiJiangHouStates() { return [LottState::WIN, LottState::SEND, LottState::LOSS, LottState::DELETE, LottState::CANCEL]; } public static function zhongJiangStates() { return [LottState::WIN, LottState::SEND]; } public function sellerCanSeeSellings(Seller $seller) { return in_array($this->lottery_state, [LottState::DRAFT, LottState::WIN, LottState::WAIT, LottState::LOSS, LottState::SEND, LottState::CANCEL, LottState::REVOKE]); } public function customerCanSeeSellings(Customer $customer) { if ($this->type == OrderType::NORMAL || $this->type == OrderType::FADAN) { if ($this->customer_id == $customer->id) { return true; } } if ($this->type == OrderType::FADAN) { if ($this->type_mode == 2) { return true; } if ($this->type_mode == 1) { return date('Y-m-d H:i:s') > $this->odds_close_time; } if ($this->type_mode == 3) { $exist = Order::where('pid', $this->id) ->where('customer_id', $customer->id) ->where('type', OrderType::FADAN) ->where('pay_state', PayState::SUCCESS) ->first(); return $exist ? true : false; } } if ($this->type == OrderType::GENDAN) { if ($this->type_mode == 2) { return true; } if ($this->type_mode == 1) { return date('Y-m-d H:i:s') > $this->odds_close_time; } if ($this->type_mode == 3) { return $this->pay_state == PayState::SUCCESS ? true : false; } } if ($this->type == OrderType::UNION) { if ($this->type_mode == 2) { return true; } if ($this->type_mode == 1) { return date('Y-m-d H:i:s') > $this->odds_close_time; } if ($this->type_mode == 3) { $exist = Order::where('pid', $this->id) ->where('customer_id', $customer->id) ->where('type', OrderType::UNION) ->where('pay_state', PayState::SUCCESS) ->first(); return $exist ? true : false; } } return false; } public function getSubject() { return '螺丝钉'; } public function setCooperateInfo(Lottery $lott) { $info = $lott->getOrderCooperateInfo($this); if ($info['shop_cooperate_id'] > 0 && $this->type == OrderType::UNION) { ThrowException::run('合作订单不支持合买'); } $this->shop_cooperate_id = $info['shop_cooperate_id']; $this->cooperate_id = $info['cooperate_id']; $this->cooperate_show = $info['cooperate_show']; $this->cooperate_brokerage = $info['cooperate_brokerage']; $this->cooperate_method = $info['cooperate_method']; $this->cooperate_common = $info['cooperate_common']; return $this; } public function setButtonsShow(Seller $seller) { $this->button_jiedan = BoolEnum::NO; $this->button_shuaidan = BoolEnum::NO; $this->button_chupiao = BoolEnum::NO; $this->button_chedan = BoolEnum::NO; $this->button_paijiang = BoolEnum::NO; $this->button_qupiao = BoolEnum::NO; // 取票 if (in_array($this->lottery_state, [LottState::WIN, LottState::WAIT, LottState::LOSS, LottState::SEND])) { if ($this->cooperate_id > 0) { if ($this->shop_id == $seller->shop_id) { if ($this->cooperate_show == BoolEnum::NO && $this->cooperate_common == 1) { $this->button_qupiao = BoolEnum::YES; } } else { if ($this->cooperate_show == BoolEnum::YES) { $this->button_qupiao = BoolEnum::YES; } } } else { $this->button_qupiao = BoolEnum::YES; } } // 派奖,取票 if ($this->lottery_state == LottState::WAIT) { if ($this->cooperate_id > 0) { if ($this->shop_id == $seller->shop_id) { if ($this->cooperate_show == BoolEnum::NO && $this->cooperate_common == 1) { $this->button_paijiang = BoolEnum::YES; $this->button_qupiao = BoolEnum::YES; } } else { if ($this->cooperate_show == BoolEnum::YES) { $this->button_paijiang = BoolEnum::YES; $this->button_qupiao = BoolEnum::YES; } } } else { $this->button_paijiang = BoolEnum::YES; $this->button_qupiao = BoolEnum::YES; } } if ($this->lottery_take_ticket == BoolEnum::YES) { $this->button_qupiao = BoolEnum::NO; } // 出票 if ($this->lottery_state == LottState::DRAFT) { if ($this->cooperate_id > 0) { if ($this->shop_id == $seller->shop_id) { if ($this->cooperate_show == BoolEnum::NO && $this->cooperate_common == 1) { $this->button_chupiao = BoolEnum::YES; $this->button_chedan = BoolEnum::YES; } } else { if ($this->cooperate_show == BoolEnum::YES) { $this->button_chupiao = BoolEnum::YES; $this->button_chedan = BoolEnum::YES; } } } else { $this->button_chupiao = BoolEnum::YES; $this->button_chedan = BoolEnum::YES; } } // 接单 if ($this->lottery_state == LottState::PENDING) { if ($this->cooperate_id > 0) { if ($this->shop_id == $seller->shop_id) { if ($this->cooperate_show == BoolEnum::NO) { $this->button_shuaidan = BoolEnum::YES; } if ($this->cooperate_common == 1) { $this->button_jiedan = BoolEnum::YES; } } else { if ($this->cooperate_show == BoolEnum::YES) { $this->button_jiedan = BoolEnum::YES; } } } else { $this->button_jiedan = BoolEnum::YES; } } return $this; } public function zuheToOptimizeZuheList() { $optimizeZuheList = null; if (OptimizeType::hasValue($this->optimize_type)) { $orderZuheList = OrderZuhe::where('order_id', $this->id)->get(); /** @var OrderZuhe $item */ foreach ($orderZuheList as $item) { $optimizeZuheList[] = $item->toBetInfo($this->odds); } } return $optimizeZuheList; } public function updateUnionOrderState($lotteryState) { if ($this->type == OrderType::UNION) { Order::where('pid', $this->id) ->where('type', OrderType::UNION) ->where('id', '<>', $this->id) ->where('pay_state', PayState::SUCCESS) ->update([ 'lottery_state' => $lotteryState ]); } } public function getMatchStartTime() { $lottery = Lottery::find($this->lottery_id); $lotteryType = $this->lotteryType; $closeTime = strtotime($this->odds_late_close_time) + $lottery->earlySecond(); if ($lotteryType->type == LottType::JCZQ) { $endStartTime = JczqOdds::leftJoin('zq_match', 'zq_match.match_id', 'jczq_odds.match_id') ->select('zq_match.start_time') ->whereIn('jczq_odds.id', array_keys($this->odds)) ->orderBy('zq_match.start_time', 'desc') ->first(); if ($endStartTime) { return strtotime($endStartTime->start_time); } } else if ($lotteryType->type == LottType::JCLQ) { $endStartTime = JclqOdds::leftJoin('jclq_match', 'jclq_match.match_id', 'jclq_odds.match_id') ->select('jclq_match.start_time') ->whereIn('jclq_odds.id', array_keys($this->odds)) ->orderBy('jclq_match.start_time', 'desc') ->first(); if ($endStartTime) { return strtotime($endStartTime->start_time); } } return $closeTime; } public function sameToReceiver($currentSellerId) { if (!$this->receive_user_id) { ThrowException::run('尚未接单'); } return $this->receive_user_id == $currentSellerId; } }