SystemGroupData.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // if($value["type"] == "upload" || $value["type"] == "uploads"){
  43. // $html_img = '';
  44. // if(is_array($value["value"])){
  45. // foreach ($value["value"] as $img) {
  46. // $html_img .= '<img class="image" data-image="'.$img.'" width="45" height="45" src="'.$img.'" /><br>';
  47. // }
  48. // }else{
  49. // $html_img = '<img class="image" data-image="'.$value["value"].'" width="45" height="45" src="'.$value["value"].'" />';
  50. // }
  51. // $info[$index]["value"] = $html_img;
  52. // }
  53. }
  54. $item->value = $info;
  55. });
  56. }
  57. /**
  58. * 获得组合数据信息+组合数据列表
  59. * @param $config_name
  60. * @param int $limit
  61. * @return array|bool|null|\think\Model
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. */
  66. public static function getGroupData($config_name, $limit = 0)
  67. {
  68. $group = SystemGroup::where('config_name', $config_name)->field('name,info,config_name')->find();
  69. if (!$group) return false;
  70. $group['data'] = self::getAllValue($config_name, $limit);
  71. return $group;
  72. }
  73. /**
  74. * 获取单个值
  75. * @param $config_name
  76. * @param int $limit
  77. * @return array
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. * @throws \think\exception\DbException
  81. */
  82. public static function getAllValue($config_name, $limit = 0)
  83. {
  84. $model = self::alias('a')->field('a.*,b.config_name')->join('system_group b', 'a.gid = b.id')
  85. ->where("b.config_name", $config_name)->where("a.status", 1)
  86. ->order('sort desc,id ASC');
  87. if ($limit > 0) $model->limit($limit);
  88. $data = [];
  89. $result = $model->select();
  90. if (!$result) return $data;
  91. foreach ($result as $key => $value) {
  92. $data[$key]["id"] = $value["id"];
  93. $fields = json_decode($value["value"], true);
  94. foreach ($fields as $index => $field) {
  95. // $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0]: ''):$field["value"];
  96. $data[$key][$index] = $field["value"];
  97. }
  98. }
  99. return $data;
  100. }
  101. /**
  102. * @param $result
  103. * @return array
  104. */
  105. public static function tidyList($result)
  106. {
  107. $data = [];
  108. if (!$result) return $data;
  109. foreach ($result as $key => $value) {
  110. $data[$key]["id"] = $value["id"];
  111. $fields = json_decode($value["value"], true);
  112. foreach ($fields as $index => $field) {
  113. $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0] : '') : $field["value"];
  114. }
  115. }
  116. return $data;
  117. }
  118. /**
  119. * 根据id获取当前记录中的数据
  120. * @param $id
  121. * @return mixed
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @throws \think\exception\DbException
  125. */
  126. public static function getDateValue($id)
  127. {
  128. $value = self::alias('a')->where(array("id" => $id))->find();
  129. $data["id"] = $value["id"];
  130. $fields = json_decode($value["value"], true);
  131. foreach ($fields as $index => $field) {
  132. $data[$index] = $field["value"];
  133. }
  134. return $data;
  135. }
  136. }