| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- namespace app\common\model\order;
- use app\common\enum\order\OrderSourceEnum;
- use app\common\model\BaseModel;
- use app\common\enum\settings\DeliveryTypeEnum;
- use app\common\enum\order\OrderPayStatusEnum;
- use app\common\enum\order\OrderTypeEnum;
- use app\common\enum\order\OrderPayTypeEnum;
- use app\common\library\helper;
- use app\common\service\order\OrderService;
- use app\common\service\order\OrderCompleteService;
- use app\common\model\store\Order as StoreOrderModel;
- /**
- * 订单模型模型
- */
- class Order extends BaseModel
- {
- protected $pk = 'order_id';
- protected $name = 'order';
- /**
- * 追加字段
- * @var string[]
- */
- protected $append = [
- 'state_text',
- 'order_source_text',
- ];
- /**
- * 订单商品列表
- * @return \think\model\relation\HasMany
- */
- public function product()
- {
- return $this->hasMany('app\\common\\model\\order\\OrderProduct', 'order_id', 'order_id')->hidden(['content']);
- }
- /**
- * 关联订单收货地址表
- * @return \think\model\relation\HasOne
- */
- public function address()
- {
- return $this->hasOne('app\\common\\model\\order\\OrderAddress');
- }
- /**
- * 关联自提订单联系方式
- * @return \think\model\relation\HasOne
- */
- public function extract()
- {
- return $this->hasOne('app\\common\\model\\order\\OrderExtract');
- }
- /**
- * 关联物流公司表
- * @return \think\model\relation\BelongsTo
- */
- public function express()
- {
- return $this->belongsTo('app\\common\\model\\settings\\Express', 'express_id', 'express_id');
- }
- /**
- * 关联自提门店表
- * @return \think\model\relation\BelongsTo
- */
- public function extractStore()
- {
- return $this->belongsTo('app\\common\\model\\store\\Store', 'extract_store_id', 'store_id');
- }
- /**
- * 关联门店店员表
- * @return \think\model\relation\BelongsTo
- */
- public function extractClerk()
- {
- return $this->belongsTo('app\\common\\model\\store\\Clerk', 'extract_clerk_id');
- }
- /**
- * 关联用户表
- * @return \think\model\relation\BelongsTo
- */
- public function user()
- {
- return $this->belongsTo('app\\common\\model\\user\\User', 'user_id', 'user_id');
- }
- /**
- * 订单状态文字描述
- * @param $value
- * @param $data
- * @return string
- */
- 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['order_source'] == OrderSourceEnum::ASSEMBLE){
- $assemble_status = $this->getAssembleStatus($data);
- if($assemble_status != ''){
- return $assemble_status;
- }
- }
- // 发货状态
- if ($data['delivery_status'] == 10) {
- return '已付款,待发货';
- }
- if ($data['receipt_status'] == 10) {
- return '已发货,待收货';
- }
- return $value;
- }
- /**
- * 拼团订单状态
- */
- private function getAssembleStatus($data){
- // 发货状态
- if ($data['assemble_status'] == 10) {
- return '已付款,未成团';
- }
- if ($data['assemble_status'] == 20 && $data['delivery_status'] == 10) {
- return '拼团成功,待发货';
- }
- if ($data['assemble_status'] == 30) {
- return '拼团失败';
- }
- return '';
- }
- /**
- * 付款状态
- * @param $value
- * @return array
- */
- public function getPayTypeAttr($value)
- {
- return ['text' => OrderPayTypeEnum::data()[$value]['name'], 'value' => $value];
- }
- /**
- * 订单来源
- * @param $value
- * @return array
- */
- public function getOrderSourceTextAttr($value, $data)
- {
- return OrderSourceEnum::data()[$data['order_source']]['name'];
- }
- /**
- * 付款状态
- * @param $value
- * @return array
- */
- public function getPayStatusAttr($value)
- {
- return ['text' => OrderPayStatusEnum::data()[$value]['name'], 'value' => $value];
- }
- /**
- * 改价金额(差价)
- * @param $value
- * @return array
- */
- public function getUpdatePriceAttr($value)
- {
- return [
- 'symbol' => $value < 0 ? '-' : '+',
- 'value' => sprintf('%.2f', abs($value))
- ];
- }
- /**
- * 发货状态
- * @param $value
- * @return array
- */
- public function getDeliveryStatusAttr($value)
- {
- $status = [10 => '待发货', 20 => '已发货'];
- return ['text' => $status[$value], 'value' => $value];
- }
- /**
- * 收货状态
- * @param $value
- * @return array
- */
- public function getReceiptStatusAttr($value)
- {
- $status = [10 => '待收货', 20 => '已收货'];
- return ['text' => $status[$value], 'value' => $value];
- }
- /**
- * 收货状态
- * @param $value
- * @return array
- */
- public function getOrderStatusAttr($value)
- {
- $status = [10 => '进行中', 20 => '已取消', 21 => '待取消', 30 => '已完成'];
- return ['text' => $status[$value], 'value' => $value];
- }
- /**
- * 配送方式
- * @param $value
- * @return array
- */
- public function getDeliveryTypeAttr($value)
- {
- return ['text' => DeliveryTypeEnum::data()[$value]['name'], 'value' => $value];
- }
- /**
- * 订单详情
- */
- public static function detail($where, $with = ['user', 'address', 'product' => ['image', 'refund'], 'extract', 'express', 'extractStore.logo', 'extractClerk'])
- {
- is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
- return (new static())->with($with)->where($filter)->find();
- }
- /**
- * 订单详情
- */
- public static function detailByNo($order_no, $with = ['user', 'address', 'product' => ['image', 'refund'], 'extract', 'express', 'extractStore.logo', 'extractClerk'])
- {
- return (new static())->with($with)->where('order_no', '=', $order_no)->find();
- }
- /**
- * 批量获取订单列表
- * @param $orderIds
- * @param array $with
- * @return array
- */
- public function getListByIds($orderIds, $with = [])
- {
- $data = $this->getListByInArray('order_id', $orderIds, $with);
- return helper::arrayColumn2Key($data, 'order_id');
- }
- /**
- * 批量更新订单
- * @param $orderIds
- * @param $data
- * @return bool
- */
- public function onBatchUpdate($orderIds, $data)
- {
- return $this->where('order_id', 'in', $orderIds)->save($data);
- }
- /**
- * 批量获取订单列表
- * @param $field
- * @param $data
- * @param array $with
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function getListByInArray($field, $data, $with = [])
- {
- return $this->with($with)
- ->where($field, 'in', $data)
- ->where('is_delete', '=', 0)
- ->select();
- }
- /**
- * 生成订单号
- * @return string
- */
- public function orderNo()
- {
- return OrderService::createOrderNo();
- }
- /**
- * 确认核销(自提订单)
- * @param $extractClerkId
- * @return bool|mixed
- */
- 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
- ]);
- // 新增订单核销记录
- StoreOrderModel::add(
- $this['order_id'],
- $this['extract_store_id'],
- $this['extract_clerk_id'],
- OrderTypeEnum::MASTER
- );
- // 执行订单完成后的操作
- $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
- $OrderCompleteService->complete([$this], static::$app_id);
- return $status;
- });
- }
- }
|