ExportLib.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace modules\dataexport\library;
  3. use app\admin\model\routine\Dataexport;
  4. use think\facade\Db;
  5. class ExportLib
  6. {
  7. protected $row = null;
  8. protected $fields = [];
  9. protected $join = [];
  10. protected $where = [];
  11. protected $order = [];
  12. public function __construct(int $id)
  13. {
  14. $this->row = Dataexport::where('id', $id)->find();
  15. foreach ($this->row->field_config as $item) {
  16. $asName = $this->row->main_table . '.' . $item['name'] . ' as ' . $this->row->main_table . '_' . $item['name'];
  17. $item['field'] = $this->row->main_table . '_' . $item['name'];
  18. $this->fields[$asName] = $item;
  19. }
  20. if ($this->row->join_table) {
  21. foreach ($this->row->join_table as $joinTable) {
  22. foreach ($joinTable['fields'] as $item) {
  23. $joinAsName = $joinTable['asname'] ? $joinTable['asname'] : $joinTable['table'];
  24. $asName = $joinAsName . '.' . $item['name'] . ' as ' . $joinAsName . '_' . $item['name'];
  25. $item['field'] = $joinAsName . '_' . $item['name'];
  26. $this->fields[$asName] = $item;
  27. }
  28. $condition = vsprintf('%s.%s = %s.%s', [
  29. $this->row->main_table,
  30. $joinTable['fk'],
  31. $joinAsName,
  32. $joinTable['pk']
  33. ]);
  34. $this->join[] = [$joinTable['table'] . ' ' . $joinAsName, $condition, $joinTable['type'] ?? 'LEFT'];
  35. }
  36. }
  37. // 筛选
  38. if ($this->row->where_field) {
  39. foreach ($this->row->where_field as $item) {
  40. $this->where[] = [$item['field'], $item['operator'], $item['value']];
  41. }
  42. }
  43. // 排序
  44. if ($this->row->order_field) {
  45. foreach ($this->row->order_field as $item) {
  46. $this->order[$item['field']] = $item['value'];
  47. }
  48. }
  49. }
  50. public function getSql(string $type = '', $data = [])
  51. {
  52. $res = Db::name($this->row->main_table)
  53. ->alias($this->row->main_table)
  54. ->field(implode(',', array_keys($this->fields)))
  55. ->where($this->where)
  56. ->order($this->order);
  57. foreach ($this->join as $item) {
  58. $res->join($item[0], $item[1], $item[2]);
  59. }
  60. if ($type == 'limit') {
  61. return $res->fetchSql()->limit($data[0], $data[1])->select();
  62. } elseif ($type == 'test') {
  63. return $res->fetchSql()->limit(10)->select();
  64. }
  65. return $res;
  66. }
  67. public function getXlsTitle(): array
  68. {
  69. $title = [];
  70. foreach ($this->fields as $key => $value) {
  71. $title[] = $value['title'] ?: $key;
  72. }
  73. return $title;
  74. }
  75. public function getFields()
  76. {
  77. return $this->fields;
  78. }
  79. public function assignment($value, $comment)
  80. {
  81. if ($comment) {
  82. $comment = explode(',', $comment);
  83. foreach ($comment as $v) {
  84. list($itemKey, $itemValue) = explode('=', $v);
  85. if ($itemKey == $value) {
  86. return $itemValue;
  87. }
  88. }
  89. }
  90. return $value;
  91. }
  92. public function getCount()
  93. {
  94. $sql = $this->getSql();
  95. return $sql->count();
  96. }
  97. }