Company.php 4.3 KB

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