Regiment.php 3.3 KB

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