123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- <?php
-
- namespace app\common\model\order;
-
- use app\common\basics\Models;
- use app\common\enum\OrderEnum;
- use app\common\enum\OrderGoodsEnum;
- use app\common\enum\PayEnum;
- use app\common\model\Client_;
- use app\common\model\Pay;
- use app\common\model\shop\Shop;
- use app\common\model\team\TeamJoin;
- use app\common\model\user\User;
- use app\common\server\ConfigServer;
- use think\facade\Db;
-
- /**
- * Class order
- * @package app\common\model\order
- */
- class Order extends Models
- {
- //订单状态
- const STATUS_WAIT_PAY = 0; //待付款
- const STATUS_WAIT_DELIVERY = 1; //待发货
- const STATUS_WAIT_RECEIVE = 2; //待收货
- const STATUS_FINISH = 3; //已完成
- const STATUS_CLOSE = 4; //已关闭
-
- /**
- * @notes 关联OrderGoods模型
- * @return \think\model\relation\HasMany
- * @author suny
- * @date 2021/7/13 6:47 下午
- */
- public function orderGoods()
- {
- return $this->hasMany('order_goods', 'order_id', 'id')
- ->field('id,order_id,goods_id,item_id,goods_name,goods_price,discount_price,spec_value,image,goods_num,is_comment,refund_status,commission_ratio,total_pay_price');
- }
-
- /**
- * @notes 订单用户
- * @return \think\model\relation\HasOne
- * @author suny
- * @date 2021/7/13 6:48 下午
- */
- public function user()
- {
- return $this->hasOne(User::class, 'id', 'user_id')
- ->field('id,sn,nickname,avatar,level,mobile,sex,create_time');
- }
-
- /**
- * @notes 关联Shop模型
- * @return \think\model\relation\HasOne
- * @author suny
- * @date 2021/7/13 6:49 下午
- */
- public function shop()
- {
- return $this->hasOne(Shop::class, 'id', 'shop_id')
- ->field('id,name,type,logo,mobile,open_invoice,spec_invoice,weekdays,run_start_time,run_end_time')
- ->append(['type_desc']);
- }
-
- /**
- * @notes 关联发票模型
- * @return \think\model\relation\HasOne
- * @author 段誉
- * @date 2022/4/11 18:39
- */
- public function invoice()
- {
- return $this->hasOne(OrderInvoice::class, 'order_id', 'id');
- }
-
- /**
- * @notes 订单状态
- * @param bool $status
- * @param int $shop_id
- * @return string|string[]
- * @author suny
- * @date 2021/7/13 6:46 下午
- */
- public static function getOrderStatus($status = true, $shop_id = 0)
- {
-
- $desc = [
- OrderEnum::ORDER_STATUS_NO_PAID => "待付款",
- OrderEnum::ORDER_STATUS_DELIVERY => "待发货",
- OrderEnum::ORDER_STATUS_GOODS => "待收货",
- OrderEnum::ORDER_STATUS_COMPLETE => "已完成",
- OrderEnum::ORDER_STATUS_DOWN => "已关闭",
- ];
- if ($status === true) {
- return $desc;
- }
- return $desc[$status] ?? '未知';
- }
-
-
- /**
- * @notes 订单类型
- * @param $type
- * @return string|string[]
- * @author suny
- * @date 2021/7/13 6:47 下午
- */
- public static function getOrderType($type)
- {
-
- $desc = [
- OrderEnum::NORMAL_ORDER => '普通订单',
- OrderEnum::SECKILL_ORDER => '秒杀订单',
- OrderEnum::TEAM_ORDER => '拼团订单',
- OrderEnum::BARGAIN_ORDER => '砍价订单',
- ];
-
- if ($type === true) {
- return $desc;
- }
- return $desc[$type] ?? '未知';
- }
-
- /**
- * @notes 配送方式
- * @param $type
- * @return string|string[]
- * @author suny
- * @date 2021/7/13 6:47 下午
- */
- public static function getDeliveryType($type)
- {
- $desc = [
- OrderEnum::DELIVERY_TYPE_EXPRESS => '快递发货',
- OrderEnum::DELIVERY_TYPE_VIRTUAL => '虚拟发货',
- OrderEnum::DELIVERY_TYPE_SELF => '线下自提',
- ];
-
- if ($type === true) {
- return $desc;
- }
- return $desc[$type] ?? '未知';
- }
-
- /**
- * @notes can_change_invoice_no 是否可修改物流单号
- * @param $value
- * @param $data
- * @return int
- * @author lbzy
- * @datetime 2024-04-02 15:31:40
- */
- function getCanChangeInvoiceNoAttr($value, $data)
- {
- return (int) in_array($data['order_status'], OrderEnum::CAN_CHANGE_INVOICE_NO_ARR);
- }
-
- /**
- * @notes 配送方式
- * @param $value
- * @param $data
- * @return string|string[]
- * @author suny
- * @date 2021/7/13 6:47 下午
- */
- public function getDeliveryTypeTextAttr($value, $data)
- {
- return self::getDeliveryType($data['delivery_type']);
- }
-
- /**
- * @notes 订单类型
- * @param $value
- * @param $data
- * @return string|string[]
- * @author suny
- * @date 2021/7/13 6:47 下午
- */
- public function getOrderTypeTextAttr($value, $data)
- {
-
- return self::getOrderType($data['order_type']);
- }
-
- /**
- * @notes 订单状态
- * @param $value
- * @param $data
- * @return string|string[]
- * @author suny
- * @date 2021/7/13 6:47 下午
- */
- public function getOrderStatusTextAttr($value, $data)
- {
-
- return self::getOrderStatus($data['order_status']);
- }
-
- /**
- * @notes 订单支付方式
- * @param $value
- * @param $data
- * @return array|mixed|string|string[]
- * @author suny
- * @date 2021/7/13 6:48 下午
- */
- public function getPayWayTextAttr($value, $data)
- {
-
- return PayEnum::getPayWay($data['pay_way']);
- }
-
- /**
- * @notes 订单支付状态
- * @param $value
- * @param $data
- * @return string|string[]
- * @author suny
- * @date 2021/7/13 6:48 下午
- */
- public function getPayStatusTextAttr($value, $data)
- {
-
- return PayEnum::getPayStatus($data['pay_status']);
- }
-
- /**
- * @notes 订单来源
- * @param $value
- * @param $data
- * @return string|string[]
- * @author suny
- * @date 2021/7/13 6:48 下午
- */
- public function getOrderSourceTextAttr($value, $data)
- {
-
- return Client_::getClient($data['order_source']);
- }
-
- /**
- * @notes 订单商品数量
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:48 下午
- */
- public function getGoodsCountAttr($value, $data)
- {
-
- return count($this->order_goods);
- }
-
-
- /**
- * @notes 收货地址
- * @param $value
- * @param $data
- * @return string
- * @author suny
- * @date 2021/7/13 6:48 下午
- */
- public function getDeliveryAddressAttr($value, $data)
- {
-
- $region = Db::name('dev_region')
- ->where('id', 'IN', [$data['province'], $data['city'], $data['district']])
- ->order('level asc')
- ->column('name');
-
- $region_desc = implode('', $region);
- return $region_desc . $data['address'];
- }
-
- public function getPcAddressAttr($value, $data)
- {
-
- $region = Db::name('dev_region')
- ->where('id', 'IN', [$data['province'], $data['city'], $data['district']])
- ->order('level asc')
- ->column('name');
-
- $region_desc = implode(' ', $region);
- return $region_desc . $data['address'];
- }
-
-
- /**
- * @notes 返回是否显示支付按钮
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:48 下午
- */
- public function getPayBtnAttr($value, $data)
- {
-
- $btn = 0;
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_NO_PAID && $data['pay_status'] == PayEnum::UNPAID) {
- $btn = 1;
- }
- return $btn;
- }
-
- /**
- * @notes 返回是否显示取消订单按钮
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:48 下午
- */
- public function getCancelBtnAttr($value, $data)
- {
-
- $btn = 0;
- if (is_string($data['create_time'])) {
- $data['create_time'] = strtotime($data['create_time']);
- }
- //多长时间内允许客户自动取消
- $cancel_limit = ConfigServer::get('transaction', 'paid_order_cancel_time', 60);
- $limit_time = $data['create_time'] + $cancel_limit * 60;
- if ($limit_time < time()) {
- return $btn;
- }
-
- if (($data['order_status'] == OrderEnum::ORDER_STATUS_NO_PAID && $data['pay_status'] == PayEnum::UNPAID)
- || ($data['pay_status'] == PayEnum::ISPAID && $data['order_status'] == OrderEnum::ORDER_STATUS_DELIVERY)) {
- $btn = 1;
- }
- return $btn;
- }
-
- /**
- * @notes 返回是否显示物流按钮
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:49 下午
- */
- public function getDeliveryBtnAttr($value, $data)
- {
-
- $btn = 0;
- // 虚拟发货类型不显示物流查询按钮
- if ($data['delivery_type'] != OrderEnum::DELIVERY_TYPE_EXPRESS) {
- return $btn;
- }
-
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_GOODS && $data['pay_status'] == PayEnum::ISPAID && $data['shipping_status'] == 1) {
- $btn = 1;
- }
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE && $data['pay_status'] == PayEnum::ISPAID && $data['shipping_status'] == 1) {
- $btn = 1;
- }
- return $btn;
-
- }
-
- /**
- * @notes 返回是否显示确认收货按钮
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:49 下午
- */
- public function getTakeBtnAttr($value, $data)
- {
- $btn = 0;
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_GOODS
- && $data['pay_status'] == PayEnum::ISPAID
- && $data['shipping_status'] == 1
- && $data['delivery_type'] != OrderEnum::DELIVERY_TYPE_SELF
- ) {
- $btn = 1;
- }
- return $btn;
- }
-
- /**
- * @notes 返回是否显示删除按钮
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:49 下午
- */
- public function getDelBtnAttr($value, $data)
- {
-
- $btn = 0;
- if (
- ($data['order_status'] == OrderEnum::ORDER_STATUS_DOWN && $data['pay_status'] == PayEnum::UNPAID) ||
- ($data['order_status'] == OrderEnum::ORDER_STATUS_DOWN && $data['pay_status'] == PayEnum::REFUNDED)
- ) {
- $btn = 1;
- }
- return $btn;
- }
-
- /**
- * @notes 返回是否显示已完成按钮
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:49 下午
- */
- public function getFinishBtnAttr($value, $data)
- {
-
- $btn = 0;
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE && $data['pay_status'] == PayEnum::ISPAID && $data['delivery_type'] != OrderEnum::DELIVERY_TYPE_SELF) {
- $btn = 1;
- }
- return $btn;
- }
-
- /**
- * @notes 返回是否显示去评论按钮
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:49 下午
- */
- public function getCommentBtnAttr($value, $data)
- {
-
- $btn = 0;
- $comment_count = 0;
- if ($data['pay_status'] == PayEnum::ISPAID && $data['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE) {
- $btn = 1;
- foreach ($this->order_goods as $item) {
- if ($item['is_comment'] == 1) {
- $comment_count += 1;
- };
- }
- if (count($this->orderGoods) == $comment_count) {
- $btn = 0;
- }
- }
-
- return $btn;
- }
-
- /**
- * @notes 返回是否显示申请退款按钮
- * @param $value
- * @param $data
- * @return int
- * @author suny
- * @date 2021/7/13 6:49 下午
- */
- public function getRefundBtnAttr($value, $data)
- {
-
- $btn = 0;
- $data['confirm_take_time'] = strtotime($data['confirm_take_time']);
- $refund_days = $data['confirm_take_time'] + ConfigServer::get('after_sale', 'refund_days', '', 0) * 86400;
- $now = time();
-
- //订单已完成、在售后期内。未申请退款、
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE && $refund_days > $now && $data['refund_status'] = OrderGoodsEnum::REFUND_STATUS_NO) {
- $btn = 1;
- }
- return $btn;
- }
-
-
- /**
- * @notes 发货内容(查看内容按钮)
- * @param $value
- * @param $data
- * @return int
- * @author 段誉
- * @date 2022/4/8 10:22
- */
- public function getContentBtnAttr($value, $data)
- {
- $btn = 0;
- if ($data['delivery_type'] == OrderEnum::DELIVERY_TYPE_VIRTUAL && $data['shipping_status'] == OrderEnum::SHIPPING_FINISH) {
- $btn = 1;
- }
- return $btn;
- }
-
-
- /**
- * @notes 申请开票按钮
- * @param $value
- * @param $data
- * @return int
- * @author 段誉
- * @date 2022/4/12 15:19
- */
- public function getSaveInvoiceBtnAttr($value, $data)
- {
- $btn = 0;
- $invoice = OrderInvoice::where(['order_id' => $data['id']])->findOrEmpty();
- if ($invoice->isEmpty()) {
- $btn = 1;
- }
- return $btn;
- }
-
-
- /**
- * @notes 查看开票按钮
- * @param $value
- * @param $data
- * @return int
- * @author 段誉
- * @date 2022/4/12 15:19
- */
- public function getViewInvoiceBtnAttr($value, $data)
- {
- $btn = 0;
- $invoice = OrderInvoice::where(['order_id' => $data['id']])->findOrEmpty();
- if (!$invoice->isEmpty()) {
- $btn = 1;
- }
- return $btn;
- }
-
- /**
- * @notes 取消订单时间
- * @param $value
- * @param $data
- * @return false|float|int|string
- * @author suny
- * @date 2021/7/13 6:49 下午
- */
- public function getOrderCancelTimeAttr($value, $data)
- {
-
- $end_time = '';
- if (is_string($data['create_time'])) {
- $data['create_time'] = strtotime($data['create_time']);
- }
- if ($data['order_status'] == 0 && $data['pay_status'] == 0) {
- $order_cancel_time = ConfigServer::get('transaction', 'unpaid_order_cancel_time', 60);
- $end_time = $data['create_time'] + $order_cancel_time * 60;
- }
- return $end_time;
- }
-
- /**
- * @notes 关联未评价子订单
- * @return \think\model\relation\HasMany
- * @author suny
- * @date 2021/7/13 6:50 下午
- */
- public function orderGoodsUnComment()
- {
-
- return $this->hasMany('order_goods', 'order_id', 'id')
- ->field('id,order_id,goods_id,item_id,goods_num,goods_name,goods_price,is_comment')
- ->where('is_comment', 0);
- }
-
- /**
- * @notes 返回是否显示修改地址按钮
- * @param $value
- * @param $data
- * @return int
- * @author ljj
- * @date 2021/11/10 11:37 上午
- */
- public function getEditAddressBtnAttr($value, $data)
- {
-
- $btn = 0;
-
- if ($data['delivery_type'] == OrderEnum::DELIVERY_TYPE_SELF || $data['delivery_type'] == OrderEnum::DELIVERY_TYPE_VIRTUAL) {
- return $btn;
- }
-
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_DELIVERY && $data['pay_status'] == PayEnum::ISPAID) {
- $btn = 1;
- }
- return $btn;
-
- }
-
- /**
- * @notes 返回是否显示去发货按钮
- * @param $value
- * @param $data
- * @return int
- * @author ljj
- * @date 2021/11/10 11:41 上午
- */
- public function getToShipBtnAttr($value, $data)
- {
- if ($data['delivery_type'] == OrderEnum::DELIVERY_TYPE_SELF) {
- return 0;
- }
-
- if ($data['order_status'] != OrderEnum::ORDER_STATUS_DELIVERY) {
- return 0;
- }
-
- if ($data['order_type'] == OrderEnum::TEAM_ORDER && $this->is_team_success) {
- return 1;
- }
-
- if ($data['order_type'] != OrderEnum::TEAM_ORDER) {
- return 1;
- }
-
- return 0;
-
- }
-
- /**
- * @notes 返回是否显示取消订单(商家端)按钮
- * @param $value
- * @param $data
- * @return int
- * @author ljj
- * @date 2021/11/10 11:41 上午
- */
- public function getShopCancelBtnAttr($value, $data)
- {
- $btn = 0;
-
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_NO_PAID && $data['pay_status'] == PayEnum::UNPAID){
- $btn = 1;
- }
-
- if ($data['pay_status'] == PayEnum::ISPAID && $data['order_status'] == OrderEnum::ORDER_STATUS_DELIVERY) {
- $btn = 1;
- }
-
- // 自提
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_GOODS && $data['delivery_type'] == 2) {
- $btn = 1;
- }
-
- return $btn;
- }
-
- /**
- * @notes is_team_success 是否拼团成功
- * @param $value
- * @param $data
- * @return int
- * @author lbzy
- * @datetime 2024-06-13 10:03:39
- */
- function getIsTeamSuccessAttr($value, $data)
- {
- $success = 0;
- if ($data['order_type'] == OrderEnum::TEAM_ORDER) {
- $team = TeamJoin::field('TJ.status,TF.status as found_status')->alias('TJ')
- ->where(['TJ.order_id'=>$data['id']])
- ->field([ 'TJ.status' ])
- ->join('team_found TF', 'TF.id = TJ.team_id')
- ->findOrEmpty()->toArray();
- if ($team) {
- $success = $team['status'] == 1 ? 1 : 0;
- }
- }
-
- return $success;
- }
-
-
- /**
- * @notes 返回是否显示跳转核销(商家端)按钮
- * @param $value
- * @param $data
- * @return int
- * @author mjf
- * @date 2022/11/03 18:57
- */
- public function getToVerificationBtnAttr($value, $data)
- {
-
- if ($data['order_status'] == OrderEnum::ORDER_STATUS_GOODS && $data['delivery_type'] == OrderEnum::DELIVERY_TYPE_SELF) {
- return 1;
- }
-
- return 0;
- }
-
-
- /**
- * @notes 核销状态描述
- * @param $value
- * @param $data
- * @return string|string[]
- * @author 段誉
- * @date 2022/11/2 14:16
- */
- public function getVerificationStatusTextAttr($value, $data)
- {
- return OrderEnum::getVerificationStatusDesc($data['verification_status']);
- }
-
- /**
- * @notes 汇付斗拱参数
- * @param $fieldValue
- * @param $data
- * @return array
- * @author lbzy
- * @datetime 2023-10-23 17:28:25
- */
- function getHfdgParamsAttr($fieldValue, $data)
- {
- return $fieldValue ? ((array) json_decode($fieldValue, true)) : [];
- }
-
- function setHfdgParamsAttr($fieldValue, $data)
- {
- if (is_string($fieldValue)) {
- return $fieldValue;
- }
- return json_encode((array) $fieldValue, JSON_UNESCAPED_UNICODE);
- }
- }
|