SystemGroupData.php 4.1 KB

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