LogModel.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * LogModel.php UTF-8
  4. * 分表Log基类
  5. *
  6. * @date : 2018/6/1 18:23
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\log;
  13. use huo\model\common\CommonModel;
  14. class LogModel extends CommonModel {
  15. protected $table_date = '';
  16. protected $table_prefix = '';
  17. protected $partition_data = [];
  18. protected $partition_field = 'date';
  19. protected $partition_rule = ['type' => 'month', 'expr' => 1];
  20. /**
  21. * @param $data
  22. * @param bool $replace
  23. * @param bool $get_last_insert_id
  24. *
  25. * @return bool|int|string
  26. */
  27. public function insertLog($data, $replace = false, $get_last_insert_id = true) {
  28. if ($_id = $this->computeTable($data['date'])->insert($data, $replace, $get_last_insert_id)) {
  29. return $_id;
  30. } else {
  31. return false;
  32. }
  33. }
  34. /**
  35. * 计算在哪张表
  36. *
  37. * @param string $date
  38. *
  39. * @return \Think\Model
  40. */
  41. public function computeTable($date = '') {
  42. $this->partition_data[$this->partition_field] = $date;
  43. $this->checkTable();
  44. return $this->partition($this->partition_data, $this->partition_field, $this->partition_rule);
  45. }
  46. /**
  47. * 计算在哪张表
  48. *
  49. * @param string $date
  50. *
  51. * @return \Think\Model
  52. */
  53. public function computeTableTwo($date = '') {
  54. $this->partition_data[$this->partition_field] = $date;
  55. $_time = strtotime($date);
  56. $_time = !empty($_time) ? $_time : time();
  57. $this->partition_data[$this->partition_field] = date('Y-m-d', $_time);
  58. $this->table = $this->getPartitionTableName(
  59. $this->partition_data, $this->partition_field, $this->partition_rule
  60. );
  61. return $this->partition($this->partition_data, $this->partition_field, $this->partition_rule);
  62. }
  63. /**
  64. * 检查表是否处在
  65. *
  66. *
  67. * @return bool
  68. */
  69. function checkTable() {
  70. return true;
  71. }
  72. }