RegimentActive.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 RegimentActive extends Model
  13. {
  14. use HasFactory;
  15. // 与模型关联的表名
  16. protected $table = 'regiment_active';
  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. $this->getList(true);
  34. // 返回结果
  35. return $id;
  36. }
  37. /**
  38. * 添加数据
  39. *
  40. */
  41. public function edit($id,$data)
  42. {
  43. // 更新时间
  44. $data['update_time'] = time();
  45. // 写入数据表
  46. $result = $this->query()->where(['id'=>$id])->update($data);
  47. // 如果操作失败
  48. if( !$result ) return $result;
  49. // 更新缓存
  50. $this->getList(true);
  51. // 返回结果
  52. return $result;
  53. }
  54. /**
  55. * 获取列表
  56. * @param Bool $force 是否强制更新
  57. *
  58. */
  59. public function getList($force = false)
  60. {
  61. // 结果数据
  62. //$list = $force ? [] : cache('admin:clockin:active:list');
  63. $where[] = ['status','=',0];
  64. $where[] = ['end_time','>=',time()];
  65. $where[] = ['start_time','<=',time()];
  66. // 不存在数据
  67. //if ( !$list ) {
  68. // 从数据库获取数据
  69. $data = $this->query()->where($where)->get();
  70. // 是否有数据
  71. $data = $data ? $data->toArray() : [];
  72. // 循环处理数据
  73. $list = [];
  74. // 进行更新
  75. foreach ($data as $value) {
  76. // 重组数据
  77. $list[$value['id']] = $value;
  78. }
  79. // 存起来
  80. //cache(['admin:clockin:active:list'=>$list]);
  81. //}
  82. // 返回结果
  83. return $list;
  84. }
  85. /**
  86. * 获取配置平台对应的应用数据
  87. *
  88. * @param Array 用户ID
  89. * @param String 指定字段
  90. *
  91. */
  92. public function getOne($id,$field='')
  93. {
  94. // 获取列表数据
  95. $list = $this->getList();
  96. // 获取数据
  97. $one = isset($list[$id]) ? $list[$id] : [];
  98. // 返回值
  99. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  100. }
  101. /**
  102. * 编码转id
  103. *
  104. * @param string $code 编码
  105. *
  106. */
  107. public function codeToId($code){
  108. return intval(str_ireplace('klpt','',$code));
  109. }
  110. /**
  111. * id转编码
  112. *
  113. * @param int $id 编码
  114. *
  115. */
  116. public function idToCode($id){
  117. return 'klpt'. str_pad($id, 9, '0', STR_PAD_LEFT);
  118. }
  119. }