<?php
namespace app\common\model;
use think\Hook;
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 = 'order';
protected $alias = 'order';
protected $append = [
'state_text', ];
public static function init()
{
parent::init();
$static = new static;
Hook::listen('order', $static);
}
public function goods()
{
$module = self::getCalledModule() ?: 'common';
return $this->hasMany("app\\{$module}\\model\\OrderGoods");
}
public function address()
{
$module = self::getCalledModule() ?: 'common';
return $this->hasOne("app\\{$module}\\model\\OrderAddress");
}
public function extract()
{
$module = self::getCalledModule() ?: 'common';
return $this->hasOne("app\\{$module}\\model\\OrderExtract");
}
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 (in_array($data['order_status'], [20, 30])) {
$orderStatus = [20 => '已取消', 30 => '已完成'];
return $orderStatus[$data['order_status']];
}
if ($data['pay_status'] == 10) {
return '待付款';
}
if ($data['delivery_status'] == 10) {
return '已付款,待发货';
}
if ($data['receipt_status'] == 10) {
return '已发货,待收货';
}
return $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 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 => '已完成'];
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 = [
'user',
'address',
'goods' => ['image'],
'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)
->where($field, 'in', $data)
->where('is_delete', '=', 0)
->select();
}
public function getListByOrderNos($orderNos, $with = [])
{
return $this->getListByInArray('order_no', $orderNos, $with);
}
public function onBatchUpdate($orderIds, $data)
{
return $this->isUpdate(true)->save($data, ['order_id' => ['in', $orderIds]]);
}
public function verificationOrder($extractClerkId)
{
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->error = '该订单不满足核销条件';
return false;
}
return $this->transaction(function () use ($extractClerkId) {
$status = $this->save([
'extract_clerk_id' => $extractClerkId, '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::MASTER
);
(new OrderCompleteService)->complete([$this], OrderTypeEnum::MASTER, static::$wxapp_id);
return $status;
});
}
}