SystemGroupData.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\admin\model\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * 数据列表 model
  7. * Class SystemGroupData
  8. * @package app\admin\model\system
  9. */
  10. class SystemGroupData extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'system_group_data';
  22. use ModelTrait;
  23. /**
  24. * 根据where条件获取当前表中的前20条数据
  25. * @param $params
  26. * @return array
  27. */
  28. public static function getList($params)
  29. {
  30. $model = new self;
  31. if ($params['gid'] !== '') $model = $model->where('gid', $params['gid']);
  32. if ($params['status'] !== '') $model = $model->where('status', $params['status']);
  33. $model = $model->order('sort desc,id ASC');
  34. return self::page($model, function ($item, $key) {
  35. $info = json_decode($item->value, true);
  36. foreach ($info as $index => $value) {
  37. if ($value["type"] == "checkbox") $info[$index]["value"] = implode(",", $value["value"]);
  38. }
  39. $item->value = $info;
  40. }, $params);
  41. }
  42. /**
  43. * 获得组合数据信息+组合数据列表
  44. * @param $config_name
  45. * @param int $limit
  46. * @return array|bool|null|\think\Model
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public static function getGroupData($config_name, $limit = 0)
  52. {
  53. $group = SystemGroup::where('config_name', $config_name)->field('name,info,config_name')->find();
  54. if (!$group) return false;
  55. $group['data'] = self::getAllValue($config_name, $limit);
  56. return $group;
  57. }
  58. /**
  59. * 获取单个值
  60. * @param $config_name
  61. * @param int $limit
  62. * @return array
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public static function getAllValue($config_name, $limit = 0)
  68. {
  69. $model = self::alias('a')->field('a.*,b.config_name')->join('system_group b', 'a.gid = b.id')
  70. ->where("b.config_name", $config_name)->where("a.status", 1)
  71. ->order('sort desc,id ASC');
  72. if ($limit > 0) $model->limit($limit);
  73. $data = [];
  74. $result = $model->select();
  75. if (!$result) return $data;
  76. foreach ($result as $key => $value) {
  77. $data[$key]["id"] = $value["id"];
  78. $fields = json_decode($value["value"], true);
  79. foreach ($fields as $index => $field) {
  80. // $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0]: ''):$field["value"];
  81. $data[$key][$index] = $field["value"];
  82. }
  83. }
  84. return $data;
  85. }
  86. /**
  87. * @param $result
  88. * @return array
  89. */
  90. public static function tidyList($result)
  91. {
  92. $data = [];
  93. if (!$result) return $data;
  94. foreach ($result as $key => $value) {
  95. $data[$key]["id"] = $value["id"];
  96. $fields = json_decode($value["value"], true);
  97. foreach ($fields as $index => $field) {
  98. $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0] : '') : $field["value"];
  99. }
  100. }
  101. return $data;
  102. }
  103. /**
  104. * 根据id获取当前记录中的数据
  105. * @param $id
  106. * @return mixed
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. * @throws \think\exception\DbException
  110. */
  111. public static function getDateValue($id)
  112. {
  113. $value = self::alias('a')->where(array("id" => $id))->find();
  114. $data["id"] = $value["id"];
  115. $fields = json_decode($value["value"], true);
  116. foreach ($fields as $index => $field) {
  117. $data[$index] = $field["value"];
  118. }
  119. return $data;
  120. }
  121. }