SystemGroupData.php 4.7 KB

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