School.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\Traits\School\Area as SchoolArea;
  5. use App\Models\Traits\School\SchoolClass as SchoolClass;
  6. use App\Models\Traits\School\SchoolGrade as SchoolGrade;
  7. /**
  8. * 商业公司模型
  9. *
  10. */
  11. class School extends Model
  12. {
  13. use HasFactory,SchoolArea,SchoolClass,SchoolGrade;
  14. // 与模型关联的表名
  15. protected $table = 'school';
  16. // 是否主动维护时间戳
  17. public $timestamps = false;
  18. // 定义时间戳字段名
  19. // const CREATED_AT = 'insert_time';
  20. // const UPDATED_AT = 'update_time';
  21. /**
  22. * 添加数据
  23. *
  24. */
  25. public function add($data)
  26. {
  27. // 时间
  28. $data['insert_time'] = time();
  29. $data['update_time'] = time();
  30. // 写入数据表
  31. $id = $this->query()->insertGetId($data);
  32. // 如果操作失败
  33. if( !$id ) return $id;
  34. // 更新缓存
  35. $this->getList(true);
  36. // 返回结果
  37. return $id;
  38. }
  39. /**
  40. * 添加数据
  41. *
  42. */
  43. public function edit($id,$data)
  44. {
  45. // 更新时间
  46. $data['update_time'] = time();
  47. // 写入数据表
  48. $result = $this->query()->where(['id'=>$id])->update($data);
  49. // 如果操作失败
  50. if( !$result ) return $result;
  51. // 更新缓存
  52. $this->getList(true);
  53. // 返回结果
  54. return $result;
  55. }
  56. /**
  57. * 获取列表
  58. * @param Bool $force 是否强制更新
  59. *
  60. */
  61. public function getList($force = false)
  62. {
  63. // 结果数据
  64. $list = $force ? [] : cache('admin:school:list');
  65. // 不存在数据
  66. if ( !$list ) {
  67. // 从数据库获取数据
  68. $data = $this->query()->where(['status'=>0])->get();
  69. // 是否有数据
  70. $data = $data ? $data->toArray() : [];
  71. // 循环处理数据
  72. $list = [];
  73. // 进行更新
  74. foreach ($data as $value) {
  75. // 重组数据
  76. $list[$value['id']] = $value;
  77. }
  78. // 存起来
  79. cache(['admin:school:list'=>$list]);
  80. }
  81. // 返回结果
  82. return $list;
  83. }
  84. /**
  85. * 获取配置平台对应的应用数据
  86. *
  87. * @param Array 用户ID
  88. * @param String 指定字段
  89. *
  90. */
  91. public function getOne($id,$field='')
  92. {
  93. // 获取列表数据
  94. $list = $this->getList();
  95. // 获取数据
  96. $one = isset($list[$id]) ? $list[$id] : [];
  97. // 返回值
  98. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  99. }
  100. /**
  101. * 获取配置平台对应的应用数据
  102. *
  103. * @param Array 用户ID
  104. * @param String 指定字段
  105. *
  106. */
  107. public function getIdByName($name)
  108. {
  109. // 获取列表数据
  110. $list = $this->getList();
  111. // 获取数据
  112. foreach ($list as $value) {
  113. // 如果有名称
  114. if( $name == $value['name'] ) return $value['id'];
  115. }
  116. // 写入数据表
  117. $id = $this->add(['name'=>$name]);
  118. // 返回结果
  119. return $id;
  120. }
  121. /**
  122. * 编码转id
  123. *
  124. * @param string $code 编码
  125. *
  126. */
  127. public function codeToId($code){
  128. return intval(str_ireplace('klsj','',$code));
  129. }
  130. /**
  131. * id转编码
  132. *
  133. * @param int $id 编码
  134. *
  135. */
  136. public function idToCode($id){
  137. return 'klsj'. str_pad($id, 9, '0', STR_PAD_LEFT);;
  138. }
  139. }