SystemGroupDataMerchant.php 5.1 KB

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