123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace app\common\model;
- use think\Model;
- use app\common\Enum\OrderActionEnum;
- /**
- * 订单操作记录模型
- */
- class OrderAction extends Model
- {
- // 表名
- protected $name = 'shop_order_action';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = '';
- // 追加属性
- protected $append = [
- 'action_type_text',
- 'user_type_text',
- 'operator_type_text',
- 'priority_text',
- 'createtime_text'
- ];
- /**
- * 获取操作类型文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getActionTypeTextAttr($value, $data)
- {
- return OrderActionEnum::getActionTypeText($data['action_type'] ?? '');
- }
- /**
- * 获取用户类型文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getUserTypeTextAttr($value, $data)
- {
- return OrderActionEnum::getUserTypeText($data['user_type'] ?? '');
- }
- /**
- * 获取操作员类型文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getOperatorTypeTextAttr($value, $data)
- {
- return OrderActionEnum::getOperatorTypeText($data['operator_type'] ?? '');
- }
- /**
- * 获取优先级文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getPriorityTextAttr($value, $data)
- {
- return OrderActionEnum::getPriorityText($data['priority'] ?? 0);
- }
- /**
- * 获取创建时间文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getCreatetimeTextAttr($value, $data)
- {
- $time = $data['createtime'] ?? time();
- return date('Y-m-d H:i:s', $time);
- }
- /**
- * 添加订单操作记录(兼容老接口)
- * @param string $order_sn 订单编号
- * @param string $operator 操作人
- * @param string $memo 备注
- * @return bool
- * @deprecated 建议使用 OrderActionService::push() 方法
- */
- public static function push($order_sn, $operator, $memo)
- {
- return self::create([
- 'order_sn' => $order_sn,
- 'operator' => $operator,
- 'memo' => $memo,
- 'action_type' => OrderActionEnum::ACTION_MODIFY,
- 'user_type' => OrderActionEnum::USER_TYPE_ADMIN,
- 'operator_type' => OrderActionEnum::OPERATOR_TYPE_ADMIN,
- 'priority' => OrderActionEnum::getDefaultPriority(OrderActionEnum::ACTION_MODIFY),
- 'user_id' => 0,
- 'ip' => request()->ip(),
- 'user_agent' => request()->server('HTTP_USER_AGENT', ''),
- 'extra_data' => '',
- ]) ? true : false;
- }
- /**
- * 按订单编号查询操作记录
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型(可选)
- * @param string $user_type 用户类型(可选)
- * @return \think\Collection
- */
- public static function getByOrderSn($order_sn, $action_type = '', $user_type = '')
- {
- $where = ['order_sn' => $order_sn];
-
- if ($action_type) {
- $where['action_type'] = $action_type;
- }
-
- if ($user_type) {
- $where['user_type'] = $user_type;
- }
- return self::where($where)
- ->order('createtime', 'desc')
- ->select();
- }
- /**
- * 获取最后一次操作记录
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型(可选)
- * @return OrderAction|null
- */
- public static function getLatestByOrderSn($order_sn, $action_type = '')
- {
- $where = ['order_sn' => $order_sn];
-
- if ($action_type) {
- $where['action_type'] = $action_type;
- }
- return self::where($where)
- ->order('createtime', 'desc')
- ->find();
- }
- /**
- * 检查操作是否存在
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型
- * @return bool
- */
- public static function hasActionType($order_sn, $action_type)
- {
- return self::where([
- 'order_sn' => $order_sn,
- 'action_type' => $action_type
- ])->count() > 0;
- }
- /**
- * 批量插入操作记录
- * @param array $data 批量数据
- * @return bool
- */
- public static function insertBatch($data)
- {
- return self::insertAll($data) ? true : false;
- }
- }
|