<?php
namespace app\common\model\sharing;
use think\Hook;
use app\common\model\BaseModel;
use app\common\model\store\shop\Order as ShopOrder;
use app\common\service\Order as OrderService;
use app\common\service\order\Complete as OrderCompleteService;
use app\common\enum\OrderType as OrderTypeEnum;
use app\common\enum\DeliveryType as DeliveryTypeEnum;
use app\common\enum\order\PayType as PayTypeEnum;
use app\common\enum\order\PayStatus as PayStatusEnum;
use app\common\library\helper;
class Order extends BaseModel
{
protected $name = 'sharing_order';
protected $append = [
'state_text', ];
public static function init()
{
parent::init();
$static = new static;
Hook::listen('sharing_order', $static);
}
public function active()
{
return $this->belongsTo('Active');
}
public function goods()
{
return $this->hasMany('OrderGoods', 'order_id');
}
public function address()
{
return $this->hasOne('OrderAddress', 'order_id');
}
public function extract()
{
return $this->hasOne('OrderExtract', 'order_id');
}
public function extractShop()
{
$module = self::getCalledModule() ?: 'common';
return $this->belongsTo("app\\{$module}\\model\\store\\Shop", 'extract_shop_id');
}
public function extractClerk()
{
$module = self::getCalledModule() ?: 'common';
return $this->belongsTo("app\\{$module}\\model\\store\\shop\\Clerk", 'extract_clerk_id');
}
public function user()
{
$module = self::getCalledModule() ?: 'common';
return $this->belongsTo("app\\{$module}\\model\\User");
}
public function express()
{
$module = self::getCalledModule() ?: 'common';
return $this->belongsTo("app\\{$module}\\model\\Express");
}
public function getStateTextAttr($value, $data)
{
if (!isset($data['active_status'])) {
$data['active_status'] = '';
}
if ($data['order_status'] == 30) {
return '已完成';
}
if ($data['order_status'] == 20) {
if ($data['order_type'] == 20 && $data['active_status'] == 30) {
return $data['is_refund'] ? '拼团未成功,已退款' : '拼团未成功,待退款';
}
return '已取消';
}
if ($data['pay_status'] == 10) {
return '待付款';
}
if ($data['order_type'] == 10) {
if ($data['delivery_status'] == 10) {
return '已付款,待发货';
}
if ($data['receipt_status'] == 10) {
return '已发货,待收货';
}
}
if ($data['order_type'] == 20) {
if ($data['active_status'] == 30) {
return $data['is_refund'] ? '拼团未成功,已退款' : '拼团未成功,待退款';
}
if ($data['active_status'] == 10) {
return '已付款,待成团';
}
if ($data['active_status'] == 20) {
if ($data['delivery_status'] == 10) {
return '拼团成功,待发货';
}
if ($data['receipt_status'] == 10) {
return '已发货,待收货';
}
}
}
return $value;
}
public function getActiveStatusAttr($value)
{
if (is_null($value)) {
return false;
}
$state = [
0 => '未拼单',
10 => '拼单中',
20 => '拼单成功',
30 => '拼单失败',
];
return ['text' => $state[$value], 'value' => $value];
}
public function getOrderPriceAttr($value, $data)
{
if ($value == 0) {
return helper::bcadd(helper::bcsub($data['total_price'], $data['coupon_money']), $data['update_price']);
}
return $value;
}
public function getUpdatePriceAttr($value)
{
return [
'symbol' => $value < 0 ? '-' : '+',
'value' => sprintf('%.2f', abs($value))
];
}
public function getOrderTypeAttr($value)
{
$status = [10 => '单独购买', 20 => '拼团'];
return ['text' => $status[$value], 'value' => $value];
}
public function getPayTypeAttr($value)
{
return ['text' => PayTypeEnum::data()[$value]['name'], 'value' => $value];
}
public function getPayStatusAttr($value)
{
return ['text' => PayStatusEnum::data()[$value]['name'], 'value' => $value];
}
public function getDeliveryStatusAttr($value)
{
$status = [10 => '待发货', 20 => '已发货'];
return ['text' => $status[$value], 'value' => $value];
}
public function getReceiptStatusAttr($value)
{
$status = [10 => '待收货', 20 => '已收货'];
return ['text' => $status[$value], 'value' => $value];
}
public function getOrderStatusAttr($value)
{
$status = [10 => '进行中', 20 => '已取消', 21 => '待取消', 30 => '已完成', 40 => '拼团失败'];
return ['text' => $status[$value], 'value' => $value];
}
public function getDeliveryTypeAttr($value)
{
return ['text' => DeliveryTypeEnum::data()[$value]['name'], 'value' => $value];
}
public function orderNo()
{
return OrderService::createOrderNo();
}
public static function detail($where, $with = [
'active',
'goods' => ['image'],
'address',
'extract',
'express',
'extract_shop.logo',
'extract_clerk'
])
{
is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
return self::get($filter, $with);
}
public function getListByIds($orderIds, $with = [])
{
$data = $this->getListByInArray('order_id', $orderIds, $with);
return helper::arrayColumn2Key($data, 'order_id');
}
private function getListByInArray($field, $data, $with = [])
{
return $this->with($with)
->alias('order')
->field('order.*, active.status as active_status')
->join('sharing_active active', 'order.active_id = active.active_id', 'LEFT')
->where($field, 'in', $data)
->where('order.is_delete', '=', 0)
->select();
}
public function getListByOrderNos($orderNos, $with = [])
{
return $this->getListByInArray('order_no', $orderNos, $with);
}
public function verificationOrder($clerkId)
{
if (
$this['pay_status']['value'] != 20
|| $this['delivery_type']['value'] != DeliveryTypeEnum::EXTRACT
|| $this['delivery_status']['value'] == 20
|| in_array($this['order_status']['value'], [20, 21])
|| ($this['order_type']['value'] == 20 ? $this['active']['status']['value'] != 20 : false)
) {
$this->error = '该订单不满足核销条件';
return false;
}
$this->transaction(function () use ($clerkId) {
$this->save([
'extract_clerk_id' => $clerkId, 'delivery_status' => 20,
'delivery_time' => time(),
'receipt_status' => 20,
'receipt_time' => time(),
'order_status' => 30
]);
ShopOrder::add(
$this['order_id'],
$this['extract_shop_id'],
$this['extract_clerk_id'],
OrderTypeEnum::SHARING
);
(new OrderCompleteService)->complete([$this], OrderTypeEnum::SHARING, static::$wxapp_id);
});
return true;
}
}