OrderAction.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use app\common\Enum\OrderActionEnum;
  5. /**
  6. * 订单操作记录模型
  7. */
  8. class OrderAction extends Model
  9. {
  10. // 表名
  11. protected $name = 'shop_order_action';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = '';
  17. // 追加属性
  18. protected $append = [
  19. 'action_type_text',
  20. 'user_type_text',
  21. 'operator_type_text',
  22. 'priority_text',
  23. 'createtime_text'
  24. ];
  25. /**
  26. * 获取操作类型文本
  27. * @param $value
  28. * @param $data
  29. * @return string
  30. */
  31. public function getActionTypeTextAttr($value, $data)
  32. {
  33. return OrderActionEnum::getActionTypeText($data['action_type'] ?? '');
  34. }
  35. /**
  36. * 获取用户类型文本
  37. * @param $value
  38. * @param $data
  39. * @return string
  40. */
  41. public function getUserTypeTextAttr($value, $data)
  42. {
  43. return OrderActionEnum::getUserTypeText($data['user_type'] ?? '');
  44. }
  45. /**
  46. * 获取操作员类型文本
  47. * @param $value
  48. * @param $data
  49. * @return string
  50. */
  51. public function getOperatorTypeTextAttr($value, $data)
  52. {
  53. return OrderActionEnum::getOperatorTypeText($data['operator_type'] ?? '');
  54. }
  55. /**
  56. * 获取优先级文本
  57. * @param $value
  58. * @param $data
  59. * @return string
  60. */
  61. public function getPriorityTextAttr($value, $data)
  62. {
  63. return OrderActionEnum::getPriorityText($data['priority'] ?? 0);
  64. }
  65. /**
  66. * 获取创建时间文本
  67. * @param $value
  68. * @param $data
  69. * @return string
  70. */
  71. public function getCreatetimeTextAttr($value, $data)
  72. {
  73. $time = $data['createtime'] ?? time();
  74. return date('Y-m-d H:i:s', $time);
  75. }
  76. /**
  77. * 添加订单操作记录(兼容老接口)
  78. * @param string $order_sn 订单编号
  79. * @param string $operator 操作人
  80. * @param string $memo 备注
  81. * @return bool
  82. * @deprecated 建议使用 OrderActionService::push() 方法
  83. */
  84. public static function push($order_sn, $operator, $memo)
  85. {
  86. return self::create([
  87. 'order_sn' => $order_sn,
  88. 'operator' => $operator,
  89. 'memo' => $memo,
  90. 'action_type' => OrderActionEnum::ACTION_MODIFY,
  91. 'user_type' => OrderActionEnum::USER_TYPE_ADMIN,
  92. 'operator_type' => OrderActionEnum::OPERATOR_TYPE_ADMIN,
  93. 'priority' => OrderActionEnum::getDefaultPriority(OrderActionEnum::ACTION_MODIFY),
  94. 'user_id' => 0,
  95. 'ip' => request()->ip(),
  96. 'user_agent' => request()->server('HTTP_USER_AGENT', ''),
  97. 'extra_data' => '',
  98. ]) ? true : false;
  99. }
  100. /**
  101. * 按订单编号查询操作记录
  102. * @param string $order_sn 订单编号
  103. * @param string $action_type 操作类型(可选)
  104. * @param string $user_type 用户类型(可选)
  105. * @return \think\Collection
  106. */
  107. public static function getByOrderSn($order_sn, $action_type = '', $user_type = '')
  108. {
  109. $where = ['order_sn' => $order_sn];
  110. if ($action_type) {
  111. $where['action_type'] = $action_type;
  112. }
  113. if ($user_type) {
  114. $where['user_type'] = $user_type;
  115. }
  116. return self::where($where)
  117. ->order('createtime', 'desc')
  118. ->select();
  119. }
  120. /**
  121. * 获取最后一次操作记录
  122. * @param string $order_sn 订单编号
  123. * @param string $action_type 操作类型(可选)
  124. * @return OrderAction|null
  125. */
  126. public static function getLatestByOrderSn($order_sn, $action_type = '')
  127. {
  128. $where = ['order_sn' => $order_sn];
  129. if ($action_type) {
  130. $where['action_type'] = $action_type;
  131. }
  132. return self::where($where)
  133. ->order('createtime', 'desc')
  134. ->find();
  135. }
  136. /**
  137. * 检查操作是否存在
  138. * @param string $order_sn 订单编号
  139. * @param string $action_type 操作类型
  140. * @return bool
  141. */
  142. public static function hasActionType($order_sn, $action_type)
  143. {
  144. return self::where([
  145. 'order_sn' => $order_sn,
  146. 'action_type' => $action_type
  147. ])->count() > 0;
  148. }
  149. /**
  150. * 批量插入操作记录
  151. * @param array $data 批量数据
  152. * @return bool
  153. */
  154. public static function insertBatch($data)
  155. {
  156. return self::insertAll($data) ? true : false;
  157. }
  158. }